同步操作将从 MegEngine/MegFlow 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# MegFlow is Licensed under the Apache License, Version 2.0 (the "License")## Copyright (c) 2019-2021 Megvii Inc. All rights reserved.## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.#!/usr/bin/env python# coding=utf-8import numpy as npdef sigmoid(x: float) -> float:return 1.0 / 1.0 + np.exp(-x)def iou(r0: np.array, r1: np.array) -> float:xx1 = max(r0[1], r1[1])yy1 = max(r0[2], r1[2])xx2 = min(r0[3], r1[3])yy2 = min(r0[4], r1[4])dx = max(0., xx2 - xx1)dy = max(0, yy2 - yy1)i = dx * dyu = (r0[3] - r0[1]) * (r0[4] - r0[2]) + (r1[3] - r1[1]) * (r1[4] -r1[2]) - iiou = i / ureturn ioudef is_overlap(rect1: np.array, rect2: np.array, iou_thr: float) -> bool:ov = iou(rect1, rect2)return ov >= iou_thrdef shrink_rect(rect):x1, y1, x2, y2 = rect# shrink person to center'''x1_new = (x2 - x1) / 4 + x1x2_new = x2 - (x2 - x1) / 4y1_new = (y2 - y1) / 4 + y1y2_new = y2 - (y2 - y1) / 4'''# shrink person to head by statw = x2 - x1 + 1h = y2 - y1 + 1head_ratio_h2w = 1.3min_head_width = 40 # set min_head if person is too smallmin_head_height = min_head_width * head_ratio_h2wratio_w = 0.3478 # 0.3478 is the width of head relative to personratio_h = 0.1709 # 0.1709 is the height of head relative to personw_head = max(w * ratio_w, min_head_width)h_head = max(h * ratio_h, min_head_height)x1_new = x1 + (w -w_head) * 0.5 # put the center of head on x at the center of persony1_new = y1 + h * 0.0931 - h_head * 0.5 # 0.0931 if the y_center of head relative to personx2_new = x1_new + w_head - 1y2_new = y1_new + h_head - 1return x1_new, y1_new, x2_new, y2_newdef filter_rect_by_score_and_size(scores, rects, idx, size):if scores[idx] == -1:return np.array([])elif rects[idx][2] - rects[idx][0] + 1 <= size or rects[idx][3] - rects[idx][1] + 1 <= size:return np.array([])else:return np.concatenate((rects[idx], scores[idx:idx + 1]))def is_overlap_v2(rect1, rect2, iou_threshold):xx1 = max(rect1[0], rect2[0])yy1 = max(rect1[1], rect2[1])xx2 = min(rect1[2], rect2[2])yy2 = min(rect1[3], rect2[3])dx = max(0, xx2 - xx1 + 1)dy = max(0, yy2 - yy1 + 1)i = dx * dyu = (rect1[2] - rect1[0] + 1) * (rect1[3] - rect1[1] + 1) + (rect2[2] - rect2[0] + 1) * (rect2[3] - rect2[1] + 1) - iov = i / ureturn rect1[4] * rect2[4] < ov or ov >= iou_thresholddef is_overlap_v1(rect1, rect2, iou_threshold):xx1 = max(rect1[0], rect2[0])yy1 = max(rect1[1], rect2[1])xx2 = min(rect1[2], rect2[2])yy2 = min(rect1[3], rect2[3])dx = max(0, xx2 - xx1 + 1)dy = max(0, yy2 - yy1 + 1)i = dx * dyu = (rect1[2] - rect1[0] + 1) * (rect1[3] - rect1[1] + 1) + (rect2[2] - rect2[0] + 1) * (rect2[3] - rect2[1] + 1) - iov = i / ureturn ov >= iou_thresholddef IoB(rect1, rect2, iob_threshold):b = (rect2[:, 2] - rect2[:, 0] + 1) * (rect2[:, 3] - rect2[:, 1] + 1)# from IPython import embed; embed() $TODOxx1 = np.maximum(rect1[:, 0], rect2[:, 0])yy1 = np.maximum(rect1[:, 1], rect2[:, 1])xx2 = np.minimum(rect1[:, 2], rect2[:, 2])yy2 = np.minimum(rect1[:, 3], rect2[:, 3])dx = np.maximum(0, xx2 - xx1 + 1)dy = np.maximum(0, yy2 - yy1 + 1)i = dx * dyov = i / bov[b < 0] = 0return ovdef IoU(rect1, rect2, iob_threshold):a1 = (rect1[:, 2] - rect1[:, 0] + 1) * (rect1[:, 3] - rect1[:, 1] + 1)a2 = (rect2[:, 2] - rect2[:, 0] + 1) * (rect2[:, 3] - rect2[:, 1] + 1)xx1 = np.maximum(rect1[:, 0], rect2[:, 0])yy1 = np.maximum(rect1[:, 1], rect2[:, 1])xx2 = np.minimum(rect1[:, 2], rect2[:, 2])yy2 = np.minimum(rect1[:, 3], rect2[:, 3])dx = np.maximum(0, xx2 - xx1 + 1)dy = np.maximum(0, yy2 - yy1 + 1)i = dx * dyov = i / (a1 + a2 - i)ov[a1 + a2 - i < 0] = 0return ovdef simple_merge(rect1, rect2):if rect2[4] > rect1[4]:return rect2else:return rect1def raw_nms(boxes, scores, iou_threshold=0.3):if 0 == len(boxes):return []rects = list(boxes)for i in range(len(rects)):rects[i] = list(rects[i])rects[i].append(scores[i])rects[i].append(i)rects.sort(key=lambda x: x[4], reverse=True)rect_valid = [True for i in range(len(rects))]for i in range(len(rects)):if rect_valid[i]:j = i + 1while j < len(rect_valid):if is_overlap_v1(rects[i], rects[j], iou_threshold):rect_valid[j] = Falsej = j + 1return [x[5] for i, x in enumerate(rects) if rect_valid[i]]def nms(boxes, scores, iou_threshold=0.3):if 0 == len(boxes):return []rects = list(boxes)for i in range(len(rects)):rects[i] = list(rects[i])rects[i].append(scores[i])rects[i].append(i)rects.sort(key=lambda x: x[1])idx = 0for i in range(len(rects)):if is_overlap_v2(rects[i], rects[idx], iou_threshold):rects[idx] = simple_merge(rects[idx], rects[i])else:idx += 1if idx != i:rects[idx] = rects[i]rects = rects[:idx + 1]rects.sort(key=lambda x: x[0])idx = 0for i in range(len(rects)):if is_overlap_v2(rects[i], rects[idx], iou_threshold):rects[idx] = simple_merge(rects[idx], rects[i])else:idx += 1if idx != i:rects[idx] = rects[i]rects = rects[:idx + 1]idx = 0while idx < len(rects):left = idx + 1right = len(rects) - 1while left <= right:if is_overlap_v2(rects[idx], rects[left], iou_threshold):rects[idx] = simple_merge(rects[idx], rects[left])rects[left] = rects[right]right -= 1else:left += 1if len(rects) != right + 1:rects = rects[:right + 1]idx += 1return [x[5] for x in rects]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。