json 可视化 2025 coco json

目录

查看各种类型;

[coco json可视化](#coco json可视化)


查看各种类型;

python 复制代码
import glob
import json
import os
import time

import numpy as np
import cv2
from PIL import Image, ImageDraw, ImageFont
from natsort import natsorted

def draw_shapes(json_path):
    # 读取 JSON
    with open(json_path, 'r', encoding='utf-8') as f:
        data = json.load(f)

    img_path = os.path.join(os.path.dirname(json_path), data["imagePath"])
    img = cv2.imread(img_path)
    if img is None:
        print(f"图片未找到: {img_path}")
        return

    shapes = data["shapes"]

    for shape in shapes:
        label = shape["label"]
        shape_type = shape["shape_type"]
        points = np.array(shape["points"], dtype=np.int32)

        color = (0, 255, 0)  # 默认绿色
        if shape_type == "ellipse":
            # points: [[cx, cy], [w, h]]
            cx, cy = map(int, points[0])
            w, h = map(int, points[1])
            cv2.ellipse(img, (cx, cy), (int(w / 2), int(h / 2)), 0, 0, 360, color, 2)

        elif shape_type in ["polygon", "contour"]:
            cv2.polylines(img, [points], isClosed=True, color=color, thickness=2)

        elif shape_type == "points":
            for (x, y) in points:
                cv2.circle(img, (int(x), int(y)), 3, (0, 0, 255), -1)

        elif shape_type == "rectangle":
            x1, y1 = points[0]
            x2, y2 = points[1]
            cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), color, 2)

        else:
            print(f"⚠️ 未知类型: {shape_type}")
            continue

        # 绘制文字标签
        cv2.putText(img, label, (int(points[0][0]), int(points[0][1]) - 5),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 255), 2)

    return img

if __name__ == '__main__':

    font = ImageFont.truetype("simhei.ttf", 22, encoding="utf-8")

    img_to =r'D:\data\tmp\data_similar\0\ellipse\1010_1905_0/'
    img_to =r'D:\data\tmp\data_similar\0\box_frame\1010_1938_0/'
    img_to =r'D:\data\tmp\data_similar\0\box_frame\1011_2037_0/'
    img_to =r'D:/data/tmp/data_similar/0/box_frame/1011_2037_0/shear_y/rotate/'
    img_to =r'D:/data/tmp/data_similar/0/box_frame/1011_2037_0/rotate/'
    img_to =r'D:\data\course_1023\jiezhi\20251023_1738_part001_seg/'
    img_to =r'C:\Users\ChanJing-01\Desktop\tmp\1024/'
    img_to =r'D:\data\Preview_10\20251021-185957_seg/'

    json_paths =glob.glob(img_to + "/*.json")

    json_paths=natsorted(json_paths,reverse=True)

    for json_path in json_paths:

        img = draw_shapes(json_path)

        cv2.imshow("Annotation Viewer", img)
        cv2.waitKey(0)
    cv2.destroyAllWindows()

coco json可视化

python 复制代码
import glob
import json
import os
import time

import numpy as np
import cv2
from PIL import Image, ImageDraw, ImageFont
from natsort import natsorted


def iou(box1, box2):
    '''
    box:[x1, y1, x2, y2]
    '''
    in_h = min(box1[3], box2[3]) - max(box1[1], box2[1])
    in_w = min(box1[2], box2[2]) - max(box1[0], box2[0])
    inner = 0 if in_h <0 or in_w <0 else in_h *in_w
    union = (box1[2] - box1[0]) * (box1[3] - box1[1]) + \
            (box2[2] - box2[0]) * (box2[3] - box2[1]) - inner
    iou = inner / union
    return iou


def iou_over(box1, box2):
    '''
    box:[x1, y1, x2, y2] 相交区域 最小的box的占比
    '''
    in_h = min(box1[3], box2[3]) - max(box1[1], box2[1])
    in_w = min(box1[2], box2[2]) - max(box1[0], box2[0])
    inner = 0 if in_h <0 or in_w <0 else in_h *in_w
    union = min((box1[2] - box1[0]) * (box1[3] - box1[1]), (box2[2] - box2[0]) * (box2[3] - box2[1]))
    iou = inner / union
    return iou

def batch_IoU(bbox, gt):
    """
    :param bbox: (n, 4)
    :param gt: (m, 4)
    :return: (n, m)
    numpy 广播机制 从后向前对齐。 维度为1 的可以重复等价为任意维度
    eg: (4,3,2)   (3,2)  (3,2)会扩充为(4,3,2)
        (4,1,2)   (3,2) (4,1,2) 扩充为(4, 3, 2)  (3, 2)扩充为(4, 3,2) 扩充的方法为重复
    广播会在numpy的函数 如sum, maximun等函数中进行
    pytorch同理。
    扩充维度的方法:
    eg: a  a.shape: (3,2)  a[:, None, :] a.shape: (3, 1, 2) None 对应的维度相当于newaxis
    """
    lt = np.maximum(bbox[:, None, :2], gt[:, :2])  # left_top (x, y)
    rb = np.minimum(bbox[:, None, 2:], gt[:, 2:])  # right_bottom (x, y)
    wh = np.maximum(rb - lt , 0)                # inter_area (w, h)
    inter_areas = wh[:, :, 0] * wh[:, :, 1]        # shape: (n, m)
    box_areas = (bbox[:, 2] - bbox[:, 0] ) * (bbox[:, 3] - bbox[:, 1] )
    gt_areas = (gt[:, 2] - gt[:, 0] ) * (gt[:, 3] - gt[:, 1] )
    IoU = inter_areas / (box_areas[:, None] + gt_areas - inter_areas)
    return IoU

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        elif isinstance(obj, np.floating):
            return float(obj)
        elif isinstance(obj, np.ndarray):
            return obj.tolist()
        else:
            return super(MyEncoder, self).default(obj)

if __name__ == '__main__':

    font = ImageFont.truetype("simhei.ttf", 22, encoding="utf-8")

    img_to =r'D:\data\quexian\det_data\det_0915\rotated_results/'
    img_to =r'D:\data\tmp\data_label\video2/'
    img_to =r'D:\data\tmp\data_similar\0928_1747_1/'
    img_to =r'D:\data\tmp\data_similar\0\string\0929_1812_0\stretch/'

    from_labels =glob.glob(img_to + "/*.json")

    from_labels=natsorted(from_labels,reverse=True)

    for f_label_file in from_labels:
        base_name= os.path.basename(f_label_file)
        to_val = json.load(open(f_label_file, 'r',encoding='utf-8'))
        imagePath = to_val['imagePath']
        img_path= img_to + imagePath
        print(img_path)
        if "shapes" not in to_val:
            continue
        to_shapes =to_val['shapes']

        if not os.path.exists(img_path):
            img_path=f_label_file.replace('.json', '.jpg')
        frame=cv2.imread(img_path)
        if len(to_shapes)<1:
            print('none',img_path)
        for to_shape in to_shapes:

            if to_shape["shape_type"]== "rectangle":

                print('label',to_shape['label'])
                box2 = to_shape['points'][0] + to_shape['points'][1]

                points = np.array(to_shape["points"])
                xmin = (min(points[:, 0]) if min(points[:, 0]) > 0 else 0)
                xmax = (max(points[:, 0]) if max(points[:, 0]) > 0 else 0)
                ymin = (min(points[:, 1]) if min(points[:, 1]) > 0 else 0)
                ymax = (max(points[:, 1]) if max(points[:, 1]) > 0 else 0)

                cv2.rectangle(frame, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (0, 0, 255), 1)

                start = time.time()
                cv2_im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                pil_im = Image.fromarray(cv2_im)
                draw = ImageDraw.Draw(pil_im)
                draw.text((int(xmin), int(ymin)-10), to_shape['label'], (255, 0, 0), font=font)
                frame = cv2.cvtColor(np.array(pil_im), cv2.COLOR_RGB2BGR)
            # cv2.rectangle(img, (int(to_shape['points'][0][0]), int(to_shape['points'][0][1])), (int(to_shape['points'][1][0]), int(to_shape['points'][1][1])), (0, 0, 255), 2)

        if np.prod(frame.shape[:2]) > 1000 * 1300:
            x_scale = np.sqrt(1000 * 1200 / np.prod(frame.shape[:2]))

            new_width = int(frame.shape[1] * x_scale)
            new_height = int(frame.shape[0] * x_scale)

            # 确保宽高都是偶数
            if new_width % 2 != 0:
                new_width += 1
            if new_height % 2 != 0:
                new_height += 1
            frame = cv2.resize(frame, (new_width, new_height), interpolation=cv2.INTER_AREA)
        cv2.imshow("image" ,frame)
        cv2.waitKey(0)
相关推荐
电饭叔12 小时前
不含Luhn算法《python语言程序设计》2018版--第8章14题利用字符串输入作为一个信用卡号之二(识别卡号有效)
java·python·算法
观音山保我别报错13 小时前
列表,元组,字典
开发语言·python
小付爱coding13 小时前
Claude Code安装教程【windows版本】
java·git·python
高频交易dragon13 小时前
5分钟和30分钟联立进行缠论信号分析
开发语言·python
0思必得013 小时前
[Web自动化] 开发者工具应用(Application)面板
运维·前端·python·自动化·web自动化·开发者工具
我是华为OD~HR~栗栗呀13 小时前
华为OD-C面经-23届学院哦
java·c++·python·华为od·华为·面试
xiaozi412013 小时前
Ruey S. Tsay《时间序列分析》Python实现笔记:综合与应用
开发语言·笔记·python·机器学习
Aspect of twilight13 小时前
PyTorch DDP分布式训练Pytorch代码讲解
人工智能·pytorch·python
@游子13 小时前
Python学习笔记-Day5
笔记·python·学习
棒棒的皮皮13 小时前
【OpenCV】Python图像处理之数字水印
图像处理·python·opencv·计算机视觉