import cv2
import os
file_path = "./ktxx"
save_path = "./plate_num"
imgs_list = os.listdir(file_path)
for img_name in imgs_list:
if img_name.endswith(".jpg"):
image = cv2.imread(os.path.join(file_path, img_name))
img_w, img_h = image.shape[1], image.shape[0]
file_read = open(os.path.join(file_path, img_name[0:-4] + ".txt"), "r")
for line in file_read.readlines():
count = 1
bbox = line.split(" ")
if int(bbox[0]) == 10: # 确定是车牌的标注
center_x = round(float(bbox[1]) * img_w)
center_y = round(float(bbox[2]) * img_h)
bbox_width = round(float(bbox[3]) * img_w)
bbox_height = round(float(bbox[4]) * img_h)
xmin = int(center_x - bbox_width / 2)
ymin = int(center_y - bbox_height / 2)
xmax = int(center_x + bbox_width / 2)
ymax = int(center_y + bbox_height / 2)
save_image = image[ymin:ymax, xmin:xmax]
cv2.imwrite(os.path.join(save_path, img_name[0:-4] + "_" + str(count) + ".jpg"), save_image)
print(img_name)
Python 扣取yolo标签中的图像示例并保存
最新推荐文章于 2024-02-24 16:05:15 发布