Skip to content

Instantly share code, notes, and snippets.

@radames
Last active January 5, 2021 14:25
Show Gist options
  • Save radames/effc25355c194bd23bbd1d0cbf87d449 to your computer and use it in GitHub Desktop.
Save radames/effc25355c194bd23bbd1d0cbf87d449 to your computer and use it in GitHub Desktop.
Example of Python with Opencv and camera face detection - repo complete https://github.com/radames/python_opencv_camera_haar
import cv2
cap = cv2.VideoCapture(0)
cap.set(3, 640) # WIDTH
cap.set(4, 480) # HEIGHT
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.xml")
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
print(len(faces))
# Display the resulting frame
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
cv2.imshow('frame', frame)
k = cv2.waitKey(1)
if k == ord('q') or k == 27:
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
@Anirudh15376
Copy link

Anirudh15376 commented Aug 26, 2020 via email

@Rasika-Gulhane
Copy link

Why is this error? Please help

error Traceback (most recent call last)
in
----> 1 faces= face_classifier.detectMultiScale(gray, 1.3, 5) #location of feature on face

error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-q0nmoxxv\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

@radames
Copy link
Author

radames commented Sep 9, 2020

Hi @Rasika-Gulhane the problem is the path for the haar cascades double check if it's correct, or use this new syntax

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.xml")

@Rasika-Gulhane
Copy link

Yes, working now. Thanks!

@Mariela1817
Copy link

practica $ python guardardatos.py
Traceback (most recent call last):
File "guardardatos.py", line 9, in
faceClassif= cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
AttributeError: 'module' object has no attribute 'data'

@radames
Copy link
Author

radames commented Dec 22, 2020

hi @Mariela1817 maybe you can try this

pip install opencv-contrib-python

@bxsyia
Copy link

bxsyia commented Jan 5, 2021

hi i am getting this error:

mouth = mouth_cascade.detectMultiScale(roi_gray)
for (mx, my, mw, mh) in mouth:
cv2.rectangle(roi_color, (mx, my), (mx + mw, my + mh), (0, 0, 0), 2)

error: OpenCV(4.3.0) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

and i have already used this code:
mouth_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_mcs_mouth.xml')

but its still not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment