Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. # returns sliced subframes
  2. def slice_frame_with_window(frame, output_shape = None, objects = [], obj_min_area = 0.001,
  3.                             step = 0.15, win_size = 0.3, init_position = [0,0], classes = [1, 2],
  4.                             aspect_ratio = [0.4, 2.0], min_size = [100, 100],
  5.                             min_object_size_rate = 0.90, min_object_size_pixels = 6):
  6.  
  7.     h, w, _ = frame.shape
  8.     step_x = round(step * w)
  9.     step_y = round(step * h)
  10.     win_size_x = round(win_size * w)
  11.     win_size_y = round(win_size * h)
  12.     x0, y0 = init_position
  13.     coords = []
  14.  
  15.     subframes = []
  16.     new_objects = []
  17.     while y0 < h:
  18.         y1 = y0 + win_size_y
  19.         x0 = init_position[0]
  20.  
  21.         while x0 < w:
  22.  
  23.             x1 = x0 + win_size_x
  24.             if x0 < 0:
  25.                 x0 = 0
  26.             if x1 > w:
  27.                 x1 = w
  28.             if y0 < 0:
  29.                 y0 = 0
  30.             if y1 > h:
  31.                 y1 = h
  32.  
  33.             subframe = frame[y0 : y1, x0 : x1]
  34.             sub_h, sub_w, _ = subframe.shape
  35.  
  36.             if (sub_h / sub_w) >= aspect_ratio[0] and (sub_h / sub_w) <= aspect_ratio[1] \
  37.                     and sub_w > min_size[0] and sub_h > min_size[1]:
  38.  
  39.                 if output_shape is not None:
  40.                     subframe = cv2.resize(subframe, (output_shape[1], output_shape[0]))
  41.                 subframe = cv2.cvtColor(subframe, cv2.COLOR_BGR2RGB)
  42.  
  43.                 subframes.append(subframe)
  44.  
  45.                 coords.append([y0 / h, x0 / w, y1 / h, x1 / w])
  46.  
  47.                 #####
  48.                 if objects != []:
  49.                     new_objects.append([])
  50.                     for bb in objects:
  51.                         cl = bb[4]
  52.  
  53.                         if cl not in classes:
  54.                             continue
  55.                         bb_x0 = int(bb[1] * w)
  56.                         bb_y0 = int(bb[0] * h)
  57.                         bb_x1 = int(bb[3] * w)
  58.                         bb_y1 = int(bb[2] * h)
  59.  
  60.                         iou = tracker.IOU([y0, x0, y1, x1],
  61.                                           [bb_y0, bb_x0, bb_y1, bb_x1])
  62.  
  63.                         #box_area = (bb_x1 - bb_x0) * (bb_y1 - bb_y0)
  64.                         #subframe_area = sub_h * sub_w
  65.                         #iou = iou * (subframe_area / box_area)
  66.  
  67.                         if iou >= obj_min_area:
  68.                             if ((bb_y1 - y0) - (bb_y0 - y0)) / (bb_y1 - bb_y0) < min_object_size_rate \
  69.                                     or (bb_y1 - y0) - (bb_y0 - y0) < min_object_size_pixels:
  70.                                 continue
  71.  
  72.                             if ((bb_x1 - x0) - (bb_x0 - x0)) / (bb_x1 - bb_x0) < min_object_size_rate\
  73.                                     or (bb_x1 - x0) - (bb_x0 - x0) < min_object_size_pixels:
  74.                                 continue
  75.                             new_box = [bb_y0 - y0, bb_x0 - x0, bb_y1 - y0, bb_x1 - x0]
  76.                             new_box = [new_box[0] / sub_h,
  77.                                        new_box[1] / sub_w,
  78.                                        new_box[2] / sub_h,
  79.                                        new_box[3] / sub_w, bb[4]]
  80.                             new_objects[-1].append(new_box)
  81.                 #####
  82.  
  83.             x0 = x0 + step_x
  84.  
  85.  
  86.         y0 = y0 + step_y
  87.  
  88.  
  89.     return subframes, coords, new_objects
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement