简体   繁体   English

无法使用 Django 2.2 中的 StreamingHttpResponse 将网络摄像头视频发送到模板(.html)文件

[英]Not able to send webcam video to template(.html) file using StreamingHttpResponse in Django 2.2

I recently started django and I wanted to see the video from the laptop camera to Django 2.2 based web app.我最近启动了 django,我想查看从笔记本电脑摄像头到基于 Django 2.2 的 web 应用程序的视频。 I was successful in viewing the cam video by directly sending response to web using function display_livefeed.通过使用 function display_livefeed 直接向 web 发送响应,我成功地查看了 cam 视频。 Following is my code of views.py of app 'camerafeed'以下是我的应用程序“camerafeed”的views.py代码

class mycamera(object):

    def __init__(self):
        self.frames = cv2.VideoCapture(0)

    def __del__(self):
        self.frames.release()

    def get_jpg_frame(self):
        is_captured, frame = self.frames.read()
        retval, jframe = cv2.imencode('.jpg', frame)
        return jframe.tobytes()

def livefeed():
    camera_object = mycamera()
    while True:
        jframe_bytes = camera_object.get_jpg_frame()
         yield (b'--frame\r\n'
                b'Content-Type: image/jpeg\r\n\r\n' + jframe_bytes + b'\r\n\r\n')


@condition(etag_func=None)
def display_livefeed(self):
     return  StreamingHttpResponse(
            livefeed(),
            content_type='multipart/x-mixed-replace; boundary=frame'
        )

I used path('monitor/', display_livefeed, name='monitor'), to see the video streaming on http://127.0.0.1:8000/monitor/ and it works perfectly >>Image from the streaming video<<我使用path('monitor/', display_livefeed, name='monitor')来查看http://127.0.0.1:8000/monitor/上的视频流,并且效果很好>>来自流视频的图像<<

Now I wanted to display on the html template just line it is done here: https://www.pyimagesearch.com/2019/09/02/opencv-stream-video-to-web-browser-html-page/ but this was done by using Flask.现在我想在 html 模板上显示,只需在此处完成: https://www.pyimagesearch.com/2019/09/02/opencv-stream-video-to-web-browser-html-page/但是这个通过使用 Flask 完成。 But I wanted to do the same using Django and got stuck.但我想使用 Django 做同样的事情并卡住了。 Here is the html file from the above link.这是来自上述链接的 html 文件。

<html>
   <head>
      <title>Pi Video Surveillance</title>
   </head>
   <body>
      <h1>Pi Video Surveillance</h1>
      <img src="{{ url_for('video_feed') }}">
   </body>

I tried to do like this using by making this function:我试图通过制作这个 function 来做到这一点:

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

But it able to see the video on html page using path('', homePageView.as_view(), name='home'), and placing code below in views.py但它可以使用path('', homePageView.as_view(), name='home') 看到 html 页面上的视频,并将代码放在下面的 views.py

class homePageView(TemplateView):
      template_name = 'home.html'

I seen and tried following:我看到并尝试了以下内容:

  1. Django StreamingHttpResponse into a Template Django StreamingHttpResponse 到模板中
  2. Opencv Live Stream from camera in Django Webpage Opencv 实时 Stream 来自 Django 网页中的相机

But may be due to the fact that i am new to all these thing including web development, I might not able to get it done.但可能是因为我对所有这些东西都很陌生,包括 web 开发,我可能无法完成它。 Kindly help explaining me how to do this.请帮助解释我如何做到这一点。

I am using python 3.7 and django 2.2我正在使用 python 3.7 和 django 2.2

I solved it, disable the monitor path from the urlpatterns.我解决了它,从 urlpatterns 禁用监视器路径。 I think that the trouble is that two sources are trying to use the camera.我认为问题在于有两个来源试图使用相机。 Just comment the只需评论

path('monitor', views.display_livefeed, name="monitor")

and it works.它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM