In this OpenCV with Python tutorial, we're going to discuss object detection with Haar Cascades. We'll do face and eye detection to start. In order to do object recognition/detection with cascade files, you first need cascade files. For the extremely popular tasks, these already exist. Detecting things like faces, cars, smiles, eyes, and license plates for example are all pretty prevalent.

First, I will show you how to use these cascade files, then I will show you how to embark on creating your very own cascades, so that you can detect any object you want, which is pretty darn cool!

You can use Google to find various Haar Cascades of things you may want to detect. You shouldn't have too much trouble finding the aforementioned types. We will use a Face cascade and Eye cascade. You can find a few more at the root directory of Haar cascades. Note the license for using/distributing these Haar Cascades.

text-based tutorial and sample code: pythonprogramming.net/haar-cascade-face-eye-detection-python-opencv-tutorial/

pythonprogramming.net
twitter.com/sentdex
www.facebook.com/pythonprogramming.net/
plus.google.com/+sentdex

Comments

    • mate gm
      mate gm

      https://github.com/voidstellar/haar-cascade-files (XML files)


      pip install opencv-python (intall opencv)

      about 6 days ago
    • Kanasai Amir
      Kanasai Amir

      Hi bro. May i know the max distance between the camera and the driver? And the camera must exactly infront of driver or can be side a bit?

      about 14 days ago
    • Mariam myriam
      Mariam myriam

      OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/pi/opencv-3.3.0/modules/imgproc/src/color.cpp, line 10638
      Traceback (most recent call last):
      File "test_detection_img.py", line 15, in <module>
      gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      cv2.error: /home/pi/opencv-3.3.0/modules/imgproc/src/color.cpp:10638: error: (-215) scn == 3 || scn == 4 in function cvtColor

      about 16 days ago
    • Rofaida Ali
      Rofaida Ali

      i would like to calculate the head inclination , could you help me please

      about 20 days ago
    • Redbeat Infected
      Redbeat Infected

      we did laugh bro :D thanks for the video

      about 25 days ago
    • Atta Ullah Afridi
      Atta Ullah Afridi

      hello everyone i hope you all are felling well , would anyone like tell me about an error i got in the same code or sentdex can you please.
      the issue i got here is
      module 'cv2.cv2' has no attribute 'cascadeclassifier'
      ??????

      about 28 days ago
    • aadishesh sharma
      aadishesh sharma In reply to Atta Ullah Afridi

      Write CascadeClassifier
      I hope it helps!

      about 23 days ago
    • Jaleel Dev
      Jaleel Dev

      Traceback (most recent call last):
      File "C:/Python/recognizer.py", line 16, in <module>
      faces = face_cascade.detectMultiScale(gray, 1.3, 5)
      cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1658: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
      i run the above code but i got the error plz help me to resolve

      about 28 days ago
    • Mariam myriam
      Mariam myriam

      Very interesting video but I don't understand why I am getting this error message, any help please:


      OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/pi/opencv-3.3.0/modules/imgproc/src/color.cpp, line 10638
      Traceback (most recent call last):
      File "new_face_eyes_detection.py", line 15, in <module>
      gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      cv2.error: /home/pi/opencv-3.3.0/modules/imgproc/src/color.cpp:10638: error: (-215) scn == 3 || scn == 4 in function cvtColor


      ps: I use raspberry PI 3 model B+ and working on raspbian

      about 1 month ago
    • Ashwini Bukegar
      Ashwini Bukegar

      error: OpenCV(4.1.0) ..\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'


      got this error please help me solving this.

      about 1 month ago
    • Ashwini Bukegar
      Ashwini Bukegar In reply to Ashwini Bukegar

      +yakkala kesava venkata sai okay sir thanku i will try with this.

      about 13 days ago
    • yakkala kesava venkata sai
      yakkala kesava venkata sai In reply to Ashwini Bukegar

      try to change the number in video caputure
      for example in my case i changed to '0' where as '1' is there in this video
      cap = cv2.VideoCapture(0)

      about 25 days ago
    • Bader Kh Ajjour
      Bader Kh Ajjour

      can you help me to write a python code for chess game, that can recognize the edges of the chessboard also the chess pieces??

      about 1 month ago
    • Vishnu Kannan
      Vishnu Kannan

      mine is damn slow

      about 1 month ago
    • Wernekinho Ye
      Wernekinho Ye

      Traceback (most recent call last):
      File "C:\Users\Cauã Wernek\Desktop\calc\faceviewer.py", line 13, in <module>
      faces = face_cascade.detectMultiScale(gray, 1.3, 5)
      NameError: name 'face_cascade' is not defined


      error pls fix

      about 1 month ago
    • Wilger Aleman
      Wilger Aleman In reply to Wernekinho Ye

      that because you havent define the right path of your haar files in the code

      about 19 days ago
    • Lucas Petersen
      Lucas Petersen In reply to Wernekinho Ye

      Show more of your code

      about 29 days ago
    • c0nstance
      c0nstance

      Hi! Thanks for the guide. Do you have a guide to help with putting a label to the person in front of the webcam?

      about 1 month ago
    • Rob Thorn
      Rob Thorn

      I have watched a number of your videos and I find them pretty good but I have some suggestions for future videos you could make. A criticism I have is that you spend too much time typing in code, something that is not really helpful. To your credit, I am able to download the code and run it for myself although you could make it a bit easier to grab the code and supporting files. But there is another suggestion that might be more helpful.

      Your videos I have seen demonstrate some feature of the OpenCV library. While this is interesting what might be more informative is a series of videos that show what different functions are available. In the same amount of time that you currently use to demonstrate one feature you might be able to demonstrate ten or twelve different features that tend to demonstrate the spectrum of available functions in the library. As I understand it, there are over one thousand individual features so a quick demo of some of them could be a good introduction to this important body of work.

      FYI: Slackware Linux (NOT ubuntu), OpenCV 4.0, Python 3.7.2 (although I prefer C++ using g++ 5.3.0)

      about 1 month ago
    • CHUNG YAU ONG
      CHUNG YAU ONG

      Do anyone have mouth and nose cascade?

      about 1 month ago
    • Faiskhanhacker
      Faiskhanhacker

      what if i want to detect change like : in simple A4 type : i put hole on it through pencil / and want to detect latest change : suppose 4 whole were already there , and if there is new one hole occurs it should highlight... I hope u got my problem ? any hint on that ? pleass

      about 1 month ago
    • Grobocop12
      Grobocop12

      you are my personal Jesus

      about 2 months ago
    • MUHAMMAD DICKYJUNIFA
      MUHAMMAD DICKYJUNIFA

      hi wanna asking,why i got an error in function detect.multiscale

      about 2 months ago
    • MUHAMMAD DICKYJUNIFA
      MUHAMMAD DICKYJUNIFA In reply to MUHAMMAD DICKYJUNIFA

      +gitsjits already did that but still got an error

      about 1 month ago
    • gitsjits
      gitsjits In reply to MUHAMMAD DICKYJUNIFA

      i think its detectMultiScale not detect.multiscale

      about 1 month ago
    • Tech M
      Tech M

      Teach how make identification of the user. I saw a video of u about it... and I did not understand much... I'm Brazilian and study English hehehe.. but I like so much of u job!! Congratulations!! I wait your answer.

      about 2 months ago
    • Rafli Alhamdi
      Rafli Alhamdi

      nice video dude

      about 2 months ago
    • MOHAMED FATIH
      MOHAMED FATIH

      cv2.error: OpenCV(4.0.0) /io/opencv/modules/objdetect/src/cascadedetect.cpp:1658: error: (-215:Assertion failed) !empty() in function 'detectMultiScale

      about 2 months ago
    • Vasu Sharma
      Vasu Sharma

      Why did you take y co-ordinate first in roi_gray and roi_color...

      about 2 months ago
    • Ñagarjuna Ředdipaku
      Ñagarjuna Ředdipaku

      Hey how to calculate symmetric face values(measurements of nose, eyes, ear, lips and face square values ) can u.. Explain that please please

      about 2 months ago
    • Richa Sharma
      Richa Sharma

      Hello sir, I would like to ask that how to evaluate the performance of haar cascade classifier in test dataset i.e. How to calculate the confusion matrix?

      about 2 months ago
    • Nurbol Bizhigit
      Nurbol Bizhigit

      That is epic,dude.Thank you for your this informative videos

      about 2 months ago
    • Skill F
      Skill F

      how to read multiple faces and eyes in an image?? I tried this method but it only detects one person's eye..what's the reason??

      about 3 months ago
    • J
      J

      Can someone please explain to me how cv2.rectangle(roi_color....) creates a rectangle on the img when we show it?

      about 3 months ago
    • syed talat
      syed talat

      how system respond me after recognizing me for example i did face recognition on raspberry pi i want that after recognition system respond me by name "hello Talat" etc. anyone can help?

      about 3 months ago
    • FIRZ25
      FIRZ25

      Traceback (most recent call last):
      File "C:/Users/Kiyoz/Desktop/opencv16.py", line 12, in <module>
      gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
      How to solve this?

      about 3 months ago
    • Prateek Joshi
      Prateek Joshi

      When I run the program, in the output screen I only get a static image with the detected rectangles and not a live video output . Is it the constraints of my computer or something else??

      about 3 months ago