-
Notifications
You must be signed in to change notification settings - Fork 1
/
detection.py
44 lines (38 loc) · 1.81 KB
/
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
from imageai.Detection import ObjectDetection
import os
import numpy as np
# PARAMETERS -----------------------------------------------
_min_percentage = 30
output_file_name = 'detection.csv'
images_folder = 'images/'
out_images_folder = 'images_out/'
# INITIALIZE -----------------------------------------------
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath( os.path.join(execution_path , "models/yolo.h5"))
detector.loadModel()
# COLLECT IMAGES -----------------------------------------------
all_images_array = []
all_files = os.listdir(execution_path+ '/' + images_folder)
for each_file in all_files:
if(not each_file.startswith('.') and each_file.endswith(".jpg")): # or .png
all_images_array.append(each_file)
# DETECTION -----------------------------------------------
results_array =[]
for image in all_images_array:
in_image, out_image = images_folder + image , out_images_folder + image
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , in_image), output_image_path=os.path.join(execution_path , out_image), minimum_percentage_probability=_min_percentage)
results_array.append(detections)
# OUTPUT -----------------------------------------------
out_file = open(output_file_name,'w')
for image, detect_objs in zip(all_images_array, results_array):
preds, probs = [], []
# TODO: not reporting the largest percentage!
for eachObject in detect_objs:
if eachObject["name"] not in preds:
preds.append(eachObject["name"])
probs.append(eachObject["percentage_probability"])
str_imag, str_pred, str_prob = str(image), ', '.join(preds), ', '.join(str(v) for v in np.round(probs,1))
str_out = [str_imag, str_pred, str_prob]
out_file.write(', '.join(str_out)+"\n")