Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # returns sliced subframes
- def slice_frame_with_window(frame, output_shape = None, objects = [], obj_min_area = 0.001,
- step = 0.15, win_size = 0.3, init_position = [0,0], classes = [1, 2],
- aspect_ratio = [0.4, 2.0], min_size = [100, 100],
- min_object_size_rate = 0.90, min_object_size_pixels = 6):
- h, w, _ = frame.shape
- step_x = round(step * w)
- step_y = round(step * h)
- win_size_x = round(win_size * w)
- win_size_y = round(win_size * h)
- x0, y0 = init_position
- coords = []
- subframes = []
- new_objects = []
- while y0 < h:
- y1 = y0 + win_size_y
- x0 = init_position[0]
- while x0 < w:
- x1 = x0 + win_size_x
- if x0 < 0:
- x0 = 0
- if x1 > w:
- x1 = w
- if y0 < 0:
- y0 = 0
- if y1 > h:
- y1 = h
- subframe = frame[y0 : y1, x0 : x1]
- sub_h, sub_w, _ = subframe.shape
- if (sub_h / sub_w) >= aspect_ratio[0] and (sub_h / sub_w) <= aspect_ratio[1] \
- and sub_w > min_size[0] and sub_h > min_size[1]:
- if output_shape is not None:
- subframe = cv2.resize(subframe, (output_shape[1], output_shape[0]))
- subframe = cv2.cvtColor(subframe, cv2.COLOR_BGR2RGB)
- subframes.append(subframe)
- coords.append([y0 / h, x0 / w, y1 / h, x1 / w])
- #####
- if objects != []:
- new_objects.append([])
- for bb in objects:
- cl = bb[4]
- if cl not in classes:
- continue
- bb_x0 = int(bb[1] * w)
- bb_y0 = int(bb[0] * h)
- bb_x1 = int(bb[3] * w)
- bb_y1 = int(bb[2] * h)
- iou = tracker.IOU([y0, x0, y1, x1],
- [bb_y0, bb_x0, bb_y1, bb_x1])
- #box_area = (bb_x1 - bb_x0) * (bb_y1 - bb_y0)
- #subframe_area = sub_h * sub_w
- #iou = iou * (subframe_area / box_area)
- if iou >= obj_min_area:
- if ((bb_y1 - y0) - (bb_y0 - y0)) / (bb_y1 - bb_y0) < min_object_size_rate \
- or (bb_y1 - y0) - (bb_y0 - y0) < min_object_size_pixels:
- continue
- if ((bb_x1 - x0) - (bb_x0 - x0)) / (bb_x1 - bb_x0) < min_object_size_rate\
- or (bb_x1 - x0) - (bb_x0 - x0) < min_object_size_pixels:
- continue
- new_box = [bb_y0 - y0, bb_x0 - x0, bb_y1 - y0, bb_x1 - x0]
- new_box = [new_box[0] / sub_h,
- new_box[1] / sub_w,
- new_box[2] / sub_h,
- new_box[3] / sub_w, bb[4]]
- new_objects[-1].append(new_box)
- #####
- x0 = x0 + step_x
- y0 = y0 + step_y
- return subframes, coords, new_objects
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement