Files
ocr-python/app/api/ocr.py
2025-07-07 12:50:10 +09:00

18 lines
613 B
Python

from fastapi import APIRouter, UploadFile, File
from PIL import Image
import io
import pytesseract
router = APIRouter()
@router.post("/")
async def process_ocr(file: UploadFile = File(...)) -> str:
#받아온 파일 byte stream으로 읽기
image_byte = await file.read()
#byte stream을 image파일로 변환
image = Image.open(io.BytesIO(image_byte))
#image > txt(ocr)
text = pytesseract.image_to_string(image=image,
lang='kor+eng',
config='--psm 6 -c preserve_interword_spaces=1')
return text.strip()