Ask Your Question
0

Face detection and time - video

asked 2020-04-03 10:16:34 -0500

updated 2020-04-03 10:42:12 -0500

berak gravatar image

I have an issue with time and face detection. When I 'am inside for loop ,the time sticks a little bit.Code:

import cv2
from datetime import datetime

cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')  
out = cv2.VideoWriter('face_detection.avi' , fourcc , 10.0 , (640 , 480))

while True:
    ret,frame = cap.read()
    font = cv2.FONT_HERSHEY_SIMPLEX
    date_time = str(datetime.today().strftime("%d-%m-%Y")) + '                ' + str(datetime.now().strftime("%H:%M:%S"))  # show date and time
    video = cv2.putText(frame,date_time,(30,400),font,1,(0,0,255),2,cv2.LINE_AA)
    faces = cv2.CascadeClassifier('haarcascade_frontalface_default.xml').detectMultiScale(frame, 1.1, 4)
    for(x,y,w,h) in faces:
        cv2.rectangle(frame , (x,y) , (x+w , y+h) , (255,0,0) , 3)

    cv2.imshow('Face Detection' , frame)
    out.write(frame)

    if(cv2.waitKey(1) == ord('q')):
        break
cap.release()
out.release()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

1

cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

this has to go before the loop. please do not reload a ~1mb xml file per frame (and then throw it away !)

berak gravatar imageberak ( 2020-04-03 10:43:51 -0500 )edit

Still doesn't work... seconds passes faster than normal... I tested many times. Why is this happening?(maybe of the for-loop inside while-loop??)

NikosOpenCV gravatar imageNikosOpenCV ( 2020-04-03 13:36:41 -0500 )edit

Here is tutorial link: Detect face

supra56 gravatar imagesupra56 ( 2020-04-03 21:05:33 -0500 )edit

My problem is the time ,not detection The seconds passes faster than normal(maybe because of the for-loop inside while loop) Does anyone knows how to fix it?

NikosOpenCV gravatar imageNikosOpenCV ( 2020-04-04 17:23:50 -0500 )edit

sorry, but it's unclear what you mean now

berak gravatar imageberak ( 2020-04-05 03:04:28 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-04-05 11:32:37 -0500

supra56 gravatar image
Code:
#!/usr/bin/env python37
#Raspberry pi 3B/3B+, 4B, Buster, OpenCV 4.2.0
#Date: 5th Apriol, 2020

import numpy as np
import cv2
from datetime import datetime

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
fourcc = cv2.VideoWriter_fourcc(*'XVID')  
out = cv2.VideoWriter('face_detection.avi', fourcc, 10.0, (640, 480))
font = cv2.FONT_HERSHEY_SIMPLEX

cap = cv2.VideoCapture(0)

while cap.isOpened():
    date_time = str(datetime.today().strftime("%d-%m-%Y")
                    + '                '
                    + str(datetime.now().strftime("%H:%M:%S")))

    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    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)

    out.write(frame)
    cv2.putText(frame, date_time, (30, 400), font, 1,
                (0, 0, 255), 2, cv2.LINE_AA)

    cv2.imshow('img', frame)
    k = cv2.waitKey(1)  
    if k is 27:
        break

cap.release()
cv2.destroyAllWindows()

In line 32, I change == to is operator for python 3.7 or later. You may used == if its doesn't work.

edit flag offensive delete link more

Comments

I noticed that you declared video ...video = cv2.putText.

supra56 gravatar imagesupra56 ( 2020-04-05 11:35:20 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2020-04-03 10:16:34 -0500

Seen: 66 times

Last updated: Apr 05