File size: 7,026 Bytes
30c8b41
 
 
 
 
 
 
 
 
 
8ee5136
 
30c8b41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0be3d4
30c8b41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6d020b
 
30c8b41
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import os
import glob
import time
import numpy as np
from os import path
import pandas as pd
import seaborn as sns
import matplotlib.pyplot  as plt
from sklearn.cluster import DBSCAN
from sklearn.preprocessing import StandardScaler
import arrange as DbscanArrange
import directories as Dir

"""

์ด์ „ ํ”„๋กœ์„ธ์Šค:
    image detection์„ ํ†ตํ•ด handwritten text image์—์„œ
    ๊ฐ๊ฐ์˜ word image๋ฅผ ์ถ”์ถœํ–ˆ์œผ๋‚˜,
    ์ˆœ์„œ๋ฅผ ์•Œ์ง€ ๋ชปํ•˜๊ฒŒ ๋‹จ์–ด๋งŒ ํ™•์ธ์ด ๋˜์–ด
    ์›๋ž˜ ๊ธ€์˜ ์˜๋ฏธ๋ฅผ ์žƒ์–ด๋ฒ„๋ฆฌ๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Œ.
    ๋‹ค๋งŒ ์ถ”์ถœ ์‹œ์— ๋‹จ์–ด์˜ ์ขŒํ‘œ๊ฐ’์„ ํฌํ•จ์‹œํ‚ฌ ์ˆ˜ ์žˆ์—ˆ๊ธฐ์—
    ๊ฐ ๋‹จ์–ด์˜ ์ด๋ฆ„์— x,y์ขŒํ‘œ๊ฐ’๊ณผ w,h ์ •๋ณด๋ฅผ ํฌํ•จํ•˜์—ฌ
    ๋‹จ์–ด ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ์ƒ์„ฑ


ํ˜„์žฌ ํ”„๋กœ์„ธ์Šค:
    yolo model์„ ํ†ตํ•ด crop๋œ ์ด๋ฏธ์ง€๋“ค์€
    ํŒŒ์ผ ์ด๋ฆ„์— crop๋˜๊ธฐ ์ „ raw image์—์„œ์˜ ์ขŒํ‘œ ์ •๋ณด๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
    x89y147w199h184.jpg
    x10y148w157h184.jpg
    x28y149w108h180.jpg

    raw imageํŒŒ์ผ์—์„œ ๊ฐ™์€ line์— ์žˆ๋˜ word๋“ค์€
    ๊ฐ€๊นŒ์šด y๊ฐ’์„ ๊ฐ–๊ณ ์žˆ์„ ๊ฒƒ์ด๋ฏ€๋กœ,
    ๋ฐ€๋„๊ธฐ๋ฐ˜ ๊ตฐ์ง‘ํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ
    ๊ฐ™์€ line์— ์žˆ์„ ๊ฒƒ์ด๋ผ๊ณ  ์˜ˆ์ƒํ•˜๋Š” word๊ฐ’๋“ค์€
    ๋™์ผ ํด๋Ÿฌ์Šคํ„ฐ์— ์†ํ•˜๊ฒŒ ๋จ.
    ์ด๋ ‡๊ฒŒ ๋™์ผ ํด๋Ÿฌ์Šคํ„ฐ์— ์†ํ•˜๋Š” y๊ฐ’๋“ค์˜ ํ‰๊ท ์„ ๊ตฌํ•˜์—ฌ
    word์˜ ์ƒˆ๋กœ์šด y๊ฐ’์œผ๋กœ label.
    ๋‹จ์–ด ์ด๋ฏธ์ง€ ํŒŒ์ผ renameํ•  ๋•Œ, y๊ฐ’์ด ๋จผ์ € ์˜ค๊ฒŒ ํ•œ๋‹ค.
    yyyy_xxxx.jpg

    y๊ฐ’์— ๋Œ€ํ•œ ์ •๋ ฌ์ด ๋˜๊ณ 
    x๊ฐ’์— ๋Œ€ํ•ด ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋จ.
    0148_0010.jpg
    0148_0028.jpg
    0148_0089.jpg
    word file์˜ ์ˆœ์„œ๊ฐ€ ์ž๋™์ ์œผ๋กœ ์›๋ž˜ text์˜ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค.

์ฝ”๋“œ ์„ค๋ช…:
    extract_text_from_filename(),
    get_folder_contents_with_text() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด
    ํŒŒ์ผ ์ด๋ฆ„์—์„œ x๊ฐ’๊ณผ y๊ฐ’์„ ์ถ”์ถœ.
    StandardScaler๋กœ ํ‘œ์ค€ํ™”.
    Get Clustered y values using DBSCAN.
    rename_file()์„ ์ด์šฉํ•ด์„œ rename image files with y-clustered values.

"""
# ์‚ฌ์šฉ ์˜ˆ์‹œ
# Get cropped word images
# folder_path = "C:/Users/ban/TEXTAI/yolov5/runs/detect/yujin_paper/crops/word" -> cropped word folder

x_texts, y_texts, name_jpg = DbscanArrange.get_folder_contents_with_text("/home/user/app"+Dir.yolo_dir+Dir.folder_path)
file_name = pd.DataFrame(name_jpg)
file_name.columns=['file_name']

#๋”๋ฏธ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ, ๋„˜ํŒŒ์ด ๋ณ€ํ™˜ ํ›„ reshape (-1, 1)
zero_list = [0 for _ in range(len(y_texts))] #2์ฐจ์› ๋งž์ถ”๊ธฐ ์œ„ํ•œ zero ๋ฆฌ์ŠคํŠธ 
zero_list = np.array([zero_list]).reshape(-1, 1)
y_text = np.array([y_texts]).reshape(-1, 1)
#print('y_text\n', y_text)
#print('zero_list\n', zero_list)


#####################################
# ํ‘œ์ค€ํ™”
scalerX = StandardScaler() # ์Šค์ผ€์ผ ํ•จ์ˆ˜ ๊ฐ€์ ธ์™€์„œ
scalerX.fit(y_text.data) # ์Šค์ผ€์ผ
std_y_text = scalerX.transform(y_text.data).reshape(-1, 1) #์Šค์ผ€์ผ ์™„๋ฃŒ

feature = pd.DataFrame(std_y_text) # ์Šค์ผ€์ผ๋œ ๋„˜ํŒŒ์ด ์ž๋ฃŒํ˜•์„ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜
feature.columns=['feature']

data_list = [std_y_text,zero_list] # ํ‘œ์ค€ํ™”๋œ ๋ฐ์ดํ„ฐ์™€ ๋”๋ฏธ ์‚ฝ์ž…
data = pd.DataFrame(data_list[0])  #
labels = pd.DataFrame(data_list[1])

# ๋ฐ์ดํ„ฐ ์ปฌ๋Ÿผ๋ช… ์„ค์ •
labels.columns=['labels']
data.columns=['y']
# ๋‘ ๋ฐ์ดํ„ฐ์—ด ๋ณ‘ํ•ฉ
datadf = pd.concat([data,labels],axis=1)


###################################
# create model and prediction
model = DBSCAN(eps=0.04,min_samples=2)
predict = pd.DataFrame(model.fit_predict(feature))
predict.columns=['predict']

# file_name, feature, predict ๋ณ‘ํ•ฉ
r = pd.concat([file_name,feature,predict],axis=1)

#r.to_csv('C:\\Users\\ban\\Desktop\\predict_final.csv')
###########################################################



###########################################################
r = r.sort_values(by=['predict'])
#print(type(set(r['predict']))[0])
predict_list = list(set(r['predict'])) # predict ์˜ ์ˆซ์ž๋“ค์„ ์ˆ˜์ง‘
unknown_words = []
same_line = []
whole_word_map = []
final_result = {'y_mean' : [],
                'x_value' : [],
                'file_name' : []}
df_final_result = pd.DataFrame(final_result)
whole_word_map_df = pd.DataFrame(whole_word_map)

for _,line in enumerate(predict_list): # ํ•˜๋‚˜์”ฉ ์ถ”์ถœ -1, 0, 1, 2, ...์ง„์งœ ํž˜๋“ค๋‹ค
    if line >= 0: # predict ๊ฐ’์ด 0 ์ด์ƒ์ด๋ฉด,
        y_list = [] # ํด๋Ÿฌ์ŠคํŒ…๋œ y๊ฐ’๋“ค์˜ ํ‰๊ท ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ๋ฆฌ์ŠคํŠธ
        print(type(r['predict']))
        same_line = r[r['predict'] == line] # r ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์—์„œ X์ธ predict๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์—ด์„ ๊ฐ€์ ธ์™€์„œ
        file_num = 0
        y_mean_column = []
        total_word_map = []
        total_word_map_df = pd.DataFrame(total_word_map)
        for filename in same_line['file_name']: # ๊ฐ™์€ ํด๋Ÿฌ์Šคํ„ฐ์—์„œ ํŒŒ์ผ ํ•˜๋‚˜์”ฉ ๋ฝ‘์•„์„œ
            x_data, y_data = DbscanArrange.extract_text_from_filename(filename) # ํ•ด๋‹น ํŒŒ์ผ์˜ x, y๊ฐ’์„ ๋ฝ‘์•„์„œ
            y_list.append(int(y_data)) # y๊ฐ’ ํ‰๊ท ์„ ์œ„ํ•œ y๊ฐ’ ๋ฆฌ์ŠคํŠธ์— ์‚ฝ์ž…
            #x_file = {x_data:filename} # key๊ฐ’์— x๊ฐ’, value๊ฐ’์— file_name, n_line ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์— ์‚ฝ์ž…
            file_num += 1
            word_map = {'x_value' : [int(x_data)],
                        'file_name' : [filename]}

            word_map_df = pd.DataFrame(word_map)
            total_word_map_df = pd.concat([total_word_map_df, word_map_df])
            total_word_map_df = total_word_map_df.sort_values(by=['x_value'])

        y_mean = int(np.mean(y_list)) # ํ•œ ์ค„์— ๋Œ€ํ•œ ํ‰๊ท ๊ฐ’ ์–ป์Œ #y_mean ์—ด์— ์‚ฝ์ž…
        total_word_map_df['y_mean'] = y_mean # ์ถ”๊ฐ€๋œ ํ–‰ ์ˆ˜๋งŒํผ y_mean ์—ด ์ถ”๊ฐ€

    else:
        total_word_map = []
        same_line = r[r['predict'] == line]
        for filename in same_line['file_name']:
            x_data, y_data = DbscanArrange.extract_text_from_filename(filename)
            unknown_words = [r['predict']==line]
            y_mean = int(y_data)
            word_map = {'y_mean' : [y_mean],
                        'x_value' : [x_data],
                        'file_name' : [filename]}
            word_map_df = pd.DataFrame(word_map)
            total_word_map_df = pd.concat([total_word_map_df, word_map_df])

    whole_word_map_df = pd.concat([whole_word_map_df,total_word_map_df])


file_name = list(whole_word_map_df['file_name'])
x_value = list(whole_word_map_df['x_value'])
y_mean = list(whole_word_map_df['y_mean'])
whole_list = [file_name,x_value,y_mean]


cnum = 0
timestr = time.strftime("%Y%m%d%H%M%S")

#์ „์ฒด ๋ฐ์ดํ„ฐ ์…‹์— ๋Œ€ํ•ด์„œ ํ•˜๋‚˜์”ฉ ์ถ”์ถœํ•˜์—ฌ ํŒจ๋”ฉ ํ›„ ์ด๋ฆ„ ๋ณ€ํ™˜
for i in range(len(file_name)): #_, x_value, file_name, y_mean
    old_path = "/home/user/app"+ str(Dir.yolo_dir) + str(Dir.folder_path) +"/"+ str(file_name[i])
    new_path = "/home/user/app"+ str(Dir.yolo_dir) + str(Dir.folder_path) +"/"+ str(y_mean[i]).zfill(4) +"_"+ str(x_value[i]).zfill(4) + ".jpg"

    DbscanArrange.rename_file(old_path, new_path)

if os.listdir(Dir.folder_path) == True:
    folder_contents = os.listdir(Dir.folder_path)
    print('UNKNOWN WORDS: \n',folder_contents)