forked from radensaleh/Face-Detect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaceDetect.py
34 lines (26 loc) · 896 Bytes
/
faceDetect.py
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
import os.path
import numpy as np
import cv2
import json
from flask import Flask, request, Response
import uuid
def faceDetect(img):
face_cascade = cv2.CascadeClassifier('face_detect_cascade.xml')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for(x, y, w, h) in faces:
img = cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0))
path_file = ('%s.jpg' % uuid.uuid4().hex)
cv2.imwrite(path_file, img)
return json.dumps(path_file)
# api
app = Flask(__name__)
# route
@app.route('/api/upload', methods=['POST'])
def upload():
img = cv2.imdecode(np.fromstring(
request.files['image'].read(), np.uint8), cv2.IMREAD_UNCHANGED)
img_processed = faceDetect(img)
return Response(response=img_processed, status=200, mimetype="application/json")
# start server
app.run(host="0.0.0.0", post=5000)