{"id":2546,"date":"2015-12-25T08:38:30","date_gmt":"2015-12-25T08:38:30","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2546"},"modified":"2015-12-25T08:38:30","modified_gmt":"2015-12-25T08:38:30","slug":"easy-face-detection-using-opencv-and-python","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2015\/12\/25\/easy-face-detection-using-opencv-and-python\/","title":{"rendered":"Easy Face Detection using OpenCV and Python"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushPython.js\"><\/script>\n<p>A lot of applications and websites incorporate face detection features , specially social network sites and photo\/image databases. Using OpenCV, it is very easy to detect faces within images or even live video or a webcam.<\/p>\n<p>OpenCV has a component called Haar Cascade Classifier which does all the heavy lifting.\u00a0 For most simple purposes, the classifier works out of the box without requiring any training or modification. There are predefined classifier data for detection of faces, eyes, smiles, upper body, lower body etc.<\/p>\n<p>The only drawback is that only frontal faces are detected and faces which are at an angle or turned sideways are difficult to detect using the built-in data. To overcome that limitation to some extent, the sample code below uses a second classifier for faces in profile as well. So basically, the target image is scanned for frontal faces and faces in profile and both the results are independently stored. All identified faces are highlighted in blue rectangles.<\/p>\n<p>The sample code also contains eyes detection, which if found in an image, is highlighted with green rectangles. Example outputs are given below:<\/p>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-2548\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect-620x357.png\" alt=\"facedetect\" width=\"620\" height=\"357\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect-620x357.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect-300x173.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect-940x541.png 940w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect-900x518.png 900w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect.png 1252w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-2549\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect1.png\" alt=\"facedetect\" width=\"189\" height=\"267\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-2550\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect2-620x452.png\" alt=\"facedetect\" width=\"620\" height=\"452\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect2-620x452.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect2-300x219.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect2.png 673w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The two outputs below are obtained from the same image &#8211; one detected frontal faces, the other detected faces in profile.<\/p>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-2551\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect3-620x454.png\" alt=\"facedetect\" width=\"620\" height=\"454\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect3-620x454.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect3-300x220.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect3-940x689.png 940w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect3-900x660.png 900w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetect3.png 2048w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a> <a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetectprofile.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-2552\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetectprofile-620x454.png\" alt=\"facedetectprofile\" width=\"620\" height=\"454\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetectprofile-620x454.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetectprofile-300x220.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetectprofile-940x689.png 940w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetectprofile-900x660.png 900w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/12\/facedetectprofile.png 2048w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The sample code is given below. You have to change the paths as required. The paths for the Haar classifiers need to be absolute and not relative.<\/p>\n<p><pre class=\"brush: python\">import numpy as np\r\nimport cv2\r\nimport copy\r\n\r\nface_cascade = cv2.CascadeClassifier(&#039;\/var\/projects\/opencv-3.0.0\/data\/haarcascades\/haarcascade_frontalface_default.xml&#039;)\r\nalt_face_cascade = cv2.CascadeClassifier(&#039;\/var\/projects\/opencv-3.0.0\/data\/haarcascades\/haarcascade_profileface.xml&#039;)\r\neye_cascade = cv2.CascadeClassifier(&#039;\/var\/projects\/opencv-3.0.0\/data\/haarcascades\/haarcascade_eye.xml&#039;)\r\nimg = cv2.imread(&#039;\/home\/amit\/Downloads\/faces.jpeg&#039;)\r\ngray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\r\nimg2 = copy.copy(img)\r\ngray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\r\ngray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)\r\n\r\nfaces = face_cascade.detectMultiScale(gray, 1.3, 5)\r\nfor (x,y,w,h) in faces:\r\n     cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)\r\n     roi_gray = gray[y:y+h, x:x+w]\r\n     roi_color = img[y:y+h, x:x+w]\r\n     eyes = eye_cascade.detectMultiScale(roi_gray)\r\n     for (ex,ey,ew,eh) in eyes:\r\n         cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)\r\n \r\ncv2.imwrite(&quot;\/home\/amit\/Downloads\/facedetect.png&quot;, img)\r\n\r\nfaces = alt_face_cascade.detectMultiScale(gray2, 1.3, 5)\r\nfor (x,y,w,h) in faces:\r\n     cv2.rectangle(img2,(x,y),(x+w,y+h),(255,0,0),2)\r\n     roi_gray = gray[y:y+h, x:x+w]\r\n     roi_color = img[y:y+h, x:x+w]\r\n     eyes = eye_cascade.detectMultiScale(roi_gray)\r\n     for (ex,ey,ew,eh) in eyes:\r\n         cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)\r\n \r\ncv2.imwrite(&quot;\/home\/amit\/Downloads\/facedetectprofile.png&quot;, img2)\r\n<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>A lot of applications and websites incorporate face detection features , specially social network sites and photo\/image databases. Using OpenCV, it is very easy to <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2015\/12\/25\/easy-face-detection-using-opencv-and-python\/\" title=\"Easy Face Detection using OpenCV and Python\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2549,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[298,314,297],"tags":[],"class_list":["post-2546","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-opencv","category-python"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2546"}],"version-history":[{"count":4,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2546\/revisions"}],"predecessor-version":[{"id":2555,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2546\/revisions\/2555"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2549"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}