使用OpenCV在Django上运行RTSP

u0sqgete  于 2022-12-04  发布在  Go
关注(0)|答案(1)|浏览(261)

我需要在Django的网站上安装一个监控摄像头。我找到了一个关于Youtube的教程,但是很简单。下面是我的代码:

from django.shortcuts import render
from django.views.decorators import gzip
from django.http import StreamingHttpResponse

import cv2

# Create your views here.

@gzip.gzip_page
def Home(request):
    try:
        cam = videoCamera()
        return StreamingHttpResponse(gen(cam), content_type="multipart/x-mixed-replace;boundary=frame")
    except:
        pass

    return render(request, 'index.html')

# Video Capture
class videoCamera(object):

    video = cv2.VideoCapture("[user]:[password]@rtsp://[ip-address]:554/sub")

    while True:
        _, frame = video.read()
        cv2.imshow("RTSP", frame)
        k = cv2.waitKey(10)
        if k == ord('q'):
            break

    video.release()
    cv2.destroyAllWindows()

但是,我遇到一个错误:

cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

有人能帮我吗?

ktecyv1j

ktecyv1j1#

在类中尝试这个摄像机不要忘记导入线程

class VideoCamera(object):
   def __init__(self):
       self.video = cv2.VideoCapture("put your video link here")
       (self.grabbed , self.frame) = self.video.read()
       threading.Thread(target=self.update , args=()).start()
    
    
   def __del__(self):
       self.video.release()

   def get_frame(self):
       image = self.frame 
       _ , jpeg = cv2.imencode(' .jpg' , image)
       return jpeg.tobytes()

   def update(self):
       while True:
           (self.grabbed , self.frame) = self.video.read()

相关问题