# Streams from cameras stop if there are two of them on a page

**URL:** <https://forum.djangoproject.com/t/streams-from-cameras-stop-if-there-are-two-of-them-on-a-page/24972>\
**Category:** Getting Started\
**Created:** [November 2, 2023, 11:41am UTC](https://forum.djangoproject.com/t/streams-from-cameras-stop-if-there-are-two-of-them-on-a-page/24972 "2023-11-02T11:41:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![epigraphe](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/epigraphe/32/16766_2.png) [@epigraphe](https://forum.djangoproject.com/u/epigraphe)\
**Post date:** [November 2, 2023, 11:41am UTC](https://forum.djangoproject.com/t/streams-from-cameras-stop-if-there-are-two-of-them-on-a-page/24972/1 "2023-11-02T11:41:07Z")

</div>

Hello.

Without much hope, but I’ll write for help.

I made an application that shows the image from the camera after detection and calculation in real time.

I show the video on the site.  
If I show a stream from one camera

```auto
<img src="{% url 'video_feed' %}" alt="Video Feed 1" width="800" height="600">

```

it works perfectly.

If there are two streams, but from the same camera - ideal.

```auto
<img src="{% url 'video_feed' %}" alt="Video Feed 1" width="800" height="600">
<img src="{% url 'video_feed' %}" alt="Video Feed 1" width="800" height="600">

```

If two streams are from different cameras, it freezes almost completely.

```auto
<img src="{% url 'video_feed' %}" alt="Video Feed 1" width="800" height="600">
<img src="{% url 'second_video_feed' %}" alt="Video Feed 2" width="800" height="600">

```

I open each stream separately - perfect.

[http://127.0.0.1:8000/video\_feed/](http://127.0.0.1:8000/video_feed/)  
[http://127.0.0.1:8000/second\_video\_feed/](http://127.0.0.1:8000/second_video_feed/)

What could be the reason?  
Even tried threading, but to no avail. The final version of views.py I enclose

```auto
from django.views.decorators import gzip
from .object_counter import CountObject_Camera1, VideoCapture_Camera1, CountObject_Camera2, VideoCapture_Camera2
from django.shortcuts import render
import cv2
import threading

rtsp_url1 = "rtsp:///cam/realmonitor?channel=1&subtype=00&authbasic=[AUTH]"  
rtsp_url2 = "rtsp:///cam/realmonitor?channel=1&subtype=00&authbasic=[AUTH]"  

count_object_instance1 = CountObject_Camera1(rtsp_url1)
count_object_instance2 = CountObject_Camera2(rtsp_url2)

video_capture1 = VideoCapture_Camera1(rtsp_url1)
video_capture2 = VideoCapture_Camera1(rtsp_url2)

lock1 = threading.Lock()
lock2 = threading.Lock()

@gzip.gzip_page
def video_feed(request):
    def generate(camera_instance, count_object_instance, lock):
        while True:
            with lock:
                frame = camera_instance.read()

            annotated_frame = count_object_instance.process_frame(frame)
            _, buffer = cv2.imencode('.jpg', annotated_frame)
            frame_bytes = buffer.tobytes()

            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n')

    return StreamingHttpResponse(generate(video_capture1, count_object_instance1, lock1), content_type='multipart/x-mixed-replace; boundary=frame')

def second_video_feed(request):
    def generate(camera_instance, count_object_instance, lock):
        while True:
            with lock:
                frame = camera_instance.read()

            annotated_frame = count_object_instance.process_frame(frame)
            _, buffer = cv2.imencode('.jpg', annotated_frame)
            frame_bytes = buffer.tobytes()

            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n')

    return StreamingHttpResponse(generate(video_capture2, count_object_instance2, lock2), content_type='multipart/x-mixed-replace; boundary=frame')
def index(request):
    return render(request, 'index.html')

```

Thank you

---

<div class="post-metadata">

**Author:** ![epigraphe](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/epigraphe/32/16766_2.png) [@epigraphe](https://forum.djangoproject.com/u/epigraphe)\
**Post date:** [November 10, 2023, 10:37am UTC](https://forum.djangoproject.com/t/streams-from-cameras-stop-if-there-are-two-of-them-on-a-page/24972/2 "2023-11-10T10:37:57Z")

</div>

It was a problem with the detector, not with Django
