将yolo生成的txt文件里面的长宽相乘再乘以π,置信度两个信息保存在新建的txt名字的excel文件里面,python实现
时间: 2024-03-10 15:48:23 浏览: 30
可以通过以下代码实现:
```python
import os
import csv
# 定义YOLO生成的txt文件路径
txt_path = 'path/to/txt/file/'
# 定义要保存到的Excel文件名
excel_file = 'path/to/excel/file/excel_file.csv'
# 获取所有txt文件名
txt_files = [f for f in os.listdir(txt_path) if f.endswith('.txt')]
# 遍历每个txt文件,提取信息并保存到Excel
with open(excel_file, mode='w', newline='') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(['File Name', 'Area', 'Confidence'])
for txt_file in txt_files:
with open(os.path.join(txt_path, txt_file), mode='r') as f:
lines = f.readlines()
for line in lines:
# 解析每一行
line_data = line.split()
class_id = int(line_data[0])
confidence = float(line_data[1])
x_center = float(line_data[2])
y_center = float(line_data[3])
width = float(line_data[4])
height = float(line_data[5])
# 计算面积
area = width * height * 3.1415926
# 将信息保存到Excel
writer.writerow([txt_file, area, confidence])
```
该代码会遍历指定路径下的所有txt文件,提取每个文件中每个检测框的长、宽、置信度信息,并通过计算将长宽相乘再乘以π得到面积信息,最终将文件名、面积、置信度信息保存到指定Excel文件中。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)