face_recognition/video_multiprocess.py

97 lines
No EOL
3.2 KiB
Python

from multiprocessing import Process, Queue
from queue import Empty
import cv2
import logging
import argparse
import numpy as np
def record(device_id, q1,q2, q3):
capture = cv2.VideoCapture(device_id)
while True:
ret, image = capture.read()
logging.debug('r')
q1.put(image)
q2.put(image)
q3.put(image)
def process1(in_q, out_q):
while True:
image = in_q.get()
logging.debug('r')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cgray = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
out_q.put(cgray)
def process2(in_q, out_q):
while True:
image = in_q.get()
logging.debug('r')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cgray = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
out_q.put(cgray)
def display(image_res, q1, q2, q3):
prev_image1 = np.zeros((image_res[1],image_res[0],3), np.uint8)
prev_image2 = np.zeros((image_res[1],image_res[0],3), np.uint8)
prev_image3 = np.zeros((image_res[1],image_res[0],3), np.uint8)
prev_image4 = np.zeros((image_res[1],image_res[0],3), np.uint8)
while True:
logging.debug('r')
try:
image1 = q1.get_nowait()
image1 = cv2.resize(image1, (image_res[0], image_res[1]))
prev_image1 = image1
except Empty as e:
image1 = prev_image1
try:
image2 = q2.get_nowait()
image2 = cv2.resize(image2, (image_res[0], image_res[1]))
prev_image2 = image2
except Empty as e:
image2 = prev_image2
try:
image3 = q3.get_nowait()
image3 = cv2.resize(image3, (image_res[0], image_res[1]))
prev_image3 = image3
except Empty as e:
image3 = prev_image3
image4 = prev_image4
img_concate_Verti1 = np.concatenate((image1,image2),axis=0)
img_concate_Verti2 = np.concatenate((image3,image4),axis=0)
grid_img = np.concatenate((img_concate_Verti1,img_concate_Verti2),axis=1)
cv2.imshow("Output", grid_img)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Visualise face recognition algorithms.')
parser.add_argument('--camera', '-c', type=int, default=0,
help='Numeric id of the camera')
args = parser.parse_args()
image_size = (int(1920/2), int(1080/2))
# TODO should we use queues here at all?
# https://docs.python.org/3/library/multiprocessing.html#programming-guidelines
# TODO: queue maxsize, or prefrabily some sort of throttled queue (like zmq hight water mark)
q_webcam1 = Queue()
q_webcam2 = Queue()
q_webcam3 = Queue()
q_process1 = Queue()
q_process2 = Queue()
p1 = Process(target=record, args=(args.camera, q_webcam1, q_webcam2,q_webcam3))
p2 = Process(target=display, args=(image_size, q_webcam1, q_process1, q_process2 ))
p3 = Process(target=process1, args=(q_webcam2, q_process1,))
p4 = Process(target=process2, args=(q_webcam3, q_process2,))
p1.start()
p2.start()
p3.start()
p4.start()
p2.join()