I have written a code, in which I am supposed to insert a video as input. Then I want to extract the faces, from that video. Then I want to store those detected faces from a video in a folder. But my code is not working. It does not show any output. Does it need any third party software? Please help me. Here what I have tried.
What I have tried:
import cv2
import numpy as np
face_classifier = cv2.CascadeClassifier('harcascades/haarcascade_frontalface_default.xml')
# detect face and return the croppped facee
def face_extractor(img):
grey_scale = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(grey_scale, 1.1, 5)
if faces is ():
return None
#cropping found faces
for (x, y, w, h) in faces:
cropped = img[y:y+h , x:x+w]
return cropped
#video Input
cap = cv2.VideoCapture('C:\Users\ADMIN\Desktop\frames.mp4')
#just a counter
count = 0
#keeping on extracting facess
while True:
ret, frame = cap.read()
if face_extractor(frame) is not None:
count +=1
face = cv2.resize(face_extractor(frame),(400,400))
face = cv2.cvtColor(face,cv2.COLOR_BGR2GRAY)
file_storage = './DATASET/' + str (count) + '.jpg'
cv2.imwrite(file_storage,face)
#put text on line image as counting
cv2.putText(face,str(count),(60,60),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
cv2.imshow("Cropped face ",face)
else:
print ("face not found. . .")
pass
if cv2.waitKey(1)==13 :
break
cap.release()
cv2.destroyAllWindows()
print ("done. . . .")