-
Notifications
You must be signed in to change notification settings - Fork 2
/
detect_adapter.py
235 lines (174 loc) · 9.04 KB
/
detect_adapter.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# Import our general libraries
import os
import sys
import time
# For PyTorch on Apple silicon
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
# Import CodeProject.AI SDK
from codeproject_ai_sdk import RequestData, ModuleRunner, LogMethod, JSON
# Import the method of the module we're wrapping
from PIL import Image
from options import Options
from detect import do_detection
class YOLOv8_adapter(ModuleRunner):
def __init__(self):
super().__init__()
self.opts = Options()
self.models_last_checked = None
self.custom_model_names = [] # We'll use this to cache the available model names
# These will be adjusted based on the hardware / packages found
self.use_CUDA = self.opts.use_CUDA
self.use_MPS = self.opts.use_MPS
self.use_DirectML = self.opts.use_DirectML
if self.use_CUDA and self.half_precision == 'enable' and \
not self.system_info.hasTorchHalfPrecision:
self.half_precision = 'disable'
def initialise(self):
# if the module was launched outside of the server then the queue name
# wasn't set. This is normally fine, but here we want the queue to be
# the same as the other object detection queues
if not self.launched_by_server:
self.queue_name = "objectdetection_queue"
# CUDA takes precedence
if self.use_CUDA:
self.use_CUDA = self.system_info.hasTorchCuda
# Potentially solve an issue around CUDNN_STATUS_ALLOC_FAILED errors
try:
import cudnn as cudnn
if cudnn.is_available():
cudnn.benchmark = False
except:
pass
# If no CUDA, maybe we're on an Apple Silicon Mac?
if self.use_CUDA:
self.use_MPS = False
self.use_DirectML = False
else:
self.use_MPS = self.system_info.hasTorchMPS
# If we're not on Apple Silicon and we're not already using CUDA, and we're
# in WSL or Windows, then DirectML is a good option if allowed and available.
# if self.use_DirectML and \
# (self.system_info.in_WSL or self.system_info.os == "Windows") and \
# not self.use_CUDA and not self.use_MPS:
# self.use_DirectML = self.system_info.hasTorchDirectML
# else:
# self.use_DirectML = False
self.use_DirectML = False # Unfortunately we can't get PyTorch-DirectML working
self.can_use_GPU = self.system_info.hasTorchCuda or self.system_info.hasTorchMPS # or self.use_DirectML
if self.use_CUDA:
self.inference_device = "GPU"
self.inference_library = "CUDA"
elif self.use_MPS:
self.inference_device = "GPU"
self.inference_library = "MPS"
elif self.use_DirectML:
self.inference_device = "GPU"
self.inference_library = "DirectML"
self._num_items_found = 0
self._histogram = {}
def process(self, data: RequestData) -> JSON:
response = None
if data.command == "list-custom": # list all models available
# The route to here is /v1/vision/custom/list
response = self._list_custom_models()
elif data.command == "segment": # Perform object segmentation
# The route to here is /v1/vision/segmentation
threshold: float = float(data.get_value("min_confidence", "0.4"))
img: Image = data.get_image(0)
response = do_detection(self, self.opts.models_dir,
self.opts.std_seg_model_name, self.opts.resolution_pixels,
self.use_CUDA, self.accel_device_name,
self.use_MPS, self.use_DirectML, self.half_precision,
img, threshold, True)
elif data.command == "detect": # Perform 'standard' object detection
# The route to here is /v1/vision/detection
threshold: float = float(data.get_value("min_confidence", "0.4"))
img: Image = data.get_image(0)
response = do_detection(self, self.opts.models_dir,
self.opts.std_model_name, self.opts.resolution_pixels,
self.use_CUDA, self.accel_device_name,
self.use_MPS, self.use_DirectML, self.half_precision,
img, threshold, False)
elif data.command == "custom": # Perform custom object detection
if not self.custom_model_names:
return { "success": False, "error": "No custom models found" }
threshold: float = float(data.get_value("min_confidence", "0.4"))
img: Image = data.get_image(0)
# The route to here is /v1/vision/custom/<model-name>. if mode-name = general,
# or no model provided, then a built-in general purpose mode will be used.
model_dir:str = self.opts.custom_models_dir
model_name:str = "general"
if data.segments and data.segments[0]:
model_name = data.segments[0]
# Map the "general" model to our current "general" model
# if model_name == "general": # use the standard YOLO model
# model_dir = opts.models_dir
# model_name = opts.std_model_name
if model_name == "general": # Use the custom IP Cam general model
model_dir = self.opts.custom_models_dir
model_name = "ipcam-general"
self.log(LogMethod.Info | LogMethod.Server,
{
"filename": __file__,
"loglevel": "information",
"method": sys._getframe().f_code.co_name,
"message": f"Detecting using {model_name}"
})
use_mX_GPU = False # self.opts.use_MPS - Custom models don't currently work with pyTorch on MPS
response = do_detection(self, model_dir, model_name,
self.opts.resolution_pixels, self.use_CUDA,
self.accel_device_name, use_mX_GPU,
self.use_DirectML, self.half_precision,
img, threshold)
else:
response = { "success" : False }
self.report_error(None, __file__, f"Unknown command {data.command}")
return response
def status(self) -> JSON:
statusData = super().status()
statusData["numItemsFound"] = self._num_items_found
statusData["histogram"] = self._histogram
return statusData
def update_statistics(self, response):
super().update_statistics(response)
if "success" in response and response["success"] and "predictions" in response:
predictions = response["predictions"]
self._num_items_found += len(predictions)
for prediction in predictions:
label = prediction["label"]
if label not in self._histogram:
self._histogram[label] = 1
else:
self._histogram[label] += 1
def selftest(self) -> JSON:
file_name = os.path.join("test", "home-office.jpg")
request_data = RequestData()
request_data.queue = self.queue_name
request_data.command = "detect"
request_data.add_file(file_name)
request_data.add_value("min_confidence", 0.4)
result = self.process(request_data)
print(f"Info: Self-test for {self.module_id}. Success: {result['success']}")
# print(f"Info: Self-test output for {self.module_id}: {result}")
return { "success": result['success'], "message": "Object detection test successful" }
def _list_custom_models(self):
"""
Lists the custom models we have in the assets folder. This ignores the
yolo* files.
"""
# We'll only refresh the list of models at most once a minute
if self.models_last_checked is None or (time.time() - self.models_last_checked) >= 60:
self.custom_model_names = []
try:
models_path = self.opts.custom_models_dir
if os.path.exists(models_path):
self.custom_model_names = [entry.name[:-3] for entry in os.scandir(models_path)
if (entry.is_file()
and entry.name.endswith(".pt")
and not entry.name.startswith("yolov8"))]
except:
pass
self.models_last_checked = time.time()
return { "success": True, "models": self.custom_model_names }
if __name__ == "__main__":
YOLOv8_adapter().start_loop()