-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.py
64 lines (55 loc) · 2.1 KB
/
detect.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
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
import numpy as np
import cv2
from PIL import Image, ImageDraw
import io
def detect(img, mtcnn, landmark_state='false', data='false'):
color = (255, 136, 75) # Orange
# Convent btye to string
nparr = np.fromstring(img, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
# Covert cv2 to pil
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# Detect with mtcnn and return bounding boxes and landmarks
boxes, prob, landmarks = mtcnn.detect(img, landmarks=True)
if boxes is not None:
if data.lower() == 'true':
if landmark_state.lower() == 'true':
return {
'bounding_boxes': [ [ int(b) for b in box ] for box in boxes ],
'landmarks': [ [ [ int(p) for p in points] for points in landmark] for landmark in landmarks ],
}
else:
return {
'bounding_boxes': [ [ int(b) for b in box ] for box in boxes ],
}
draw = ImageDraw.Draw(img)
for idx in range(len(boxes)):
# Draw Rectangle
draw.rectangle(boxes[idx].tolist(), outline=color, width=3)
if landmark_state.lower() == 'true':
# Draw Landmarks
for landmark in landmarks[idx]:
r = 3
x, y = landmark
leftUpPoint = (x-r, y-r)
rightDownPoint = (x+r, y+r)
twoPointList = [leftUpPoint, rightDownPoint]
draw.ellipse(twoPointList, fill=color)
# Convert int to byte
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
return img_byte_arr
else:
if data.lower() == 'true':
if landmark_state.lower() == 'true':
return {
'bounding_boxes': [],
'landmarks': []
}
else:
return {
'bounding_boxes': []
}
else :
return None