Unable to detect face and eye with OpenCV in Python -
this code detect face , eyes using webcam getting error
traceback (most recent call last): file "d:/acads/7.1 sem/btp/facedetect-master/6.py", line 28, in <module> eyes = eyecascade.detectmultiscale(roi) nameerror: name 'roi' not defined
but when use code detect faces , eyes in image working without error
import matplotlib import matplotlib.pyplot plt import cv2 import sys import numpy np import os facecascade = cv2.cascadeclassifier('haarcascade_frontalface_default.xml') eyecascade= cv2.cascadeclassifier('haarcascade_eye.xml') video_capture = cv2.videocapture(0) while true: # capture frame-by-frame ret, frame = video_capture.read() faces = facecascade.detectmultiscale(frame) (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) roi = frame[y:y+h, x:x+w] eyes = eyecascade.detectmultiscale(roi) (ex,ey,ew,eh) in eyes: cv2.rectangle(roi,(ex,ey),(ex+ew,ey+eh), 255, 2) cv2.imshow('video', frame) if cv2.waitkey(1) & 0xff == ord('q'): break video_capture.release() cv2.destroyallwindows()
i think problem of indentation.
roi
out of scope when go out of faces
loop.
for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) roi = frame[y:y+h, x:x+w] # eyes detection runs each face eyes = eyecascade.detectmultiscale(roi) (ex,ey,ew,eh) in eyes: cv2.rectangle(roi,(ex,ey),(ex+ew,ey+eh), 255, 2)
Comments
Post a Comment