Abhilashvj/planogram-compliance
6
1import glob2import json3import os4import xml.etree.ElementTree as ET5 6import cv27 8# from sklearn.externals import joblib9import joblib10import numpy as np11import pandas as pd12 13# from .variables import old_ocr_req_cols14# from .skew_correction import PageSkewWraper15 16const_HW = 1.29411764717const_W = 60018# https://www.forbes.com/sites/forbestechcouncil/2020/06/02/leveraging-technologies-to-align-realograms-and-planograms-for-grocery/?sh=506b8b78e86c19 20 21# https://stackoverflow.com/questions/39403183/python-opencv-sorting-contours22# http://devdoc.net/linux/OpenCV-3.2.0/da/d0c/tutorial_bounding_rects_circles.html23# https://stackoverflow.com/questions/10297713/find-contour-of-the-set-of-points-in-opencv24# https://stackoverflow.com/questions/16538774/dealing-with-contours-and-bounding-rectangle-in-opencv-2-4-python-2-725# https://stackoverflow.com/questions/50308055/creating-bounding-boxes-for-contours26# https://stackoverflow.com/questions/57296398/how-can-i-get-better-results-of-bounding-box-using-find-contours-of-opencv27# http://amroamroamro.github.io/mexopencv/opencv/generalContours_demo1.html28# https://gist.github.com/bigsnarfdude/d811e31ee17495f82f10db12651ae82d29# http://man.hubwiz.com/docset/OpenCV.docset/Contents/Resources/Documents/da/d0c/tutorial_bounding_rects_circles.html30# https://www.analyticsvidhya.com/blog/2021/05/document-layout-detection-and-ocr-with-detectron2/31# https://colab.research.google.com/drive/1m6gaQF6Q4M0IaSjoo_4jWllKJjK-i6fw?usp=sharing#scrollTo=lEyl3wYKHAe132# https://stackoverflow.com/questions/39403183/python-opencv-sorting-contours33# https://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/bounding_rects_circles/bounding_rects_circles.html34# https://www.pyimagesearch.com/2016/03/21/ordering-coordinates-clockwise-with-python-and-opencv/35 36 37def bucket_sort(df, colmn, ymax_col="ymax", ymin_col="ymin"):38 df["line_number"] = 039 colmn.append("line_number")40 array_value = df[colmn].values41 start_index = Line_counter = counter = 042 ymax, ymin, line_no = (43 colmn.index(ymax_col),44 colmn.index(ymin_col),45 colmn.index("line_number"),46 )47 while counter < len(array_value):48 current_ymax = array_value[start_index][ymax]49 for next_index in range(start_index, len(array_value)):50 counter += 151 52 next_ymin = array_value[next_index][ymin]53 next_ymax = array_value[next_index][ymax]54 if current_ymax > next_ymin:55 56 array_value[next_index][line_no] = Line_counter + 157 # if current_ymax < next_ymax:58 59 # current_ymax = next_ymax60 else:61 counter -= 162 break63 # print(counter, len(array_value), start_index)64 start_index = counter65 Line_counter += 166 return pd.DataFrame(array_value, columns=colmn)67 68 69def do_sorting(df):70 df.sort_values(["ymin", "xmin"], ascending=True, inplace=True)71 df["idx"] = df.index72 if "line_number" in df.columns:73 print("line number removed")74 df.drop("line_number", axis=1, inplace=True)75 req_colns = ["xmin", "ymin", "xmax", "ymax", "idx"]76 temp_df = df.copy()77 temp = bucket_sort(temp_df.copy(), req_colns)78 df = df.merge(temp[["idx", "line_number"]], on="idx")79 df.sort_values(["line_number", "xmin"], ascending=True, inplace=True)80 df = df.reset_index(drop=True)81 df = df.reset_index(drop=True)82 return df83 84 85def xml_to_csv(xml_file):86 # https://gist.github.com/rotemtam/88d9a4efae243fc77ed4a0f9917c8f6c87 xml_list = []88 # for xml_file in glob.glob(path + '/*.xml'):89 # https://discuss.streamlit.io/t/unable-to-read-files-using-standard-file-uploader/2258/290 tree = ET.parse(xml_file)91 root = tree.getroot()92 for member in root.findall("object"):93 bbx = member.find("bndbox")94 xmin = int(bbx.find("xmin").text)95 ymin = int(bbx.find("ymin").text)96 xmax = int(bbx.find("xmax").text)97 ymax = int(bbx.find("ymax").text)98 label = member.find("name").text99 100 value = (101 root.find("filename").text,102 int(root.find("size")[0].text),103 int(root.find("size")[1].text),104 label,105 xmin,106 ymin,107 xmax,108 ymax,109 )110 xml_list.append(value)111 column_name = [112 "filename",113 "width",114 "height",115 "cls",116 "xmin",117 "ymin",118 "xmax",119 "ymax",120 ]121 xml_df = pd.DataFrame(xml_list, columns=column_name)122 return xml_df123 124 125# def annotate_planogram_compliance(img0, sorted_xml_df, wrong_indexes, target_names):126# # annotator = Annotator(img0, line_width=3, pil=True)127# det = sorted_xml_df[['xmin', 'ymin', 'xmax', 'ymax','cls']].values128# # det[:, :4] = scale_coords((640, 640), det[:, :4], img0.shape).round()129# for i, (*xyxy, cls) in enumerate(det):130 131# c = int(cls) # integer class132 133# if i in wrong_indexes:134# # print(xyxy, "Wrong detection", (255, 0, 0))135# label = "Wrong detection"136# color = (0,0,255)137# else:138# # print(xyxy, label, (0, 255, 0))139# label = f'{target_names[c]}'140# color = (0,255, 0)141# org = (int(xyxy[0]), int(xyxy[1]) )142# top_left = org143# bottom_right = (int(xyxy[2]), int(xyxy[3]))144# # print("#"*50)145# # print(f"Anooatting cv2 rectangle with shape: { img0.shape}, top left: { top_left}, bottom right: { bottom_right} , color : { color }, thickness: {3}, cv2.LINE_8")146# # print("#"*50)147# cv2.rectangle(img0, top_left, bottom_right , color, 3, cv2.LINE_8)148 149# cv2.putText(img0, label, tuple(org), cv2. FONT_HERSHEY_SIMPLEX , 0.5, color)150 151# return img0152 153 154def annotate_planogram_compliance(155 img0, sorted_df, correct_indexes, wrong_indexes, target_names156):157 # annotator = Annotator(img0, line_width=3, pil=True)158 det = sorted_df[["xmin", "ymin", "xmax", "ymax", "cls"]].values159 # 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"]].values164 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, color173 )174 except Exception as e:175 print("Error: " + str(e))176 continue177 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"]].values182 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, color191 )192 except Exception as e:193 print("Error: " + str(e))194 continue195 196 return img0197 