{"id":383,"date":"2011-08-26T10:58:30","date_gmt":"2011-08-26T02:58:30","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=383"},"modified":"2011-08-26T13:44:10","modified_gmt":"2011-08-26T05:44:10","slug":"people-detection-in-opencv-again","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2011\/08\/26\/people-detection-in-opencv-again\/","title":{"rendered":"People Detection in OpenCV again"},"content":{"rendered":"<p>There are a number of enquiries about the people detection video I did a while ago. It is a step by step explanation of what I have done. I use the XCode 4 in OSX Lion with OpenCV 2.3 to try out the following.<\/p>\n<p>The first step is to download and build the latest OpenCV 2.3 into the folder at \/Developer\/OpenCV-2.3.0. The headers are in the <strong>include<\/strong> folder. Please note that you may have to copy the individual include folders from the <strong>modules<\/strong> folder. I build the shared libraries in the <strong>lib\/Release<\/strong> folder.<\/p>\n<p>The code is a modification of the sample <strong>peopledetect.cpp<\/strong>.<\/p>\n<p>The second step is to display the video capture image. I use the example from the C++ reference manual in the highgui section.<\/p>\n<pre lang=\"cpp\">\r\n#include <iostream>\r\n#include <opencv2\/opencv.hpp>\r\n\r\nusing namespace std;\r\nusing namespace cv;\r\n\r\nint main (int argc, const char * argv[])\r\n{\r\n    VideoCapture cap(CV_CAP_ANY);\r\n    if (!cap.isOpened())\r\n        return -1;\r\n    \r\n    Mat img;\r\n    namedWindow(\"video capture\", CV_WINDOW_AUTOSIZE);\r\n    while (true)\r\n    {\r\n        cap >> img;\r\n        imshow(\"video capture\", img);\r\n        if (waitKey(10) >= 0)\r\n            break;\r\n    }\r\n    return 0;\r\n}\r\n<\/pre>\n<p>The last step is to combine the two examples into one, with a little adjustment of the detection parameters and the display rectangle size.<\/p>\n<pre lang=\"cpp\">\r\n#include <iostream>\r\n#include <opencv2\/opencv.hpp>\r\n\r\nusing namespace std;\r\nusing namespace cv;\r\n\r\nint main (int argc, const char * argv[])\r\n{\r\n    VideoCapture cap(CV_CAP_ANY);\r\n    cap.set(CV_CAP_PROP_FRAME_WIDTH, 320);\r\n    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240);    \r\n    if (!cap.isOpened())\r\n        return -1;\r\n    \r\n    Mat img;\r\n    HOGDescriptor hog;\r\n    hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());\r\n    \r\n    namedWindow(\"video capture\", CV_WINDOW_AUTOSIZE);\r\n    while (true)\r\n    {\r\n        cap >> img;\r\n        if (!img.data)\r\n            continue;\r\n        \r\n        vector<Rect> found, found_filtered;\r\n        hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2);\r\n\r\n        size_t i, j;\r\n        for (i=0; i<found.size(); i++)\r\n        {\r\n            Rect r = found[i];\r\n            for (j=0; j<found.size(); j++)\r\n                if (j!=i &#038;&#038; (r &#038; found[j])==r)\r\n                    break;\r\n            if (j==found.size())\r\n                found_filtered.push_back(r);\r\n        }\r\n        for (i=0; i<found_filtered.size(); i++)\r\n        {\r\n\t    Rect r = found_filtered[i];\r\n            r.x += cvRound(r.width*0.1);\r\n\t    r.width = cvRound(r.width*0.8);\r\n\t    r.y += cvRound(r.height*0.06);\r\n\t    r.height = cvRound(r.height*0.9);\r\n\t    rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 2);\r\n\t}\r\n        imshow(\"video capture\", img);\r\n        if (waitKey(20) >= 0)\r\n            break;\r\n    }\r\n    return 0;\r\n}\r\n<\/pre>\n<p>Please note that the performance is pretty slow even though the capture size is 320 x 240.<\/p>\n<p><iframe loading=\"lazy\" width=\"480\" height=\"390\" src=\"http:\/\/www.youtube.com\/embed\/mFnZ2bqMnSI?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are a number of enquiries about the people detection video I did a while ago. It is a step by step explanation of what I have done. I use the XCode 4 in OSX Lion with OpenCV 2.3 to try out the following. The first step is to download and build the latest OpenCV [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[3,16],"class_list":["post-383","post","type-post","status-publish","format-standard","hentry","category-testing","tag-opencv","tag-people-detection"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/comments?post=383"}],"version-history":[{"count":8,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/383\/revisions"}],"predecessor-version":[{"id":391,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/383\/revisions\/391"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=383"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}