| 1 | /** |
| 2 | * Copyright (C) 2007 binarymillenium |
| 3 | * Copyright (C) 2011 Dan Dennedy <[email protected]> |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <opencv2/opencv.hpp> |
| 24 | #include "frei0r.hpp" |
| 25 | #include "frei0r/math.h" |
| 26 | |
| 27 | #define USE_ROI |
| 28 | #define PAD (40) |
| 29 | |
| 30 | class FaceDetect; |
| 31 | frei0r::construct<FaceDetect> plugin("opencvfacedetect", |
| 32 | "detect faces and draw shapes on them", |
| 33 | "binarymillenium, ddennedy", |
| 34 | 2,0, F0R_COLOR_MODEL_PACKED32); |
| 35 | |
| 36 | class FaceDetect: public frei0r::filter |
| 37 | { |
| 38 | |
| 39 | private: |
| 40 | cv::Mat image; |
| 41 | unsigned count; |
| 42 | std::vector<cv::Rect> objects; |
| 43 | cv::Rect roi; |
| 44 | cv::CascadeClassifier cascade; |
| 45 | |
| 46 | // plugin parameters |
| 47 | std::string classifier; |
| 48 | double shape; |
| 49 | double recheck; |
| 50 | double threads; |
| 51 | double search_scale; |
| 52 | double neighbors; |
| 53 | double smallest; |
| 54 | double scale; |
| 55 | double stroke; |
| 56 | bool antialias; |
| 57 | double alpha; |
| 58 | f0r_param_color color[5]; |
| 59 | |
| 60 | std::string old_classifier; |
| 61 | |
| 62 | public: |
| 63 | FaceDetect(int width, int height) |
| 64 | : count(0) |
| 65 | { |
| 66 | roi.width = roi.height = 0; |
| 67 | roi.x = roi.y = 0; |
| 68 | classifier = "/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml"; |
| 69 | register_param(classifier, |
| 70 | "Classifier", |
| 71 | "Full path to the XML pattern model for recognition; look in /usr/share/opencv/haarcascades"); |
| 72 | threads = 0.01; //number of CPUs |
| 73 | register_param(threads, "Threads", "How many threads to use divided by 100; 0 uses CPU count"); |
| 74 | shape = 0.0; |
| 75 | register_param(shape, "Shape", "The shape to draw: 0=circle, 0.1=ellipse, 0.2=rectangle, 1=random"); |
| 76 | recheck = 0.025; |
| 77 | register_param(recheck, "Recheck", "How often to detect an object in number of frames, divided by 1000"); |
| 78 | search_scale = 0.12; // increase size of search window by 20% on each pass |
| 79 | register_param(search_scale, "Search scale", "The search window scale factor, divided by 10"); |
| 80 | neighbors = 0.02; // require 2 neighbors |
| 81 | register_param(neighbors, "Neighbors", "Minimum number of rectangles that makes up an object, divided by 100"); |
| 82 | smallest = 0.0; // smallest window size is trained default |
| 83 | register_param(smallest, "Smallest", "Minimum window size in pixels, divided by 1000"); |
| 84 | scale = 1.0 / 1.5; |
| 85 | register_param(scale, "Scale", "Down scale the image prior detection"); |
| 86 | stroke = 0.0; |
| 87 | register_param(stroke, "Stroke", "Line width, divided by 100, or fill if 0"); |
| 88 | antialias = false; |
| 89 | register_param(antialias, "Antialias", "Draw with antialiasing"); |
| 90 | alpha = 1.0; |
| 91 | register_param(alpha, "Alpha", "The alpha channel value for the shapes"); |
| 92 | f0r_param_color color0 = {1.0, 1.0, 1.0}; |
| 93 | color[0] = color0; |
| 94 | register_param(color[0], "Color 1", "The color of the first object"); |
| 95 | f0r_param_color color1 = {0.0, 0.5, 1.0}; |
| 96 | color[1] = color1; |
| 97 | register_param(color[1], "Color 2", "The color of the second object"); |
| 98 | f0r_param_color color2 = {0.0, 1.0, 1.0}; |
| 99 | color[2] = color2; |
| 100 | register_param(color[2], "Color 3", "The color of the third object"); |
| 101 | f0r_param_color color3 = {0.0, 1.0, 0.0}; |
| 102 | color[3] = color3; |
| 103 | register_param(color[3], "Color 4", "The color of the fourth object"); |
| 104 | f0r_param_color color4 = {1.0, 0.5, 0.0}; |
| 105 | color[4] = color4; |
| 106 | register_param(color[4], "Color 5", "The color of the fifth object"); |
| 107 | srand(::time(NULL)); |
| 108 | } |
| 109 | |
| 110 | ~FaceDetect() |
| 111 | { |
| 112 | } |
| 113 | |
| 114 | void update(double time, |
| 115 | uint32_t* out, |
| 116 | const uint32_t* in) |
| 117 | { |
| 118 | if (cascade.empty()) { |
| 119 | cv::setNumThreads(cvRound(threads * 100)); |
| 120 | if (classifier.length() > 0 && classifier != old_classifier) { |
| 121 | if (!cascade.load(classifier.c_str())) |
| 122 | fprintf(stderr, "ERROR: Could not load classifier cascade %s\n", classifier.c_str()); |
| 123 | old_classifier = classifier; |
| 124 | } |
| 125 | else { |
| 126 | memcpy(out, in, size * 4); |
| 127 | return; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // sanitize parameters |
| 132 | search_scale = CLAMP(search_scale, 0.11, 1.0); |
| 133 | neighbors = CLAMP(neighbors, 0.01, 1.0); |
| 134 | |
| 135 | // copy input image to OpenCV |
| 136 | image = cv::Mat(height, width, CV_8UC4, (void*)in); |
| 137 | |
| 138 | // only re-detect periodically to control performance and reduce shape jitter |
| 139 | int recheckInt = abs(cvRound(recheck * 1000)); |
| 140 | if ( recheckInt > 0 && count % recheckInt ) |
| 141 | { |
| 142 | // skip detect |
| 143 | count++; |
| 144 | // fprintf(stderr, "draw-only counter %u\n", count); |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | count = 1; // reset the recheck counter |
| 149 | if (objects.size() > 0) // reset the list of objects |
| 150 | objects.clear(); |
| 151 | double elapsed = (double) cv::getTickCount(); |
| 152 | |
| 153 | objects = detect(); |
| 154 | |
| 155 | // use detection time to throttle frequency of re-detect vs. redraw (automatic recheck) |
| 156 | elapsed = cv::getTickCount() - elapsed; |
| 157 | elapsed = elapsed / ((double) cv::getTickFrequency() * 1000.0); |
| 158 | |
| 159 | // Automatic recheck uses an undocumented negative parameter value, |
| 160 | // which is not compliant, but technically feasible. |
| 161 | if (recheck < 0 && cvRound( elapsed / (1000.0 / (recheckInt + 1)) ) <= recheckInt) |
| 162 | count += recheckInt - cvRound( elapsed / (1000.0 / (recheckInt + 1))); |
| 163 | // fprintf(stderr, "detection time = %gms counter %u\n", elapsed, count); |
| 164 | } |
| 165 | |
| 166 | draw(); |
| 167 | |
| 168 | // copy filtered OpenCV image to output |
| 169 | memcpy(out, image.data, size * 4); |
| 170 | } |
| 171 | |
| 172 | private: |
| 173 | std::vector<cv::Rect> detect() |
| 174 | { |
| 175 | std::vector<cv::Rect> faces; |
| 176 | if (cascade.empty()) return faces; |
| 177 | double scale = this->scale == 0? 1.0 : this->scale; |
| 178 | cv::Mat image_roi = image; |
| 179 | cv::Mat gray, small; |
| 180 | int min = cvRound(smallest * 1000. * scale); |
| 181 | |
| 182 | // use a region of interest to improve performance |
| 183 | // This idea comes from the More than Technical blog: |
| 184 | // http://www.morethantechnical.com/2009/08/09/near-realtime-face-detection-on-the-iphone-w-opencv-port-wcodevideo/ |
| 185 | if ( roi.width > 0 && roi.height > 0) |
| 186 | { |
| 187 | image_roi = image(roi); |
| 188 | } |
| 189 | |
| 190 | // use an equalized grayscale to improve detection |
| 191 | cv::cvtColor(image_roi, gray, cv::COLOR_BGR2GRAY); |
| 192 | |
| 193 | // use a smaller image to improve performance |
| 194 | cv::resize(gray, small, cv::Size(cvRound(gray.cols * scale), cvRound(gray.rows * scale))); |
| 195 | cv::equalizeHist(small, small); |
| 196 | |
| 197 | // detect with OpenCV |
| 198 | cascade.detectMultiScale(small, faces, 1.1, 2, 0, cv::Size(min, min)); |
| 199 | |
| 200 | #ifdef USE_ROI |
| 201 | if (faces.size() == 0) |
| 202 | { |
| 203 | // clear the region of interest |
| 204 | roi.width = roi.height = 0; |
| 205 | roi.x = roi.y = 0; |
| 206 | } |
| 207 | else if (faces.size() > 0) |
| 208 | { |
| 209 | // determine the region of interest from the detected objects |
| 210 | int minx = width * scale; |
| 211 | int miny = height * scale; |
| 212 | int maxx, maxy = 0; |
| 213 | for (size_t i = 0; i < faces.size(); i++) |
| 214 | { |
| 215 | faces[i].x+= roi.x * scale; |
| 216 | faces[i].y+= roi.y * scale; |
| 217 | minx = MIN(faces[i].x, minx); |
| 218 | miny = MIN(faces[i].y, miny); |
| 219 | maxx = MAX(faces[i].x + faces[i].width, maxx); |
| 220 | maxy= MAX(faces[i].y + faces[i].height, maxy); |
| 221 | } |
| 222 | minx= MAX(minx - PAD, 0); |
| 223 | miny= MAX(miny - PAD, 0); |
| 224 | maxx = MIN(maxx + PAD, width * scale); |
| 225 | maxy = MIN(maxy + PAD, height * scale); |
| 226 | |
| 227 | // store the region of interest |
| 228 | roi.x = minx / scale; |
| 229 | roi.y = miny / scale; |
| 230 | roi.width = (maxx - minx) / scale; |
| 231 | roi.height = (maxy - miny) / scale; |
| 232 | } |
| 233 | #endif |
| 234 | return faces; |
| 235 | } |
| 236 | |
| 237 | void draw() |
| 238 | { |
| 239 | double scale = this->scale == 0? 1.0 : this->scale; |
| 240 | cv::Scalar colors[5] = { |
| 241 | cv::Scalar(cvRound(color[0].r * 255), cvRound(color[0].g * 255), cvRound(color[0].b * 255), cvRound(alpha * 255)), |
| 242 | cv::Scalar(cvRound(color[1].r * 255), cvRound(color[1].g * 255), cvRound(color[1].b * 255), cvRound(alpha * 255)), |
| 243 | cv::Scalar(cvRound(color[2].r * 255), cvRound(color[2].g * 255), cvRound(color[2].b * 255), cvRound(alpha * 255)), |
| 244 | cv::Scalar(cvRound(color[3].r * 255), cvRound(color[3].g * 255), cvRound(color[3].b * 255), cvRound(alpha * 255)), |
| 245 | cv::Scalar(cvRound(color[4].r * 255), cvRound(color[4].g * 255), cvRound(color[4].b * 255), cvRound(alpha * 255)), |
| 246 | }; |
| 247 | |
| 248 | for (size_t i = 0; i < objects.size(); i++) |
| 249 | { |
| 250 | cv::Rect* r = (cv::Rect*) &objects[i]; |
| 251 | cv::Point center; |
| 252 | int thickness = stroke <= 0? cv::FILLED : cvRound(stroke * 100); |
| 253 | int linetype = antialias? cv::LINE_AA : 8; |
| 254 | |
| 255 | center.x = cvRound((r->x + r->width * 0.5) / scale); |
| 256 | center.y = cvRound((r->y + r->height * 0.5) / scale); |
| 257 | |
| 258 | switch (shape == 1.0? (rand() % 3) : cvRound(shape * 10)) |
| 259 | { |
| 260 | default: |
| 261 | case 0: |
| 262 | { |
| 263 | int radius = cvRound((r->width + r->height) * 0.25 / scale); |
| 264 | cv::circle(image, center, radius, colors[i % 5], thickness, linetype); |
| 265 | break; |
| 266 | } |
| 267 | case 1: |
| 268 | { |
| 269 | cv::ellipse(image, center, cv::Size(r->width / scale, (r->height / scale) * 1.2), 90, 0, 360, colors[i % 5], thickness, linetype); |
| 270 | break; |
| 271 | } |
| 272 | case 2: |
| 273 | { |
| 274 | cv::Point pt1 = cv::Point(r->x / scale, r->y / scale); |
| 275 | cv::Point pt2 = cv::Point((r->x + r->width) / scale, (r->y + r->height) / scale); |
| 276 | cv::rectangle(image, pt1, pt2, colors[i % 5], thickness, linetype); |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | }; |