During opencv-python, the child process cannot start normally

A producer function is responsible for reading frame, from the camera and a consumer function is responsible for using opencv to identify whether there is anyone inside.
if it is a single process, it can run normally, but if you try to use producer-consumer mode through an Manager.Queue communication, simply call cv2 and you will get an error.

the code is as follows:

import cv2
import time
import os
import sys
from multiprocessing import Process, Queue, Pool, Manager
from functools import wraps
from collections import defaultdict


basedir = os.path.abspath(os.path.dirname(__file__))
xml = os.path.join(basedir, "opencv-master/data/haarcascades/haarcascade_frontalface_default.xml")
face_cascade = cv2.CascadeClassifier(xml)
cap = cv2.VideoCapture(0)
start = time.time()
total_fps = 0
manager = Manager()
queue = manager.Queue()


def producer(queue):
    print("producer start")
    fcount = 0
    max_fcount = 20
    sample_rate = 10
    while cap.isOpened() and cv2.waitKey(1) != 27 and fcount < max_fcount:
        ret, frame = cap.read()
        if not ret:
            break
        queue.put(frame)
        cv2.imshow("My Camera",frame)
        fcount += 1
    print("producer stop")
    queue.put("stop")


@timing
def consumer(queue):
    print("consumer start")
    i = -1
    while True:
        info = queue.get()
        i += 1
        print(f"{i}:{type(info)}")
        if isinstance(info, str):
            break
        
        gray = cv2.cvtColor(info, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(
            gray,
            scaleFactor=1.15,
            minNeighbors=5,
            minSize=(15, 15),
        )
        
        for x, y, w, h in faces:
            top_left = (x, y)
            bottom = (x + w, y + h)
            cv2.rectangle(info, top_left, bottom, (125, 255, 255), 1)
        global total_fps
        global start
        total_fps += 1
        fps = "fps:" + str(round(total_fps / (time.time() - start), 0))
        cv2.putText(info, fps, (10, 10), 1, 1, (175, 255, 175), thickness=1)

        cv2.imshow("My Camera",info)

        if not isinstance(faces, tuple):
            -sharp 
            print(f"@{time.ctime()}, {len(faces)} detected!")
            file_name = str(int(time.time())) + ".jpg"
            cv2.imwrite(os.path.join(basedir, "Persons", file_name), info)
        else:
            print("")
    print("consumer stop")


if __name__ == "__main__":
    c = Process(target=consumer, args=(queue,))
    -sharp p.daemon = True
    c.daemon = True
    -sharp p.start()
    c.start()
    -sharp p.join()
    producer(queue)
    c.join()
The symptom of

is that if you type continue, before the following line of code in constumer, you will have all kinds of normal
, but you will report an error as long as you use cv2,.
gray = cv2.cvtColor (info, cv2.COLOR_BGR2GRAY)

if you report an error as shown in the following figure, you can"t see any valid information yet:

could you tell me what"s wrong with it?

Menu