SakshiRathi77 commited on
Commit
f0df289
1 Parent(s): 9d67f5e

Update app_utils.py

Browse files
Files changed (1) hide show
  1. app_utils.py +99 -42
app_utils.py CHANGED
@@ -33,6 +33,63 @@ const_W = 600
33
  # https://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/bounding_rects_circles/bounding_rects_circles.html
34
  # https://www.pyimagesearch.com/2016/03/21/ordering-coordinates-clockwise-with-python-and-opencv/
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  def bucket_sort(df, colmn, ymax_col="ymax", ymin_col="ymin"):
38
  df["line_number"] = 0
@@ -151,46 +208,46 @@ def xml_to_csv(xml_file):
151
  # return img0
152
 
153
 
154
- def annotate_planogram_compliance(
155
- img0, sorted_df, correct_indexes, wrong_indexes, target_names
156
- ):
157
- # annotator = Annotator(img0, line_width=3, pil=True)
158
- det = sorted_df[["xmin", "ymin", "xmax", "ymax", "cls"]].values
159
- # det[:, :4] = scale_coords((640, 640), det[:, :4], img0.shape).round()
160
- for x, y in zip(*correct_indexes):
161
- try:
162
- row = sorted_df[sorted_df["line_number"] == x + 1].iloc[y]
163
- xyxy = row[["xmin", "ymin", "xmax", "ymax"]].values
164
- label = f'{target_names[row["cls"]]}'
165
- color = (0, 255, 0)
166
- # org = (int(xyxy[0]), int(xyxy[1]) )
167
- top_left = (int(row["xmin"]), int(row["ymin"]))
168
- bottom_right = (int(row["xmax"]), int(row["ymax"]))
169
- cv2.rectangle(img0, top_left, bottom_right, color, 3, cv2.LINE_8)
170
-
171
- cv2.putText(
172
- img0, label, top_left, cv2.FONT_HERSHEY_SIMPLEX, 0.5, color
173
- )
174
- except Exception as e:
175
- print("Error: " + str(e))
176
- continue
177
-
178
- for x, y in zip(*wrong_indexes):
179
- try:
180
- row = sorted_df[sorted_df["line_number"] == x + 1].iloc[y]
181
- xyxy = row[["xmin", "ymin", "xmax", "ymax"]].values
182
- label = f'{target_names[row["cls"]]}'
183
- color = (0, 0, 255)
184
- # org = (int(xyxy[0]), int(xyxy[1]) )
185
- top_left = (row["xmin"], row["ymin"])
186
- bottom_right = (row["xmax"], row["ymax"])
187
- cv2.rectangle(img0, top_left, bottom_right, color, 3, cv2.LINE_8)
188
-
189
- cv2.putText(
190
- img0, label, top_left, cv2.FONT_HERSHEY_SIMPLEX, 0.5, color
191
- )
192
- except Exception as e:
193
- print("Error: " + str(e))
194
- continue
195
 
196
- return img0
 
33
  # https://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/bounding_rects_circles/bounding_rects_circles.html
34
  # https://www.pyimagesearch.com/2016/03/21/ordering-coordinates-clockwise-with-python-and-opencv/
35
 
36
+ from PIL import Image, ImageDraw, ImageFont
37
+ import numpy as np
38
+
39
+ def annotate_planogram_compliance(
40
+ img0, sorted_df, correct_indexes, wrong_indexes, target_names
41
+ ):
42
+ # Convert numpy array to PIL image
43
+ img_pil = Image.fromarray(img0)
44
+
45
+ # Create ImageDraw object
46
+ draw = ImageDraw.Draw(img_pil)
47
+
48
+ # Load a font
49
+ font = ImageFont.truetype("arial.ttf", 16) # You may need to adjust the font path
50
+
51
+ for x, y in zip(*correct_indexes):
52
+ try:
53
+ row = sorted_df[sorted_df["line_number"] == x + 1].iloc[y]
54
+ xyxy = row[["xmin", "ymin", "xmax", "ymax"]].values
55
+ label = f'{target_names[row["cls"]]}'
56
+ color = (0, 255, 0)
57
+ top_left = (row["xmin"], row["ymin"])
58
+ bottom_right = (row["xmax"], row["ymax"])
59
+
60
+ # Draw bounding box
61
+ draw.rectangle([tuple(top_left), tuple(bottom_right)], outline=color)
62
+
63
+ # Draw label
64
+ draw.text(top_left, label, fill=color, font=font)
65
+ except Exception as e:
66
+ print("Error: " + str(e))
67
+ continue
68
+
69
+ for x, y in zip(*wrong_indexes):
70
+ try:
71
+ row = sorted_df[sorted_df["line_number"] == x + 1].iloc[y]
72
+ xyxy = row[["xmin", "ymin", "xmax", "ymax"]].values
73
+ label = f'{target_names[row["cls"]]}'
74
+ color = (0, 0, 255)
75
+ top_left = (row["xmin"], row["ymin"])
76
+ bottom_right = (row["xmax"], row["ymax"])
77
+
78
+ # Draw bounding box
79
+ draw.rectangle([tuple(top_left), tuple(bottom_right)], outline=color)
80
+
81
+ # Draw label
82
+ draw.text(top_left, label, fill=color, font=font)
83
+ except Exception as e:
84
+ print("Error: " + str(e))
85
+ continue
86
+
87
+ # Convert PIL image back to numpy array
88
+ annotated_img_np = np.array(img_pil)
89
+
90
+ return annotated_img_np
91
+
92
+
93
 
94
  def bucket_sort(df, colmn, ymax_col="ymax", ymin_col="ymin"):
95
  df["line_number"] = 0
 
208
  # return img0
209
 
210
 
211
+ # def annotate_planogram_compliance(
212
+ # img0, sorted_df, correct_indexes, wrong_indexes, target_names
213
+ # ):
214
+ # # annotator = Annotator(img0, line_width=3, pil=True)
215
+ # det = sorted_df[["xmin", "ymin", "xmax", "ymax", "cls"]].values
216
+ # # det[:, :4] = scale_coords((640, 640), det[:, :4], img0.shape).round()
217
+ # for x, y in zip(*correct_indexes):
218
+ # try:
219
+ # row = sorted_df[sorted_df["line_number"] == x + 1].iloc[y]
220
+ # xyxy = row[["xmin", "ymin", "xmax", "ymax"]].values
221
+ # label = f'{target_names[row["cls"]]}'
222
+ # color = (0, 255, 0)
223
+ # # org = (int(xyxy[0]), int(xyxy[1]) )
224
+ # top_left = (int(row["xmin"]), int(row["ymin"]))
225
+ # bottom_right = (int(row["xmax"]), int(row["ymax"]))
226
+ # cv2.rectangle(img0, top_left, bottom_right, color, 3, cv2.LINE_8)
227
+
228
+ # cv2.putText(
229
+ # img0, label, top_left, cv2.FONT_HERSHEY_SIMPLEX, 0.5, color
230
+ # )
231
+ # except Exception as e:
232
+ # print("Error: " + str(e))
233
+ # continue
234
+
235
+ # for x, y in zip(*wrong_indexes):
236
+ # try:
237
+ # row = sorted_df[sorted_df["line_number"] == x + 1].iloc[y]
238
+ # xyxy = row[["xmin", "ymin", "xmax", "ymax"]].values
239
+ # label = f'{target_names[row["cls"]]}'
240
+ # color = (0, 0, 255)
241
+ # # org = (int(xyxy[0]), int(xyxy[1]) )
242
+ # top_left = (row["xmin"], row["ymin"])
243
+ # bottom_right = (row["xmax"], row["ymax"])
244
+ # cv2.rectangle(img0, top_left, bottom_right, color, 3, cv2.LINE_8)
245
+
246
+ # cv2.putText(
247
+ # img0, label, top_left, cv2.FONT_HERSHEY_SIMPLEX, 0.5, color
248
+ # )
249
+ # except Exception as e:
250
+ # print("Error: " + str(e))
251
+ # continue
252
 
253
+ # return img0