lhofstetter commited on
Commit
971df0f
·
verified ·
1 Parent(s): 7b80f90

Upload object_detection_services.py

Browse files
Files changed (1) hide show
  1. object_detection_services.py +199 -0
object_detection_services.py ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from glob2 import glob
3
+ import torch
4
+ import torchvision
5
+ import cv2 as cv
6
+ import matplotlib.pyplot as plt
7
+ import matplotlib
8
+ import matplotlib.patches as patches
9
+ import numpy as np
10
+ from dataclasses import dataclass, field, asdict, astuple, InitVar
11
+ from typing import List, Dict, Tuple
12
+
13
+
14
+ @dataclass
15
+ class BBoxCoordinates:
16
+ x_min: int = field(init=True, default=None)
17
+ x_max: int = field(init=True, default=None)
18
+ y_min: int = field(init=True, default=None)
19
+ y_max: int = field(init=True, default=None)
20
+ x_center: float = field(init=False, default=False)
21
+ y_center: float = field(init=False, default=False)
22
+ height: int = field(init=False, default=False)
23
+ width: int = field(init=False, default=False)
24
+ as_array: np.ndarray = field(init=False, default=False)
25
+
26
+ def __post_init__(self):
27
+ self.height = self.x_max - self.x_min
28
+ self.width = self.y_max - self.y_min
29
+ self.x_center = 0.5 * (self.x_max + self.x_min)
30
+ self.y_center = 0.5 * (self.y_max + self.y_min)
31
+
32
+ def shift_coordinates(self, x_shift, y_shift, x_new_low_bound=None, x_new_upp_bound=None,
33
+ y_new_low_bound=None, y_new_upp_bound=None):
34
+ self.x_min = self.x_min - x_shift
35
+ if x_new_low_bound is not None:
36
+ self.x_min = max(self.x_min, x_new_low_bound)
37
+ self.x_max = self.x_max - x_shift
38
+ if x_new_upp_bound is not None:
39
+ self.x_max = min(self.x_max, x_new_upp_bound)
40
+ self.y_min = self.y_min - y_shift
41
+ if y_new_low_bound is not None:
42
+ self.y_min = max(self.y_min, y_new_low_bound)
43
+ self.y_max = self.y_max - y_shift
44
+ if y_new_upp_bound is not None:
45
+ self.y_max = min(self.y_max, y_new_upp_bound)
46
+ self.height = self.x_max - self.x_min
47
+ self.width = self.y_max - self.y_min
48
+ self.x_center = 0.5 * (self.x_max + self.x_min)
49
+ self.y_center = 0.5 * (self.y_max + self.y_min)
50
+
51
+ def check_valid(self):
52
+ if self.width and self.height:
53
+ return True
54
+ else:
55
+ return False
56
+
57
+ def make_yolo_label_string(self, label_number, img_size, float_precision):
58
+ final_string = str(int(label_number))
59
+ for this_float in [self.y_center, self.x_center, self.width, self.height]:
60
+ final_string = final_string + ' ' + '{:.{n}f}'.format(this_float/img_size, n=float_precision)
61
+ return final_string
62
+
63
+
64
+ def evaluate_yolo(model_path: str, img_path: str, img_size: int, bound_1: Tuple[int, int], bound_2: Tuple[int, int]):
65
+ model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path, verbose=None) # local model
66
+ inpt_img = cv.imread(img_path)
67
+ img_1 = inpt_img[bound_1[0]:bound_1[0] + img_size, bound_1[1]:bound_1[1] + img_size, :]
68
+ img_2 = inpt_img[bound_2[0]:bound_2[0] + img_size, bound_2[1]:bound_2[1] + img_size, :]
69
+ inpt_imgs = [img_1, img_2]
70
+ results = model(inpt_imgs, size=img_size)
71
+
72
+ result_list_images = []
73
+ results_1 = results.pandas().xyxy[0].reset_index() # img1 predictions (pandas)
74
+ results_2 = results.pandas().xyxy[1].reset_index() # img1 predictions (pandas)
75
+
76
+ for index, row in results_1.iterrows():
77
+ result_list_images.append(
78
+ [row['xmin'] + bound_1[1], row['xmax'] + bound_1[1], row['ymin'] + bound_1[0], row['ymax'] + bound_1[0]])
79
+ for index, row in results_2.iterrows():
80
+ result_list_images.append(
81
+ [row['xmin'] + bound_2[1], row['xmax'] + bound_2[1], row['ymin'] + bound_2[0], row['ymax'] + bound_2[0]])
82
+
83
+ result_arr = np.asarray(result_list_images)
84
+ x_min = np.min(result_arr[:, 0])
85
+ x_max = np.max(result_arr[:, 1])
86
+ y_min = np.min(result_arr[:, 2])
87
+ y_max = np.max(result_arr[:, 3])
88
+
89
+ bound_final_x = max(min(int(0.5 * (x_min + x_max - img_size)), 3208 - img_size), 0)
90
+ bound_final_y = max(min(int(0.5 * (y_min + y_max - img_size)), 2200 - img_size), 0)
91
+
92
+ img_final = inpt_img[bound_final_y:bound_final_y + img_size, bound_final_x:bound_final_x + img_size, :]
93
+ imgs_final = [img_final]
94
+ results_final = model(imgs_final, size=img_size)
95
+ bboxes_final = results_final.pandas().xyxy[0].reset_index() # img1 predictions (pandas)
96
+
97
+ final_bbox_list = []
98
+ for index, row in bboxes_final.iterrows():
99
+ this_box = BBoxCoordinates(row['ymin'] + bound_final_y, row['ymax'] + bound_final_y,
100
+ row['xmin'] + bound_final_x, row['xmax'] + bound_final_x)
101
+ final_bbox_list.append(this_box)
102
+
103
+ return final_bbox_list
104
+
105
+
106
+ def evaluate_yolo_2(model_loaded, img_path: str, img_size: int, bound_1: Tuple[int, int], bound_2: Tuple[int, int]):
107
+ model = model_loaded
108
+ inpt_img = cv.imread(img_path)
109
+ img_1 = inpt_img[bound_1[0]:bound_1[0] + img_size, bound_1[1]:bound_1[1] + img_size, :]
110
+ img_2 = inpt_img[bound_2[0]:bound_2[0] + img_size, bound_2[1]:bound_2[1] + img_size, :]
111
+ inpt_imgs = [img_1, img_2]
112
+ results = model(inpt_imgs, size=img_size)
113
+
114
+ result_list_images = []
115
+ results_1 = results.pandas().xyxy[0].reset_index() # img1 predictions (pandas)
116
+ results_2 = results.pandas().xyxy[1].reset_index() # img1 predictions (pandas)
117
+
118
+ for index, row in results_1.iterrows():
119
+ result_list_images.append(
120
+ [row['xmin'] + bound_1[1], row['xmax'] + bound_1[1], row['ymin'] + bound_1[0], row['ymax'] + bound_1[0]])
121
+ for index, row in results_2.iterrows():
122
+ result_list_images.append(
123
+ [row['xmin'] + bound_2[1], row['xmax'] + bound_2[1], row['ymin'] + bound_2[0], row['ymax'] + bound_2[0]])
124
+
125
+ result_arr = np.asarray(result_list_images)
126
+ x_min = np.min(result_arr[:, 0])
127
+ x_max = np.max(result_arr[:, 1])
128
+ y_min = np.min(result_arr[:, 2])
129
+ y_max = np.max(result_arr[:, 3])
130
+
131
+ bound_final_x = max(min(int(0.5 * (x_min + x_max - img_size)), 3208 - img_size), 0)
132
+ bound_final_y = max(min(int(0.5 * (y_min + y_max - img_size)), 2200 - img_size), 0)
133
+
134
+ img_final = inpt_img[bound_final_y:bound_final_y + img_size, bound_final_x:bound_final_x + img_size, :]
135
+ imgs_final = [img_final]
136
+ results_final = model(imgs_final, size=img_size)
137
+ bboxes_final = results_final.pandas().xyxy[0].reset_index() # img1 predictions (pandas)
138
+
139
+ final_bbox_list = []
140
+ for index, row in bboxes_final.iterrows():
141
+ this_box = BBoxCoordinates(row['ymin'] + bound_final_y, row['ymax'] + bound_final_y,
142
+ row['xmin'] + bound_final_x, row['xmax'] + bound_final_x)
143
+ final_bbox_list.append(this_box)
144
+
145
+ return final_bbox_list
146
+
147
+
148
+ #
149
+ #
150
+ # img_folder_path = 'C:/Users/hofst/PycharmProjects/ImageAnalysis_SBB/training_data/ext_label_1/images/'
151
+ # img_list = [os.path.normpath(this_path) for this_path in glob(img_folder_path + '*.png')]
152
+ # b_box_list = []
153
+ # model_path_1 = 'C:\\Users\\hofst\\PycharmProjects\\ImageAnalysis_SBB\\yolov5\\data\\richard\\yolo_training_test_1408_size_m\\weights\\best.pt'
154
+ # img_path_new = 'C:\\Users\\hofst\\PycharmProjects\\ImageAnalysis_SBB\\training_data\\test_1.png'
155
+ # bound_1_new = (300, 92)
156
+ # bound_2_new = (650, 1500)
157
+ # img_size_new = 1408
158
+ #
159
+ # this_model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path_1, verbose=False) # local model
160
+ #
161
+ # for img_path_new in img_list:
162
+ # test_result = evaluate_yolo_2(this_model, img_path_new, img_size_new, bound_1_new, bound_2_new)
163
+ # b_box_list.append(test_result)
164
+ # matplotlib.use('TkAgg') # Change backend after loading model
165
+ # fig, ax = plt.subplots()
166
+ # # Display the image
167
+ # ax.imshow(cv.imread(img_path_new), cmap='gray')
168
+ # # Create a Rectangle patch
169
+ # rect = patches.Rectangle((test_result[0].y_min, test_result[0].x_min), test_result[0].y_max - test_result[0].y_min, test_result[0].x_max - test_result[0].x_min, linewidth=1, edgecolor='r', facecolor='none')
170
+ # rect_2 = patches.Rectangle((test_result[1].y_min, test_result[1].x_min), test_result[1].y_max - test_result[1].y_min, test_result[1].x_max - test_result[1].x_min, linewidth=1, edgecolor='b', facecolor='none')
171
+ #
172
+ # # Add the patch to the Axes
173
+ # ax.add_patch(rect)
174
+ # ax.add_patch(rect_2)
175
+ # fig.show()
176
+ #
177
+ # #x_1_list = []
178
+ # #y_1_list = []
179
+ # #x_2_list = []
180
+ # #y_2_list = []
181
+ #
182
+ # #for box_pair in b_box_list:
183
+ # # box_1 = box_pair[0]
184
+ # # box_2 = box_pair[1]
185
+ # # x_1_list.append(box_1.y_min)
186
+ # # y_1_list.append(box_1.x_max)
187
+ # # x_1_list.append(box_2.y_min)
188
+ # # y_1_list.append(box_2.x_max)
189
+ #
190
+ # # x_2_list.append(box_1.y_max)
191
+ # # y_2_list.append(box_1.x_min)
192
+ # # x_2_list.append(box_2.y_max)
193
+ # # y_2_list.append(box_2.x_min)
194
+ #
195
+ # #ax.scatter(x_1_list, y_1_list)
196
+ # #ax.scatter(x_2_list, y_2_list)
197
+ # #plt.imshow(cv.imread(img_path_new), cmap='gray')
198
+ # #plt.grid()
199
+