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)
相关推荐
V胡桃夹子2 分钟前
pyenv-win 完整安装+使用手册
python·pyenv
ego.iblacat6 分钟前
Python 连接 MySQL 数据库
数据库·python·mysql
humors2211 小时前
各厂商工具包网址
java·数据库·python·华为·sdk·苹果·工具包
pzx_0011 小时前
【优化器】 随机梯度下降 SGD 详解
人工智能·python·算法
大邳草民1 小时前
Python 中 global 与 nonlocal 的语义与机制
开发语言·笔记·python
程序员小远2 小时前
软件测试用例总结
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
2501_948114242 小时前
技术解码:Gemini交互式模拟API与高负载网关的选型逻辑
人工智能·python·ai
AC赳赳老秦2 小时前
OpenClaw text-translate技能:多语言批量翻译,解决跨境工作沟通难题
大数据·运维·数据库·人工智能·python·deepseek·openclaw
JaydenAI2 小时前
[Python编程思想与技巧-01]我所理解的Python元模型
python·元宇宙·元类·元模型
清水白石0082 小时前
《Python 架构师的自动化哲学:从基础语法到企业级作业调度系统与 Airflow 止损实战》
数据库·python·自动化