{"id":5174,"date":"2014-03-14T09:00:40","date_gmt":"2014-03-14T13:00:40","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5174"},"modified":"2014-03-14T08:27:43","modified_gmt":"2014-03-14T12:27:43","slug":"detecting-faces-in-images","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2014\/03\/14\/detecting-faces-in-images\/","title":{"rendered":"Detecting Faces in Images"},"content":{"rendered":"<div class=\"content\"><!--introduction--><!--\/introduction--><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week is more of a challenge than a \"Pick\".<\/p><p>If I were to search the MATLAB Central File Exchange for \"face detection\" (with the quotation marks) I would get a dazzling--and somewhat overwhelming--array of <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?utf8=%E2%9C%93&amp;term=%22face+detection%22\">44 hits<\/a>. Trying to detect faces (or anything else*) in images seems to me a reasonable thing to want to do, and in my mind typifies the challenges that the <a href=\"https:\/\/www.mathworks.com\/products\/computer-vision\/\">Computer Vision System Toolbox<\/a> was made to address. In fact, I shudder to think of what classical \"image processing\" code designed to detect faces might look like.<\/p><p>But how do we navigate the 44 files on the File Exchange that facilitate that detection process? Where do we start? I can sort the results by number of downloads, by rating, by relevancy,  ....<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/sort.png\" alt=\"\"> <\/p><p>Some of those downloads seem to have great potential. But where does one start?<\/p><p>For this post, I'm going to go \"out-of-model\" and focus not on a submitted File Exchange file, but on core capabilities of the Computer Vision System Toolbox. This Toolbox has a <a href=\"\">\"Cascade Object Detector\"<\/a> that facilitates out-of-the-box detection of faces, eyes, noses, mouths, and upper bodies. In fact, there are two in-product face detectors, and \"FrontalFaceCART\" (frontal face detection using \"Classification And Regression Tree\" analysis) is the default tool behavior.<\/p><p>Using the detectors in the Computer Vision System Toolbox is stunningly simple; we simply <a href=\"https:\/\/en.wikipedia.org\/wiki\/Instance_(computer_science)\">instantiate an instance<\/a> of the cascade object detector and, for visualization purposes, a vision.ShapeInserter:<\/p><pre class=\"codeinput\">faceDetector = vision.CascadeObjectDetector;\r\nshapeInserter = vision.ShapeInserter(<span class=\"string\">'BorderColor'<\/span>,<span class=\"string\">'Custom'<\/span>,<span class=\"string\">'CustomBorderColor'<\/span>,[0 255 255]);\r\n<\/pre><p>We then read input image:<\/p><pre class=\"codeinput\">I = imread(<span class=\"string\">'visionteam.jpg'<\/span>);\r\nimshow(I);shg;\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/facedetectionpotw_01.png\" alt=\"\"> <p>Then we simply \"step\" the instantiated objects on the image of interest. The CascadeObjectDetector returns the bounding boxes of the detected objects, and the ShapeInserter delimits them in the image:<\/p><pre class=\"codeinput\">bbox = step(faceDetector, I);\r\n<span class=\"comment\">% Draw boxes around detected faces and display results<\/span>\r\nI_faces = step(shapeInserter, I, int32(bbox));\r\nimshow(I_faces), title(<span class=\"string\">'Detected faces'<\/span>);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/facedetectionpotw_02.png\" alt=\"\"> <p>Detecting faces using MathWorks' functionality is trivially easy. It requires MATLAB, the Image Processing Toolbox, and the Computer Vision System Toolbox, and a few readily reproducible lines of code.<\/p><p><b><i>Swag to the first person who shows me how to detect faces in \"visionteam.jpg\" with a File Exchange submission!<\/i><\/b><\/p><p>By the way, you can readily train MATLAB to find objects that are not in the \"pre-trained\" list above. For that, you might find these tools useful:<\/p><div><ul><li><a href=\"\">trainingImageLabeler<\/a><\/li><li><a href=\"\">trainCascadeObjectDetector<\/a><\/li><li><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/39627-cascade-trainer--specify-ground-truth--train-a-detector\">trainCascadeGUI<\/a><\/li><\/ul><\/div><p><a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5174#respond\">Comments welcome<\/a>!<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_a62f3fd51ca34434826c2b25fae8eee2() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='a62f3fd51ca34434826c2b25fae8eee2 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a62f3fd51ca34434826c2b25fae8eee2';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2014 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_a62f3fd51ca34434826c2b25fae8eee2()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2014a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2014a<br><\/p><\/div><!--\r\na62f3fd51ca34434826c2b25fae8eee2 ##### SOURCE BEGIN #####\r\n%% Finding faces in images\r\n\r\n%% \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s Pick this week is\r\n% more of a challenge than a \"Pick\".\r\n\r\n%% \r\n% If I were to search the MATLAB Central File Exchange for \"face detection\"\r\n% (with the quotation marks) I would get a dazzlingREPLACE_WITH_DASH_DASHand somewhat\r\n% overwhelmingREPLACE_WITH_DASH_DASHarray of\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?utf8=%E2%9C%93&term=%22face+detection%22 44 hits>.\r\n% Trying to detect faces (or anything else*) in images seems to\r\n% me a reasonable thing to want to do, and in my mind typifies the\r\n% challenges that the\r\n% <https:\/\/www.mathworks.com\/products\/computer-vision\/ Computer Vision System Toolbox>\r\n% was made to address. In fact, I shudder to think\r\n% of what classical \"image processing\" code designed to detect faces might\r\n% look like. \r\n\r\n%% \r\n% But how do we navigate the 44 files on the File Exchange that facilitate\r\n% that detection process? Where do we start? I can sort the results by\r\n% number of downloads, by rating, by relevancy, by number of downloads,\r\n% ....\r\n\r\n%%\r\n% \r\n% <<https:\/\/blogs.mathworks.com\/pick\/files\/sort.png>>\r\n% \r\n% Some of those downloads seem to have great potential. But where does one start?\r\n\r\n%% \r\n% For this post, I'm going to go \"out-of-model\" and focus not on a\r\n% submitted File Exchange file, but on core capabilities of the Computer\r\n% Vision System Toolbox. This Toolbox has a\r\n% < \"Cascade Object Detector\"> \r\n% that facilitates out-of-the-box detection of faces, eyes,\r\n% noses, mouths, and upper bodies. In fact, there are two in-product face\r\n% detectors, and \"FrontalFaceCART\" (frontal face detection using \"Classification And Regression Tree\" analysis) \r\n% is the default tool behavior.\r\n\r\n%%\r\n% Using the detectors in the Computer Vision System Toolbox is stunningly simple; we simply\r\n% <https:\/\/en.wikipedia.org\/wiki\/Instance_(computer_science) instantiate an instance>\r\n% of the cascade object detector and, for visualization\r\n% purposes, of the vision.ShapeInserter:\r\n\r\nfaceDetector = vision.CascadeObjectDetector;   \r\nshapeInserter = vision.ShapeInserter('BorderColor','Custom','CustomBorderColor',[0 255 255]); \r\n\r\n%%\r\n% We then read input image:\r\nI = imread('visionteam.jpg');\r\nimshow(I);shg;\r\n\r\n%%\r\n% Then we simply \"step\" the instantiated objects on the image of interest.\r\n% The CascadeObjectDetector returns the bounding boxes of the detected\r\n% objects, and the ShapeInserter delimits them in the image:\r\n\r\nbbox = step(faceDetector, I); \r\n% Draw boxes around detected faces and display results              \r\nI_faces = step(shapeInserter, I, int32(bbox));    \r\nimshow(I_faces), title('Detected faces'); \r\n\r\n%% \r\n% Detecting faces using MathWorks' functionality is\r\n% trivially easy. It requires MATLAB, the Image Processing Toolbox, and the\r\n% Computer Vision System Toolbox, and a few readily reproducible lines of\r\n% code. \r\n\r\n%%\r\n% *_Swag to the first person who shows me how to detect faces in \"visionteam.jpg\" \r\n% with a File Exchange submission!_*\r\n\r\n%%\r\n% By the way, you can readily train MATLAB to find objects that are not\r\n% in the \"pre-trained\" list above. For that, you might find these tools\r\n% useful:\r\n% \r\n% * < trainingImageLabeler>\r\n% * < trainCascadeObjectDetector>\r\n% * <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/39627-cascade-trainer--specify-ground-truth--train-a-detector trainCascadeGUI>\r\n\r\n%%\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=4912#respond Comments welcome>!\r\n\r\n##### SOURCE END ##### a62f3fd51ca34434826c2b25fae8eee2\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/sort.png\" onError=\"this.style.display ='none';\" \/><\/div><p>Brett's Pick this week is more of a challenge than a \"Pick\".If I were to search the MATLAB Central File Exchange for \"face detection\" (with the quotation marks) I would get a dazzling--and somewhat... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/03\/14\/detecting-faces-in-images\/\">read more >><\/a><\/p>","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5174"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5174"}],"version-history":[{"count":9,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5174\/revisions"}],"predecessor-version":[{"id":5190,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5174\/revisions\/5190"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}