Face Detection using Python

data_science
python

#1

Hi all,

I am trying another project which is based on face detection from images using python. I have done some parts of it but I am stuck at a particular part in the project. Please find the link and the error that I am facing and want to know how to solve this error.

Link: https://realpython.com/face-recognition-with-python/

Code:
import sys
imagePath = sys.argv[1]
cascPath = “C:/Users/Vatsal Shah/Desktop/Vatsal/Rutgers/Notes for Sem 3/abc/haarcascade_frontalface_default.xml”

faceCascade = cv2.CascadeClassifier(cascPath)
image = cv2.imread(imagePath)

cv2.imshow(“Faces found”, image)
cv2.waitKey(0)

Error:

error Traceback (most recent call last)
in ()
----> 1 cv2.imshow(“Faces found”, image)
2 cv2.waitKey(0)

error: OpenCV(3.4.1) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:356: error: (-215) size.width>0 && size.height>0 in function cv::imshow

If you can look out let me know.

Best,
vatsy61


#2
`indent preformatted text by 4 spaces`import cv2

face_cascade=cv2.CascadeClassifier(’/home/tarunvakkalagadda/Desktop/Projects/haarcascade_frontalface_default.xml’)
eye_cascade=cv2.CascadeClassifier(’/home/tarunvakkalagadda/Desktop/Projects/haarcascade_eye.xml’)
#function for detection
def detect_face(gray,frame):
#get the b/w and convert to original image
faces = face_cascade.detectMultiScale(gray,1.3,5)
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,1.1,3)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
return frame
video_capture = cv2.VideoCapture(0)
while True:
_,frame=video_capture.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
canvas = detect_face(gray,frame)
cv2.imshow(‘Video’,canvas)
if cv2.waitKey(1) & 0xFF==ord(‘q’):
break
video_capture.release()
cv2.destroyAllWindows()