【发布时间】:2020-05-16 02:59:37
【问题描述】:
我正在尝试使用 django 在网页上显示来自手机(使用 IP 网络摄像头)的实时流。 代码:
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')
def video(request):
return StreamingHttpResponse(gen(IPWebCam(camera_ip)),content_type='multipart/x-mixed-replace; boundary=frame')
class IPWebCam(object):
def __init__(self,camera_ip):
self.url = "https://"+ camera_ip + "/shot.jpg"
print(self.url)
def __del__(self):
cv2.destroyAllWindows()
def get_frame(self):
imgResp = urllib.request.urlopen(self.url,context=context)
imgNp = np.array(bytearray(imgResp.read()), dtype=np.uint8)
img = cv2.imdecode(imgNp, -1)
# We are using Motion JPEG, but OpenCV defaults to capture raw images,
# so we must encode it into JPEG in order to correctly display the
# video stream
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
resize = cv2.resize(img, (270,250), interpolation=cv2.INTER_LINEAR)
ret, jpeg = cv2.imencode('.jpg', resize)
return jpeg.tobytes()
所以这里video 是显示实时流的视图。我正在使用"{% url 'video' %}" 在模板上呈现视图,并在 urlpatterns 中添加了视图。我显示流的模板具有网格视图。我想在每个网格中显示具有不同 IP 的摄像机。现在我能够在所有网格中显示相同的 IP 网络摄像头流,因为只有一个视图,即 video,并且我在模板中呈现相同的视图。
我想知道,如何在对应的网格中显示多个流?一页上有六个网格。我是否应该为每个网格创建一个视图,但会有超过 1 页,我不能继续创建这些视图并将它们添加到 urlpatterns。有没有办法可以使用相同的视图并使用动态 url 来解决这个问题?
【问题讨论】:
标签: django python-3.x django-views django-templates