如何将变量从视频 view1(在 view2 内)传递到 Django 中的 view2 模板
How to pass a variable from video view1 (which is inside view2) to view2's template in Django
我有一个看法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")
它使用 class VideoCamera
这是一个 openCV class 来显示相机并在 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
class返回的变量prediction
发送到模板home.html
,这样我就可以输出给用户看。我知道我通常可以将字典 context
传递给 home.html
但我看不到将它从函数 gen
传递到视图 home
的方法,因为它在 StreamingHttpResponse 中被调用在 home.html
.
的 <img>
标签中被调用
如果你只想得到一帧的预测,你不能只添加一个预测方法并像这样在模板视图中调用该方法吗?:
# 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
我有一个看法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")
它使用 class VideoCamera
这是一个 openCV class 来显示相机并在 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
class返回的变量prediction
发送到模板home.html
,这样我就可以输出给用户看。我知道我通常可以将字典 context
传递给 home.html
但我看不到将它从函数 gen
传递到视图 home
的方法,因为它在 StreamingHttpResponse 中被调用在 home.html
.
<img>
标签中被调用
如果你只想得到一帧的预测,你不能只添加一个预测方法并像这样在模板视图中调用该方法吗?:
# 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