infer_mnn.py
3.94 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
90
91
92
93
94
95
96
97
98
99
import cv2
from PIL import Image, ImageFont, ImageDraw
import MNN
import numpy as np
def keep_shape_resize(frame, size=128):
w, h = frame.size
temp = max(w, h)
mask = Image.new('RGB', (temp, temp), (0, 0, 0))
if w >= h:
position = (0, (w - h) // 2)
else:
position = ((h - w) // 2, 0)
mask.paste(frame, position)
mask = mask.resize((size, size))
return mask
def image_infer_mnn(mnn_model_path, image_path, class_list):
image = Image.open(image_path)
input_image = keep_shape_resize(image)
input_data = np.array(input_image).astype(np.float32).transpose((2, 0, 1)) / 255
interpreter = MNN.Interpreter(mnn_model_path)
session = interpreter.createSession()
input_tensor = interpreter.getSessionInput(session)
tmp_input = MNN.Tensor((1, 3, 128, 128), MNN.Halide_Type_Float, input_data, MNN.Tensor_DimensionType_Caffe)
input_tensor.copyFrom(tmp_input)
interpreter.runSession(session)
infer_result = interpreter.getSessionOutput(session)
output_data = infer_result.getData()
out = output_data.index(max(output_data))
draw = ImageDraw.Draw(image)
font = ImageFont.truetype(r"C:\Windows\Fonts\BRITANIC.TTF", 35)
draw.text((10, 10), class_list[int(out)], font=font, fill='red')
return image
def video_infer_mnn(mnn_model_path, video_path):
cap = cv2.VideoCapture(video_path)
interpreter = MNN.Interpreter(mnn_model_path)
session = interpreter.createSession()
input_tensor = interpreter.getSessionInput(session)
while True:
_, frame = cap.read()
if _:
image_data = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image_data = Image.fromarray(image_data)
image_data = keep_shape_resize(image_data, 128)
input_data = np.array(image_data).astype(np.float32).transpose((2, 0, 1)) / 255
tmp_input = MNN.Tensor((1, 3, 128, 128), MNN.Halide_Type_Float, input_data, MNN.Tensor_DimensionType_Caffe)
input_tensor.copyFrom(tmp_input)
interpreter.runSession(session)
infer_result = interpreter.getSessionOutput(session)
output_data = infer_result.getData()
out = output_data.index(max(output_data))
cv2.putText(frame, class_list[int(out)], (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), thickness=2)
cv2.imshow('frame', frame)
if cv2.waitKey(24) & 0XFF == ord('q'):
break
else:
break
def camera_infer_mnn(mnn_model_path, camera_id):
cap = cv2.VideoCapture(camera_id)
interpreter = MNN.Interpreter(mnn_model_path)
session = interpreter.createSession()
input_tensor = interpreter.getSessionInput(session)
while True:
_, frame = cap.read()
if _:
image_data = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image_data = Image.fromarray(image_data)
image_data = keep_shape_resize(image_data, 128)
input_data = np.array(image_data).astype(np.float32).transpose((2, 0, 1)) / 255
tmp_input = MNN.Tensor((1, 3, 128, 128), MNN.Halide_Type_Float, input_data, MNN.Tensor_DimensionType_Caffe)
input_tensor.copyFrom(tmp_input)
interpreter.runSession(session)
infer_result = interpreter.getSessionOutput(session)
output_data = infer_result.getData()
out = output_data.index(max(output_data))
cv2.putText(frame, class_list[int(out)], (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), thickness=2)
cv2.imshow('frame', frame)
if cv2.waitKey(24) & 0XFF == ord('q'):
break
else:
break
if __name__ == '__main__':
class_list = ['mask', 'no_mask']
image_path = 'test_image/mask_2995.jpg'
mnn_model_path = 'cls_abnormal_face_mnn_1.0.0_v0.0.1.mnn'
# image
image = image_infer_mnn(mnn_model_path, image_path, class_list)
image.show()
# camera
# camera_infer_mnn(mnn_model_path,0)