模型测试结果保存labelme

该博客介绍了如何使用NMS(非极大抑制)后的模型输出,将检测结果转化为带有Polygon标注的JSON格式,适用于目标检测任务的可视化和标注。通过定义函数`gen_json`,将图片信息、检测框坐标、类别名等转换为符合V3.14.2标准的CVAT JSON格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#生成json代码

def gen_json(src, det, width, heigh, dst):

    info = []
    for i in range(len(det)):
        shapes = {}
        box = [int(i) for i in det[i][:4]]
        class_name = label_name[int(det[i][-1])]
        #yolov5R
        # cx, cy = (box[0] + box[2]) / 2, (box[1] + box[3]) / 2
        # w, h = box[2] - box[0], box[3] - box[1]
        # rotatedRect = ((cx, cy), (w, h), 30)
        # rotatedBox = cv2.boxPoints(rotatedRect)
        # rotatedBox = np.int0(rotatedBox)
        # points = rotatedBox.tolist()

        points = [[box[0],box[1]], [box[2], box[1]], [box[2],box[3]], [box[0],box[3]]]

        shapes['label'] = class_name
        shapes['line_color'] = None
        shapes['fill_color'] = None
        shapes['points'] = points
        shapes['shape_type'] = 'polygon'
        info.append(shapes)

    with open(src, 'rb') as image_file:
        image_text_data = image_file.read()
        image_text_bytes = base64.b64encode(image_text_data)
        image_text_tring = image_text_bytes.decode('utf-8')
    name = os.path.basename(src)
    print(name)

    annotations_info = {}
    annotations_info['version'] = '3.14.2'
    annotations_info['flags'] = {}
    annotations_info['shapes'] = info
    annotations_info['lineColor'] = [0, 255, 0, 128]
    annotations_info['fillColor'] = [255, 0, 0, 128]
    annotations_info['imagePath'] = src
    # annotations_info['imageData'] = utils.img_arr_to_b64(img).decode('utf-8')
    annotations_info['imageData'] = image_text_tring
    annotations_info['imageHeight'] = heigh
    annotations_info['imageWidth'] = width

    with open(os.path.join(dst, name.replace('.jpg', '.json')), "w",
              encoding='utf-8') as json_file:
        json.dump(annotations_info, json_file, ensure_ascii=False)
    return 0

模型输出结果,NMS后,生成json:

# NMS
 pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)
 for det in pred:  
     det = np.array(det)
     if det is not None and len(det):
         det[:, :4] = scale_coords(im.shape[2:], det[:, :4], im0s.shape).round()
 # print('det: ', det, det.size)
 if det.size == 0:
     continue
 gen_json(path, det, dst)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值