Implement the following: ・ Launch the webcam in the browser ・ Capture the image of the camera screen -Send the captured image to the server and save it
/home/capture_img
|--run.py
|--app
| |--api.py
| |--service.py
| |--static
| | |--main.js
| |--templates
| | |--index.html
|--images #Where to save the captured image
run.py
run.py
from app.api import api
if __name__ == '__main__':
api.run(host='0.0.0.0', port=8000)
app/api.py
app/api.py
from flask import Flask, request, make_response, render_template, url_for
from . import service
api = Flask(__name__)
@api.route('/', methods=['GET'])
def index():
return render_template('index.html')
@api.route('/capture_img', methods=['POST'])
def capture_img():
msg = service.save_img(request.form["img"])
return make_response(msg)
app/service.py
app/service.py
import base64
import numpy as np
import cv2
def save_img(img_base64):
#binary <- string base64
img_binary = base64.b64decode(img_base64)
#jpg <- binary
img_jpg=np.frombuffer(img_binary, dtype=np.uint8)
#raw image <- jpg
img = cv2.imdecode(img_jpg, cv2.IMREAD_COLOR)
#Path to save the decoded image
image_file="/home/capture_img/images/img0000.jpg "
#Save image
cv2.imwrite(image_file, img)
return "SUCCESS"
templates/index.html
templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>get_capture</title>
</head>
<body>
<h3>Capture capture by pressing the space key</h3>
<video id="video" width="640" height="480" autoplay></video>
<canvas id="canvas" class="canvas-wrapper"></canvas>
</body>
<script src="{{url_for('static', filename='main.js')}}"></script>
<style>
/*Hide canvas*/
#canvas {
display: none !important;
}
</style>
</html>
static/main.js
static/main.js
var video = document.getElementById('video');
// getUserMedia()Get camera footage with
var media = navigator.mediaDevices.getUserMedia({ video: true });
//Pour into video tags for real-time playback (streaming)
media.then((stream) => {
video.srcObject = stream;
});
var canvas = document.getElementById('canvas');
canvas.setAttribute('width', video.width);
canvas.setAttribute('height', video.height);
video.addEventListener(
'timeupdate',
function () {
var context = canvas.getContext('2d');
context.drawImage(video, 0, 0, video.width, video.height);
},
true
);
//Set the listener that executes capture acquisition when the space key is pressed
document.addEventListener('keydown', (event) => {
var keyName = event.key;
if (keyName === ' ') {
console.log(`keydown: SpaceKey`);
context = canvas.getContext('2d');
//Remove the head of the acquired base64 data
var img_base64 = canvas.toDataURL('image/jpeg').replace(/^.*,/, '')
captureImg(img_base64);
}
});
var xhr = new XMLHttpRequest();
//Captured image data(base64)POST
function captureImg(img_base64) {
const body = new FormData();
body.append('img', img_base64);
xhr.open('POST', 'http://localhost:8000/capture_img', true);
xhr.onload = () => {
console.log(xhr.responseText)
};
xhr.send(body);
}
root@ed9f7bedad16:/# pip install opencv-python flask
root@ed9f7bedad16:/# python /home/capture_img/run.py
* Serving Flask app "app.api" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:8000/ (Press CTRL+C to quit)
root@ed9f7bedad16:/# ls /home/capture_img/images/
img0000.jpg
The captured image has been saved.
that's all.
Recommended Posts