使用django的RTSP流

e5nszbig  于 2023-03-20  发布在  Go
关注(0)|答案(1)|浏览(270)

我试图把一个网址基于我的views.py,但它返回错误:

找不到'transmition'的反转。'transmition'不是有效的视图函数或模式名称。

项目urls.py:

from django.contrib import admin
from django.urls import path, include

from django.conf.urls.static import static
from django.conf import settings
from plataforma.views import *
from django.views.generic.base import TemplateView

urlpatterns = [
                  path("admin/", admin.site.urls),
                  path('contas/', include('django.contrib.auth.urls')),
                  path('', TemplateView.as_view(template_name='index.html'), name='index'),
                  path('plataforma/', TemplateView.as_view(template_name='plataforma.html'), name='plataforma'),
                  path('plataforma/stream', TemplateView.as_view(template_name='stream.html'), name='stream'),
              ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

应用程序urls.py:

from django.urls import path

from .views import *

urlpatterns = [
    path('plataforma', PlataformaView.as_view(), name='plataforma'),
    path('stream/', StreamView.as_view(), name='stream'),
    path('transmition/', transmition, name='transmition'),
]

应用程序views.py:

from django.shortcuts import render, redirect
from django.contrib import messages
from django.core.mail import EmailMessage
from django.views.decorators import gzip
from django.views.generic import TemplateView
from django.http import StreamingHttpResponse

import cv2
import threading

@gzip.gzip_page
def transmition(request):
    try:
        cam = VideoCamera()
        return StreamingHttpResponse(gen(cam), mimetype="multipart/x-mixed-replace;boundary=frame")
    except:
        pass
    # return render(request, 'stream.html')

class VideoCamera(object):
    def __init__(self):
        self.video = cv2.VideoCapture(
            "rtsp://jorge:jorge33621005@189.124.26.190:554/cam/realmonitor?channel=1&subtype=1")
        (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()

def gen(camera):
    while True:
        frame = camera.get_frame()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

class PlataformaView(TemplateView):
    template_name = 'plataforma.html'

class StreamView(TemplateView):
    template_name = 'stream.html'

数据流.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <h1>Test</h1>
  <img src="{% url 'transmition' %}">
</body>
</html>

由于我是一个初学者,我不能在HTML页面中传输RTSP流。我需要流和样式的'传输'功能。

hc8w905p

hc8w905p1#

您可以在应用的www.example.com中定义app_nameurls.py,如下所示:

from django.urls import path

from .views import *

app_name='plataforma'

urlpatterns = [
    path('', PlataformaView.as_view(), name='plataforma_view'),
    path('stream/', StreamView.as_view(), name='stream'),
    path('transmition/', transmition, name='transmition'),
]

然后在URL标记中调用时使用它,如下所示:

<img src="{% url 'plataforma:transmition' %}">

您可以根据需要给予任何app_name

编辑

您还应该删除项目的www.example.com中不必要的路由urls.py,因此它应该如下所示:

from django.contrib import admin
from django.urls import path, include

from django.conf.urls.static import static
from django.conf import settings
from plataforma.views import *
from django.views.generic.base import TemplateView

urlpatterns = [
    path("admin/", admin.site.urls),
    path('contas/', include('django.contrib.auth.urls')),
    path('', TemplateView.as_view(template_name='index.html'), name='index'),
    path('plataforma/', include('plataforma.urls'), namespace='plataforma')

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

相关问题