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

    • TheWay4wardAcademy
      TheWay4wardAcademy

      What version of opencv are u using. I just installed opencv 3.4.1 and in the gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) it keeps crashing

      about 8 days ago
    • müptezel cihat
      müptezel cihat

      5:03 there is some nice drum going on in the background :D

      about 9 days ago
    • Samyukth S
      Samyukth S

      cv2.error: OpenCV(4.1.0) /Users/travis/build/skvark/opencv-python/opencv/modules/objdetect/src/cascadedetect.cpp:1658: error: (-215:Assertion failed) !empty() in function 'detectMultiScale'
      getting this error, could you help please?

      about 9 days ago
    • Joseph Ved
      Joseph Ved

      Hi,
      Thank you for your wonderful work.
      What would be the "BEST" camera to get for this type of projects? Is it the more resolution the better? What do you recommend under $45

      about 13 days ago
    • Jonathan Loyola
      Jonathan Loyola

      my video is very slow. know that have prblem?

      about 16 days ago
    • DAVID SUN
      DAVID SUN

      Thanks for your wonderful videos!
      I have one question: why convert img to gray? I change faces = face_cascade.detectMultiScale(gray) to faces = face_cascade.detectMultiScale(img), still work. So, I think we don't need convert. Am I right?

      about 23 days ago
    • müptezel cihat
      müptezel cihat In reply to DAVID SUN

      its probably faster

      about 9 days ago
    • Marcin Ziajkowski
      Marcin Ziajkowski

      What should I add to that code if I want to use GPU to calculate operations? (Or mayby GPU is used by default)

      about 26 days ago
    • Amitabh Phatak
      Amitabh Phatak

      Just ran my first face detection program.Thanks a lot !

      about 27 days ago
    • sentdex
      sentdex In reply to Amitabh Phatak

      Woo!

      about 27 days ago
    • Darshan Shanbhag
      Darshan Shanbhag

      thank you so much it was very helpful

      about 1 month ago
    • Jose F. Gonzalez
      Jose F. Gonzalez

      Nice joke dude, i love your videos. Really helpful

      about 1 month ago
    • Bernard FAVRE
      Bernard FAVRE

      my final project . https://www.youtube.com/watch?v=skgOfjJbXuc

      about 1 month ago
    • ilham rahman
      ilham rahman

      ModuleNotFoundError: No module named 'cv2'
      why is that?

      about 1 month ago
    • Neeramitra Reddy
      Neeramitra Reddy In reply to ilham rahman

      Install it using pip install opencv or conda install opencv

      about 1 month ago
    • Qwerty Qwerty
      Qwerty Qwerty

      While writing this code i encountered error if its okay then please contact.

      about 1 month ago
    • Khan Saad Bin Hasan
      Khan Saad Bin Hasan

      Doesn't work well with glasses

      about 1 month ago
    • rafael gatiik rojas
      rafael gatiik rojas

      alguien me podria decir porque dice esto ?
      Traceback (most recent call last):
      File "/home/pi/Documents/r-2/ojos_cara.py", line 11, in <module>
      gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
      error: /build/opencv-U1UwfN/opencv-2.4.9.1+dfsg1/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function cvtColor

      about 1 month ago
    • yash gupta
      yash gupta

      hi, sentdex,
      can you please help me in my project...actually one of my mid tasks is to calculate the number of black rod in a black and white image
      thanks in advance

      about 2 months ago
    • Carlos Chavita
      Carlos Chavita

      excellent friend video I would like you to help me with some facial recognition I want to make when someone recognizes the database light a bulb and last on and off, the video continues capturing faces so the led is on hope you can help me friend!!!

      about 2 months ago
    • Matem
      Matem

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


      pip install opencv-python (intall opencv)

      about 2 months 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 2 months 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 2 months ago
    • Rofaida Ali
      Rofaida Ali

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

      about 2 months ago
    • Redbeat Infected
      Redbeat Infected

      we did laugh bro :D thanks for the video

      about 2 months 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 3 months ago
    • aadishesh sharma
      aadishesh sharma In reply to Atta Ullah Afridi

      Write CascadeClassifier
      I hope it helps!

      about 2 months 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 3 months 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 3 months 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 3 months ago
    • Ashwini Bukegar
      Ashwini Bukegar In reply to Ashwini Bukegar

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

      about 2 months 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 2 months 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 3 months ago
    • Vishnu Kannan
      Vishnu Kannan

      mine is damn slow

      about 3 months ago
    • Batata MESTRE
      Batata MESTRE

      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 3 months ago
    • Wilger Aleman
      Wilger Aleman In reply to Batata MESTRE

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

      about 2 months ago
    • Lucas Petersen
      Lucas Petersen In reply to Batata MESTRE

      Show more of your code

      about 3 months 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 3 months ago