Last active
January 5, 2021 14:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
hi @Mariela1817 maybe you can try this
pip install opencv-contrib-python
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
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'