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

被折叠的 条评论
为什么被折叠?



