b8d9812e by 乔峰昇

mnn

1 parent 386478a4
...@@ -2,29 +2,34 @@ ...@@ -2,29 +2,34 @@
2 <project version="4"> 2 <project version="4">
3 <component name="ChangeListManager"> 3 <component name="ChangeListManager">
4 <list default="true" id="8255f694-c607-4431-a530-39f2e0df4506" name="Default Changelist" comment=""> 4 <list default="true" id="8255f694-c607-4431-a530-39f2e0df4506" name="Default Changelist" comment="">
5 <change beforePath="$PROJECT_DIR$/.idea/.gitignore" beforeDir="false" /> 5 <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
6 <change beforePath="$PROJECT_DIR$/.idea/face_mask_classifier.iml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/face_mask_classifier.iml" afterDir="false" /> 6 <change beforePath="$PROJECT_DIR$/infer_mnn.py" beforeDir="false" afterPath="$PROJECT_DIR$/infer_mnn.py" afterDir="false" />
7 <change beforePath="$PROJECT_DIR$/.idea/inspectionProfiles/Project_Default.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/inspectionProfiles/Project_Default.xml" afterDir="false" /> 7 <change beforePath="$PROJECT_DIR$/mobilenet_v2.mnn" beforeDir="false" afterPath="$PROJECT_DIR$/cls_abnormal_face_mnn_1.0.0_v0.0.1.mnn" afterDir="false" />
8 <change beforePath="$PROJECT_DIR$/.idea/inspectionProfiles/profiles_settings.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/inspectionProfiles/profiles_settings.xml" afterDir="false" /> 8 <change beforePath="$PROJECT_DIR$/mobilenet_v2.onnx" beforeDir="false" afterPath="$PROJECT_DIR$/cls_abnormal_face_onnx_1.0.0_v0.0.1.onnx" afterDir="false" />
9 <change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
10 <change beforePath="$PROJECT_DIR$/dataset.py" beforeDir="false" />
11 <change beforePath="$PROJECT_DIR$/image/img.png" beforeDir="false" />
12 <change beforePath="$PROJECT_DIR$/image/img_1.png" beforeDir="false" />
13 <change beforePath="$PROJECT_DIR$/infer.py" beforeDir="false" afterPath="$PROJECT_DIR$/infer.py" afterDir="false" />
14 <change beforePath="$PROJECT_DIR$/net.py" beforeDir="false" />
15 <change beforePath="$PROJECT_DIR$/params/cls_face_mobilenet_v2.pth" beforeDir="false" />
16 <change beforePath="$PROJECT_DIR$/test.py" beforeDir="false" />
17 <change beforePath="$PROJECT_DIR$/train.py" beforeDir="false" afterPath="$PROJECT_DIR$/train.py" afterDir="false" />
18 <change beforePath="$PROJECT_DIR$/utils.py" beforeDir="false" />
19 </list> 9 </list>
20 <option name="SHOW_DIALOG" value="false" /> 10 <option name="SHOW_DIALOG" value="false" />
21 <option name="HIGHLIGHT_CONFLICTS" value="true" /> 11 <option name="HIGHLIGHT_CONFLICTS" value="true" />
22 <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> 12 <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
23 <option name="LAST_RESOLUTION" value="IGNORE" /> 13 <option name="LAST_RESOLUTION" value="IGNORE" />
24 </component> 14 </component>
15 <component name="FileTemplateManagerImpl">
16 <option name="RECENT_TEMPLATES">
17 <list>
18 <option value="Python Script" />
19 </list>
20 </option>
21 </component>
25 <component name="Git.Settings"> 22 <component name="Git.Settings">
26 <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> 23 <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
27 </component> 24 </component>
25 <component name="GitSEFilterConfiguration">
26 <file-type-list>
27 <filtered-out-file-type name="LOCAL_BRANCH" />
28 <filtered-out-file-type name="REMOTE_BRANCH" />
29 <filtered-out-file-type name="TAG" />
30 <filtered-out-file-type name="COMMIT_BY_MESSAGE" />
31 </file-type-list>
32 </component>
28 <component name="ProjectId" id="24r53vDpNxHFy2XFaKewyUFOSTL" /> 33 <component name="ProjectId" id="24r53vDpNxHFy2XFaKewyUFOSTL" />
29 <component name="ProjectViewState"> 34 <component name="ProjectViewState">
30 <option name="hideEmptyMiddlePackages" value="true" /> 35 <option name="hideEmptyMiddlePackages" value="true" />
...@@ -34,10 +39,52 @@ ...@@ -34,10 +39,52 @@
34 <component name="PropertiesComponent"> 39 <component name="PropertiesComponent">
35 <property name="RunOnceActivity.OpenProjectViewOnStart" value="true" /> 40 <property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
36 <property name="RunOnceActivity.ShowReadmeOnStart" value="true" /> 41 <property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
37 <property name="last_opened_file_path" value="$PROJECT_DIR$" /> 42 <property name="last_opened_file_path" value="$PROJECT_DIR$/../Pytorch-Image-Classifier-Collection" />
38 <property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" /> 43 <property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
39 </component> 44 </component>
40 <component name="RunManager"> 45 <component name="RunManager" selected="Python.infer_mnn">
46 <configuration name="cc" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
47 <module name="face_mask_classifier" />
48 <option name="INTERPRETER_OPTIONS" value="" />
49 <option name="PARENT_ENVS" value="true" />
50 <envs>
51 <env name="PYTHONUNBUFFERED" value="1" />
52 </envs>
53 <option name="SDK_HOME" value="" />
54 <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
55 <option name="IS_MODULE_SDK" value="true" />
56 <option name="ADD_CONTENT_ROOTS" value="true" />
57 <option name="ADD_SOURCE_ROOTS" value="true" />
58 <option name="SCRIPT_NAME" value="$PROJECT_DIR$/cc.py" />
59 <option name="PARAMETERS" value="" />
60 <option name="SHOW_COMMAND_LINE" value="false" />
61 <option name="EMULATE_TERMINAL" value="false" />
62 <option name="MODULE_MODE" value="false" />
63 <option name="REDIRECT_INPUT" value="false" />
64 <option name="INPUT_FILE" value="" />
65 <method v="2" />
66 </configuration>
67 <configuration name="ddd" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
68 <module name="face_mask_classifier" />
69 <option name="INTERPRETER_OPTIONS" value="" />
70 <option name="PARENT_ENVS" value="true" />
71 <envs>
72 <env name="PYTHONUNBUFFERED" value="1" />
73 </envs>
74 <option name="SDK_HOME" value="" />
75 <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
76 <option name="IS_MODULE_SDK" value="true" />
77 <option name="ADD_CONTENT_ROOTS" value="true" />
78 <option name="ADD_SOURCE_ROOTS" value="true" />
79 <option name="SCRIPT_NAME" value="$PROJECT_DIR$/ddd.py" />
80 <option name="PARAMETERS" value="" />
81 <option name="SHOW_COMMAND_LINE" value="false" />
82 <option name="EMULATE_TERMINAL" value="false" />
83 <option name="MODULE_MODE" value="false" />
84 <option name="REDIRECT_INPUT" value="false" />
85 <option name="INPUT_FILE" value="" />
86 <method v="2" />
87 </configuration>
41 <configuration name="infer_mnn" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true"> 88 <configuration name="infer_mnn" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
42 <module name="face_mask_classifier" /> 89 <module name="face_mask_classifier" />
43 <option name="INTERPRETER_OPTIONS" value="" /> 90 <option name="INTERPRETER_OPTIONS" value="" />
...@@ -53,15 +100,22 @@ ...@@ -53,15 +100,22 @@
53 <option name="SCRIPT_NAME" value="$PROJECT_DIR$/infer_mnn.py" /> 100 <option name="SCRIPT_NAME" value="$PROJECT_DIR$/infer_mnn.py" />
54 <option name="PARAMETERS" value="" /> 101 <option name="PARAMETERS" value="" />
55 <option name="SHOW_COMMAND_LINE" value="false" /> 102 <option name="SHOW_COMMAND_LINE" value="false" />
56 <option name="EMULATE_TERMINAL" value="false" /> 103 <option name="EMULATE_TERMINAL" value="true" />
57 <option name="MODULE_MODE" value="false" /> 104 <option name="MODULE_MODE" value="false" />
58 <option name="REDIRECT_INPUT" value="false" /> 105 <option name="REDIRECT_INPUT" value="false" />
59 <option name="INPUT_FILE" value="" /> 106 <option name="INPUT_FILE" value="" />
60 <method v="2" /> 107 <method v="2" />
61 </configuration> 108 </configuration>
109 <list>
110 <item itemvalue="Python.cc" />
111 <item itemvalue="Python.ddd" />
112 <item itemvalue="Python.infer_mnn" />
113 </list>
62 <recent_temporary> 114 <recent_temporary>
63 <list> 115 <list>
64 <item itemvalue="Python.infer_mnn" /> 116 <item itemvalue="Python.infer_mnn" />
117 <item itemvalue="Python.ddd" />
118 <item itemvalue="Python.cc" />
65 </list> 119 </list>
66 </recent_temporary> 120 </recent_temporary>
67 </component> 121 </component>
......
1
2 import os
3
4 import cv2 1 import cv2
5 from PIL import Image, ImageFont, ImageDraw 2 from PIL import Image, ImageFont, ImageDraw
6 import torch
7 from torchvision import transforms
8 import MNN 3 import MNN
4 import numpy as np
9 5
10 6
11 def keep_shape_resize(frame, size=128): 7 def keep_shape_resize(frame, size=128):
...@@ -24,12 +20,10 @@ def keep_shape_resize(frame, size=128): ...@@ -24,12 +20,10 @@ def keep_shape_resize(frame, size=128):
24 def image_infer_mnn(mnn_model_path, image_path, class_list): 20 def image_infer_mnn(mnn_model_path, image_path, class_list):
25 image = Image.open(image_path) 21 image = Image.open(image_path)
26 input_image = keep_shape_resize(image) 22 input_image = keep_shape_resize(image)
27 preprocess = transforms.Compose([transforms.ToTensor()]) 23 input_data = np.array(input_image).astype(np.float32).transpose((2, 0, 1)) / 255
28 input_data = preprocess(input_image)
29 interpreter = MNN.Interpreter(mnn_model_path) 24 interpreter = MNN.Interpreter(mnn_model_path)
30 session = interpreter.createSession() 25 session = interpreter.createSession()
31 input_tensor = interpreter.getSessionInput(session) 26 input_tensor = interpreter.getSessionInput(session)
32 input_data = input_data.cpu().numpy().squeeze()
33 tmp_input = MNN.Tensor((1, 3, 128, 128), MNN.Halide_Type_Float, input_data, MNN.Tensor_DimensionType_Caffe) 27 tmp_input = MNN.Tensor((1, 3, 128, 128), MNN.Halide_Type_Float, input_data, MNN.Tensor_DimensionType_Caffe)
34 input_tensor.copyFrom(tmp_input) 28 input_tensor.copyFrom(tmp_input)
35 interpreter.runSession(session) 29 interpreter.runSession(session)
...@@ -53,9 +47,7 @@ def video_infer_mnn(mnn_model_path, video_path): ...@@ -53,9 +47,7 @@ def video_infer_mnn(mnn_model_path, video_path):
53 image_data = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 47 image_data = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
54 image_data = Image.fromarray(image_data) 48 image_data = Image.fromarray(image_data)
55 image_data = keep_shape_resize(image_data, 128) 49 image_data = keep_shape_resize(image_data, 128)
56 preprocess = transforms.Compose([transforms.ToTensor()]) 50 input_data = np.array(image_data).astype(np.float32).transpose((2, 0, 1)) / 255
57 input_data = preprocess(image_data)
58 input_data = input_data.cpu().numpy().squeeze()
59 tmp_input = MNN.Tensor((1, 3, 128, 128), MNN.Halide_Type_Float, input_data, MNN.Tensor_DimensionType_Caffe) 51 tmp_input = MNN.Tensor((1, 3, 128, 128), MNN.Halide_Type_Float, input_data, MNN.Tensor_DimensionType_Caffe)
60 input_tensor.copyFrom(tmp_input) 52 input_tensor.copyFrom(tmp_input)
61 interpreter.runSession(session) 53 interpreter.runSession(session)
...@@ -81,9 +73,7 @@ def camera_infer_mnn(mnn_model_path, camera_id): ...@@ -81,9 +73,7 @@ def camera_infer_mnn(mnn_model_path, camera_id):
81 image_data = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 73 image_data = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
82 image_data = Image.fromarray(image_data) 74 image_data = Image.fromarray(image_data)
83 image_data = keep_shape_resize(image_data, 128) 75 image_data = keep_shape_resize(image_data, 128)
84 preprocess = transforms.Compose([transforms.ToTensor()]) 76 input_data = np.array(image_data).astype(np.float32).transpose((2, 0, 1)) / 255
85 input_data = preprocess(image_data)
86 input_data = input_data.cpu().numpy().squeeze()
87 tmp_input = MNN.Tensor((1, 3, 128, 128), MNN.Halide_Type_Float, input_data, MNN.Tensor_DimensionType_Caffe) 77 tmp_input = MNN.Tensor((1, 3, 128, 128), MNN.Halide_Type_Float, input_data, MNN.Tensor_DimensionType_Caffe)
88 input_tensor.copyFrom(tmp_input) 78 input_tensor.copyFrom(tmp_input)
89 interpreter.runSession(session) 79 interpreter.runSession(session)
...@@ -98,14 +88,12 @@ def camera_infer_mnn(mnn_model_path, camera_id): ...@@ -98,14 +88,12 @@ def camera_infer_mnn(mnn_model_path, camera_id):
98 break 88 break
99 89
100 90
101
102 if __name__ == '__main__': 91 if __name__ == '__main__':
103 class_list = ['mask', 'no_mask'] 92 class_list = ['mask', 'no_mask']
104 image_path = 'test_image/mask_2997.jpg' 93 image_path = 'test_image/mask_2995.jpg'
105 mnn_model_path = 'mobilenet_v2.mnn' 94 mnn_model_path = 'cls_abnormal_face_mnn_1.0.0_v0.0.1.mnn'
106 # image 95 # image
107 # image=image_infer_mnn(mnn_model_path,image_path,class_list) 96 image = image_infer_mnn(mnn_model_path, image_path, class_list)
108 # image.show() 97 image.show()
109
110 # camera 98 # camera
111 camera_infer_mnn(mnn_model_path, 0) 99 # camera_infer_mnn(mnn_model_path,0)
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!