infer_mnn.py
3.43 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
import os
import cv2
from PIL import Image, ImageFont, ImageDraw
import numpy as np
from torchvision import transforms
import MNN
def image_infer_mnn(mnn_model_path, image_path, class_list):
image = cv2.imread(image_path)
input_image = cv2.resize(image,(128,128))
input_data = 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))
cv2.putText(image,class_list[int(out)],(50, 50),cv2.FONT_HERSHEY_SIMPLEX,2,(0,0,255))
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 _:
input_image = cv2.resize(frame, (128, 128))
input_data = input_image.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 _:
input_image = cv2.resize(frame, (128, 128))
input_data = input_image.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_2997.jpg'
mnn_model_path = 'cls_abnormal_face_mnn_1.0.0_v0.0.1.mnn'
# image
# for i in os.listdir('test_image'):
# image=image_infer_mnn(mnn_model_path,os.path.join('test_image',i),class_list)
# cv2.imshow('image',image)
# cv2.waitKey(0)
# camera
camera_infer_mnn(mnn_model_path, 0)