Face-Detection-OpenCV icon indicating copy to clipboard operation
Face-Detection-OpenCV copied to clipboard

How to View cropped face on video

Open ghost opened this issue 6 years ago • 0 comments

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")) #how many frames per second #???
#print "timing" 
#print(S1)

millis = int(round(time.time() * 5000)) #it is simply watching the clock in milli second and waiting for 1000 milli seconds to pass #?
				    #when it does it resets the time and calls your face detection and repeats
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()

Any help is appreciated. Thanks in advance.

ghost avatar May 14 '18 07:05 ghost