结果:

代码:
import os
from PIL import Image
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator
from matplotlib import ticker
# 设置显示中文字体
mpl.rcParams["font.sans-serif"] = ["SimHei"]
# txt文件夹路径
txts_path = "自己的路径"
# 图片文件夹路径
jpgs_path = "自己的路径"
# 存储w/h比例的列表
ratios = []
# 遍历文件夹中的每个txt文件
for txt_file in os.listdir(txts_path):
if txt_file.endswith(".txt"):
txt_path = os.path.join(txts_path, txt_file)
txtname = os.path.basename(txt_path)
jpgname = txtname.replace(".txt", ".jpg")
jpg_path = os.path.join(jpgs_path, jpgname)
image = Image.open(jpg_path)
width, height = image.size
with open(txt_path, "r") as file:
lines = file.readlines()
for line in lines:
category, x, y, w, h = line.strip().

该代码段使用Python的PIL和matplotlib库分析文本文件夹中的图像数据,计算并绘制标注框的长宽比直方图,用于理解图像对象的比例分布。程序读取txt文件中的坐标信息,计算比例并保存为直方图。
最低0.47元/天 解锁文章
6068





