Spaces:
Runtime error
Runtime error
Update app_utils.py
Browse files32 unmodified lines333435363738112 unmodified lines15115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519632 unmodified lines# https://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/bounding_rects_circles/bounding_rects_circles.html# https://www.pyimagesearch.com/2016/03/21/ordering-coordinates-clockwise-with-python-and-opencv/def bucket_sort(df, colmn, ymax_col="ymax", ymin_col="ymin"):df["line_number"] = 0112 unmodified lines# return img0def annotate_planogram_compliance(img0, sorted_df, correct_indexes, wrong_indexes, target_names):# annotator = Annotator(img0, line_width=3, pil=True)det = sorted_df[["xmin", "ymin", "xmax", "ymax", "cls"]].values# det[:, :4] = scale_coords((640, 640), det[:, :4], img0.shape).round()for x, y in zip(*correct_indexes):try:row = sorted_df[sorted_df["line_number"] == x + 1].iloc[y]xyxy = row[["xmin", "ymin", "xmax", "ymax"]].valueslabel = f'{target_names[row["cls"]]}'color = (0, 255, 0)# org = (int(xyxy[0]), int(xyxy[1]) )top_left = (int(row["xmin"]), int(row["ymin"]))bottom_right = (int(row["xmax"]), int(row["ymax"]))cv2.rectangle(img0, top_left, bottom_right, color, 3, cv2.LINE_8)cv2.putText(img0, label, top_left, cv2.FONT_HERSHEY_SIMPLEX, 0.5, color)except Exception as e:print("Error: " + str(e))continuefor x, y in zip(*wrong_indexes):try:row = sorted_df[sorted_df["line_number"] == x + 1].iloc[y]xyxy = row[["xmin", "ymin", "xmax", "ymax"]].valueslabel = f'{target_names[row["cls"]]}'color = (0, 0, 255)# org = (int(xyxy[0]), int(xyxy[1]) )top_left = (row["xmin"], row["ymin"])bottom_right = (row["xmax"], row["ymax"])cv2.rectangle(img0, top_left, bottom_right, color, 3, cv2.LINE_8)cv2.putText(img0, label, top_left, cv2.FONT_HERSHEY_SIMPLEX, 0.5, color)except Exception as e:print("Error: " + str(e))continuereturn img032 unmodified lines333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495112 unmodified lines20820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225332 unmodified lines# https://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/bounding_rects_circles/bounding_rects_circles.html# https://www.pyimagesearch.com/2016/03/21/ordering-coordinates-clockwise-with-python-and-opencv/from PIL import Image, ImageDraw, ImageFontimport numpy as npdef annotate_planogram_compliance(img0, sorted_df, correct_indexes, wrong_indexes, target_names):# Convert numpy array to PIL imageimg_pil = Image.fromarray(img0)# Create ImageDraw objectdraw = ImageDraw.Draw(img_pil)# Load a fontfont = ImageFont.truetype("arial.ttf", 16) # You may need to adjust the font pathfor x, y in zip(*correct_indexes):try:row = sorted_df[sorted_df["line_number"] == x + 1].iloc[y]xyxy = row[["xmin", "ymin", "xmax", "ymax"]].valueslabel = f'{target_names[row["cls"]]}'color = (0, 255, 0)top_left = (row["xmin"], row["ymin"])bottom_right = (row["xmax"], row["ymax"])# Draw bounding boxdraw.rectangle([tuple(top_left), tuple(bottom_right)], outline=color)# Draw labeldraw.text(top_left, label, fill=color, font=font)except Exception as e:print("Error: " + str(e))continuefor x, y in zip(*wrong_indexes):try:row = sorted_df[sorted_df["line_number"] == x + 1].iloc[y]xyxy = row[["xmin", "ymin", "xmax", "ymax"]].valueslabel = f'{target_names[row["cls"]]}'color = (0, 0, 255)top_left = (row["xmin"], row["ymin"])bottom_right = (row["xmax"], row["ymax"])# Draw bounding boxdraw.rectangle([tuple(top_left), tuple(bottom_right)], outline=color)# Draw labeldraw.text(top_left, label, fill=color, font=font)except Exception as e:print("Error: " + str(e))continue# Convert PIL image back to numpy arrayannotated_img_np = np.array(img_pil)return annotated_img_npdef bucket_sort(df, colmn, ymax_col="ymax", ymin_col="ymin"):df["line_number"] = 0112 unmodified lines# return img0# def annotate_planogram_compliance(# img0, sorted_df, correct_indexes, wrong_indexes, target_names# ):# # annotator = Annotator(img0, line_width=3, pil=True)# det = sorted_df[["xmin", "ymin", "xmax", "ymax", "cls"]].values# # det[:, :4] = scale_coords((640, 640), det[:, :4], img0.shape).round()# for x, y in zip(*correct_indexes):# try:# row = sorted_df[sorted_df["line_number"] == x + 1].iloc[y]# xyxy = row[["xmin", "ymin", "xmax", "ymax"]].values# label = f'{target_names[row["cls"]]}'# color = (0, 255, 0)# # org = (int(xyxy[0]), int(xyxy[1]) )# top_left = (int(row["xmin"]), int(row["ymin"]))# bottom_right = (int(row["xmax"]), int(row["ymax"]))# cv2.rectangle(img0, top_left, bottom_right, color, 3, cv2.LINE_8)# cv2.putText(# img0, label, top_left, cv2.FONT_HERSHEY_SIMPLEX, 0.5, color# )# except Exception as e:# print("Error: " + str(e))# continue# for x, y in zip(*wrong_indexes):# try:# row = sorted_df[sorted_df["line_number"] == x + 1].iloc[y]# xyxy = row[["xmin", "ymin", "xmax", "ymax"]].values# label = f'{target_names[row["cls"]]}'# color = (0, 0, 255)# # org = (int(xyxy[0]), int(xyxy[1]) )# top_left = (row["xmin"], row["ymin"])# bottom_right = (row["xmax"], row["ymax"])# cv2.rectangle(img0, top_left, bottom_right, color, 3, cv2.LINE_8)# cv2.putText(# img0, label, top_left, cv2.FONT_HERSHEY_SIMPLEX, 0.5, color# )# except Exception as e:# print("Error: " + str(e))# continue# return img0
app_utils.py+99-42