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)
相关推荐
wazmlp0018873694 小时前
python第三次作业
开发语言·python
深蓝电商API5 小时前
住宅代理与数据中心代理在爬虫中的选择
爬虫·python
历程里程碑6 小时前
普通数组----合并区间
java·数据结构·python·算法·leetcode·职场和发展·tornado
weixin_395448916 小时前
mult_yolov5_post_copy.c_cursor_0205
c语言·python·yolo
执风挽^6 小时前
Python基础编程题2
开发语言·python·算法·visual studio code
纤纡.6 小时前
PyTorch 入门精讲:从框架选择到 MNIST 手写数字识别实战
人工智能·pytorch·python
kjkdd7 小时前
6.1 核心组件(Agent)
python·ai·语言模型·langchain·ai编程
小镇敲码人7 小时前
剖析CANN框架中Samples仓库:从示例到实战的AI开发指南
c++·人工智能·python·华为·acl·cann
萧鼎7 小时前
Python 包管理的“超音速”革命:全面上手 uv 工具链
开发语言·python·uv
alvin_20057 小时前
python之OpenGL应用(二)Hello Triangle
python·opengl