agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2020 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 19 | * USA. |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "pluginProcessor.h" |
| 23 | // System includes |
| 24 | #include <algorithm> |
| 25 | #include <cstring> |
| 26 | // OpenCV headers |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 27 | #include <opencv2/core.hpp> |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 28 | #include <opencv2/imgcodecs.hpp> |
| 29 | #include <opencv2/imgproc.hpp> |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 30 | // Logger |
| 31 | #include <pluglog.h> |
| 32 | |
| 33 | extern "C" { |
| 34 | #include <libavutil/display.h> |
| 35 | } |
| 36 | |
| 37 | const char sep = separator(); |
| 38 | |
| 39 | const std::string TAG = "FORESEG"; |
| 40 | |
| 41 | PluginParameters* mPluginParameters = getGlobalPluginParameters(); |
| 42 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 43 | namespace jami { |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 44 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 45 | PluginProcessor::PluginProcessor(const std::string& dataPath) |
| 46 | : pluginInference {TFModel {dataPath + sep + "models" + sep + mPluginParameters->model}} |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 47 | { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 48 | initModel(); |
| 49 | setBackgroundImage(mPluginParameters->image); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void |
agsantos | 9dcf430 | 2020-09-01 18:21:48 -0400 | [diff] [blame] | 53 | PluginProcessor::setBackgroundImage(const std::string& backgroundPath) |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 54 | { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 55 | cv::Size size = cv::Size {0, 0}; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 56 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 57 | if (!backgroundImage.empty()) |
| 58 | size = backgroundImage.size(); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 59 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 60 | cv::Mat newBackgroundImage = cv::imread(backgroundPath); |
| 61 | if (newBackgroundImage.cols == 0) { |
| 62 | Plog::log(Plog::LogPriority::ERR, TAG, "Background image not Loaded"); |
| 63 | } else { |
| 64 | Plog::log(Plog::LogPriority::INFO, TAG, "Background image Loaded"); |
| 65 | cv::cvtColor(newBackgroundImage, newBackgroundImage, cv::COLOR_BGR2RGB); |
| 66 | newBackgroundImage.convertTo(newBackgroundImage, CV_32FC3); |
| 67 | if (size.height) { |
| 68 | cv::resize(newBackgroundImage, newBackgroundImage, size); |
| 69 | backgroundRotation = 0; |
| 70 | } |
| 71 | backgroundImage = newBackgroundImage.clone(); |
| 72 | newBackgroundImage.release(); |
| 73 | hasBackground_ = true; |
| 74 | } |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void |
| 78 | PluginProcessor::initModel() |
| 79 | { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 80 | try { |
| 81 | pluginInference.init(); |
| 82 | } catch (std::exception& e) { |
| 83 | Plog::log(Plog::LogPriority::ERR, TAG, e.what()); |
| 84 | } |
| 85 | std::ostringstream oss; |
| 86 | oss << "Model is allocated " << pluginInference.isAllocated(); |
| 87 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 88 | } |
| 89 | |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 90 | #ifdef TFLITE |
| 91 | void |
| 92 | PluginProcessor::feedInput(const cv::Mat& frame) |
| 93 | { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 94 | auto pair = pluginInference.getInput(); |
| 95 | uint8_t* inputPointer = pair.first; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 96 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 97 | cv::Mat temp(frame.rows, frame.cols, CV_8UC3, inputPointer); |
| 98 | frame.convertTo(temp, CV_8UC3); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 99 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 100 | inputPointer = nullptr; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 101 | } |
| 102 | #else |
| 103 | void |
| 104 | PluginProcessor::feedInput(const cv::Mat& frame) |
| 105 | { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 106 | pluginInference.ReadTensorFromMat(frame); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 107 | } |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 108 | #endif // TFLITE |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 109 | |
| 110 | int |
| 111 | PluginProcessor::getBackgroundRotation() |
| 112 | { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 113 | return backgroundRotation; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void |
| 117 | PluginProcessor::setBackgroundRotation(int angle) |
| 118 | { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 119 | if (backgroundRotation != angle && (backgroundRotation - angle) != 0) { |
agsantos | b74f4cb | 2020-10-01 14:30:43 -0400 | [diff] [blame] | 120 | rotateFrame(backgroundRotation - angle, backgroundImage); |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 121 | backgroundRotation = angle; |
| 122 | } |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 126 | PluginProcessor::computePredictions() |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 127 | { |
agsantos | b74f4cb | 2020-10-01 14:30:43 -0400 | [diff] [blame] | 128 | if (count == 0) { |
| 129 | // Run the graph |
| 130 | pluginInference.runGraph(); |
| 131 | auto predictions = pluginInference.masksPredictions(); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 132 | |
agsantos | b74f4cb | 2020-10-01 14:30:43 -0400 | [diff] [blame] | 133 | // Save the predictions |
| 134 | computedMask = predictions; |
| 135 | } |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void |
| 139 | PluginProcessor::printMask() |
| 140 | { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 141 | for (size_t i = 0; i < computedMask.size(); i++) { |
| 142 | // Log the predictions |
| 143 | std::ostringstream oss; |
| 144 | oss << "\nclass: " << computedMask[i] << std::endl; |
| 145 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| 146 | } |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 147 | } |
| 148 | |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 149 | void |
agsantos | 31d1a0b | 2020-10-23 14:05:53 -0400 | [diff] [blame] | 150 | PluginProcessor::resetInitValues(const cv::Size& modelInputSize) |
| 151 | { |
| 152 | previousMasks[0] = cv::Mat(modelInputSize.height, modelInputSize.width, CV_32FC1, double(0.)); |
| 153 | previousMasks[1] = cv::Mat(modelInputSize.height, modelInputSize.width, CV_32FC1, double(0.)); |
| 154 | kSize = cv::Size(modelInputSize.width * kernelSize, modelInputSize.height * kernelSize); |
| 155 | if (kSize.height % 2 == 0) { |
| 156 | kSize.height -= 1; |
| 157 | } |
| 158 | if (kSize.width % 2 == 0) { |
| 159 | kSize.width -= 1; |
| 160 | } |
| 161 | count = 0; |
| 162 | grabCutMode = cv::GC_INIT_WITH_MASK; |
| 163 | grabCutIterations = 5; |
| 164 | } |
| 165 | |
| 166 | void |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 167 | copyByLine(uchar* frameData, uchar* applyMaskData, const int lineSize, cv::Size size) |
| 168 | { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 169 | if (3 * size.width == lineSize) { |
| 170 | std::memcpy(frameData, applyMaskData, size.height * size.width * 3); |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 171 | } else { |
| 172 | int rows = size.height; |
| 173 | int offset = 0; |
| 174 | int maskoffset = 0; |
| 175 | for (int i = 0; i < rows; i++) { |
| 176 | std::memcpy(frameData + offset, applyMaskData + maskoffset, lineSize); |
| 177 | offset += lineSize; |
| 178 | maskoffset += 3 * size.width; |
| 179 | } |
| 180 | } |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 184 | PluginProcessor::drawMaskOnFrame( |
| 185 | cv::Mat& frame, cv::Mat& frameReduced, std::vector<float> computedMask, int lineSize, int angle) |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 186 | { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 187 | if (computedMask.empty()) { |
| 188 | return; |
| 189 | } |
agsantos | b74f4cb | 2020-10-01 14:30:43 -0400 | [diff] [blame] | 190 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 191 | int maskSize = static_cast<int>(std::sqrt(computedMask.size())); |
| 192 | cv::Mat maskImg(maskSize, maskSize, CV_32FC1, computedMask.data()); |
agsantos | b74f4cb | 2020-10-01 14:30:43 -0400 | [diff] [blame] | 193 | cv::Mat* applyMask = &frameReduced; |
| 194 | cv::Mat output; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 195 | |
agsantos | b74f4cb | 2020-10-01 14:30:43 -0400 | [diff] [blame] | 196 | if (count == 0) { |
| 197 | rotateFrame(-angle, maskImg); |
| 198 | #ifdef TFLITE |
| 199 | for (int i = 0; i < maskImg.cols; i++) { |
| 200 | for (int j = 0; j < maskImg.rows; j++) { |
| 201 | if (maskImg.at<float>(j, i) == 15) |
| 202 | maskImg.at<float>(j, i) = 1.; |
| 203 | else |
| 204 | maskImg.at<float>(j, i) = smoothFactors[0] * previousMasks[0].at<float>(j, i) |
| 205 | + smoothFactors[1] * previousMasks[1].at<float>(j, i); |
| 206 | } |
| 207 | } |
| 208 | cv::morphologyEx(maskImg, |
| 209 | maskImg, |
| 210 | cv::MORPH_CLOSE, |
| 211 | cv::getStructuringElement(cv::MORPH_ELLIPSE, kSize), |
| 212 | cv::Point(-1, -1), |
| 213 | 4); |
| 214 | #else |
| 215 | cv::resize(maskImg, maskImg, cv::Size(frameReduced.cols, frameReduced.rows)); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 216 | |
agsantos | b74f4cb | 2020-10-01 14:30:43 -0400 | [diff] [blame] | 217 | double m, M; |
| 218 | cv::minMaxLoc(maskImg, &m, &M); |
| 219 | |
| 220 | if (M < 2) { // avoid detection if there is any one in frame |
| 221 | maskImg = 0. * maskImg; |
| 222 | } else { |
| 223 | for (int i = 0; i < maskImg.cols; i++) { |
| 224 | for (int j = 0; j < maskImg.rows; j++) { |
| 225 | maskImg.at<float>(j, i) = (maskImg.at<float>(j, i) - m) / (M - m); |
| 226 | |
| 227 | if (maskImg.at<float>(j, i) < 0.4) |
| 228 | maskImg.at<float>(j, i) = 0.; |
| 229 | else if (maskImg.at<float>(j, i) < 0.7) { |
| 230 | float value = maskImg.at<float>(j, i) * smoothFactors[0] |
| 231 | + previousMasks[0].at<float>(j, i) * smoothFactors[1] |
| 232 | + previousMasks[1].at<float>(j, i) * smoothFactors[2]; |
| 233 | maskImg.at<float>(j, i) = 0.; |
| 234 | if (value > 0.7) |
| 235 | maskImg.at<float>(j, i) = 1.; |
| 236 | } else |
| 237 | maskImg.at<float>(j, i) = 1.; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | #endif |
| 242 | if (cv::countNonZero(maskImg) != 0) { |
| 243 | #ifdef TFLITE |
| 244 | cv::Mat tfMask; |
| 245 | tfMask = maskImg.clone(); |
| 246 | tfMask *= 255.; |
| 247 | tfMask.convertTo(tfMask, CV_8UC1); |
| 248 | cv::threshold(tfMask, tfMask, 127, 255, cv::THRESH_BINARY); |
| 249 | if (cv::countNonZero(tfMask) != 0) { |
| 250 | #endif |
| 251 | cv::Mat dilate; |
agsantos | 31d1a0b | 2020-10-23 14:05:53 -0400 | [diff] [blame] | 252 | cv::dilate(maskImg, |
| 253 | dilate, |
| 254 | cv::getStructuringElement(cv::MORPH_ELLIPSE, kSize), |
| 255 | cv::Point(-1, -1), |
| 256 | 2); |
agsantos | b74f4cb | 2020-10-01 14:30:43 -0400 | [diff] [blame] | 257 | cv::erode(maskImg, |
| 258 | maskImg, |
| 259 | cv::getStructuringElement(cv::MORPH_ELLIPSE, kSize), |
| 260 | cv::Point(-1, -1), |
| 261 | 2); |
| 262 | for (int i = 0; i < maskImg.cols; i++) { |
| 263 | for (int j = 0; j < maskImg.rows; j++) { |
| 264 | if (dilate.at<float>(j, i) != maskImg.at<float>(j, i)) |
| 265 | maskImg.at<float>(j, i) = grabcutClass; |
| 266 | } |
| 267 | } |
| 268 | maskImg.convertTo(maskImg, CV_8UC1); |
| 269 | applyMask->convertTo(*applyMask, CV_8UC1); |
| 270 | cv::Rect rect(1, 1, maskImg.rows, maskImg.cols); |
agsantos | 31d1a0b | 2020-10-23 14:05:53 -0400 | [diff] [blame] | 271 | cv::grabCut(*applyMask, |
| 272 | maskImg, |
| 273 | rect, |
| 274 | bgdModel, |
| 275 | fgdModel, |
| 276 | grabCutIterations, |
| 277 | grabCutMode); |
| 278 | |
| 279 | grabCutMode = cv::GC_EVAL; |
| 280 | grabCutIterations = 1; |
| 281 | |
agsantos | b74f4cb | 2020-10-01 14:30:43 -0400 | [diff] [blame] | 282 | maskImg = maskImg & 1; |
| 283 | #ifdef TFLITE |
| 284 | cv::bitwise_and(maskImg, tfMask, maskImg); |
| 285 | } |
| 286 | #endif |
| 287 | maskImg.convertTo(maskImg, CV_32FC1); |
| 288 | maskImg *= 255.; |
agsantos | 31d1a0b | 2020-10-23 14:05:53 -0400 | [diff] [blame] | 289 | GaussianBlur(maskImg, maskImg, cv::Size(7, 7), 0); // float mask from 0 to 255. |
agsantos | b74f4cb | 2020-10-01 14:30:43 -0400 | [diff] [blame] | 290 | maskImg = maskImg / 255.; |
| 291 | } |
| 292 | previousMasks[1] = previousMasks[0].clone(); |
| 293 | previousMasks[0] = maskImg.clone(); |
| 294 | } |
| 295 | |
| 296 | cv::Mat roiMaskImg = previousMasks[0].clone(); |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 297 | cv::Mat roiMaskImgComplementary = 1. - roiMaskImg; // mask from 1. to 0 |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 298 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 299 | std::vector<cv::Mat> channels; |
| 300 | std::vector<cv::Mat> channelsComplementary; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 301 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 302 | channels.emplace_back(roiMaskImg); |
| 303 | channels.emplace_back(roiMaskImg); |
| 304 | channels.emplace_back(roiMaskImg); |
| 305 | channelsComplementary.emplace_back(roiMaskImgComplementary); |
| 306 | channelsComplementary.emplace_back(roiMaskImgComplementary); |
| 307 | channelsComplementary.emplace_back(roiMaskImgComplementary); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 308 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 309 | cv::merge(channels, roiMaskImg); |
| 310 | cv::merge(channelsComplementary, roiMaskImgComplementary); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 311 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 312 | int origType = frameReduced.type(); |
| 313 | int roiMaskType = roiMaskImg.type(); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 314 | |
agsantos | b74f4cb | 2020-10-01 14:30:43 -0400 | [diff] [blame] | 315 | frameReduced.convertTo(output, roiMaskType); |
| 316 | output = output.mul(roiMaskImg); |
| 317 | output += backgroundImage.mul(roiMaskImgComplementary); |
| 318 | output.convertTo(output, origType); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 319 | |
agsantos | b74f4cb | 2020-10-01 14:30:43 -0400 | [diff] [blame] | 320 | cv::resize(output, output, cv::Size(frame.cols, frame.rows)); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 321 | |
agsantos | b74f4cb | 2020-10-01 14:30:43 -0400 | [diff] [blame] | 322 | copyByLine(frame.data, output.data, lineSize, cv::Size(frame.cols, frame.rows)); |
| 323 | count++; |
| 324 | count = count % frameCount; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | void |
| 328 | PluginProcessor::rotateFrame(int angle, cv::Mat& mat) |
| 329 | { |
agsantos | b74f4cb | 2020-10-01 14:30:43 -0400 | [diff] [blame] | 330 | if (angle == -90) |
| 331 | cv::rotate(mat, mat, cv::ROTATE_90_COUNTERCLOCKWISE); |
| 332 | else if (std::abs(angle) == 180) |
| 333 | cv::rotate(mat, mat, cv::ROTATE_180); |
| 334 | else if (angle == 90) |
| 335 | cv::rotate(mat, mat, cv::ROTATE_90_CLOCKWISE); |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 336 | } |
agsantos | 9dcf430 | 2020-09-01 18:21:48 -0400 | [diff] [blame] | 337 | |
| 338 | bool |
| 339 | PluginProcessor::hasBackground() const |
| 340 | { |
| 341 | return hasBackground_; |
| 342 | } |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 343 | } // namespace jami |