EasyOCR๋ฅผ ํ์ฉํด ์คํฌ๋ฆฐ์ท์ ํ ์คํธ๋ก ์ ํ
import easyocr
# EasyOCR Reader ์์ฑ
reader = easyocr.Reader(['en', 'ko']) # ์์ด์ ํ๊ตญ์ด ์ง์
# ์ด๋ฏธ์ง์์ ํ
์คํธ ์ถ์ถ
result = reader.readtext('C:\\Users\\dasom\\Desktop\\google api\\sample.png')
# ํ
์คํธ ์ถ๋ ฅ
for detection in result:
print(detection[1]) # ํ
์คํธ ๋ถ๋ถ๋ง ์ถ๋ ฅ
๋ฅ๋ฌ๋ ๊ธฐ๋ฐ EasyOCR๋ฅผ ์ฌ์ฉํด๋ณด์์ต๋๋ค.


Tesseract OCR๋ณด๋ค๋ ์ ๋์ค์ง๋ง ์ฌ์ ํ ์ฑ๋ฅ์ด ๊ตฌ๋ฆฝ๋๋ค.
์ด์ ๋จ์ ๋ฐฉ๋ฒ์ Google Cloud Vision API ๋๋ chat gpt๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
์ฒ์์ ๋ฅ๋ฌ๋์ ์ด์ฉํด ๋ชจ๋ธ์ ๋ง๋ค๊ณ ํ ์คํธ ์ ํ ์ฑ๋ฅ์ด ๋์ด๋ ๋ชจ๋ธ์ ์ฐ๋ ค๊ณ ํ๋๋ฐ์.
๊ทธ๋ฌ๋ ค๋จผ ์๋ฒฝํ ํ ์คํธ ํ์ผ์ด ์กด์ฌํ๊ณ ๊ทธ๊ฑธ๋ก ๋ชจ๋ธ ํ๋ จ์ ํด์ผ๋ง ํฉ๋๋ค.
ํ์ง๋ง ํ ์คํธ ํ์ผ์ด ์กด์ฌํ์ง ์์ผ๋ ๋ง์ง ์๋ ๋ฐฉ๋ฒ์ธ๊ฑฐ ๊ฐ์์ google cloud vision api๋ฅผ ์ด์ฉํด ๋ณด๋ ค ํฉ๋๋ค.
์ฌ์ค ๊ฐ์ฅ ๋น ๋ฅด๊ณ ์์ฌ์ด ๋ฐฉ๋ฒ์ chat gpt์ด๊ธด ํ์ง๋ง ๊ณต๋ถํ๋ ค๊ณ ํ๋ ํ๋ก์ ํธ๋๊น api๋ฅผ ์ด์ฉํด ๋ณด๊ฒ ์ต๋๋ค ใ ใ ..
Google Cloud Vision API๋ฅผ ํ์ฉํด ์คํฌ๋ฆฐ์ท์ ํ ์คํธ๋ก ์ ํ
import io
import os
from google.cloud import vision
# ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํ๋ ๋ฐฉ๋ฒ
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r"C:\Users\user\Desktop\google api\emoticon.json"
# Google Cloud Vision API ํด๋ผ์ด์ธํธ ์์ฑ
client = vision.ImageAnnotatorClient()
# ์ด๋ฏธ์ง ํ์ผ ๊ฒฝ๋ก
image_path = 'C:\\Users\\user\\Desktop\\google api\\sample.png'
# ์ด๋ฏธ์ง ์ฝ๊ธฐ
with io.open(image_path, 'rb') as image_file:
content = image_file.read()
# ์ด๋ฏธ์ง ๋ฐ์ดํฐ ์ค์
image = vision.Image(content=content)
# ์ด๋ฏธ์ง์์ ํ
์คํธ ๊ฐ์ง
response = client.text_detection(image=image)
texts = response.text_annotations
# ์ถ์ถ๋ ํ
์คํธ ์ถ๋ ฅ
for text in texts:
print(f"Detected text: {text.description}")
# ์๋ฌ ํธ๋ค๋ง
if response.error.message:
raise Exception(f'{response.error.message}')
Googl Cloud vision api๋ ํ๋ฌ์ 1000ํ์ ์ ํํ์ฌ ๋ฌด๋ฃ์ ๋๋ค.
์ ๊ท ์ฌ์ฉ์๋ ํ๋ฌ์ 30$ ์ดํ๊น์ง๋ ๋ฌด๋ฃ๋ผ๊ณ ํด์ google cloud ๊ณ์ ์์ฑ ํ ํ๋ก์ ํธ๋ฅผ ๋ง๋ค์์ต๋๋ค.
๊ถํ์ ๋ถ์ฌํ๊ณ emotcion.json ํ์ผ์ ๋ค์ด๋ฐ์์ ํ๊ฒฝ๋ณ์๋ก ์ค์ ํด์คฌ์ต๋๋ค!!
ํด๋น ๋ฐฉ๋ฒ์ ๊ตฌ๊ธ์ ๊ฒ์ํ์๋ฉด ์์ธํ๊ฒ ๋์ค๋ ์ค๋ช ์ ์๋ตํ๊ฒ ์ต๋๋ค.
๊ทธ ํ , ์ ์ฝ๋๋ฅผ ์์ฑํด์ ์ถ์ถํด ๋ณด์์ต๋๋ค.


์์ฃผ์์ฃผ ์ ๋๋ค์ ๋ง์กฑ์ค๋ฝ์ต๋๋ค.
์ฝ๋๋ฅผ ์กฐ๊ธ ์์ ํด์ ํ์ค ๋จ์๋ก ์ ์ฅํ๋ ค๊ณ ํฉ๋๋ค.
import io
import os
import pandas as pd
from google.cloud import vision
# Google Cloud Vision API ํด๋ผ์ด์ธํธ ์์ฑ
client = vision.ImageAnnotatorClient()
# ์ด๋ฏธ์ง ํ์ผ ๊ฒฝ๋ก
image_path = 'C:\\Users\\user\\Desktop\\google api\\sample2.png'
# ์ด๋ฏธ์ง ์ฝ๊ธฐ
with io.open(image_path, 'rb') as image_file:
content = image_file.read()
# ์ด๋ฏธ์ง ๋ฐ์ดํฐ ์ค์
image = vision.Image(content=content)
# ์ด๋ฏธ์ง์์ ํ
์คํธ ๊ฐ์ง
response = client.text_detection(image=image)
texts = response.text_annotations
# ์ฒซ ๋ฒ์งธ ํ
์คํธ(์ ์ฒด ํ
์คํธ)๋ฅผ ์ค ๋จ์๋ก ๋ถ๋ฆฌ
if texts:
full_text = texts[0].description # ์ฒซ ๋ฒ์งธ ํญ๋ชฉ์ด ์ ์ฒด ํ
์คํธ
lines = full_text.split('\n') # ์ค๋ฐ๊ฟ(\n)์ ๊ธฐ์ค์ผ๋ก ํ
์คํธ ๋ถ๋ฆฌ
# ์ค ๋จ์๋ก ๋ถ๋ฆฌ๋ ํ
์คํธ๋ฅผ ๋ฐ์ดํฐํ๋ ์์ผ๋ก ๋ณํ
df = pd.DataFrame({'Detected Text': lines})
# ๋ฐ์ดํฐํ๋ ์ ์ถ๋ ฅ
print(df)
# ์๋ฌ ํธ๋ค๋ง
if response.error.message:
raise Exception(f'{response.error.message}')
์ด๋ ๊ฒ ํ๋ฉด ํ ์คํธ๊ฐ ํ์ค ๋จ์๋ก ์ ์ฅ๋ฉ๋๋ค.
๊ทผ๋ฐ ๊น๋ํ ๋ฐฉ์์ผ๋ก ์ ์ฅ์ด ๋์ง ์์ต๋๋ค.
์ด๋ฏธ์ง์์ ํ ์คํธ๋ฅผ ์ถ์ถํ์ฌ ์ค ๋จ์๋ก ์ธ์ํ ํ, ๊ฐ ์ค์ ๋ณ๋๋ก ์ถ๋ ฅํ๋ ๋ฐฉ์์ผ๋ก ์ฝ๋๋ฅผ ์์ ํ ์ ์์ต๋๋ค. Google Cloud Vision API๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ค ๋จ์๋ก ํ ์คํธ๋ฅผ ์ถ์ถํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ฏ๋ก ์ด๋ฅผ ํ์ฉํ ์ ์์ต๋๋ค.
ํด๋น ๊ธฐ๋ฅ์ ํ์ฉํ์ฌ ํ ์คํธ ์ถ์ถ > ์ค ๋จ์๋ก ์ ์ฅ์ด ์๋๋ผ
ํ ์คํธ ์ถ์ถ์ ์ค ๋จ์๋ก ์ถ์ถ > ์ค ๋จ์๋ก ์ ์ฅ ๋ฐฉ์์ผ๋ก ์ฝ๋๋ฅผ ์กฐ๊ธ ์์ ํ๊ฒ ์ต๋๋ค.
import io
import os
import pandas as pd
from google.cloud import vision
# Google Cloud Vision API ํด๋ผ์ด์ธํธ ์์ฑ
client = vision.ImageAnnotatorClient() # Google Cloud Vision API์์ ์ํธ์์ฉ์ ์ํด ํด๋ผ์ด์ธํธ๋ฅผ ์์ฑํฉ๋๋ค. ์ด ํด๋ผ์ด์ธํธ๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง๋ฅผ Vision API๋ก ๋ณด๋ด๊ณ ํ
์คํธ๋ฅผ ๋ถ์ํ ์ ์์ต๋๋ค.
# ์ด๋ฏธ์ง ํ์ผ ๊ฒฝ๋ก
image_path = 'C:\\Users\\user\\Desktop\\google api\\sample4.png' # ์ด๋ฏธ์ง ํ์ผ์ ๊ฒฝ๋ก์
๋๋ค. ์ด ๊ฒฝ๋ก๋ ์ด๋ฏธ์ง๊ฐ ์ ์ฅ๋ ์์น๋ฅผ ๊ฐ๋ฆฌํต๋๋ค.
# ์ด๋ฏธ์ง ์ฝ๊ธฐ
with io.open(image_path, 'rb') as image_file: # rb ๋ชจ๋๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง๋ฅผ ๋ฐ์ด๋๋ฆฌ๋ก ์ฝ์ต๋๋ค. io.open()์ ์ด๋ฏธ์ง ํ์ผ์ ์ฝ๊ธฐ ์ํด ์ฌ์ฉ๋ฉ๋๋ค.
content = image_file.read() # ํ์ผ์ ๋ด์ฉ์ ์ฝ์ด์์ content ๋ณ์์ ์ ์ฅํฉ๋๋ค. ์ด ๋ด์ฉ์ Vision API๋ก ์ ์ก๋ ์ด๋ฏธ์ง์ ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ์
๋๋ค.
# ์ด๋ฏธ์ง ๋ฐ์ดํฐ ์ค์
image = vision.Image(content=content) # Vision API์์ ์ฌ์ฉํ ์ ์๋ ์ด๋ฏธ์ง ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค. content ๋ณ์์๋ ๋ฐ์ด๋๋ฆฌ ์ด๋ฏธ์ง ๋ฐ์ดํฐ๊ฐ ๋ค์ด๊ฐ ์์ต๋๋ค.
# ์ด๋ฏธ์ง์์ ํ
์คํธ ๊ฐ์ง
response = client.text_detection(image=image) # Vision API์ ํ
์คํธ ๊ฐ์ง๋ฅผ ์์ฒญํฉ๋๋ค. ์ด ์์ฒญ์ ์ด๋ฏธ์ง์์ ํ
์คํธ๋ฅผ ์ฐพ์์ ์๋ต(response) ๊ฐ์ฒด๋ก ๋ฐํํฉ๋๋ค. response์๋ ๊ฐ์ง๋ ํ
์คํธ ์ ๋ณด๊ฐ ๋ค์ด ์์ต๋๋ค.
texts = response.text_annotations
# ๊ฐ ํ
์คํธ ์ค ๋จ์๋ก ๋ถ๋ฆฌ
if texts: # ํ
์คํธ๊ฐ ๊ฐ์ง๋์๋์ง ํ์ธํฉ๋๋ค. ๋ง์ฝ ํ
์คํธ๊ฐ ๊ฐ์ง๋์ง ์์๋ค๋ฉด ์ดํ ์ฝ๋๊ฐ ์คํ๋์ง ์์ต๋๋ค.
# ์ฒซ ๋ฒ์งธ ํ
์คํธ ํญ๋ชฉ์ ์ ์ฒด ํ
์คํธ๊ฐ ํฌํจ๋จ
full_text = texts[0].description # ์ ์ฒด ํ
์คํธ๋ texts[0].description์ ์ ์ฅ๋์ด ์์ต๋๋ค. ์ด ํ
์คํธ๋ ์ด๋ฏธ์ง์์ ๊ฐ์ง๋ ๋ชจ๋ ํ
์คํธ์
๋๋ค.
lines = full_text.split('\n') # ํ
์คํธ๋ฅผ ์ค๋ฐ๊ฟ(\n)์ ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌํ์ฌ ๊ฐ ์ค์ lines ๋ฆฌ์คํธ๋ก ๋ง๋ญ๋๋ค.
# ์ค ๋จ์๋ก ๋ถ๋ฆฌ๋ ํ
์คํธ๋ฅผ ๋ฐ์ดํฐํ๋ ์์ผ๋ก ๋ณํ
df = pd.DataFrame({'Detected Text': lines}) # ๋ถ๋ฆฌ๋ ํ
์คํธ ์ค์ ๋ฐ์ดํฐํ๋ ์์ผ๋ก ๋ณํํฉ๋๋ค. ๋ฐ์ดํฐํ๋ ์์ ํ ์ด(Detected Text)๋ก ๊ตฌ์ฑ๋๋ฉฐ, ๊ฐ ํ์ ๊ฐ์ง๋ ํ
์คํธ์ ํ ์ค์ ๋ํ๋
๋๋ค.
# ๋ฐ์ดํฐํ๋ ์ ์ถ๋ ฅ
print(df)
# ์๋ฌ ํธ๋ค๋ง
if response.error.message:
raise Exception(f'{response.error.message}')
์ฃผ์์ ๋จ๊ฒจ์ ์ฝ๋์ ๋ํ ํด์ค์ ๋ฌ์์ต๋๋ค.
์ฝ๋์ ๋ํ ์ฃผ์์ ๊ณ์ ๋จ๊ธฐ๋ ์ด์ ๋ ๋ค๋ฅธ ๋ถ๋ค์ด ์ดํดํ๊ธฐ ํธํจ๋ ์์ง๋ง,
๋์ค์ ์ ๊ฐ ๋ค์ ๋ณผ๋ ์ ์ด๋ฐ ์ฝ๋๋ฅผ ์ผ์ง? ํ๋ ๊ฒฝ์ฐ๊ฐ ์๊ธฐ ์ํด ๋๋ถ๋ถ ์์ฑํ๋ ํธ์ ๋๋ค.


์์ฃผ ์ ์ถ์ถ๋ฉ๋๋ค.
์ด ๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํด๋ด ์๋ค.
ํด๋น ๋ฐ์ดํฐ๋ฅผ csv๋ก ๋ฐ๊พผ๋ค์์ ์ค๊ฐ ์ค๊ฐ์ ์ ๋๋ก ๋ณํํ์ง ๋ชปํ ๊ฒ๋ค์ ํ์ธํ์ฌ ์์์ ์ผ๋ก ๋ณํํด์ฃผ์์ต๋๋ค.
# ์ง์ ํ ์นผ๋ผ๋ช
๋ฆฌ์คํธ
column_names = ['Text']
left = pd.read_csv('C:\\Users\\user\\Desktop\\analysis\\project\emoticon\\199.csv', encoding='utf-8-sig', header=None, names=column_names)
# ์ง์ ํ ์นผ๋ผ๋ช
๋ฆฌ์คํธ
column_names = ['Text']
right = pd.read_csv('C:\\Users\\user\\Desktop\\analysis\\project\emoticon\\200.csv', encoding='utf-8-sig', header=None, names=column_names)
import pandas as pd
# DataFrame์ผ๋ก ๋ถ๋ฌ์์ ๋ ์ด์ ๊ฐ์ ๊ตฌ์กฐ์ผ ์ ์์ต๋๋ค.
left_df = pd.DataFrame(left, columns=["Text"])
right_df = pd.DataFrame(right, columns=["Text"])
# ์ง์ ํญ๋ชฉ(์ด๋ชจํฐ์ฝ)๊ณผ ํ์ ํญ๋ชฉ(์๊ฐ๋ช
) ๋ถ๋ฆฌ
emoticons = left_df.iloc[::2].reset_index(drop=True) # ์ง์ ์ธ๋ฑ์ค ํญ๋ชฉ (์ด๋ชจํฐ์ฝ)
authors = left_df.iloc[1::2].reset_index(drop=True) # ํ์ ์ธ๋ฑ์ค ํญ๋ชฉ (์๊ฐ๋ช
)
emoticons2 = right_df.iloc[::2].reset_index(drop=True) # ์ง์ ์ธ๋ฑ์ค ํญ๋ชฉ (์ด๋ชจํฐ์ฝ)
authors2 = right_df.iloc[1::2].reset_index(drop=True) # ํ์ ์ธ๋ฑ์ค ํญ๋ชฉ (์๊ฐ๋ช
)
# ์๋ก์ด DataFrame ๊ตฌ์ฑ
result_df = pd.DataFrame({
"์ด๋ชจํฐ์ฝ": emoticons["Text"],
"์๊ฐ๋ช
": authors["Text"]
})
result_df2 = pd.DataFrame({
"์ด๋ชจํฐ์ฝ": emoticons2["Text"],
"์๊ฐ๋ช
": authors2["Text"]
})
# ๊ฒฐ๊ณผ ์ถ๋ ฅ
print(result_df)
result_df['์์'] = [i for i in range(1, 2*len(result_df), 2)] # 1๋ถํฐ ์์ํ๋ ํ์ ๋ฆฌ์คํธ ์์ฑ
result_df2['์์'] = [i for i in range(2, 2*len(result_df2)+1, 2)] # 2๋ถํฐ ์์ํ๋ ํ์ ๋ฆฌ์คํธ ์์ฑ
result = pd.concat([result_df, result_df2], ignore_index=True)
result_sorted = result.sort_values(by='์์')
result_sorted_reset = result_sorted.reset_index(drop=True)
๊ทธ ๋ค์์ ๋ฐ์ดํฐ๋ฅผ ํ๋๋ก ํฉ์ณค์ต๋๋ค.
๋ค์์ ํด์ผ ํ ๊ฒ์ ํ๋์ฉ ๋ฐ์ดํฐ๋ฅผ ์์์ ์ผ๋ก ์์งํด์ผ ํฉ๋๋ค.