정우진

covert annotation

import cv2
import os
import sys
import re
import glob
path = sys.argv[1]
save_path = sys.argv[2]
def cropFolerlevel(path, save_path):
img_list = [file for file in glob.glob(path+'\\*.png')]
txt_list = [file for file in glob.glob(path+'\\*.txt')]
for i in range(img_list.__len__()):
txt = open(txt_list[i])
img = cv2.imread(img_list[i])
name = img_list[i].split('\\')[-1]
p = list(map(int, re.split(' |\n', txt.readlines()[7])[1:5]))
crop_img = img[p[1]:p[1]+p[3], p[0]:p[0]+p[2]]
cv2.imwrite(save_path+'\\'+name , crop_img)
def recursive(path, save_path):
files = os.listdir(path)
for file in files:
fullname = os.path.join(path, file)
savename = os.path.join(save_path, file)
if os.path.isdir(fullname):
recursive(fullname, savename)
if not os.path.isdir(save_path):
os.makedirs(save_path)
cropFolerlevel(path, save_path)
recursive(path, save_path)
\ No newline at end of file
import cv2
import os
import sys
import glob
import re
path = sys.argv[1]
save_path = sys.argv[2]
img_list = [cv2.imread(file) for file in glob.glob(path + '\\*.png')]
txt_list = [file for file in glob.glob(path + '\\*.txt')]
print(cv2.__version__)
print(txt_list)
print(img_list[1])
txt = open(txt_list[1])
img = img_list[1]
p = list(map(int, re.split(' |\n', txt.readlines()[7])[1:5]))
print(p)
cv2.imshow('hello', img)
cv2.waitKey(0)
corp_img = img[p[1]:p[1]+p[3], p[0]:p[0]+p[2]]
cv2.imshow('hello', corp_img)
cv2.imwrite(save_path+'\\result.png', corp_img)
import os
import csv
import xlsxwriter
from PIL import Image
import re
# make empty csv file
workbook = xlsxwriter.Workbook("dataset.xlsx")
worksheet = workbook.add_worksheet("sheet1")
# resize col
worksheet.set_column("A:A", 60)
# get current directory path
directory = os.getcwd()
img_file_name = []
text_file_name = []
for filename in os.listdir(directory):
if filename.endswith(".png"):
path = os.path.join(directory, filename)
img_file_name.append(path)
elif filename.endswith(".txt"):
path = os.path.join(directory, filename)
text_file_name.append(path)
print(img_file_name) # for check
print(text_file_name) # for check
# text annotation
text = []
for t in text_file_name:
plate = []
with open(t, "r") as f:
for line in f:
if line[:5] == "plate":
plate.append(line.strip("plate: ").strip())
elif line[:14] == "position_plate":
numbers = list(map(int, line.strip("position_plate: ").strip().split()))
plate.append(numbers)
elif "char" in line:
numbers = re.findall("\d+", line[8:])
plate.append(list(map(int, numbers)))
text.append(plate)
print(text) # for check
#################################################################################
#################### error : 마지막 image, text에 대해서만 됨 ####################
#################################################################################
# insert to csv
idx = 0
k = 0
for filename in img_file_name:
# col A, B
for i in range(k, k + len(text[idx][0]) - 1):
A = "A" + str(i + 1)
path = os.path.join(directory, filename)
worksheet.write(A, path)
B = "B" + str(i + 1)
worksheet.write(B, text[idx][0])
k += len(text[idx][0]) - 1
idx += 1
idx = 0
k = 0
for filename in text_file_name:
# col C, D, E, F : x1, y1, x2, y2
j = 0
for i in range(k, k + len(text[0]) - 2):
C = "C" + str(i + 1)
worksheet.write(C, text[idx][j + 2][0])
D = "D" + str(i + 1)
worksheet.write(D, text[idx][j + 2][1])
E = "E" + str(i + 1)
worksheet.write(E, text[idx][j + 2][0] + text[idx][j + 2][2])
F = "F" + str(i + 1)
worksheet.write(F, text[idx][j + 2][1] + text[idx][j + 2][3])
j += 1
k += len(text[0]) - 2
idx += 1
workbook.close()
import os
import csv
import xlsxwriter
from PIL import Image
import re
# make empty csv file
workbook = xlsxwriter.Workbook("dataset.xlsx")
worksheet = workbook.add_worksheet("sheet1")
# resize col
worksheet.set_column("A:A", 60)
# get current directory path
directory = os.getcwd()
img_file_name = []
text_file_name = []
for filename in os.listdir(directory):
if filename.endswith(".png"):
path = os.path.join(directory, filename)
img_file_name.append(path)
elif filename.endswith(".txt"):
path = os.path.join(directory, filename)
text_file_name.append(path)
print(img_file_name) # for check
print(text_file_name) # for check
# text annotation
text = []
for t in text_file_name:
plate = []
with open(t, "r") as f:
for line in f:
if line[:5] == "plate":
plate.append(line.strip("plate: ").strip())
elif line[:14] == "position_plate":
numbers = list(map(int, line.strip("position_plate: ").strip().split()))
plate.append(numbers)
elif "char" in line:
numbers = re.findall("\d+", line[8:])
plate.append(list(map(int, numbers)))
text.append(plate)
print(text) # for check
#################################################################################
#################### error : 마지막 image, text에 대해서만 됨 ####################
#################################################################################
# insert to csv
idx = 0
# for filename in os.listdir(directory):
for filename in img_file_name:
# if filename.endswith(".png"):
# col A, B
for i in range(len(text[idx][0]) - 1):
A = "A" + str(i + 1)
path = os.path.join(directory, filename)
worksheet.write(A, path)
B = "B" + str(i + 1)
worksheet.write(B, text[idx][0])
idx += 1
for filename in text_file_name:
# if filename.endswith(".txt"):
# col C, D, E, F : x1, y1, x2, y2
for i in range(len(text[0]) - 2):
C = "C" + str(i + 1)
worksheet.write(C, text[idx][i + 2][0])
D = "D" + str(i + 1)
worksheet.write(D, text[idx][i + 2][1])
E = "E" + str(i + 1)
worksheet.write(E, text[idx][i + 2][0] + text[idx][i + 2][2])
F = "F" + str(i + 1)
worksheet.write(F, text[idx][i + 2][1] + text[idx][i + 2][3])
idx += 1
workbook.close()
This diff is collapsed. Click to expand it.