keras我已经使用和创建了一个人脸识别模型tensorflow,现在我尝试使用 Flask 和 Python 将其转换为 Web 应用程序。我的要求是,我需要在网页上显示一个实时网络摄像头,通过单击按钮,它应该拍摄照片并将其保存到指定的目录,并且使用该图片,应用程序应该识别该人。如果在数据集中没有找到该人,则应在网页上显示一条消息,表明发现了未知身份。为了完成这项工作,我开始学习 Flask,之后当涉及到要求时,这对我来说非常困难。有人帮我解决这个问题。
您想要做的是通过使用网络摄像头 Stream 与 Flask 进行流式传输,并通过机器学习来处理它。Flask 中 Web 服务器的主脚本将允许您加载 index.html 文件,然后通过 /video_feed 路径流式传输每个帧:
from flask import Flask, render_template, Response, jsonify
from camera import VideoCamera
import cv2
app = Flask(__name__)
video_stream = VideoCamera()
@app.route('/')
def index():
return render_template('index.html')
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')
@app.route('/video_feed')
def video_feed():
return Response(gen(video_stream),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='127.0.0.1', debug=True,port="5000")
Run Code Online (Sandbox Code Playgroud)
然后,您需要 VideoCamera 类,您将在其中处理每个帧,并可以在其中对帧进行所需的每个预测或处理。相机.py文件:
class VideoCamera(object):
def __init__(self):
self.video = cv2.VideoCapture(0)
def __del__(self):
self.video.release()
def get_frame(self):
ret, frame = self.video.read()
# DO WHAT YOU WANT WITH TENSORFLOW / KERAS AND OPENCV
ret, jpeg = cv2.imencode('.jpg', frame)
return jpeg.tobytes()
Run Code Online (Sandbox Code Playgroud)
最后是在 html 文件index.html中显示视频流的页面(在templates/文件夹中,如果不存在,则生成它):
<!DOCTYPE html>
<html lang="en">
<head>
<title>Video Stream</title>
</head>
<body>
<img src="{{ url_for('video_feed') }}" />
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
from flask import Flask,request,jsonify
import numpy as np
import cv2
import tensorflow as tf
import base64
app = Flask(__name__)
graph = tf.get_default_graph()
@app.route('/')
def hello_world():
return """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<video id="video" width="640" height="480" autoplay></video>
<button id="snap">Snap Photo</button>
<canvas id="canvas" width="640" height="480"></canvas>
</body>
<script>
var video = document.getElementById('video');
if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true }).then(function(stream) {
//video.src = window.URL.createObjectURL(stream);
video.srcObject = stream;
video.play();
});
}
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var video = document.getElementById('video');
// Trigger photo take
document.getElementById("snap").addEventListener("click", function() {
context.drawImage(video, 0, 0, 640, 480);
var request = new XMLHttpRequest();
request.open('POST', '/submit?image=' + video.toString('base64'), true);
request.send();
});
</script>
</html>
"""
# HtmlVideoElement
@app.route('/test',methods=['GET'])
def test():
return "hello world!"
@app.route('/submit',methods=['POST'])
def submit():
image = request.args.get('image')
print(type(image))
return ""`
Run Code Online (Sandbox Code Playgroud)
我已经这样做了,但问题是,当在装饰器中调用 API /submit 时,当打印图像变量的类型时,我将图像存储为 HTMLVideoElement,我不知道如何将其转换为 Jpeg 格式并将其用于进一步的目的。
| 归档时间: |
|
| 查看次数: |
14199 次 |
| 最近记录: |