Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onnxruntime 部署部分为什么是将角度作为类回归,这样我测试了没有原本模型检测准确度 #3

Open
LHJ-YM opened this issue Jan 31, 2024 · 0 comments

Comments

@LHJ-YM
Copy link

LHJ-YM commented Jan 31, 2024

def preprocess(self, img, new_shape):
    img = self.letterbox(img, new_shape, auto=False)[0]
    img = img.transpose((2, 0, 1))[::-1]  # HWC to CHW, BGR to RGB
    img = np.ascontiguousarray(img).astype('float32')
    img /= 255  # 0 - 255 to 0.0 - 1.0
    if len(img.shape) == 3:
        img = img[None]  # expand for batch dim
    return img

def postprecess(self, prediction, src_img, new_shape):

    nc = prediction.shape[2] - 5 - 180  # number of classes

    xc = prediction[..., 4] > CONF_THRES
    outputs = prediction[:][xc]

    generate_boxes, bboxes, scores = [], [], []

    for out in outputs:

        cx, cy, longside, shortside, obj_score = out[:5]
        class_scores = out[5: 5+nc]
        class_idx = np.argmax(class_scores)

        max_class_score = class_scores[class_idx] * obj_score
        if max_class_score < CONF_THRES:
            continue

        theta_scores = out[5+nc:]
        theta_idx = np.argmax(theta_scores)
        theta_pred = (theta_idx - 90) / 180 * PI

        bboxes.append([[cx, cy], [longside, shortside], max_class_score])
        scores.append(max_class_score)
        generate_boxes.append([
            cx, cy, longside, shortside, 
            theta_pred, max_class_score, class_idx
        ])

    indices = cv2.dnn.NMSBoxesRotated(
        bboxes, scores, CONF_THRES, NMS_THRES
    )
    det = np.array(generate_boxes)[indices.flatten()]

    pred_poly = self.rbox2poly(det[:, :5])

    pred_poly = self.scale_polys(new_shape, pred_poly, src_img.shape)
    det = np.concatenate((pred_poly, det[:, -2:]), axis=1) # (n, [poly conf cls])

    for *poly, conf, cls in reversed(det):
        c = int(cls)
        label = f'{CLASSES[c]} {conf:.2f}'
        self.poly_label(src_img, poly, label, COLORS[c])
    
    cv2.imshow('Result', src_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant