我创建了一个 django Web 应用程序并编写了代码以使用 opencv 从我的笔记本电脑摄像头获取视频源。
def start(request):
print("HI")
def testDevice():
cap = cv2.VideoCapture(0)
if cap is None or not cap.isOpened():
print('Warning: unable to open video source: ', source)
testDevice(0) # no printout
testDevice(1) # prints message
global video_thread
if video_thread is None or not video_thread.is_alive():
stop_event.clear()
video_thread = threading.Thread(target=run_video_feed)
video_thread.start()
return HttpResponse("Video feed started successfully.")
else:
return HttpResponse("Video feed is already running.")
上面的代码在本地主机中运行时获取视频源。 但是当我将其上传(托管)到 pythonanywhere 中时,它没有获取视频源并打印警告:无法打开视频源。
我制作了一个示例来解决此问题,方法是使用 Django Channels(用于 asgi 支持)在客户端和服务器之间设置 websocket 流。
注意:据我了解,Pythonanywhere 仅支持 wsgi 架构,因此如果遵循此方法,您可以选择其他托管提供商。
您可以在此存储库中查看:https://github.com/snowby666/Django-OpenCV-Video-Streaming