정우진

covert annotation

1 +import cv2
2 +import os
3 +import sys
4 +import re
5 +import glob
6 +
7 +path = sys.argv[1]
8 +save_path = sys.argv[2]
9 +
10 +def cropFolerlevel(path, save_path):
11 + img_list = [file for file in glob.glob(path+'\\*.png')]
12 + txt_list = [file for file in glob.glob(path+'\\*.txt')]
13 +
14 + for i in range(img_list.__len__()):
15 + txt = open(txt_list[i])
16 + img = cv2.imread(img_list[i])
17 + name = img_list[i].split('\\')[-1]
18 +
19 + p = list(map(int, re.split(' |\n', txt.readlines()[7])[1:5]))
20 + crop_img = img[p[1]:p[1]+p[3], p[0]:p[0]+p[2]]
21 + cv2.imwrite(save_path+'\\'+name , crop_img)
22 +
23 +def recursive(path, save_path):
24 + files = os.listdir(path)
25 + for file in files:
26 + fullname = os.path.join(path, file)
27 + savename = os.path.join(save_path, file)
28 + if os.path.isdir(fullname):
29 + recursive(fullname, savename)
30 + if not os.path.isdir(save_path):
31 + os.makedirs(save_path)
32 + cropFolerlevel(path, save_path)
33 +
34 +recursive(path, save_path)
...\ No newline at end of file ...\ No newline at end of file
1 +import cv2
2 +import os
3 +import sys
4 +import glob
5 +import re
6 +
7 +path = sys.argv[1]
8 +save_path = sys.argv[2]
9 +img_list = [cv2.imread(file) for file in glob.glob(path + '\\*.png')]
10 +txt_list = [file for file in glob.glob(path + '\\*.txt')]
11 +
12 +print(cv2.__version__)
13 +print(txt_list)
14 +print(img_list[1])
15 +txt = open(txt_list[1])
16 +img = img_list[1]
17 +p = list(map(int, re.split(' |\n', txt.readlines()[7])[1:5]))
18 +print(p)
19 +cv2.imshow('hello', img)
20 +cv2.waitKey(0)
21 +corp_img = img[p[1]:p[1]+p[3], p[0]:p[0]+p[2]]
22 +cv2.imshow('hello', corp_img)
23 +cv2.imwrite(save_path+'\\result.png', corp_img)
1 +import os
2 +import csv
3 +import xlsxwriter
4 +from PIL import Image
5 +import re
6 +
7 +# make empty csv file
8 +workbook = xlsxwriter.Workbook("dataset.xlsx")
9 +worksheet = workbook.add_worksheet("sheet1")
10 +
11 +# resize col
12 +worksheet.set_column("A:A", 60)
13 +
14 +# get current directory path
15 +directory = os.getcwd()
16 +
17 +img_file_name = []
18 +text_file_name = []
19 +for filename in os.listdir(directory):
20 + if filename.endswith(".png"):
21 + path = os.path.join(directory, filename)
22 + img_file_name.append(path)
23 +
24 + elif filename.endswith(".txt"):
25 + path = os.path.join(directory, filename)
26 + text_file_name.append(path)
27 +
28 +print(img_file_name) # for check
29 +print(text_file_name) # for check
30 +
31 +# text annotation
32 +text = []
33 +for t in text_file_name:
34 + plate = []
35 + with open(t, "r") as f:
36 + for line in f:
37 + if line[:5] == "plate":
38 + plate.append(line.strip("plate: ").strip())
39 +
40 + elif line[:14] == "position_plate":
41 + numbers = list(map(int, line.strip("position_plate: ").strip().split()))
42 + plate.append(numbers)
43 +
44 + elif "char" in line:
45 + numbers = re.findall("\d+", line[8:])
46 + plate.append(list(map(int, numbers)))
47 + text.append(plate)
48 +print(text) # for check
49 +
50 +#################################################################################
51 +#################### error : 마지막 image, text에 대해서만 됨 ####################
52 +#################################################################################
53 +# insert to csv
54 +idx = 0
55 +k = 0
56 +for filename in img_file_name:
57 + # col A, B
58 + for i in range(k, k + len(text[idx][0]) - 1):
59 + A = "A" + str(i + 1)
60 + path = os.path.join(directory, filename)
61 + worksheet.write(A, path)
62 +
63 + B = "B" + str(i + 1)
64 + worksheet.write(B, text[idx][0])
65 +
66 + k += len(text[idx][0]) - 1
67 + idx += 1
68 +
69 +idx = 0
70 +k = 0
71 +for filename in text_file_name:
72 + # col C, D, E, F : x1, y1, x2, y2
73 + j = 0
74 + for i in range(k, k + len(text[0]) - 2):
75 + C = "C" + str(i + 1)
76 + worksheet.write(C, text[idx][j + 2][0])
77 +
78 + D = "D" + str(i + 1)
79 + worksheet.write(D, text[idx][j + 2][1])
80 +
81 + E = "E" + str(i + 1)
82 + worksheet.write(E, text[idx][j + 2][0] + text[idx][j + 2][2])
83 +
84 + F = "F" + str(i + 1)
85 + worksheet.write(F, text[idx][j + 2][1] + text[idx][j + 2][3])
86 +
87 + j += 1
88 + k += len(text[0]) - 2
89 + idx += 1
90 +
91 +workbook.close()
1 +import os
2 +import csv
3 +import xlsxwriter
4 +from PIL import Image
5 +import re
6 +
7 +# make empty csv file
8 +workbook = xlsxwriter.Workbook("dataset.xlsx")
9 +worksheet = workbook.add_worksheet("sheet1")
10 +
11 +# resize col
12 +worksheet.set_column("A:A", 60)
13 +
14 +# get current directory path
15 +directory = os.getcwd()
16 +
17 +img_file_name = []
18 +text_file_name = []
19 +for filename in os.listdir(directory):
20 + if filename.endswith(".png"):
21 + path = os.path.join(directory, filename)
22 + img_file_name.append(path)
23 +
24 + elif filename.endswith(".txt"):
25 + path = os.path.join(directory, filename)
26 + text_file_name.append(path)
27 +
28 +print(img_file_name) # for check
29 +print(text_file_name) # for check
30 +
31 +# text annotation
32 +text = []
33 +for t in text_file_name:
34 + plate = []
35 + with open(t, "r") as f:
36 + for line in f:
37 + if line[:5] == "plate":
38 + plate.append(line.strip("plate: ").strip())
39 +
40 + elif line[:14] == "position_plate":
41 + numbers = list(map(int, line.strip("position_plate: ").strip().split()))
42 + plate.append(numbers)
43 +
44 + elif "char" in line:
45 + numbers = re.findall("\d+", line[8:])
46 + plate.append(list(map(int, numbers)))
47 + text.append(plate)
48 +print(text) # for check
49 +
50 +#################################################################################
51 +#################### error : 마지막 image, text에 대해서만 됨 ####################
52 +#################################################################################
53 +# insert to csv
54 +idx = 0
55 +# for filename in os.listdir(directory):
56 +for filename in img_file_name:
57 + # if filename.endswith(".png"):
58 + # col A, B
59 + for i in range(len(text[idx][0]) - 1):
60 + A = "A" + str(i + 1)
61 + path = os.path.join(directory, filename)
62 + worksheet.write(A, path)
63 +
64 + B = "B" + str(i + 1)
65 + worksheet.write(B, text[idx][0])
66 +
67 + idx += 1
68 +
69 +
70 +for filename in text_file_name:
71 + # if filename.endswith(".txt"):
72 + # col C, D, E, F : x1, y1, x2, y2
73 + for i in range(len(text[0]) - 2):
74 + C = "C" + str(i + 1)
75 + worksheet.write(C, text[idx][i + 2][0])
76 +
77 + D = "D" + str(i + 1)
78 + worksheet.write(D, text[idx][i + 2][1])
79 +
80 + E = "E" + str(i + 1)
81 + worksheet.write(E, text[idx][i + 2][0] + text[idx][i + 2][2])
82 +
83 + F = "F" + str(i + 1)
84 + worksheet.write(F, text[idx][i + 2][1] + text[idx][i + 2][3])
85 +
86 + idx += 1
87 +
88 +workbook.close()
This diff is collapsed. Click to expand it.