使用python,将已打好标签的json数据(包含矩形框的四个点像素坐标)绘制一个矩形框,与对应缺陷图片组合可视化输出,在一个新的文件夹显示

用python将json数据与图片可视化
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
关注
import cv2 import json import os # 设置文件路径 json_file_path = 'path/to/json/file.json' image_folder_path = 'path/to/image/folder' output_folder_path = 'path/to/output/folder' # 创建用于显示的窗口 cv2.namedWindow('image', cv2.WINDOW_NORMAL) # 读取json文件 with open(json_file_path, 'r') as f: data = json.load(f) # 循环读取json中每个图片信息 for i in range(len(data['images'])): image_name = data['images'][i]['file_name'] image_path = os.path.join(image_folder_path, image_name) # 读取图片文件 img = cv2.imread(image_path) # 获得当前图片的所有标签 annotations = [a for a in data['annotations'] if a['image_id'] == i] # 遍历所有标签并绘制对应的矩形框 for a in annotations: x, y, w, h = a['bbox'] cv2.rectangle(img, (int(x), int(y)), (int(x + w), int(y + h)), (0,255,0), 2) # 在窗口中显示图片 cv2.imshow('image', img) # 将图片保存到输出文件夹中 output_path = os.path.join(output_folder_path, image_name) cv2.imwrite(output_path, img) cv2.waitKey(0) # 关闭窗口 cv2.destroyAllWindows()
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用