000000000722.jpg,67,80,233,248,73,233,67,240,74,248,80,241,hand坐标信息包含的是x1,y1,x2,y2这种格式,该怎么修改是上面代码
时间: 2023-09-23 21:12:17 浏览: 58
hand_dataset.tar.gz
如果您的坐标信息是以x1,y1,x2,y2的格式提供的,您可以稍微修改代码以适应这种格式。以下是修改后的示例代码:
```python
import os
# 提供的标注数据
annotations = [
"000000000036.jpg,216,257,208,257,230,257,257,255,243,208,216,211,hand",
"000000000036.jpg,419,469,339,386,449,339,419,343,439,386,469,381,hand",
"000000000077.jpg,29,47,254,276,47,259,38,254,29,272,38,276,hand",
"000000000077.jpg,291,307,58,69,291,62,294,69,307,65,305,58,hand",
"000000000077.jpg,292,305,58,69,292,63,295,69,305,64,302,58,hand",
"000000000077.jpg,307,315,76,86,315,78,311,76,307,85,311,86,hand",
"000000000165.jpg,109,182,210,253,109,224,116,253,182,239,174,210,hand",
"000000000165.jpg,219,274,262,312,243,262,219,280,250,
...
]
# 创建保存拆分后txt文件的目录
output_dir = "output"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 拆分标注数据为单独的txt文件
for annotation in annotations:
# 解析文件名、坐标和标签
filename, *coords, label = annotation.split(",")
# 构建文件路径
filepath = os.path.join(output_dir, f"{filename.split('.')[0]}.txt")
# 写入坐标和标签到txt文件
with open(filepath, "a") as file:
for i in range(0, len(coords), 4):
xmin = coords[i]
ymin = coords[i+1]
xmax = coords[i+2]
ymax = coords[i+3]
file.write(f"{xmin},{ymin},{xmax},{ymax},{label}\n")
```
在上述示例中,我们对`for`循环进行了修改,以便正确解析`x1,y1,x2,y2`格式的坐标信息。然后,我们将每个坐标信息写入单独的txt文件中,每行包含`xmin,ymin,xmax,ymax,label`的信息。最终,您将在`output`目录中找到按文件名拆分的单独txt文件。
阅读全文