首页 > 解决方案 > 进程中发送的函数不能改变类字段的值

问题描述

变量 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()

标签: pythonpython-3.xmultiprocessing

解决方案


推荐阅读