-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
1732 lines (1408 loc) · 106 KB
/
__init__.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import cv2
from imageai.Detection.keras_retinanet.models.resnet import resnet50_retinanet
from imageai.Detection.keras_retinanet.utils.image import read_image_bgr, read_image_array, read_image_stream, \
preprocess_image, resize_image
from imageai.Detection.keras_retinanet.utils.visualization import draw_box, draw_caption
from imageai.Detection.keras_retinanet.utils.colors import label_color
import matplotlib.pyplot as plt
import matplotlib.image as pltimage
import numpy as np
import tensorflow as tf
import os
from keras import backend as K
from keras.layers import Input
from PIL import Image
import colorsys
import threading
from imageai.Detection.YOLOv3.models import yolo_main, tiny_yolo_main
from imageai.Detection.YOLOv3.utils import letterbox_image, yolo_eval
def get_session():
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
return tf.Session(config=config)
class ObjectDetection:
"""
This is the object detection class for images in the ImageAI library. It provides support for RetinaNet
, YOLOv3 and TinyYOLOv3 object detection networks . After instantiating this class, you can set it's properties and
make object detections using it's pre-defined functions.
The following functions are required to be called before object detection can be made
* setModelPath()
* At least of of the following and it must correspond to the model set in the setModelPath()
[setModelTypeAsRetinaNet(), setModelTypeAsYOLOv3(), setModelTypeAsTinyYOLOv3()]
* loadModel() [This must be called once only before performing object detection]
Once the above functions have been called, you can call the detectObjectsFromImage() function of
the object detection instance object at anytime to obtain observable objects in any image.
"""
def __init__(self):
self.__modelType = ""
self.modelPath = ""
self.__modelPathAdded = False
self.__modelLoaded = False
self.__model_collection = []
# Instance variables for RetinaNet Model
self.__input_image_min = 1333
self.__input_image_max = 800
self.numbers_to_names = {0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus',
6: 'train',
7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign',
12: 'parking meter',
13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow',
20: 'elephant',
21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag',
27: 'tie',
28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball',
33: 'kite',
34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard',
38: 'tennis racket',
39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon',
45: 'bowl',
46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot',
52: 'hot dog',
53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant',
59: 'bed',
60: 'dining table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote',
66: 'keyboard',
67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink',
72: 'refrigerator',
73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear',
78: 'hair dryer',
79: 'toothbrush'}
# Unique instance variables for YOLOv3 and TinyYOLOv3 model
self.__yolo_iou = 0.45
self.__yolo_score = 0.1
self.__yolo_anchors = np.array(
[[10., 13.], [16., 30.], [33., 23.], [30., 61.], [62., 45.], [59., 119.], [116., 90.], [156., 198.],
[373., 326.]])
self.__yolo_model_image_size = (416, 416)
self.__yolo_boxes, self.__yolo_scores, self.__yolo_classes = "", "", ""
self.sess = K.get_session()
# Unique instance variables for TinyYOLOv3.
self.__tiny_yolo_anchors = np.array(
[[10., 14.], [23., 27.], [37., 58.], [81., 82.], [135., 169.], [344., 319.]])
def setModelTypeAsRetinaNet(self):
"""
'setModelTypeAsRetinaNet()' is used to set the model type to the RetinaNet model
for the video object detection instance instance object .
:return:
"""
self.__modelType = "retinanet"
def setModelTypeAsYOLOv3(self):
"""
'setModelTypeAsYOLOv3()' is used to set the model type to the YOLOv3 model
for the video object detection instance instance object .
:return:
"""
self.__modelType = "yolov3"
def setModelTypeAsTinyYOLOv3(self):
"""
'setModelTypeAsTinyYOLOv3()' is used to set the model type to the TinyYOLOv3 model
for the video object detection instance instance object .
:return:
"""
self.__modelType = "tinyyolov3"
def setModelPath(self, model_path):
"""
'setModelPath()' function is required and is used to set the file path to a RetinaNet
object detection model trained on the COCO dataset.
:param model_path:
:return:
"""
if (self.__modelPathAdded == False):
self.modelPath = model_path
self.__modelPathAdded = True
def loadModel(self, detection_speed="normal"):
"""
'loadModel()' function is required and is used to load the model structure into the program from the file path defined
in the setModelPath() function. This function receives an optional value which is "detection_speed".
The value is used to reduce the time it takes to detect objects in an image, down to about a 10% of the normal time, with
with just slight reduction in the number of objects detected.
* prediction_speed (optional); Acceptable values are "normal", "fast", "faster", "fastest" and "flash"
:param detection_speed:
:return:
"""
if (self.__modelType == "retinanet"):
if (detection_speed == "normal"):
self.__input_image_min = 800
self.__input_image_max = 1333
elif (detection_speed == "fast"):
self.__input_image_min = 400
self.__input_image_max = 700
elif (detection_speed == "faster"):
self.__input_image_min = 300
self.__input_image_max = 500
elif (detection_speed == "fastest"):
self.__input_image_min = 200
self.__input_image_max = 350
elif (detection_speed == "flash"):
self.__input_image_min = 100
self.__input_image_max = 250
elif (self.__modelType == "yolov3"):
if (detection_speed == "normal"):
self.__yolo_model_image_size = (416, 416)
elif (detection_speed == "fast"):
self.__yolo_model_image_size = (320, 320)
elif (detection_speed == "faster"):
self.__yolo_model_image_size = (208, 208)
elif (detection_speed == "fastest"):
self.__yolo_model_image_size = (128, 128)
elif (detection_speed == "flash"):
self.__yolo_model_image_size = (96, 96)
elif (self.__modelType == "tinyyolov3"):
if (detection_speed == "normal"):
self.__yolo_model_image_size = (832, 832)
elif (detection_speed == "fast"):
self.__yolo_model_image_size = (576, 576)
elif (detection_speed == "faster"):
self.__yolo_model_image_size = (416, 416)
elif (detection_speed == "fastest"):
self.__yolo_model_image_size = (320, 320)
elif (detection_speed == "flash"):
self.__yolo_model_image_size = (272, 272)
if (self.__modelLoaded == False):
if (self.__modelType == ""):
raise ValueError("You must set a valid model type before loading the model.")
elif (self.__modelType == "retinanet"):
model = resnet50_retinanet(num_classes=80)
model.load_weights(self.modelPath)
global graph
#graph = tf.get_default_graph() # ISSAUGOM GRAPH
self.__model_collection.append(model)
self.__modelLoaded = True
elif (self.__modelType == "yolov3"):
model = yolo_main(Input(shape=(None, None, 3)), len(self.__yolo_anchors) // 3,
len(self.numbers_to_names))
model.load_weights(self.modelPath)
hsv_tuples = [(x / len(self.numbers_to_names), 1., 1.)
for x in range(len(self.numbers_to_names))]
self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
self.colors = list(
map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
self.colors))
np.random.seed(10101)
np.random.shuffle(self.colors)
np.random.seed(None)
self.__yolo_input_image_shape = K.placeholder(shape=(2,))
self.__yolo_boxes, self.__yolo_scores, self.__yolo_classes = yolo_eval(model.output,
self.__yolo_anchors,
len(self.numbers_to_names),
self.__yolo_input_image_shape,
score_threshold=self.__yolo_score,
iou_threshold=self.__yolo_iou)
self.__model_collection.append(model)
self.__modelLoaded = True
elif (self.__modelType == "tinyyolov3"):
model = tiny_yolo_main(Input(shape=(None, None, 3)), len(self.__tiny_yolo_anchors) // 2,
len(self.numbers_to_names))
print("__init__ Model is Loading - Ident/Graph : " + str(threading.get_ident()) + str(tf.get_default_graph()))
model.load_weights(self.modelPath) # Edited
global graph
graph = tf.get_default_graph() # SAVING GRAPH (graph loaded at the beginning and used later should be same)
hsv_tuples = [(x / len(self.numbers_to_names), 1., 1.)
for x in range(len(self.numbers_to_names))]
self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
self.colors = list(
map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
self.colors))
np.random.seed(10101)
np.random.shuffle(self.colors)
np.random.seed(None)
self.__yolo_input_image_shape = K.placeholder(shape=(2,))
self.__yolo_boxes, self.__yolo_scores, self.__yolo_classes = yolo_eval(model.output,
self.__tiny_yolo_anchors,
len(self.numbers_to_names),
self.__yolo_input_image_shape,
score_threshold=self.__yolo_score,
iou_threshold=self.__yolo_iou)
self.__model_collection.append(model)
self.__modelLoaded = True
def detectObjectsFromImage(self, input_image="", output_image_path="", input_type="file", output_type="file",
extract_detected_objects=False, minimum_percentage_probability=50,
display_percentage_probability=True, display_object_name=True, thread_safe=False):
"""
'detectObjectsFromImage()' function is used to detect objects observable in the given image path:
* input_image , which can be a filepath, image numpy array or image file stream
* output_image_path (only if output_type = file) , file path to the output image that will contain the detection boxes and label, if output_type="file"
* input_type (optional) , file path/numpy array/image file stream of the image. Acceptable values are "file", "array" and "stream"
* output_type (optional) , file path/numpy array/image file stream of the image. Acceptable values are "file" and "array"
* extract_detected_objects (optional) , option to save each object detected individually as an image and return an array of the objects' image path.
* minimum_percentage_probability (optional, 50 by default) , option to set the minimum percentage probability for nominating a detected object for output.
* display_percentage_probability (optional, True by default), option to show or hide the percentage probability of each object in the saved/returned detected image
* display_display_object_name (optional, True by default), option to show or hide the name of each object in the saved/returned detected image
* thread_safe (optional, False by default), enforce the loaded detection model works across all threads if set to true, made possible by forcing all Tensorflow inference to run on the default graph.
The values returned by this function depends on the parameters parsed. The possible values returnable
are stated as below
- If extract_detected_objects = False or at its default value and output_type = 'file' or
at its default value, you must parse in the 'output_image_path' as a string to the path you want
the detected image to be saved. Then the function will return:
1. an array of dictionaries, with each dictionary corresponding to the objects
detected in the image. Each dictionary contains the following property:
* name (string)
* percentage_probability (float)
* box_points (list of x1,y1,x2 and y2 coordinates)
- If extract_detected_objects = False or at its default value and output_type = 'array' ,
Then the function will return:
1. a numpy array of the detected image
2. an array of dictionaries, with each dictionary corresponding to the objects
detected in the image. Each dictionary contains the following property:
* name (string)
* percentage_probability (float)
* box_points (list of x1,y1,x2 and y2 coordinates)
- If extract_detected_objects = True and output_type = 'file' or
at its default value, you must parse in the 'output_image_path' as a string to the path you want
the detected image to be saved. Then the function will return:
1. an array of dictionaries, with each dictionary corresponding to the objects
detected in the image. Each dictionary contains the following property:
* name (string)
* percentage_probability (float)
* box_points (list of x1,y1,x2 and y2 coordinates)
2. an array of string paths to the image of each object extracted from the image
- If extract_detected_objects = True and output_type = 'array', the the function will return:
1. a numpy array of the detected image
2. an array of dictionaries, with each dictionary corresponding to the objects
detected in the image. Each dictionary contains the following property:
* name (string)
* percentage_probability (float)
* box_points (list of x1,y1,x2 and y2 coordinates)
3. an array of numpy arrays of each object detected in the image
:param input_image:
:param output_image_path:
:param input_type:
:param output_type:
:param extract_detected_objects:
:param minimum_percentage_probability:
:param display_percentage_probability:
:param display_object_name:
:param thread_safe:
:return image_frame:
:return output_objects_array:
:return detected_objects_image_array:
"""
if (self.__modelLoaded == False):
raise ValueError("You must call the loadModel() function before making object detection.")
elif (self.__modelLoaded == True):
try:
if (self.__modelType == "retinanet"):
output_objects_array = []
detected_objects_image_array = []
if (input_type == "file"):
image = read_image_bgr(input_image)
elif (input_type == "array"):
image = read_image_array(input_image)
elif (input_type == "stream"):
image = read_image_stream(input_image)
detected_copy = image.copy()
detected_copy = cv2.cvtColor(detected_copy, cv2.COLOR_BGR2RGB)
detected_copy2 = image.copy()
detected_copy2 = cv2.cvtColor(detected_copy2, cv2.COLOR_BGR2RGB)
image = preprocess_image(image)
image, scale = resize_image(image, min_side=self.__input_image_min, max_side=self.__input_image_max)
model = self.__model_collection[0]
if thread_safe == True:
with self.sess.graph.as_default():
_, _, detections = model.predict_on_batch(np.expand_dims(image, axis=0))
else:
_, _, detections = model.predict_on_batch(np.expand_dims(image, axis=0))
predicted_numbers = np.argmax(detections[0, :, 4:], axis=1)
scores = detections[0, np.arange(detections.shape[1]), 4 + predicted_numbers]
detections[0, :, :4] /= scale
min_probability = minimum_percentage_probability / 100
counting = 0
for index, (label, score), in enumerate(zip(predicted_numbers, scores)):
if score < min_probability:
continue
counting += 1
objects_dir = output_image_path + "-objects"
if (extract_detected_objects == True and output_type == "file"):
if (os.path.exists(objects_dir) == False):
os.mkdir(objects_dir)
color = label_color(label)
detection_details = detections[0, index, :4].astype(int)
draw_box(detected_copy, detection_details, color=color)
if (display_object_name == True and display_percentage_probability == True):
caption = "{} {:.3f}".format(self.numbers_to_names[label], (score * 100))
draw_caption(detected_copy, detection_details, caption)
elif (display_object_name == True):
caption = "{} ".format(self.numbers_to_names[label])
draw_caption(detected_copy, detection_details, caption)
elif (display_percentage_probability == True):
caption = " {:.3f}".format((score * 100))
draw_caption(detected_copy, detection_details, caption)
each_object_details = {}
each_object_details["name"] = self.numbers_to_names[label]
each_object_details["percentage_probability"] = score * 100
each_object_details["box_points"] = detection_details.tolist()
output_objects_array.append(each_object_details)
if (extract_detected_objects == True):
splitted_copy = detected_copy2.copy()[detection_details[1]:detection_details[3],
detection_details[0]:detection_details[2]]
if (output_type == "file"):
splitted_image_path = os.path.join(objects_dir,
self.numbers_to_names[label] + "-" + str(
counting) + ".jpg")
pltimage.imsave(splitted_image_path, splitted_copy)
detected_objects_image_array.append(splitted_image_path)
elif (output_type == "array"):
detected_objects_image_array.append(splitted_copy)
if (output_type == "file"):
pltimage.imsave(output_image_path, detected_copy)
if (extract_detected_objects == True):
if (output_type == "file"):
return output_objects_array, detected_objects_image_array
elif (output_type == "array"):
return detected_copy, output_objects_array, detected_objects_image_array
else:
if (output_type == "file"):
return output_objects_array
elif (output_type == "array"):
return detected_copy, output_objects_array
elif (self.__modelType == "yolov3" or self.__modelType == "tinyyolov3"):
output_objects_array = []
detected_objects_image_array = []
if (input_type == "file"):
image = Image.open(input_image)
input_image = read_image_bgr(input_image)
elif (input_type == "array"):
image = Image.fromarray(np.uint8(input_image))
input_image = read_image_array(input_image)
elif (input_type == "stream"):
image = Image.open(input_image)
input_image = read_image_stream(input_image)
detected_copy = input_image
detected_copy = cv2.cvtColor(detected_copy, cv2.COLOR_BGR2RGB)
detected_copy2 = input_image
detected_copy2 = cv2.cvtColor(detected_copy2, cv2.COLOR_BGR2RGB)
new_image_size = (self.__yolo_model_image_size[0] - (self.__yolo_model_image_size[0] % 32),
self.__yolo_model_image_size[1] - (self.__yolo_model_image_size[1] % 32))
boxed_image = letterbox_image(image, new_image_size)
image_data = np.array(boxed_image, dtype="float32")
image_data /= 255.
image_data = np.expand_dims(image_data, 0)
model = self.__model_collection[0]
if thread_safe == True:
with self.sess.graph.as_default():
out_boxes, out_scores, out_classes = self.sess.run(
[self.__yolo_boxes, self.__yolo_scores, self.__yolo_classes],
feed_dict={
model.input: image_data,
self.__yolo_input_image_shape: [image.size[1], image.size[0]],
K.learning_phase(): 0
})
else:
out_boxes, out_scores, out_classes = self.sess.run(
[self.__yolo_boxes, self.__yolo_scores, self.__yolo_classes],
feed_dict={
model.input: image_data,
self.__yolo_input_image_shape: [image.size[1], image.size[0]],
K.learning_phase(): 0
})
min_probability = minimum_percentage_probability / 100
counting = 0
for a, b in reversed(list(enumerate(out_classes))):
predicted_class = self.numbers_to_names[b]
box = out_boxes[a]
score = out_scores[a]
if score < min_probability:
continue
counting += 1
objects_dir = output_image_path + "-objects"
if (extract_detected_objects == True and output_type == "file"):
if (os.path.exists(objects_dir) == False):
os.mkdir(objects_dir)
label = "{} {:.2f}".format(predicted_class, score)
top, left, bottom, right = box
top = max(0, np.floor(top + 0.5).astype('int32'))
left = max(0, np.floor(left + 0.5).astype('int32'))
bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
try:
color = label_color(b)
except:
color = (255, 0, 0)
detection_details = [left, top, right, bottom]
draw_box(detected_copy, detection_details, color=color)
if (display_object_name == True and display_percentage_probability == True):
draw_caption(detected_copy, detection_details, label)
elif (display_object_name == True):
draw_caption(detected_copy, detection_details, predicted_class)
elif (display_percentage_probability == True):
draw_caption(detected_copy, detection_details, str(score * 100))
each_object_details = {}
each_object_details["name"] = predicted_class
each_object_details["percentage_probability"] = score * 100
each_object_details["box_points"] = detection_details
output_objects_array.append(each_object_details)
if (extract_detected_objects == True):
splitted_copy = detected_copy2.copy()[detection_details[1]:detection_details[3],
detection_details[0]:detection_details[2]]
if (output_type == "file"):
splitted_image_path = os.path.join(objects_dir,
predicted_class + "-" + str(
counting) + ".jpg")
pltimage.imsave(splitted_image_path, splitted_copy)
detected_objects_image_array.append(splitted_image_path)
elif (output_type == "array"):
detected_objects_image_array.append(splitted_copy)
if (output_type == "file"):
pltimage.imsave(output_image_path, detected_copy)
if (extract_detected_objects == True):
if (output_type == "file"):
return output_objects_array, detected_objects_image_array
elif (output_type == "array"):
return detected_copy, output_objects_array, detected_objects_image_array
else:
if (output_type == "file"):
return output_objects_array
elif (output_type == "array"):
return detected_copy, output_objects_array
except:
raise ValueError(
"Ensure you specified correct input image, input type, output type and/or output image path ")
def CustomObjects(self, person=False, bicycle=False, car=False, motorcycle=False, airplane=False,
bus=False, train=False, truck=False, boat=False, traffic_light=False, fire_hydrant=False,
stop_sign=False,
parking_meter=False, bench=False, bird=False, cat=False, dog=False, horse=False, sheep=False,
cow=False, elephant=False, bear=False, zebra=False,
giraffe=False, backpack=False, umbrella=False, handbag=False, tie=False, suitcase=False,
frisbee=False, skis=False, snowboard=False,
sports_ball=False, kite=False, baseball_bat=False, baseball_glove=False, skateboard=False,
surfboard=False, tennis_racket=False,
bottle=False, wine_glass=False, cup=False, fork=False, knife=False, spoon=False, bowl=False,
banana=False, apple=False, sandwich=False, orange=False,
broccoli=False, carrot=False, hot_dog=False, pizza=False, donut=False, cake=False, chair=False,
couch=False, potted_plant=False, bed=False,
dining_table=False, toilet=False, tv=False, laptop=False, mouse=False, remote=False,
keyboard=False, cell_phone=False, microwave=False,
oven=False, toaster=False, sink=False, refrigerator=False, book=False, clock=False, vase=False,
scissors=False, teddy_bear=False, hair_dryer=False,
toothbrush=False):
"""
The 'CustomObjects()' function allows you to handpick the type of objects you want to detect
from an image. The objects are pre-initiated in the function variables and predefined as 'False',
which you can easily set to true for any number of objects available. This function
returns a dictionary which must be parsed into the 'detectCustomObjectsFromImage()'. Detecting
custom objects only happens when you call the function 'detectCustomObjectsFromImage()'
* true_values_of_objects (array); Acceptable values are 'True' and False for all object values present
:param boolean_values:
:return: custom_objects_dict
"""
custom_objects_dict = {}
input_values = [person, bicycle, car, motorcycle, airplane,
bus, train, truck, boat, traffic_light, fire_hydrant, stop_sign,
parking_meter, bench, bird, cat, dog, horse, sheep, cow, elephant, bear, zebra,
giraffe, backpack, umbrella, handbag, tie, suitcase, frisbee, skis, snowboard,
sports_ball, kite, baseball_bat, baseball_glove, skateboard, surfboard, tennis_racket,
bottle, wine_glass, cup, fork, knife, spoon, bowl, banana, apple, sandwich, orange,
broccoli, carrot, hot_dog, pizza, donut, cake, chair, couch, potted_plant, bed,
dining_table, toilet, tv, laptop, mouse, remote, keyboard, cell_phone, microwave,
oven, toaster, sink, refrigerator, book, clock, vase, scissors, teddy_bear, hair_dryer,
toothbrush]
actual_labels = ["person", "bicycle", "car", "motorcycle", "airplane",
"bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign",
"parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear",
"zebra",
"giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis",
"snowboard",
"sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
"tennis racket",
"bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich",
"orange",
"broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch", "potted plant",
"bed",
"dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone",
"microwave",
"oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear",
"hair dryer",
"toothbrush"]
for input_value, actual_label in zip(input_values, actual_labels):
if (input_value == True):
custom_objects_dict[actual_label] = "valid"
else:
custom_objects_dict[actual_label] = "invalid"
return custom_objects_dict
def detectCustomObjectsFromImage(self, custom_objects=None, input_image="", output_image_path="", input_type="file",
output_type="file", extract_detected_objects=False,
minimum_percentage_probability=50, display_percentage_probability=True,
display_object_name=True, thread_safe=False):
"""
'detectCustomObjectsFromImage()' function is used to detect predefined objects observable in the given image path:
* custom_objects , an instance of the CustomObject class to filter which objects to detect
* input_image , which can be file to path, image numpy array or image file stream
* output_image_path , file path to the output image that will contain the detection boxes and label, if output_type="file"
* input_type (optional) , file path/numpy array/image file stream of the image. Acceptable values are "file", "array" and "stream"
* output_type (optional) , file path/numpy array/image file stream of the image. Acceptable values are "file" and "array"
* extract_detected_objects (optional, False by default) , option to save each object detected individually as an image and return an array of the objects' image path.
* minimum_percentage_probability (optional, 50 by default) , option to set the minimum percentage probability for nominating a detected object for output.
* display_percentage_probability (optional, True by default), option to show or hide the percentage probability of each object in the saved/returned detected image
* display_display_object_name (optional, True by default), option to show or hide the name of each object in the saved/returned detected image
* thread_safe (optional, False by default), enforce the loaded detection model works across all threads if set to true, made possible by forcing all Tensorflow inference to run on the default graph.
The values returned by this function depends on the parameters parsed. The possible values returnable
are stated as below
- If extract_detected_objects = False or at its default value and output_type = 'file' or
at its default value, you must parse in the 'output_image_path' as a string to the path you want
the detected image to be saved. Then the function will return:
1. an array of dictionaries, with each dictionary corresponding to the objects
detected in the image. Each dictionary contains the following property:
* name (string)
* percentage_probability (float)
* box_points (list of x1,y1,x2 and y2 coordinates)
- If extract_detected_objects = False or at its default value and output_type = 'array' ,
Then the function will return:
1. a numpy array of the detected image
2. an array of dictionaries, with each dictionary corresponding to the objects
detected in the image. Each dictionary contains the following property:
* name (string)
* percentage_probability (float)
* box_points (list of x1,y1,x2 and y2 coordinates)
- If extract_detected_objects = True and output_type = 'file' or
at its default value, you must parse in the 'output_image_path' as a string to the path you want
the detected image to be saved. Then the function will return:
1. an array of dictionaries, with each dictionary corresponding to the objects
detected in the image. Each dictionary contains the following property:
* name (string)
* percentage_probability (float)
* box_points (list of x1,y1,x2 and y2 coordinates)
2. an array of string paths to the image of each object extracted from the image
- If extract_detected_objects = True and output_type = 'array', the the function will return:
1. a numpy array of the detected image
2. an array of dictionaries, with each dictionary corresponding to the objects
detected in the image. Each dictionary contains the following property:
* name (string)
* percentage_probability (float)
* box_points (list of x1,y1,x2 and y2 coordinates)
3. an array of numpy arrays of each object detected in the image
:param input_image:
:param output_image_path:
:param input_type:
:param output_type:
:param extract_detected_objects:
:param minimum_percentage_probability:
:return output_objects_array:
:param display_percentage_probability:
:param display_object_name
:return detected_copy:
:return detected_detected_objects_image_array:
"""
if (self.__modelLoaded == False):
raise ValueError("You must call the loadModel() function before making object detection.")
elif (self.__modelLoaded == True):
try:
if (self.__modelType == "retinanet"):
output_objects_array = []
detected_objects_image_array = []
if (input_type == "file"):
image = read_image_bgr(input_image)
elif (input_type == "array"):
image = read_image_array(input_image)
elif (input_type == "stream"):
image = read_image_stream(input_image)
detected_copy = image.copy()
detected_copy = cv2.cvtColor(detected_copy, cv2.COLOR_BGR2RGB)
detected_copy2 = image.copy()
detected_copy2 = cv2.cvtColor(detected_copy2, cv2.COLOR_BGR2RGB)
image = preprocess_image(image)
image, scale = resize_image(image, min_side=self.__input_image_min, max_side=self.__input_image_max)
model = self.__model_collection[0]
if thread_safe == True:
with self.sess.graph.as_default():
_, _, detections = model.predict_on_batch(np.expand_dims(image, axis=0))
else:
_, _, detections = model.predict_on_batch(np.expand_dims(image, axis=0))
predicted_numbers = np.argmax(detections[0, :, 4:], axis=1)
scores = detections[0, np.arange(detections.shape[1]), 4 + predicted_numbers]
detections[0, :, :4] /= scale
min_probability = minimum_percentage_probability / 100
counting = 0
for index, (label, score), in enumerate(zip(predicted_numbers, scores)):
if score < min_probability:
continue
if (custom_objects != None):
check_name = self.numbers_to_names[label]
if (custom_objects[check_name] == "invalid"):
continue
counting += 1
objects_dir = output_image_path + "-objects"
if (extract_detected_objects == True and output_type == "file"):
if (os.path.exists(objects_dir) == False):
os.mkdir(objects_dir)
color = label_color(label)
detection_details = detections[0, index, :4].astype(int)
draw_box(detected_copy, detection_details, color=color)
if (display_object_name == True and display_percentage_probability == True):
caption = "{} {:.3f}".format(self.numbers_to_names[label], (score * 100))
draw_caption(detected_copy, detection_details, caption)
elif (display_object_name == True):
caption = "{} ".format(self.numbers_to_names[label])
draw_caption(detected_copy, detection_details, caption)
elif (display_percentage_probability == True):
caption = " {:.3f}".format((score * 100))
draw_caption(detected_copy, detection_details, caption)
each_object_details = {}
each_object_details["name"] = self.numbers_to_names[label]
each_object_details["percentage_probability"] = score * 100
each_object_details["box_points"] = detection_details.tolist()
output_objects_array.append(each_object_details)
if (extract_detected_objects == True):
splitted_copy = detected_copy2.copy()[detection_details[1]:detection_details[3],
detection_details[0]:detection_details[2]]
if (output_type == "file"):
splitted_image_path = os.path.join(objects_dir,
self.numbers_to_names[label] + "-" + str(
counting) + ".jpg")
pltimage.imsave(splitted_image_path, splitted_copy)
detected_objects_image_array.append(splitted_image_path)
elif (output_type == "array"):
detected_objects_image_array.append(splitted_copy)
if (output_type == "file"):
pltimage.imsave(output_image_path, detected_copy)
if (extract_detected_objects == True):
if (output_type == "file"):
return output_objects_array, detected_objects_image_array
elif (output_type == "array"):
return detected_copy, output_objects_array, detected_objects_image_array
else:
if (output_type == "file"):
return output_objects_array
elif (output_type == "array"):
return detected_copy, output_objects_array
elif (self.__modelType == "yolov3" or self.__modelType == "tinyyolov3"):
output_objects_array = []
detected_objects_image_array = []
if (input_type == "file"):
image = Image.open(input_image)
input_image = read_image_bgr(input_image)
elif (input_type == "array"):
image = Image.fromarray(np.uint8(input_image))
input_image = read_image_array(input_image)
elif (input_type == "stream"):
image = Image.open(input_image)
input_image = read_image_stream(input_image)
detected_copy = input_image
detected_copy = cv2.cvtColor(detected_copy, cv2.COLOR_BGR2RGB)
detected_copy2 = input_image
detected_copy2 = cv2.cvtColor(detected_copy2, cv2.COLOR_BGR2RGB)
new_image_size = (self.__yolo_model_image_size[0] - (self.__yolo_model_image_size[0] % 32),
self.__yolo_model_image_size[1] - (self.__yolo_model_image_size[1] % 32))
boxed_image = letterbox_image(image, new_image_size)
image_data = np.array(boxed_image, dtype="float32")
image_data /= 255.
image_data = np.expand_dims(image_data, 0)
model = self.__model_collection[0]
if thread_safe == True:
with self.sess.graph.as_default():
out_boxes, out_scores, out_classes = self.sess.run(
[self.__yolo_boxes, self.__yolo_scores, self.__yolo_classes],
feed_dict={
model.input: image_data,
self.__yolo_input_image_shape: [image.size[1], image.size[0]],
K.learning_phase(): 0
})
else:
out_boxes, out_scores, out_classes = self.sess.run(
[self.__yolo_boxes, self.__yolo_scores, self.__yolo_classes],
feed_dict={
model.input: image_data,
self.__yolo_input_image_shape: [image.size[1], image.size[0]],
K.learning_phase(): 0
})
min_probability = minimum_percentage_probability / 100
counting = 0
for a, b in reversed(list(enumerate(out_classes))):
predicted_class = self.numbers_to_names[b]
box = out_boxes[a]
score = out_scores[a]
if score < min_probability:
continue
if (custom_objects != None):
if (custom_objects[predicted_class] == "invalid"):
continue
counting += 1
objects_dir = output_image_path + "-objects"
if (extract_detected_objects == True and output_type == "file"):
if (os.path.exists(objects_dir) == False):
os.mkdir(objects_dir)
label = "{} {:.2f}".format(predicted_class, score)
top, left, bottom, right = box
top = max(0, np.floor(top + 0.5).astype('int32'))
left = max(0, np.floor(left + 0.5).astype('int32'))
bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
try:
color = label_color(b)
except:
color = (255, 0, 0)
detection_details = [left, top, right, bottom]
draw_box(detected_copy, detection_details, color=color)
if (display_object_name == True and display_percentage_probability == True):
draw_caption(detected_copy, detection_details, label)
elif (display_object_name == True):
draw_caption(detected_copy, detection_details, predicted_class)
elif (display_percentage_probability == True):
draw_caption(detected_copy, detection_details, str(score * 100))
each_object_details = {}
each_object_details["name"] = predicted_class
each_object_details["percentage_probability"] = score * 100
each_object_details["box_points"] = detection_details
output_objects_array.append(each_object_details)
if (extract_detected_objects == True):
splitted_copy = detected_copy2.copy()[detection_details[1]:detection_details[3],
detection_details[0]:detection_details[2]]
if (output_type == "file"):
splitted_image_path = os.path.join(objects_dir,
predicted_class + "-" + str(
counting) + ".jpg")
pltimage.imsave(splitted_image_path, splitted_copy)
detected_objects_image_array.append(splitted_image_path)
elif (output_type == "array"):
detected_objects_image_array.append(splitted_copy)
if (output_type == "file"):
pltimage.imsave(output_image_path, detected_copy)
if (extract_detected_objects == True):
if (output_type == "file"):
return output_objects_array, detected_objects_image_array
elif (output_type == "array"):
return detected_copy, output_objects_array, detected_objects_image_array
else:
if (output_type == "file"):
return output_objects_array
elif (output_type == "array"):
return detected_copy, output_objects_array
except:
raise ValueError(
"Ensure you specified correct input image, input type, output type and/or output image path ")
class VideoObjectDetection:
"""
This is the object detection class for videos and camera live stream inputs in the ImageAI library. It provides support for RetinaNet,
YOLOv3 and TinyYOLOv3 object detection networks. After instantiating this class, you can set it's properties and
make object detections using it's pre-defined functions.
The following functions are required to be called before object detection can be made
* setModelPath()
* At least of of the following and it must correspond to the model set in the setModelPath()
[setModelTypeAsRetinaNet(), setModelTypeAsYOLOv3(), setModelTinyYOLOv3()]
* loadModel() [This must be called once only before performing object detection]
Once the above functions have been called, you can call the detectObjectsFromVideo() function
or the detectCustomObjectsFromVideo() of the object detection instance object at anytime to
obtain observable objects in any video or camera live stream.
"""
def __init__(self):
self.__modelType = ""
self.modelPath = ""
self.__modelPathAdded = False
self.__modelLoaded = False
self.__detector = None
self.__input_image_min = 1333
self.__input_image_max = 800
self.__detection_storage = None
self.numbers_to_names = {0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus',
6: 'train',
7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign',
12: 'parking meter',
13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow',
20: 'elephant',
21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag',
27: 'tie',
28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball',
33: 'kite',
34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard',
38: 'tennis racket',
39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon',
45: 'bowl',
46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot',
52: 'hot dog',
53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant',
59: 'bed',
60: 'dining table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote',
66: 'keyboard',
67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink',
72: 'refrigerator',
73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear',
78: 'hair dryer',
79: 'toothbrush'}
# Unique instance variables for YOLOv3 model
self.__yolo_iou = 0.45
self.__yolo_score = 0.1
self.__yolo_anchors = np.array(
[[10., 13.], [16., 30.], [33., 23.], [30., 61.], [62., 45.], [59., 119.], [116., 90.], [156., 198.],
[373., 326.]])
self.__yolo_model_image_size = (416, 416)
self.__yolo_boxes, self.__yolo_scores, self.__yolo_classes = "", "", ""
self.sess = K.get_session()
# Unique instance variables for TinyYOLOv3.
self.__tiny_yolo_anchors = np.array(
[[10., 14.], [23., 27.], [37., 58.], [81., 82.], [135., 169.], [344., 319.]])