我有一个基于Python
和Flask
的简单Web应用程序,带有三个HTML按钮。
<!DOCTYPE html>
<html>
<head>
<title>Raspberry Pi OpenCV Webstream</title>
<!--<link rel = "stylesheet" href = "{{ url_for('static', filename='css/main.css') }}">-->
</head>
<body>
<header>
<h1 class = "Title">Raspberry Pi OpenCV Webstream</h1>
</header>
<main>
<table width = "100%" border = "0">
<tr>
<td>
<img height = "800" width = "800" src = "{{ url_for('VideoFrame') }}">
</td>
</tr>
<tr>
<td>
<form method = "post" action = "/">
<button class = "ActionButton" type = "submit" name = "action_start" value = "1">Start</button>
<button class = "ActionButton" type = "submit" name = "action_pause" value = "1">Pause</button>
<button class = "ActionButton" type = "submit" name = "action_stop" value = "1">Stop</button>
</form>
</td>
</tr>
</table>
</main>
</body>
</html>
还有我的Python
应用程序
from OpenCVWebstream import OpenCVWebstream
IsActive = True
def onStop():
global IsActive
IsActive = False
if(__name__ == "__main__"):
app = OpenCVWebstream(onStop = onStop)
app.Run()
while(IsActive):
pass
print("[INFO] Exit application...")
exit(0)
import cv2
import time
from threading import Thread
from multiprocessing import Process
from flask import Flask
from flask import Response
from flask import render_template
from flask import request
class OpenCVWebstream():
def __init__(self, CameraID = 0, onStop = None):
self.__app = Flask(__name__)
# On stop handler
self.__onStop = onStop
# Open the camera
#self.__Camera = cv2.VideoCapture(CameraID)
# Current image
self.__CurrentFrame = None
self.__CaptureWorker = None
self.__VideoActive = False
self.__VideoPaused = False
# Register endpoints
self.__app.add_url_rule(rule = "/", endpoint = "index", view_func = self.__showIndex, methods = ["GET", "POST"])
self.__app.add_url_rule(rule = "/VideoFrame", endpoint = "VideoFrame", view_func = self.__getVideoFrame)
self.__Server = Thread(target = self.__flaskThread)
self.__Server.setDaemon(True)
def __getFrame(self):
while True:
if(self.__CurrentFrame is None):
continue
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n'+ self.__CurrentFrame + b'\r\n')
# This time is neccessary when you use Firefox
time.sleep(0.01)
def __flaskThread(self):
self.__app.run(host = "0.0.0.0", port = 80)
def __captureThread(self):
while(True):
if(not(self.__VideoPaused)):
#ret, self.__CurrentFrame = self.__Camera.read()
pass
if(not(self.__VideoActive)):
break
"""
Endpoints
"""
def __showIndex(self):
if(request.method == "POST"):
if(request.form.get("action_start") == "1"):
self.StartCapture()
elif(request.form.get("action_pause") == "1"):
self.PauseCapture()
elif(request.form.get("action_stop") == "1"):
self.Stop()
return render_template("index.html")
def __getVideoFrame(self):
return Response(self.__getFrame(), mimetype = "multipart/x-mixed-replace; boundary=frame")
"""
Public methods
"""
def StartCapture(self):
self.__VideoActive = True
self.__VideoPaused = False
self.__CaptureWorker = Thread(target = self.__captureThread)
self.__CaptureWorker.daemon = True
self.__CaptureWorker.start()
print("[DEBUG - {}] Capture started!".format(time.strftime("%H:%M:%S")))
def PauseCapture(self):
self.__VideoPaused = True
print("[DEBUG - {}] Capture paused!".format(time.strftime("%H:%M:%S")))
def StopCapture(self):
self.__VideoActive = False
if(self.__CaptureWorker is not None):
self.__CaptureWorker.join()
print("[DEBUG - {}] Capture stopped!".format(time.strftime("%H:%M:%S")))
def Run(self):
print("[DEBUG - {}] Starting server...".format(time.strftime("%H:%M:%S")))
# Start the server
self.__Server.start()
def Stop(self):
print("[DEBUG - {}] Shutdown server...".format(time.strftime("%H:%M:%S")))
# Wait for the capture thread
self.StopCapture()
# Release the camera
self.__Camera.release()
if(self.__onStop is not None):
self.__onStop()
我可以在Windows PC上运行此应用程序,并通过Chrome
以0.0.0.0:80连接到Web服务器,单击一些按钮,应用程序将接受此操作。
[DEBUG - 17:54:14] Capture started!
127.0.0.1 - - [06/Sep/2019 17:54:14] "POST / HTTP/1.1" 200 -
但是在我的Raspberry Pi上不起作用。我升级了所有软件包和操作系统,在Raspberry Pi上复制了整个代码,然后启动了应用程序
$ python3 app.py
现在,我通过Raspberry Pi IP地址从Chrome
(在Windows PC上)建立了新连接,并单击了一些按钮,但是应用程序对任何按钮都没有反应。
那么,为什么应用程序可以在Windows PC上完美运行,并且可以通过本地主机访问Web服务器,而不能在我的Raspberry Pi上运行?