Reputation: 1
I have successfully detected faces, save cropped face in a folder. Then view the total count of detected faces on the video using cv2.puttext.
Now I want to show each cropped face on the video just like I am showing the total count.
The code is as follows:
import numpy as np
import cv2
import time
from time import strftime
num = 0
total = 0
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
previous_millis = 0
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'Person Count Algorithm',(10,50), font, 1,(255,0,0),2,cv2.LINE_AA)
S1=int(strftime("%S"))
#print "timing"
#print(S1)
millis = int(round(time.time() * 5000))
interval = 2000
#print(millis)
if(int(millis-previous_millis) >= interval):
previous_millis = millis
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
#time.sleep(2)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
cv2.putText(img, 'person', (x,y-10), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255,0,0), 2)
cv2.imwrite('crop_faces/crop'+str(num)+'.jpg',roi_color)
num = num + 1
print ("FOUND", len(faces), 'PERSON')
total += len(faces)
print ('Total Count:', (total))
font = cv2.FONT_HERSHEY_SIMPLEX
img = cv2.circle(img, (470, 63), 63, (255,0,0), 3)
cv2.putText(img, 'Total Count:', (420,40), font, 0.5,(255,0,0),1,cv2.LINE_AA)
cv2.putText(img, str(total), (436,100), font, 2,(255,0,0),2,cv2.LINE_AA)
cv2.imshow('image',img)
k = cv2.waitKey(1) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
Upvotes: 0
Views: 190
Reputation: 6468
faces = []
face = numpy.copy(img[y:y+h,x:x+w])
face_width, face_height
.face
in the faces
list. faces.append(face)
. Make sure the cropped face does not already exist in the list.px=0, py=0
. Copy all the faces from the list to the image starting from px,py
. img[py:py+face_height,px:px+face_width] = faces[0] ...
Upvotes: 0