【问题标题】:How to pass a variable from video view1 (which is inside view2) to view2's template in Django如何将视频view1(在view2内部)中的变量传递给Django中的view2模板
【发布时间】:2021-06-07 05:35:53
【问题描述】:

我有观点home

def home(request):
    return render(request, 'detection/home.html')

这是它的模板templates/detection/home.html

{% extends "detection/base.html" %}

{% block content %}

<h1>Camera View</h1>

<img src="{% url 'cam-feed' %}"></img>

{% endblock content %}

基于 templates/detection/base.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    {% block content %}
    {% endblock content %}
</body>
</html>

在此页面中,从home.html 可以看出,我使用视图cam_feed 显示相机输出:

def cam_feed(request):
    return StreamingHttpResponse(gen(VideoCamera(), content_type="multipart/x-mixed-replace;boundary=frame")

它使用类VideoCamera,这是一个openCV类来显示相机,并在get_frame中输出一个prediction变量:

class VideoCamera(object):
    def __init__(self):
        self.video = cv2.VideoCapture(0)

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

    def get_frame(self):
        _, image = self.video.read()

        ### All the detections

        # Person Existence Classification
        # RGB_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        im = Image.fromarray(image)
        im = im.resize((128, 128))
        img_array = np.array(im)
        img_array = np.expand_dims(img_array, axis=0)
        prediction = int(model.predict(img_array)[0][0])
        
        _, jpeg = cv2.imencode('.jpg', image)
        return jpeg.tobytes()

cam_feed 还使用函数gen 以适当的形式传递相机输出:

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')

如何将上面VideoCamera类返回的变量prediction(最好每次接收到新帧并进行预测)发送到模板home.html,以便我可以为用户输出查看。我知道我通常可以将字典 context 传递给 home.html 但我看不到将它从函数 gen 传递到查看 home 的方法,因为它在 StreamingHttpResponse 内部调用,在 &lt;img&gt; 的标签中调用home.html.

【问题讨论】:

    标签: django django-views video-streaming streaminghttpresponse


    【解决方案1】:

    如果你只想得到一帧的预测,你不能像这样在模板视图中添加一个预测方法并调用该方法吗?:

    # where VideoCamera is defined:
    class VideoCamera(object):
        def __init__(self):
            self.video = cv2.VideoCapture(0)
    
        def __del__(self):
            self.video.release()
    
        def get_frame(self):
            # unchanged...
    
        def get_prediction(self):
            _, image = self.video.read()
    
            ### All the detections
    
            # Person Existence Classification
            # RGB_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            im = Image.fromarray(image)
            im = im.resize((128, 128))
            img_array = np.array(im)
            img_array = np.expand_dims(img_array, axis=0)
            prediction = int(model.predict(img_array)[0][0])
            return prediction
    
    # in views.py:
    def home(request):
        cam = VideoCamera()
        prediction = cam.get_prediction()
        return render(request, 'detection/home.html', context={'prediction': prediction})
    

    另外你可能想看看django-channels

    【讨论】:

    • 我实际上不想只获得一帧的预测。我想在每次相机获取下一帧(或至少每隔一定时间间隔)时打印预测。但是您的解决方案仅打印第一帧的预测。抱歉,如果我的问题不清楚。
    • 是的,我正在研究 django-channels。如果您能告诉我如何针对这个特定问题进行设置,那就太好了。
    • 我真的不是这方面的专家,但我发现频道文档中的安装章节非常容易理解,如果您只想尝试一下的话。
    • 如果您想不断更新客户端的预测,也许您可​​以以某种方式使用框架对其进行编码,然后在客户端使用 js 或您想在那里使用的任何内容再次将其分开。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多