1 将图片的名字后重新命名
因为图片名本身有需要的信息
# -*- coding:utf-8 -*-
import sys
# sys.path.append('E:\\Anaconda\\libs')
import os #os:操作系统相关的信息模块
import shutil
from os.path import join
import cv2
import random #导入随机函数
# 存放原始图片地址
data_base_dir = 'ocr/test_images/ROIS/rcnnROI'
file_dir = r'D:\BUFFER\Pycharm\OpencvLearn\ocr\test_images\ROIs\rcnnROI'
dest_dir = r"D:\BUFFER\Pycharm\OpencvLearn\ocr\test_images\ROIs\rcnnROI\AI"
file_list = [] # 建立列表,用于保存图片信息
# 读取图片文件,并将图片地址、图片名和标签写到txt文件中
write_file_name = 'ocr/test_images/ROIS/rcnnROI/numbers.txt'
write_file = open(write_file_name, "w") # 以只写方式打开write_file_name文件
count = 0
for file in os.listdir(data_base_dir): # file为current_dir当前目录下图片名
if file.endswith(".jpg"): # 如果file以jpg结尾
for _ in range(2):
pass
imageName = str(count) + '.jpg'
write_name = './train_data/data/'+ imageName + '\t' + file[:-4] # 图片路径 + 图片名 + 标签
det = join(dest_dir, imageName)
scr_dir = join(file_dir, file)
shutil.copyfile(scr_dir ,det)
count += 1
file_list.append(write_name) # 将write_name添加到file_list列表最后
sorted(file_list) # 将列表中所有元素随机排列
number_of_lines = len(file_list) # 列表中元素个数
# 将图片信息写入txt文件中,逐行写入
for current_line in range(number_of_lines):
write_file.write(file_list[current_line] + '\n')
#关闭文件
write_file.close()
2 可以用这些数据增强生成更多的样本,并保留图片信息
# -*- coding:utf-8 -*-
import sys
# sys.path.append('E:\\Anaconda\\libs')
import os #os:操作系统相关的信息模块
import shutil
from os.path import join
import cv2
import numpy as np
import random #导入随机函数
# 存放原始图片地址
data_base_dir = 'ocr/test_images/ROIS/rcnnROI'
file_dir = r'D:\BUFFER\Pycharm\OpencvLearn\ocr\test_images\ROIs\rcnnROI'
dest_dir = r"D:\BUFFER\Pycharm\OpencvLearn\ocr\test_images\ROIs\rcnnROI\AI"
file_list = [] # 建立列表,用于保存图片信息
# 读取图片文件,并将图片地址、图片名和标签写到txt文件中
write_file_name = 'ocr/test_images/ROIS/rcnnROI/numbers.txt'
write_file = open(write_file_name, "w") # 以只写方式打开write_file_name文件
count = 0
for file in os.listdir(data_base_dir): # file为current_dir当前目录下图片名
if file.endswith(".jpg"): # 如果file以jpg结尾
for item in range(4):
imageName = str(count) + '.jpg'
write_name = './train_data/data/' + imageName + '\t' + file[:-4] # 图片路径 + 图片名 + 标签
det = join(dest_dir, imageName)
if item == 0:
scr_dir = join(file_dir, file)
shutil.copyfile(scr_dir ,det)
# 增强图片
if item == 1:
img = cv2.imread(scr_dir,cv2.IMREAD_GRAYSCALE)
kernel = np.ones((5,5), np.uint8)
open_img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
left_thred = 240
thresh1 = cv2.threshold(open_img, left_thred, 255, cv2.THRESH_BINARY)[1]
cv2.imwrite(det, thresh1)
if item == 2:
img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
kernel = np.ones((5, 5), np.uint8)
open_img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
blur = cv2.blur(open_img, (5,5))
cv2.imwrite(det, blur)
if item == 3:
img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
kernel = np.ones((5, 5), np.uint8)
open_img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
thresh1 = cv2.threshold(open_img, left_thred, 255, cv2.THRESH_BINARY)[1]
blur = cv2.blur(thresh1, (5, 5))
cv2.imwrite(det, blur)
count += 1
file_list.append(write_name) # 将write_name添加到file_list列表最后
sorted(file_list) # 将列表中所有元素随机排列
number_of_lines = len(file_list) # 列表中元素个数
# 将图片信息写入txt文件中,逐行写入
for current_line in range(number_of_lines):
write_file.write(file_list[current_line] + '\n')
#关闭文件
write_file.close()