我正在从事一个涉及实时视频流和手势识别的项目。然而,我找到了很多资源来获取从客户端到服务器端的实时视频流,但没有什么能快速和简单地实现。我试过AIORTC,但我不明白
我不知道如何在 Django 或 Flask 中使用 webRTC 来完成这样的任务,如果您对使用 python 的 webRTC 有任何想法,请帮助我。或者帮我解决这个问题,
我正在尝试将帧从 javascript 获取到 flask 作为 blob,但我不知道如何从 blob 重新创建图像, 我已经尝试过
base64.b64decode()
但是它不起作用。
这是我的 getImage.html 文件
<!DOCTYPE html>
<html>
<head>
<title>Post an Image test</title>
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
</head>
<style>
/* mirror the image */
video, canvas {
transform: scale(-1, 1); /*For Firefox (& IE) */
-webkit-transform: scale(-1, 1); /*for Chrome & Opera (& Safari) */
}
</style>
<body>
<video id="myVideo" height="480" width="640" autoplay></video>
<script>
let v = document.getElementById("myVideo");
//create a canvas to grab an image for upload
let imageCanvas = document.createElement('canvas');
let imageCtx = imageCanvas.getContext("2d");
//Add file blob to a form and post
function postFile(file) {
let formdata = new FormData();
formdata.append("image", file);
let xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:5000/image', true);
xhr.onload = function () {
if (this.status === 200)
console.log(this.response);
else
console.error(xhr);
};
xhr.send(formdata);
}
//Get the image from the canvas
function sendImagefromCanvas() {
//Make sure the canvas is set to the current video size
imageCanvas.width = v.videoWidth;
imageCanvas.height = v.videoHeight;
imageCtx.drawImage(v, 0, 0, v.videoWidth, v.videoHeight);
//Convert the canvas to blob and post the file
imageCanvas.toBlob(postFile, 'image/jpeg');
}
//Take a picture on click
v.onclick = function() {
console.log('click');
sendImagefromCanvas();
};
window.onload = function () {
//Get camera video
navigator.mediaDevices.getUserMedia({video: {width: 640, height: 480}, audio: false})
.then(stream => {
v.srcObject = stream;
})
.catch(err => {
console.log('navigator.getUserMedia error: ', err)
});
};
</script>
</body>
</html>
这里是server.py文件
from flask import Flask, request, Response
import time
import base64
import cv2
import base64
PATH_TO_TEST_IMAGES_DIR = './images'
app = Flask(__name__)
@app.route('/')
def index():
return Response(open('./static/getImage.html').read(), mimetype="text/html")
# save the image as a picture
@app.route('/image', methods=['POST'])
def image():
blob = request.files['image'] # get the image
f = ('%s.jpeg' % time.strftime("%Y%m%d-%H%M%S"))
print("images saved!!!!!!!")
print(blob)
blob.save('%s/%s' % (PATH_TO_TEST_IMAGES_DIR, "realTime.jpeg"))
data=base64.b64decode(blob)
print(data)
return Response("%s saved" % f)
if __name__ == '__main__':
app.run(debug=True, host='localhost')
大多数浏览器都有跨源策略。确保这一点。
在
blob = request.files['image'] # get the image
看起来你需要:
blob = request.files['image'].read()