-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
89 lines (60 loc) · 1.92 KB
/
app.py
File metadata and controls
89 lines (60 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from flask import Flask, jsonify, request, redirect, send_file
from flask_cors import CORS, cross_origin
from webcam import Webcam
from io import BytesIO
from PIL import Image
import base64
import re
app = Flask(__name__)
webcam = Webcam()
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
@app.route("/")
@cross_origin()
def hello():
return "Hello World!"
@app.route('/status')
@cross_origin()
def status():
if webcam.running:
return 'Running'
else:
return 'Not Running'
@app.route('/start')
@cross_origin()
def start():
return 'running'
@app.route('/pic', methods=['GET', 'POST'])
@cross_origin()
def upload_image():
# Check if a valid image file was uploaded
if request.method == 'POST':
if 'file' not in request.files:
if request.form['file']:
image_data = re.sub('^data:image/.+;base64,', '', request.form['file'])
file = Image.open(BytesIO(base64.b64decode(image_data)))
else:
return 'No file'
else:
file = request.files['file']
file = Image.open(BytesIO(file.stream.read()))
print(file)
if file:
# The image file seems valid! Detect faces and return the result.
result = webcam.detect_faces_in_image(file)
return serve_pil_image(result)
# If no valid image file was uploaded, show the file upload form:
return 'no valid image'
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def serve_pil_image(pil_img):
#img_io = BytesIO()
#pil_img.save(img_io, 'PNG', quality=70)
#img_io.seek(0)
#return send_file(img_io, mimetype='image/png')
buffered = BytesIO()
pil_img.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue())
return img_str
if __name__ == '__main__':
app.run(debug=True)