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)
相关推荐
TheSumSt16 小时前
Python丨课程笔记Part2:方法论进阶部分
开发语言·笔记·python
知远同学16 小时前
使用virtualenv 和 anaconda 创建管理虚拟环境的区别
python
山沐与山17 小时前
【设计模式】Python状态模式:从入门到实战
python·设计模式·状态模式
Swizard17 小时前
别让你的密钥在互联网上“裸奔”!用 python-dotenv 优雅管理你的敏感配置
python
无心水17 小时前
【Stable Diffusion 3.5 FP8】8、生产级保障:Stable Diffusion 3.5 FP8 伦理安全与问题排查
人工智能·python·安全·docker·stable diffusion·ai镜像开发·镜像实战开发
深蓝海拓17 小时前
PySide6从0开始学习的笔记(十八) MVC(Model-View-Controller)模式的图形渲染体系
笔记·python·qt·学习·pyqt
一招定胜负17 小时前
杂记:cv2.imshow显示中文乱码解决过程
python·opencv
唐叔在学习17 小时前
Pyinstaller进阶之构建管理大杀器-SPEC文件
后端·python·程序员
爱吃山竹的大肚肚17 小时前
在Java中,从List A中找出List B没有的数据(即求差集)
开发语言·windows·python
weixin_4624462317 小时前
【原创实践】Python 将 Markdown 文件转换为 Word(docx)完整实现
开发语言·python·word