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
<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.
<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.
<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/second_video_feed/
What could be the reason?
Even tried threading, but to no avail. The final version of views.py I enclose
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