{"id":78649,"date":"2020-05-10T18:21:34","date_gmt":"2020-05-10T12:51:34","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=78649"},"modified":"2020-05-10T18:21:34","modified_gmt":"2020-05-10T12:51:34","slug":"face-recognition-project-python-opencv","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/","title":{"rendered":"Real-time Face Recognition with Python &amp; OpenCV"},"content":{"rendered":"<p><strong>Real-time Face recognition python project with OpenCV<\/strong><\/p>\n<p>In this beginner&#8217;s project, we will learn how to implement real-time human face recognition. We will build this project in Python using OpenCV.<\/p>\n<p>We will study the Haar Cascade Classifier algorithms in OpenCV. Haar Cascade Classifier is a popular algorithm for object detection.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/Face-Recognition-Python-Project.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78652\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/05\/Face-Recognition-Python-Project.gif\" alt=\"Face Recognition Python Project\" width=\"1914\" height=\"991\" \/><\/a><\/p>\n<h2>Face Recognition Python Project:<\/h2>\n<p>Face Recognition is a technology in computer vision. In Face recognition \/ detection we locate and visualize the human faces in any digital image.<\/p>\n<p>It is a subdomain of Object Detection, where we try to observe the instance of semantic objects. These objects are of particular class such as animals, cars, humans, etc. Face Detection technology has importance in many fields like marketing and security.<\/p>\n<h3>Cascade Classifiers and Haar Features:<\/h3>\n<p>Cascade Classifiers and Haar Features are the methods used for Object Detection.<\/p>\n<p>It is a machine learning algorithm where we train a cascade function with tons of images. These images are in two categories: positive images containing the target object and negative images not containing the target object.<\/p>\n<p>There are different types of cascade classifiers according to different target objects. In our project, we will use a classifier that considers the human face to recognize it as the target object.<\/p>\n<p>Haar Feature selection technique has a target to extract human face features. Haar features are like convolution kernels. These features are different permutations of black and white rectangles. In each feature calculation, we find the sum of pixels under white and black rectangles.<\/p>\n<h3>Haar-cascade Detection in OpenCV:<\/h3>\n<p>OpenCV provides pre-trained models on Haar features and <a href=\"https:\/\/docs.opencv.org\/2.4\/modules\/objdetect\/doc\/cascade_classification.html\">Cascade classifiers<\/a>. These models are located in OpenCV installation. You can find the necessary XML files at:<\/p>\n<pre>\/home\/&lt;username&gt;\/.local\/lib\/&lt;python-version&gt;\/site-packages\/cv2\/data\/<\/pre>\n<p>In the below code we will see how to use these pre-trained Haar cascade models to detect Human Face. We will implement a real-time human face recognition with python<\/p>\n<h3>Steps to implement human face recognition with Python &amp; OpenCV:<\/h3>\n<p>First, create a python file face_detection.py and paste the below code:<\/p>\n<p>1. Imports:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import cv2\nimport os<\/pre>\n<p>2. Initialize the classifier:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">cascPath=os.path.dirname(cv2.__file__)+\"\/data\/haarcascade_frontalface_default.xml\"\nfaceCascade = cv2.CascadeClassifier(cascPath)<\/pre>\n<p>3. Apply faceCascade on webcam frames:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">video_capture = cv2.VideoCapture(0)\n\nwhile True:\n    # Capture frame-by-frame\n    ret, frames = video_capture.read()\n\n    gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)\n\n    faces = faceCascade.detectMultiScale(\n        gray,\n        scaleFactor=1.1,\n        minNeighbors=5,\n        minSize=(30, 30),\n        flags=cv2.CASCADE_SCALE_IMAGE\n    )\n\n    # Draw a rectangle around the faces\n    for (x, y, w, h) in faces:\n        cv2.rectangle(frames, (x, y), (x+w, y+h), (0, 255, 0), 2)\n\n    # Display the resulting frame\n    cv2.imshow('Video', frames)\n\n    if cv2.waitKey(1) &amp; 0xFF == ord('q'):\n        break<\/pre>\n<p>4. Release the capture frames:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">video_capture.release()\ncv2.destroyAllWindows()<\/pre>\n<p>5. Now, run the project file using:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">python3 face_detection.py<\/pre>\n<p>You will observe the bounding boxes in webcam frames. To stop the webcam capture press &#8220;q&#8221;.<\/p>\n<h2>Summary:<\/h2>\n<p>In this deep learning project, we developed a model for real-time human face recognition with python and opencv.<\/p>\n<p>We discussed about Face detection, Cascade classifier, and Haar features, and finally how to use pre-trained model to detect human face in real-time.<\/p>\n<p><strong>Did you like the tutorial?<\/strong> Please rate <strong><a href=\"https:\/\/www.facebook.com\/TechVidvan\/\">TechVidvan on Facebook<\/a><\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Real-time Face recognition python project with OpenCV In this beginner&#8217;s project, we will learn how to implement real-time human face recognition. We will build this project in Python using OpenCV. We will study the&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":78653,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[210],"tags":[2596,2597,2598,2599,2600,391,483],"class_list":["post-78649","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-machine-learning","tag-computer-vision","tag-deep-learning","tag-deep-learning-project","tag-face-detection","tag-face-recognition-python-project","tag-opencv-project","tag-python-project"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Real-time Face Recognition with Python &amp; OpenCV - TechVidvan<\/title>\n<meta name=\"description\" content=\"Develop real-time face recognition python project with OpenCV. This deep learning project for beginners uses Haar Cascade Classifier algorithms in OpenCV.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Real-time Face Recognition with Python &amp; OpenCV - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Develop real-time face recognition python project with OpenCV. This deep learning project for beginners uses Haar Cascade Classifier algorithms in OpenCV.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-10T12:51:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Face-Recognition-python-project-opencv.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Real-time Face Recognition with Python &amp; OpenCV - TechVidvan","description":"Develop real-time face recognition python project with OpenCV. This deep learning project for beginners uses Haar Cascade Classifier algorithms in OpenCV.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/","og_locale":"en_US","og_type":"article","og_title":"Real-time Face Recognition with Python &amp; OpenCV - TechVidvan","og_description":"Develop real-time face recognition python project with OpenCV. This deep learning project for beginners uses Haar Cascade Classifier algorithms in OpenCV.","og_url":"https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-05-10T12:51:34+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Face-Recognition-python-project-opencv.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Real-time Face Recognition with Python &amp; OpenCV","datePublished":"2020-05-10T12:51:34+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/"},"wordCount":422,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Face-Recognition-python-project-opencv.jpg","keywords":["computer vision","Deep learning","deep learning project","Face Detection","face recognition python project","opencv project","Python project"],"articleSection":["Machine Learning Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/","url":"https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/","name":"Real-time Face Recognition with Python &amp; OpenCV - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Face-Recognition-python-project-opencv.jpg","datePublished":"2020-05-10T12:51:34+00:00","description":"Develop real-time face recognition python project with OpenCV. This deep learning project for beginners uses Haar Cascade Classifier algorithms in OpenCV.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Face-Recognition-python-project-opencv.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/05\/Face-Recognition-python-project-opencv.jpg","width":802,"height":420,"caption":"Face Recognition python project opencv"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/face-recognition-project-python-opencv\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Real-time Face Recognition with Python &amp; OpenCV"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/78649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=78649"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/78649\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/78653"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=78649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=78649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=78649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}