# Assertion fctx-\>async\_lock

**URL:** <https://forum.opencv.org/t/assertion-fctx-async-lock/10887>\
**Category:** Python\
**Tags:** programming\
**Created:** [November 9, 2022, 11:01am UTC](https://forum.opencv.org/t/assertion-fctx-async-lock/10887 "2022-11-09T11:01:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ilay\_Asis](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ilay_asis/32/6448_2.png) [@Ilay\_Asis](https://forum.opencv.org/u/Ilay_Asis)\
**Post date:** [November 9, 2022, 11:01am UTC](https://forum.opencv.org/t/assertion-fctx-async-lock/10887/1 "2022-11-09T11:01:57Z")

</div>

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

```auto
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())

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [November 9, 2022, 7:01pm UTC](https://forum.opencv.org/t/assertion-fctx-async-lock/10887/2 "2022-11-09T19:01:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ilay\_Asis](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ilay_asis/32/6448_2.png) [@Ilay\_Asis](https://forum.opencv.org/u/Ilay_Asis)\
**Post date:** [November 9, 2022, 7:09pm UTC](https://forum.opencv.org/t/assertion-fctx-async-lock/10887/3 "2022-11-09T19:09:04Z")

</div>

Ok tnx but what I can do to fix that?

---

<div class="post-metadata">

**Author:** ![Ilay\_Asis](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ilay_asis/32/6448_2.png) [@Ilay\_Asis](https://forum.opencv.org/u/Ilay_Asis)\
**Post date:** [November 16, 2022, 8:28pm UTC](https://forum.opencv.org/t/assertion-fctx-async-lock/10887/4 "2022-11-16T20:28:22Z")

</div>

> [@crackwitz](#):
>
> usable from multiple thread

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?

---

<div class="post-metadata">

**Author:** ![HerrAskin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/herraskin/32/9303_2.png) [@HerrAskin](https://forum.opencv.org/u/HerrAskin)\
**Post date:** [December 30, 2023, 10:47am UTC](https://forum.opencv.org/t/assertion-fctx-async-lock/10887/5 "2023-12-30T10:47:09Z")

</div>

> [@crackwitz](#):
>
> `VideoCapture` objects are not guaranteed to be thread-safe (usable from multiple threads).

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

```auto
mutex = Lock()

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

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [December 30, 2023, 2:02pm UTC](https://forum.opencv.org/t/assertion-fctx-async-lock/10887/6 "2023-12-30T14:02:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [December 30, 2023, 2:02pm UTC](https://forum.opencv.org/t/assertion-fctx-async-lock/10887/7 "2023-12-30T14:02:49Z")

</div>

Closed on December 30, 2023, 2:02pm UTC


