Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do concurrent executions in python?

Description

I have a file `FaceDetection.py` which detects the face from webcam footage using `opencv` and then writes frames into `output` object, converting it into a video. If the face is present in the frame then a function `sendImg()` is called which then calls the function `faceRec()` from another imported file. At last this method prints the face name if the face is known or prints "unknown".

Problem I am facing

face recognition process is quite expensive and that's why I thought that running from different file and with a thread will create a background process and output.write() function won't be interrupted while doing face recognition. But, as sendImg() function gets called, output.write() function gets interrupted, resulting in skipped frames in saved video.

Resolution I am seeking

I want to do the face recognition without interrupting the output.write() function. So, resulting video will be smooth and no frames would be skipped.

Here is the FaceDetection.py

import threading
from datetime import datetime
import cv2
from FaceRecognition import FaceRecognition

face_cascade = cv2.CascadeClassifier('face_cascade.xml')
fr = FaceRecognition()
img = ""

cap = cv2.VideoCapture(0)
path = 'C:/Users/-- Kanbhaa --/Documents/Recorded/'

vid_name = str("recording_"+datetime.now().strftime("%b-%d-%Y_%H:%M").replace(":","_")+".avi")
vid_cod = cv2.VideoWriter_fourcc(*'MPEG')
output = cv2.VideoWriter( str(path+vid_name) , vid_cod, 20.0 ,(640,480))

def sendImage(imgI):
    fr.faceRec(imgI)

while True:
    t,img = cap.read()
    output.write(img)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray,2,4)

    for _ in faces:
        t1 = threading.Thread(target=sendImage,args=(img,))
        t1.start()

    cv2.imshow('img',img)
    if cv2.waitKey(1) & 0XFF == ord('x'):
        break
                                                        
cap.release()
output.release()

Here is the FaceRecognition.py

from simple_facerec import SimpleFacerec


class FaceRecognition:
    def __init__(self):
        global sfr
        # Encode faces from a folder
        sfr = SimpleFacerec()
        sfr.load_encoding_images("C:/Users/UserName/Documents/FaceRecognitionSystem/images/")

    def faceRec(self,frame):
        global sfr
        # Detect Faces
        face_locations, face_names = sfr.detect_known_faces(frame)
        for face_loc, name in zip(face_locations, face_names):
            if name != "Unknown":
                print("known Person detected :) => " + name)
            else:
                print("Unknown Person detected :( => Frame captured...")

Imported file SimpleFacerec in the above code is from youtube. So, I didn't code it. It can be found below.

SimpleFacerec.py

like image 544
Anirudhdhsinh Jadeja Avatar asked Sep 12 '26 07:09

Anirudhdhsinh Jadeja


1 Answers

you write it wrong. It should be

t1 = threading.Thread(target=sendImage, args=(img,))
like image 88
danangjoyoo Avatar answered Sep 13 '26 20:09

danangjoyoo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!