Advertisement
neronv2

Untitled

Dec 8th, 2022
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.97 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3.  
  4. from utils.detect_peaks import detect_peaks
  5.  
  6.  
  7. def filter_side_sku(
  8.         shelves,
  9.         img_shape,
  10.         empty_space_keys=["emptiness_place"],
  11.         shift_multiplier=0.009,
  12.         max_shelf_size=0.375,
  13.         max_side_cut=8,
  14.         max_total_cut_multiplier=6,
  15.         is_stop_if_tag=False,
  16.         mpd_var=0.04,
  17.         min_sku_num=20,
  18.         debug=False,
  19. ):
  20.     new_img = np.zeros(img_shape[:-1])
  21.  
  22.     left_side = int(max_shelf_size * img_shape[1])
  23.     right_side = img_shape[1] - left_side
  24.  
  25.     num_sku = 0
  26.     num_empty_space = 0
  27.     for shelf in shelves:
  28.         for place in shelf["places"]:
  29.             for sku in place["skus"]:
  30.                 num_sku += 1
  31.                 if sku["id_dscr"] in empty_space_keys or "empt" in sku["id_dscr"]:
  32.                     num_empty_space += 1
  33.                     continue
  34.                 if left_side > int(sku["bbox"][0]) or right_side < int(sku["bbox"][0]):
  35.                     new_img[
  36.                     int(sku["bbox"][1]): int(sku["bbox"][3]),
  37.                     int(sku["bbox"][0]): int(sku["bbox"][2]),
  38.                     ] = 1
  39.  
  40.     if num_sku < min_sku_num:
  41.         return shelves, 0, img_shape[1]
  42.  
  43.     if num_sku - num_empty_space < 10:
  44.         if debug:
  45.             print()
  46.         return shelves, 0, img_shape[1]
  47.  
  48.     x_sum = new_img.sum(axis=0)
  49.  
  50.     x = -x_sum + new_img.shape[1]
  51.  
  52.     height = int(x_sum.shape[0] * 0.98)
  53.     # peaks, _ = ss.find_peaks(x, height=height, prominence=1, width=20)
  54.     peaks = detect_peaks(
  55.         x, mph=height, mpd=new_img.shape[1] * mpd_var, edge="both", show=debug
  56.     )
  57.  
  58.  
  59.     peaks = peaks[
  60.         np.where(((left_side > peaks) | (right_side + (right_side * 0.1) < peaks)))[0]
  61.     ]
  62.  
  63.     for left_x, value in enumerate(x_sum):
  64.         if value != 0:
  65.             break
  66.  
  67.     left_slice_peaks = peaks[peaks < left_side]
  68.     if len(left_slice_peaks) > 0:
  69.         left_x = left_slice_peaks[-1]
  70.  
  71.     right_x = img_shape[1]
  72.     for right_x, value in enumerate(reversed(x_sum)):
  73.         if value != 0:
  74.             break
  75.  
  76.     right_x = x_sum.shape[0] - right_x
  77.     right_slice_peaks = peaks[peaks > right_side]
  78.     if len(right_slice_peaks) > 0:
  79.         right_x = right_slice_peaks[0]
  80.  
  81.     magic_shift_c = int(img_shape[1] * shift_multiplier)
  82.  
  83.     right_x += magic_shift_c if right_x != img_shape[1] else img_shape[1]
  84.     left_x -= magic_shift_c if left_x != 0 and left_x - magic_shift_c > 0 else 0
  85.  
  86.     left_x = 0 if left_x > left_side else left_x
  87.     right_x = x_sum.shape[0] if right_x < right_side else right_x
  88.  
  89.     new_shelves, status = create_new_shelf(
  90.         shelves, left_x, right_x,
  91.         empty_space_keys,
  92.         max_side_cut=max_side_cut,
  93.         max_total_cut_multiplier=max_total_cut_multiplier,
  94.         is_stop_if_tag=is_stop_if_tag, debug=debug
  95.     )
  96.     if not status:
  97.         right_x = img_shape[1]
  98.         left_x = 0
  99.  
  100.     if debug:
  101.         print ("Peaks: {0} , left_x: {1}, right_x: {2}".format(peaks, left_x, right_x))
  102.         print ("Left side: {0}, right: {1}".format(left_side, right_side))
  103.         plt.figure(figsize=(10, 10))
  104.         plt.imshow(new_img)
  105.         plt.show()
  106.     return new_shelves, left_x, right_x
  107.  
  108.  
  109. def create_new_shelf(
  110.         shelves,
  111.         left_x,
  112.         right_x,
  113.         empty_space_keys,
  114.         max_side_cut=8,
  115.         max_total_cut_multiplier=6,
  116.         is_stop_if_tag=False,
  117.         debug=False,
  118. ):
  119.     new_shelves = []
  120.     k = 0
  121.     sku_total = 0
  122.     sku_total_replaced_num = 0
  123.     for shelf_id, shelf in enumerate(shelves):
  124.         new_shelves.append({key: shelf[key] for key in shelf.keys() if key != "places"})
  125.         new_shelves[shelf_id]["places"] = []
  126.         sku_replaced_num_left = 0
  127.         sku_replaced_num_right = 0
  128.         place_deleted = 0
  129.         for place_id_, place in enumerate(shelf["places"]):
  130.             place_id = place_id_ - place_deleted
  131.             new_shelves[shelf_id]["places"].append(
  132.                 {key: place[key] for key in place.keys() if key != "skus"}
  133.             )
  134.             new_shelves[shelf_id]["places"][place_id]["id"] = place_id
  135.             new_shelves[shelf_id]["places"][place_id]["skus"] = []
  136.  
  137.             for sku in place["skus"]:
  138.  
  139.                 if sku["id_dscr"] in empty_space_keys or "empt" in sku["id_dscr"]:
  140.                     is_sku_empty_space = True
  141.                 else:
  142.                     is_sku_empty_space = False
  143.  
  144.                 if not is_sku_empty_space:
  145.                     sku_total += 1
  146.  
  147.                 if (left_x < int(sku["bbox"][0]) < right_x) and (
  148.                         left_x < int(sku["bbox"][2]) < right_x
  149.                 ):
  150.                     new_sku = sku.copy()
  151.                     new_sku["id_number"] = k
  152.                     k += 1
  153.  
  154.                     if "skus" not in new_shelves[shelf_id]["places"][place_id].keys():
  155.                         new_shelves[shelf_id]["places"][place_id]["skus"] = [new_sku]
  156.                     else:
  157.                         new_shelves[shelf_id]["places"][place_id]["skus"].append(new_sku)
  158.  
  159.                 elif not is_sku_empty_space:
  160.                     # counting left and right deleted skus
  161.                     if left_x > int(sku["bbox"][0]):
  162.                         sku_replaced_num_left += 1
  163.                     else:
  164.                         sku_replaced_num_right += 1
  165.  
  166.                     sku_total_replaced_num += 1
  167.  
  168.                 if (
  169.                         sku_replaced_num_left > max_side_cut
  170.                         or sku_replaced_num_right > max_side_cut
  171.                 ):
  172.                     if debug:
  173.                         print(
  174.                             "Early stopping",
  175.                             "left",
  176.                             sku_replaced_num_left,
  177.                             "right",
  178.                             sku_replaced_num_right,
  179.                         )
  180.                     return shelves, False
  181.  
  182.                 if is_stop_if_tag:
  183.                     for tag in sku["tag"]:
  184.                         if left_x > int(tag["bbox"][0]) or right_x < int(
  185.                                 tag["bbox"][0]
  186.                         ):
  187.                             if debug:
  188.                                 print("early stopping")
  189.                             return shelves, False
  190.             # delete places if out of bound
  191.             if not (
  192.                     left_x < int(place["bbox"][0]) < right_x
  193.                     and left_x < int(place["bbox"][2]) < right_x
  194.             ):
  195.                 del new_shelves[shelf_id]["places"][place_id]
  196.                 place_deleted += 1
  197.  
  198.         if sku_total_replaced_num > max_total_cut_multiplier * len(shelves):
  199.             if debug:
  200.                 print(
  201.                     "Early stopping",
  202.                     "total",
  203.                     sku_total_replaced_num,
  204.                     max_total_cut_multiplier * len(shelves),
  205.                 )
  206.  
  207.             return shelves, False
  208.  
  209.     return new_shelves, True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement