InvoHide_Inisw_teem8 / recognize.py
esun-choi's picture
Initial Commit
8804c8f
raw
history blame
1.02 kB
from transformers import TrOCRProcessor, VisionEncoderDecoderModel, AutoTokenizer
import unicodedata
# huggingface ์—์„œ trocr ๋ชจ๋ธ weight์„ ๊ฐ€์ ธ์˜ค๊ณ  ํ•ด๋‹น weight์„ fine tuning ํ•˜์—ฌ์„œ trocr_weight folder์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. (tokenizer, processor๋„ ๊ฐ™์ด์ €์žฅ)
# recognize๊ฐ€ ๋ฐ›๋Š” ์ด๋ฏธ์ง€๋Š” ์†ก์žฅ๋‚ด์—์„œ craft๋กœ ํฌ๋กญ๋œ ๋ถ€๋ถ„์ด๊ณ  text๊ฐ€ ์žˆ๋Š”๊ณณ์œผ๋กœ ์ถ”์ •๋˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.
# ํ•ด๋‹น ์˜์—ญ์—์„œ ์žˆ์„๋ฒ•ํ•œ text๋‚ด์šฉ์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.
def recongize(img):
processor = TrOCRProcessor.from_pretrained("trocr_weight")
model = VisionEncoderDecoderModel.from_pretrained("trocr_weight")
tokenizer = AutoTokenizer.from_pretrained("trocr_weight")
pixel_values = processor(img, return_tensors="pt").pixel_values
generated_ids = model.generate(pixel_values, max_length=64)
generated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
generated_text = unicodedata.normalize("NFC", generated_text)
return generated_text