1.读label文件 ,然后把图片重命名成字符序列+文件名的形式,然后保存在result文件中。
import cv2
import os
with open('C:/pycharm_projects/lp_dataset_label/lp.names') as f:
labels = f.readlines()
labels_list = [] # A-Z 0-9
for l in labels:
l = l.replace('\n','')
labels_list.append(l)
label_path = 'C:/pycharm_projects/get_label/datase3_lp_recognition_528/labels/'
img_path = 'C:/pycharm_projects/get_label/datase3_lp_recognition_528/images/'
label_name_list = os.listdir(label_path)
for label_name in label_name_list:
print(label_name)
with open(label_path + label_name) as file:
file_label_name = file.readlines()
str_name = ''
img = cv2.imread(img_path + label_name.split('.')[0] + '.jpg')
w, h, _ = img.shape
if int(w) < 300 :
file_label_name.sort(key=lambda x: float(x.split(' ')[1]) * 0.3 + float(x.split(' ')[2]) * 0.7)
else:
file_label_name.sort(key=lambda x:float(x.split(' ')[1]))
for f in file_label_name:
f=f.replace('\n','')
str_name += labels_list[int(f.split(' ')[0])]
cv2.imwrite('./result/%s__%s.jpg' % (str_name,label_name.split('.')[0]), img)
2.train和valid的划分
import os
import shutil
from random import shuffle
img_path = "C:/pycharm_projects/get_label/data_arugement/images"
gt_path = "C:/pycharm_projects/get_label/data_arugement/labels"
train_img_path = "C:/pycharm_projects/get_label/data_arugement/train/img"
train_gt_path = "C:/pycharm_projects/get_label/data_arugement/train/gt"
valid_img_path = "C:/pycharm_projects/get_label/data_arugement/valid/img"
valid_gt_path = "C:/pycharm_projects/get_label/data_arugement/valid/gt"
images_list = os.listdir(img_path)
shuffle(images_list)
i = 0
for image in images_list:
if i < 400:
shutil.move(img_path + '/' + image, valid_img_path + '/' + image)
shutil.move(gt_path + '/' + image.split('.')[0] + '.txt' ,
valid_gt_path + '/' + image.split('.')[0] + '.txt')
else:
shutil.move(img_path + '/' + image, train_img_path + '/' + image)
shutil.move(gt_path + '/' + image.split('.')[0] + '.txt',
train_gt_path + '/' + image.split('.')[0] + '.txt')
i +=1
3.json文件的合并
import json
import os
json_folder_path = 'C:/pycharm_projects/get_label/550_json'
output_path = 'C:/pycharm_projects/get_label/550_json/550.json'
anno_file_list = os.listdir(json_folder_path)
first_flag = True
merge_anno = {}
for anno_file in anno_file_list:
anno_path = os.path.join(json_folder_path, anno_file)
print('open json file {}'.format(anno_path))
with open(anno_path, 'r', encoding='utf-8') as f:
anno = json.load(f)
if first_flag:
merge_anno['info'] = anno['info']
merge_anno['categories'] = anno['categories']
merge_anno['images'] = anno['images']
merge_anno['annotations'] = anno['annotations']
first_flag = False
else:
merge_anno['images'].extend(anno['images'])
merge_anno['annotations'].extend(anno['annotations'])
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(merge_anno, f, indent=4, ensure_ascii=False)
4.两层文件夹下的图片,合并一起。
path = "C:/pycharm_projects/SG_filter"
names = os.listdir(path)
i = 1
for name in names:
img_names = os.listdir(path + '/' + name)
for img in img_names:
print(img)
image = cv2.imread(path + '/' + name + '/' + img)
h, w, _ =image.shape
if int(h) <500 :
continue
cv2.imwrite('%s.jpg' % i,image)
i +=1
5.给图片加个边框生成新的图片,并把以前的图片大小作为gt
import numpy as np
import cv2
import os
path = 'C:/pycharm_projects/get_label/528_val'
save_path = 'C:/pycharm_projects/get_label/data_arugement'
images = os.listdir(path)
print(images)
for iamge in images:
if iamge.split('.')[1] =='jpg':
print(path + iamge)
img = cv2.imread(path + '/' + iamge)
r, c = img.shape[:2]
bottom = img[r - 2:r, 0:c]
mean = cv2.mean(bottom)[0]
bordersize = 100
t,b,l,r = np.random.randint(10, 200,4)
border = cv2.copyMakeBorder(img, top=t, bottom=b, left=l, right=r,
borderType=cv2.BORDER_CONSTANT, value=[mean, mean, mean])
h, w, _ =img.shape
with open(save_path + '/' + iamge.replace('jpg', 'txt'),'w') as f:
f.write(str(l) + ',' + str(t) + ',' + str(l + w) + ',' + str(t) + ',' + str(l + w) + ',' + str(t + h) + ',' + str(l) + ',' +
str(t + h) + ',' + 'LP' + '\n')
f.close()
cv2.imwrite(save_path + '/' + iamge, border)