Spaces:
Sleeping
Sleeping
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 | |