nyonyong
Directory fix
f6d020b
raw
history blame
7.03 kB
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)