forked from OlafenwaMoses/ImageAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_object_detection.py
305 lines (252 loc) · 12.5 KB
/
test_object_detection.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import os, sys
from typing import List
import shutil
import cv2
import uuid
from PIL import Image
import numpy as np
import pytest
from os.path import dirname
sys.path.insert(1, os.path.join(dirname(dirname(os.path.abspath(__file__)))))
from imageai.Detection import ObjectDetection
test_folder = dirname(os.path.abspath(__file__))
def delete_cache(paths: List[str]):
for path in paths:
if os.path.isfile(path):
os.remove(path)
elif os.path.isdir(path):
shutil.rmtree(path)
@pytest.mark.parametrize(
"input_image, output_type, extract_objects",
[
(os.path.join(test_folder, test_folder, "data-images", "1.jpg"), "file", False),
(os.path.join(test_folder, test_folder, "data-images", "4.jpg"), "file", False),
(os.path.join(test_folder, test_folder, "data-images", "1.jpg"), "file", True),
(cv2.imread(os.path.join(test_folder, test_folder, "data-images", "1.jpg")), "array", False),
(cv2.imread(os.path.join(test_folder, test_folder, "data-images", "1.jpg")), "array", True),
(Image.open(os.path.join(test_folder, test_folder, "data-images", "1.jpg")), "array", True),
]
)
def test_object_detection_retinanet(input_image, output_type, extract_objects):
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath(os.path.join(test_folder, "data-models", "retinanet_resnet50_fpn_coco-eeacb38b.pth"))
detector.loadModel()
output_img_path = os.path.join(test_folder, "data-images", str(uuid.uuid4()) + ".jpg")
if output_type == "array":
if extract_objects:
output_image_array, detections, extracted_objects = detector.detectObjectsFromImage(input_image=input_image, output_type=output_type, extract_detected_objects=extract_objects)
assert len(extracted_objects) > 1
for extracted_obj in extracted_objects:
assert type(extracted_obj) == np.ndarray
assert type(detections) == list
else:
output_image_array, detections = detector.detectObjectsFromImage(input_image=input_image, output_type=output_type)
assert type(output_image_array) == np.ndarray
assert type(detections) == list
else:
if extract_objects:
detections, extracted_object_paths = detector.detectObjectsFromImage(input_image=input_image, output_image_path=output_img_path, extract_detected_objects=True)
assert type(detections) == list
assert os.path.isfile(output_img_path)
assert len(extracted_object_paths) > 3
delete_cache(
extracted_object_paths
)
delete_cache(
[extracted_object_paths[0], output_img_path]
)
else:
detections = detector.detectObjectsFromImage(input_image=input_image, output_image_path=output_img_path)
assert type(detections) == list
delete_cache(
[output_img_path]
)
for eachObject in detections:
assert type(eachObject) == dict
assert "name" in eachObject.keys()
assert type(eachObject["name"]) == str
assert "percentage_probability" in eachObject.keys()
assert type(eachObject["percentage_probability"]) == float
assert "box_points" in eachObject.keys()
assert type(eachObject["box_points"]) == list
box_points = eachObject["box_points"]
for point in box_points:
assert type(point) == int
assert box_points[0] < box_points[2]
assert box_points[1] < box_points[3]
@pytest.mark.parametrize(
"input_image, output_type, extract_objects",
[
(os.path.join(test_folder, test_folder, "data-images", "1.jpg"), "file", False),
(os.path.join(test_folder, test_folder, "data-images", "4.jpg"), "file", False),
(os.path.join(test_folder, test_folder, "data-images", "1.jpg"), "file", True),
(cv2.imread(os.path.join(test_folder, test_folder, "data-images", "1.jpg")), "array", False),
(cv2.imread(os.path.join(test_folder, test_folder, "data-images", "1.jpg")), "array", True),
(Image.open(os.path.join(test_folder, test_folder, "data-images", "1.jpg")), "array", True),
]
)
def test_object_detection_yolov3(input_image, output_type, extract_objects):
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(test_folder, "data-models", "yolov3.pt"))
detector.loadModel()
output_img_path = os.path.join(test_folder, "data-images", str(uuid.uuid4()) + ".jpg")
if output_type == "array":
if extract_objects:
output_image_array, detections, extracted_objects = detector.detectObjectsFromImage(input_image=input_image, output_type=output_type, extract_detected_objects=extract_objects)
assert len(extracted_objects) > 1
assert type(detections) == list
for extracted_obj in extracted_objects:
assert type(extracted_obj) == np.ndarray
else:
output_image_array, detections = detector.detectObjectsFromImage(input_image=input_image, output_type=output_type)
assert type(output_image_array) == np.ndarray
assert type(detections) == list
else:
if extract_objects:
detections, extracted_object_paths = detector.detectObjectsFromImage(input_image=input_image, output_image_path=output_img_path, extract_detected_objects=True)
assert os.path.isfile(output_img_path)
assert len(extracted_object_paths) > 3
assert type(detections) == list
delete_cache(
extracted_object_paths
)
delete_cache(
[extracted_object_paths[0], output_img_path]
)
else:
detections = detector.detectObjectsFromImage(input_image=input_image, output_image_path=output_img_path)
assert type(detections) == list
delete_cache(
[output_img_path]
)
for eachObject in detections:
assert type(eachObject) == dict
assert "name" in eachObject.keys()
assert type(eachObject["name"]) == str
assert "percentage_probability" in eachObject.keys()
assert type(eachObject["percentage_probability"]) == float
assert "box_points" in eachObject.keys()
assert type(eachObject["box_points"]) == list
box_points = eachObject["box_points"]
for point in box_points:
assert type(point) == int
assert box_points[0] < box_points[2]
assert box_points[1] < box_points[3]
@pytest.mark.parametrize(
"input_image, output_type, extract_objects",
[
(os.path.join(test_folder, test_folder, "data-images", "1.jpg"), "file", False),
(os.path.join(test_folder, test_folder, "data-images", "4.jpg"), "file", False),
(os.path.join(test_folder, test_folder, "data-images", "1.jpg"), "file", True),
(cv2.imread(os.path.join(test_folder, test_folder, "data-images", "1.jpg")), "array", False),
(cv2.imread(os.path.join(test_folder, test_folder, "data-images", "1.jpg")), "array", True),
(Image.open(os.path.join(test_folder, test_folder, "data-images", "11.jpg")), "array", True),
]
)
def test_object_detection_tiny_yolov3(input_image, output_type, extract_objects):
detector = ObjectDetection()
detector.setModelTypeAsTinyYOLOv3()
detector.setModelPath(os.path.join(test_folder, "data-models", "tiny-yolov3.pt"))
detector.loadModel()
output_img_path = os.path.join(test_folder, "data-images", str(uuid.uuid4()) + ".jpg")
if output_type == "array":
if extract_objects:
output_image_array, detections, extracted_objects = detector.detectObjectsFromImage(input_image=input_image, output_type=output_type, extract_detected_objects=extract_objects)
assert len(extracted_objects) > 1
assert type(detections) == list
for extracted_obj in extracted_objects:
assert type(extracted_obj) == np.ndarray
else:
output_image_array, detections = detector.detectObjectsFromImage(input_image=input_image, output_type=output_type)
assert type(output_image_array) == np.ndarray
assert type(detections) == list
else:
if extract_objects:
detections, extracted_object_paths = detector.detectObjectsFromImage(input_image=input_image, output_image_path=output_img_path, extract_detected_objects=True)
assert os.path.isfile(output_img_path)
assert len(extracted_object_paths) > 1
assert type(detections) == list
delete_cache(
extracted_object_paths
)
delete_cache(
[extracted_object_paths[0], output_img_path]
)
else:
detections = detector.detectObjectsFromImage(input_image=input_image, output_image_path=output_img_path)
assert type(detections) == list
delete_cache(
[output_img_path]
)
for eachObject in detections:
assert type(eachObject) == dict
assert "name" in eachObject.keys()
assert type(eachObject["name"]) == str
assert "percentage_probability" in eachObject.keys()
assert type(eachObject["percentage_probability"]) == float
assert "box_points" in eachObject.keys()
assert type(eachObject["box_points"]) == list
box_points = eachObject["box_points"]
for point in box_points:
assert type(point) == int
assert box_points[0] < box_points[2]
assert box_points[1] < box_points[3]
@pytest.mark.parametrize(
"input_image",
[
(os.path.join(test_folder, test_folder, "data-images", "11.jpg")),
(cv2.imread(os.path.join(test_folder, test_folder, "data-images", "11.jpg"))),
(Image.open(os.path.join(test_folder, test_folder, "data-images", "11.jpg"))),
]
)
def test_object_detection_retinanet_custom_objects(input_image):
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath(os.path.join(test_folder, "data-models", "retinanet_resnet50_fpn_coco-eeacb38b.pth"))
detector.loadModel()
custom = detector.CustomObjects(person=True, cell_phone=True)
custom_detections = detector.detectObjectsFromImage(input_image=input_image, custom_objects=custom)
for custom_detection in custom_detections:
assert custom_detection["name"] in ["person", "cell phone"]
detections = detector.detectObjectsFromImage(input_image=input_image)
assert len(detections) > len(custom_detections)
@pytest.mark.parametrize(
"input_image",
[
(os.path.join(test_folder, test_folder, "data-images", "11.jpg")),
(cv2.imread(os.path.join(test_folder, test_folder, "data-images", "11.jpg"))),
(Image.open(os.path.join(test_folder, test_folder, "data-images", "11.jpg"))),
]
)
def test_object_detection_yolov3_custom_objects(input_image):
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(test_folder, "data-models", "yolov3.pt"))
detector.loadModel()
custom = detector.CustomObjects(person=True, cell_phone=True)
custom_detections = detector.detectObjectsFromImage(input_image=input_image, custom_objects=custom)
for custom_detection in custom_detections:
assert custom_detection["name"] in ["person", "cell phone"]
detections = detector.detectObjectsFromImage(input_image=input_image)
assert len(detections) > len(custom_detections)
@pytest.mark.parametrize(
"input_image",
[
(os.path.join(test_folder, test_folder, "data-images", "11.jpg")),
(cv2.imread(os.path.join(test_folder, test_folder, "data-images", "11.jpg"))),
(Image.open(os.path.join(test_folder, test_folder, "data-images", "11.jpg"))),
]
)
def test_object_detection_tiny_yolov3_custom_objects(input_image):
detector = ObjectDetection()
detector.setModelTypeAsTinyYOLOv3()
detector.setModelPath(os.path.join(test_folder, "data-models", "tiny-yolov3.pt"))
detector.loadModel()
custom = detector.CustomObjects(person=True, cell_phone=True)
custom_detections = detector.detectObjectsFromImage(input_image=input_image, custom_objects=custom)
for custom_detection in custom_detections:
assert custom_detection["name"] in ["person", "cell phone"]
detections = detector.detectObjectsFromImage(input_image=input_image)
assert len(detections) > len(custom_detections)