变量self.process_this_frame在过程中未更改。 现在,我只想处理应处理的上一帧并跳过其他帧。
import cv2
import face_recognition
import multiprocessing
class FaceLocationSender:
def __init__(self, camera_url):
self.video_capture = cv2.VideoCapture(camera_url)
self.face_locations = []
self.process_this_frame = True
def get_faces_from_frame(self, frame):
self.face_locations = face_recognition.face_locations(frame)
self.process_this_frame = True
def start(self):
while True:
ret, frame = self.video_capture.read()
small_frame = cv2.resize(frame, (0, 0), fx=1/2, fy=1/2)
rgb_small_frame = small_frame[:, :, ::-1]
if self.process_this_frame:
self.process_this_frame = False
process = multiprocessing.Process(target=self.get_faces_from_frame, args=(rgb_small_frame,))
process.start()
for (top, right, bottom, left) in self.face_locations:
top *= 2
right *= 2
bottom *= 2
left *= 2
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 0), 2)
cv2.imshow('Video', cv2.resize(frame, (1200, 600)))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
self.video_capture.release()
cv2.destroyAllWindows()