【发布时间】:2020-04-11 02:01:58
【问题描述】:
假设我有一个用 python 编写的代码,在循环中我从流视频中获取帧!如何将这些帧实时传输到编写在 electron.js 上的图形界面?
【问题讨论】:
假设我有一个用 python 编写的代码,在循环中我从流视频中获取帧!如何将这些帧实时传输到编写在 electron.js 上的图形界面?
【问题讨论】:
【讨论】:
您可以创建一个 Flask 服务器并将所有帧直接传递到 html 页面。然后从您的电子代码调用服务器以显示流程。
webstreaming.py(你的 Flask 服务器):
# initialize video capture object to read video from external webcam
video_capture = cv2.VideoCapture(1)
# if there is no external camera then take the built-in camera
if not video_capture.read()[0]:
video_capture = cv2.VideoCapture(0)
# initialize a flask object
app = Flask(__name__)
@app.route("/")
def index():
# return the rendered template
return render_template("index.html")
def analyze():
# grab global references to the video stream, output frame, and
# lock variables
global video_capture, outputFrame, lock, status
while(video_capture.isOpened()):
# read video frame by frame
ret, frame = video_capture.read()
frame = cv2.flip(frame, 1)
# acquire the lock, set the output frame, and release the
# lock
with lock:
outputFrame = frame.copy()
def generate():
# grab global references to the output frame and lock variables
global outputFrame, lock
# loop over frames from the output stream
while True:
# wait until the lock is acquired
with lock:
# check if the output frame is available, otherwise skip
# the iteration of the loop
if outputFrame is None:
continue
# encode the frame in JPEG format
(flag, encodedImage) = cv2.imencode(".jpg", outputFrame)
# ensure the frame was successfully encoded
if not flag:
continue
# yield the output frame in the byte format
yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
bytearray(encodedImage) + b'\r\n')
#This route accepts request against the url http://domain_name/change_label
#You could add the argument, method=["POST"],<br>So the route only accepts post request against that url.
@app.route("/get_label")
def change_label():
global status, lock
with lock:
return status
@app.route("/video_feed")
def video_feed():
# return the response generated along with the specific media
# type (mime type)
return Response(generate(),
mimetype="multipart/x-mixed-replace; boundary=frame")
if __name__ == "__main__":
# start a thread that will perform motion detection
t = threading.Thread(target=analyze)
t.daemon = True
t.start()
# start the flask app
app.run(host="0.0.0.0", port=8000, debug=True, threaded=True, use_reloader=False)
# release the video stream pointer
video_capture.release()
然后在你的 index.html 中(在 templates.html 中)
<html>
<head>
<title>Video Streaming Demonstration</title>
</head>
<body>
<h1>Video Streaming Demonstration</h1>
<img src="{{ url_for('video_feed') }}">
</body>
</html>
在你的 main.js 中
const {app, BrowserWindow} = require('electron')
function createWindow () {
window = new BrowserWindow({width: 800, height: 600})
window.loadFile('index.html')
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
重要提示:您应该添加将您重定向到模板/index.html 的 index.html(不要混淆这是 2 个不同的文件)。
你还需要预先启动服务器,例如使用并发包和electron-forge。
【讨论】: