Skip to content

Commit 487a228

Browse files
committed
flask
1 parent e56becc commit 487a228

File tree

5 files changed

+568
-0
lines changed

5 files changed

+568
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python3
2+
3+
from flask import Flask, render_template_string
4+
5+
6+
app = Flask(__name__)
7+
8+
9+
@app.route('/')
10+
def index():
11+
return render_template_string('''
12+
<video id="video" width="640" height="480" autoplay style="background-color: grey"></video>
13+
<button id="snap">Take Photo</button>
14+
<canvas id="canvas" width="640" height="480" style="background-color: grey"></canvas>
15+
16+
<script>
17+
18+
// Elements for taking the snapshot
19+
var video = document.getElementById('video');
20+
var canvas = document.getElementById('canvas');
21+
var context = canvas.getContext('2d');
22+
23+
// Get access to the camera!
24+
if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
25+
// Not adding `{ audio: true }` since we only want video now
26+
navigator.mediaDevices.getUserMedia({ video: true }).then(function(stream) {
27+
//video.src = window.URL.createObjectURL(stream);
28+
video.srcObject = stream;
29+
video.play();
30+
});
31+
}
32+
33+
// Trigger photo take
34+
document.getElementById("snap").addEventListener("click", function() {
35+
context.drawImage(video, 0, 0, 640, 480);
36+
});
37+
38+
</script>
39+
''')
40+
41+
42+
if __name__ == '__main__':
43+
app.run(debug=True)
44+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python3
2+
3+
# https://developers.google.com/web/fundamentals/media/capturing-images
4+
# https://github.com/jhuckaby/webcamjs
5+
# https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob
6+
7+
from flask import Flask, render_template_string, request
8+
9+
10+
app = Flask(__name__)
11+
12+
13+
@app.route('/')
14+
def index():
15+
return render_template_string('''
16+
<video id="video" width="640" height="480" autoplay style="background-color: grey"></video>
17+
<button id="send">Take & Send Photo</button>
18+
<canvas id="canvas" width="640" height="480" style="background-color: grey"></canvas>
19+
20+
<script>
21+
22+
// Elements for taking the snapshot
23+
var video = document.getElementById('video');
24+
var canvas = document.getElementById('canvas');
25+
var context = canvas.getContext('2d');
26+
27+
// Get access to the camera!
28+
if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
29+
// Not adding `{ audio: true }` since we only want video now
30+
navigator.mediaDevices.getUserMedia({ video: true }).then(function(stream) {
31+
//video.src = window.URL.createObjectURL(stream);
32+
video.srcObject = stream;
33+
video.play();
34+
});
35+
}
36+
37+
// Trigger photo take
38+
document.getElementById("send").addEventListener("click", function() {
39+
context.drawImage(video, 0, 0, 640, 480);
40+
canvas.toBlob(upload, "image/jpeg");
41+
});
42+
43+
function upload(file) {
44+
var formdata = new FormData();
45+
formdata.append("snap", file);
46+
47+
var xhr = new XMLHttpRequest();
48+
xhr.open("POST", "{{ url_for('upload') }}", true);
49+
xhr.onload = function() {
50+
if(this.status = 200) {
51+
console.log(this.response);
52+
} else {
53+
console.error(xhr);
54+
}
55+
alert(this.response);
56+
};
57+
xhr.send(formdata);
58+
}
59+
60+
</script>
61+
''')
62+
63+
@app.route('/upload', methods=['GET', 'POST'])
64+
def upload():
65+
if request.method == 'POST':
66+
#fs = request.files['snap'] # it raise error when there is no `snap` in form
67+
fs = request.files.get('snap')
68+
if fs:
69+
print('FileStorage:', fs)
70+
print('filename:', fs.filename)
71+
fs.save('image.jpg')
72+
return 'Got Snap!'
73+
else:
74+
return 'You forgot Snap!'
75+
76+
return 'Hello World!'
77+
78+
79+
if __name__ == '__main__':
80+
app.run(debug=True, port=5000)
81+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#!/usr/bin/env python3
2+
3+
# https://developers.google.com/web/fundamentals/media/capturing-images
4+
# https://github.com/jhuckaby/webcamjs
5+
# https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob
6+
# https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL
7+
# https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
8+
9+
from flask import Flask, render_template_string, request, make_response
10+
import cv2
11+
import numpy as np
12+
import datetime
13+
14+
app = Flask(__name__)
15+
16+
17+
@app.route('/')
18+
def index():
19+
return render_template_string('''
20+
<video id="video" width="320" height="240" autoplay style="background-color: grey"></video>
21+
<button id="send">Take & Send Photo</button>
22+
<canvas id="canvas" width="320" height="240" style="background-color: grey"></canvas>
23+
<img id="image" src="" width="320" height="240" style="background-color: grey"></img>
24+
25+
<script>
26+
27+
// Elements for taking the snapshot
28+
var video = document.getElementById('video');
29+
30+
// Element to display snapshot
31+
32+
// you need canvas to get image - canvas can be hiden using `createElement("canvas")`
33+
// var canvas = document.createElement("canvas");
34+
35+
var canvas = document.getElementById('canvas');
36+
var context = canvas.getContext('2d');
37+
38+
var image = document.getElementById('image');
39+
var image64 = document.getElementById('image64');
40+
41+
// Get access to the camera!
42+
if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
43+
// Not adding `{ audio: true }` since we only want video now
44+
navigator.mediaDevices.getUserMedia({ video: true }).then(function(stream) {
45+
//video.src = window.URL.createObjectURL(stream);
46+
video.srcObject = stream;
47+
video.play();
48+
49+
//console.log('setInterval')
50+
window.setInterval(function() {
51+
//context.drawImage(video, 0, 0);
52+
context.drawImage(video, 0, 0, 320, 240); // better use size because camera may gives data in different size then <video> is displaying
53+
54+
image64.src = canvas.toDataURL();
55+
canvas.toBlob(upload, "image/jpeg");
56+
}, 100);
57+
});
58+
}
59+
60+
// Trigger photo take
61+
document.getElementById("send").addEventListener("click", function() {
62+
//context.drawImage(video, 0, 0);
63+
context.drawImage(video, 0, 0, 320, 240); // better use size because camera may gives data in different size then <video> is displaying
64+
image64.src = canvas.toDataURL();
65+
66+
canvas.toBlob(upload, "image/jpeg");
67+
});
68+
69+
function upload(file) {
70+
71+
// create form
72+
var formdata = new FormData();
73+
74+
// add file to form
75+
formdata.append("snap", file);
76+
77+
// create AJAX connection
78+
var xhr = new XMLHttpRequest();
79+
xhr.open("POST", "{{ url_for('upload') }}", true);
80+
xhr.responseType = 'blob';
81+
// define function which get response
82+
xhr.onload = function() {
83+
84+
if(this.status = 200) {
85+
//console.log(this.response);
86+
} else {
87+
console.error(xhr);
88+
}
89+
90+
//alert(this.response);
91+
92+
//img.onload = function(){
93+
// ctx.drawImage(img, 0, 0)
94+
//}
95+
96+
image.src = URL.createObjectURL(this.response); // blob
97+
};
98+
99+
// send form in AJAX
100+
xhr.send(formdata);
101+
102+
//image.src = URL.createObjectURL(file);
103+
}
104+
105+
106+
</script>
107+
''')
108+
109+
def send_file_data(data, mimetype='image/jpeg', filename='output.jpg'):
110+
# https://stackoverflow.com/questions/11017466/flask-to-return-image-stored-in-database/11017839
111+
112+
response = make_response(data)
113+
response.headers.set('Content-Type', mimetype)
114+
response.headers.set('Content-Disposition', 'attachment', filename=filename)
115+
116+
return response
117+
118+
@app.route('/upload', methods=['GET', 'POST'])
119+
def upload():
120+
if request.method == 'POST':
121+
#fs = request.files['snap'] # it raise error when there is no `snap` in form
122+
fs = request.files.get('snap')
123+
if fs:
124+
#print('FileStorage:', fs)
125+
#print('filename:', fs.filename)
126+
127+
# https://stackoverflow.com/questions/27517688/can-an-uploaded-image-be-loaded-directly-by-cv2
128+
# https://stackoverflow.com/a/11017839/1832058
129+
img = cv2.imdecode(np.frombuffer(fs.read(), np.uint8), cv2.IMREAD_UNCHANGED)
130+
#print('Shape:', img.shape)
131+
# rectangle(image, start_point, end_point, color, thickness)
132+
img = cv2.rectangle(img, (20, 20), (300, 220), (0, 0, 255), 2)
133+
134+
text = datetime.datetime.now().strftime('%Y.%m.%d %H.%M.%S.%f')
135+
img = cv2.putText(img, text, (5, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
136+
#cv2.imshow('image', img)
137+
#cv2.waitKey(1)
138+
139+
# https://jdhao.github.io/2019/07/06/python_opencv_pil_image_to_bytes/
140+
ret, buf = cv2.imencode('.jpg', img)
141+
142+
#return f'Got Snap! {img.shape}'
143+
return send_file_data(buf.tobytes())
144+
else:
145+
return 'You forgot Snap!'
146+
147+
return 'Hello World!'
148+
149+
150+
if __name__ == '__main__':
151+
app.run(debug=True, port=5000)
152+

0 commit comments

Comments
 (0)