Assertion fctx->async_lock

0

I have a python app with the Django framework. I display a video stream with detection using yolov5, when i run the app it’s display this eroor Assertion fctx->async_lock failed at libavcodec/pthread_frame.c:167 I think that it happens because of my detection() function . the goal of the detection() function is to send to the client the percent of the detection any time that I have detect when my custom object appears in the video.

i also think that it’s happen because i read the same frame with two functions,i am not sure and don’t know what to do.

here is my code

cap = cv2.VideoCapture("video.mp4")

def stream():
    model.iou=0.5
    while (cap.isOpened()):
        ret, frame = cap.read()
        if not ret:
            print("Error: failed to capture image")
            break
        results = model(frame,augment=False)
        for i in results.render():
            data=im.fromarray(i)
            data.save('demo.jpg')
        annotator = Annotator(frame, line_width=2, pil=not ascii)

        im0 = annotator.result()
        image_bytes = cv2.imencode('.jpg', im0)[1].tobytes()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + image_bytes + b'\r\n')
    cap.release()
    cv2.destroyAllWindows()



def detection():
        if(cap.isOpened()):
            ret,frame = cap.read()
            if not ret:
                print("Erorr:faild to capture image")
                data={"isRunning":False} 
                dataJson=json.dumps(data)
                return dataJson
            results = model(frame, augment=False)  
            pred = results.pandas().xyxy[0]
            for index, row in pred.iterrows():
                if float(row['confidence']) > 0.15:
                    detection=float(row['confidence'])
                    det = '%.2f' % detection
                    data={"det":det,"isRunning":cap.isOpened()}
                    dataJson=json.dumps(data)
                    return dataJson
            data={"isRunning":cap.isOpened()}
            dataJson=json.dumps(data)               
            return dataJson
        data={"isRunning":False}
        dataJson=json.dumps(data)               
        return dataJson



def video_feed(request):
    return StreamingHttpResponse(stream(), content_type='multipart/x-mixed-replace; boundary=frame')   

def detection_percentage(request):
    return HttpResponse(detection())

VideoCapture objects are not guaranteed to be thread-safe (usable from multiple threads).

Ok tnx but what I can do to fix that?

if name == ‘main’:
p1= Process(target = stream)
p2= Process(target = detection)
p1.start()
p2.start()

p1.join()
p2.join()  

I try to add this but still does not work for me,u have maybe other suggestion for me?

create a single mutex for all processes and wrap the frame reading with it.

mutex = Lock()

try:
    self.mutex.acquire()
    ret, img = self.cap.read()
finally:
    self.mutex.release()

that’s an attempt at a brute force fix for whatever’s wrong with OP’s code.

OP uses flask but doesn’t say. OP also doesn’t specify what this Process constructor is.

These kinds of questions show up all the time and they’re always impossible to debug and OPs rarely cooperate.