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)
相关推荐
yy我不解释28 分钟前
关于comfyui的mmaudio音频生成插件时时间不一致问题(一)
python·ai作画·音视频·comfyui
紫丁香2 小时前
AutoGen详解一
后端·python·flask
FreakStudio2 小时前
不用费劲编译ulab了!纯Mpy矩阵micronumpy库,单片机直接跑
python·嵌入式·边缘计算·电子diy
清水白石0084 小时前
Free-Threaded Python 实战指南:机遇、风险与 PoC 验证方案
java·python·算法
飞Link4 小时前
具身智能核心架构之 Python 行为树 (py_trees) 深度剖析与实战
开发语言·人工智能·python·架构
桃气媛媛4 小时前
Pycharm常用快捷键
python·pycharm
Looooking5 小时前
Python 之获取安装包所占用磁盘空间大小
python
WenGyyyL5 小时前
ColBERT论文研读——NLP(IR)里程碑之作
人工智能·python·语言模型·自然语言处理
lxy-up5 小时前
RAG--切片策略
python
ricky_fan6 小时前
(OpenAI)Codex 安装、部署使用方式
python·macos·conda·vim