python查看coco格式的box

该脚本读取指定路径下的txt和jpg文件,用OpenCV展示图片并在物体边界上画出矩形框,按f显示上一张,esc退出,其他键显示下一张。
  • python show_box.py <path>
  • 图像和txt放在path目录下
  • 按f上一张,按esc退出,其他键下一张
import os, sys, glob, cv2

path=sys.argv[1]
al = glob.glob(path+'/*txt')
al.sort()
n=len(al)
i = 0
while(1):
	a=al[i]
	b=a.replace('txt', 'jpg')
	print(i, b)
	im=cv2.imread(b)
	ih,iw,_=im.shape
	for line in open(a):
		sp=line.split()
		c=sp[0]
		x=float(sp[1])*iw
		y=float(sp[2])*ih
		w=float(sp[3])*iw
		h=float(sp[4])*ih
		x1=int(x-w/2)
		x2=int(x+w/2)
		y1=int(y-h/2)
		y2=int(y+h/2)
		cv2.rectangle(im,(x1,y1),(x2,y2),(0,0,255))
	cv2.imshow('ss', im)
	ch=cv2.waitKey() & 0xff
	if ch == 102: #f
		i -= 1
		if i < 0: i = 0
	elif ch == 27: break
	else:
		i += 1
		if i >= n: i = n-1
将YOLO格式数据转化为COCO格式数据,可按以下步骤进行操作: ### 1. 理解两种格式的结构 - **YOLO格式**:每个图像对应一个 `.txt` 文件,文件中每行代表一个目标,格式为 `class_id x_center y_center width height`,坐标值是相对于图像宽高的比例值。 - **COCO格式**:是一个JSON文件,包含图像信息、类别信息和标注信息等。 ### 2. 准备必要的库 需要使用Python的 `json` 和 `os` 库,可能还会用到 `cv2` 库来获取图像的宽高信息。 ```python import json import os import cv2 ``` ### 3. 编写转换代码 ```python def yolo_to_coco(yolo_dir, image_dir, output_json): # 初始化COCO格式的字典 coco_data = { "images": [], "annotations": [], "categories": [] } # 构建类别信息 category_id_map = {} category_id = 0 for txt_file in os.listdir(yolo_dir): if txt_file.endswith('.txt'): with open(os.path.join(yolo_dir, txt_file), 'r') as f: lines = f.readlines() for line in lines: class_id = int(line.split()[0]) if class_id not in category_id_map: category_id_map[class_id] = category_id coco_data["categories"].append({ "id": category_id, "name": str(class_id), "supercategory": "object" }) category_id += 1 # 处理图像和标注信息 image_id = 0 annotation_id = 0 for txt_file in os.listdir(yolo_dir): if txt_file.endswith('.txt'): image_name = txt_file.replace('.txt', '.jpg') # 假设图像是jpg格式 image_path = os.path.join(image_dir, image_name) if os.path.exists(image_path): img = cv2.imread(image_path) height, width = img.shape[:2] # 添加图像信息 coco_data["images"].append({ "id": image_id, "file_name": image_name, "width": width, "height": height }) # 读取YOLO标注文件 with open(os.path.join(yolo_dir, txt_file), 'r') as f: lines = f.readlines() for line in lines: parts = line.split() class_id = int(parts[0]) x_center = float(parts[1]) y_center = float(parts[2]) w = float(parts[3]) h = float(parts[4]) # 转换为COCO格式的边界框 x = (x_center - w / 2) * width y = (y_center - h / 2) * height box_width = w * width box_height = h * height # 添加标注信息 coco_data["annotations"].append({ "id": annotation_id, "image_id": image_id, "category_id": category_id_map[class_id], "bbox": [x, y, box_width, box_height], "area": box_width * box_height, "iscrowd": 0 }) annotation_id += 1 image_id += 1 # 保存为JSON文件 with open(output_json, 'w') as f: json.dump(coco_data, f) # 使用示例 yolo_dir = 'path/to/yolo/labels' image_dir = 'path/to/images' output_json = 'path/to/output/coco.json' yolo_to_coco(yolo_dir, image_dir, output_json) ``` ### 代码解释 - 首先初始化COCO格式的字典,包含 `images`、`annotations` 和 `categories` 三个主要部分。 - 遍历YOLO标注文件,构建类别信息。 - 遍历每个YOLO标注文件,读取对应的图像,获取图像的宽高信息,并将其添加到 `images` 列表中。 - 对于每个YOLO标注,将其转换为COCO格式的边界框,并添加到 `annotations` 列表中。 - 最后将COCO格式的字典保存为JSON文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刀么克瑟拉莫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值