-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
66 lines (46 loc) · 1.51 KB
/
app.py
File metadata and controls
66 lines (46 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from typing import Union
from fastapi import FastAPI, File, UploadFile
from PIL import Image
import pytesseract
import uuid
import os
import imutils
from imutils.perspective import four_point_transform
from imutils.contours import sort_contours
import cv2
import re
import requests
from fastapi.middleware.cors import CORSMiddleware
UPLOAD_DIR = "./images"
CORS_ORIGINS = ["*", "http://localhost:9031", "https://scrap.devent.kr"]
app = FastAPI()
oem = 3
psm = 6
app.add_middleware(
CORSMiddleware,
allow_origins=CORS_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/recognizetest/{item_id}")
def read_root(item_id: str):
print(Image.open(item_id))
g = pytesseract.image_to_string(Image.open(item_id), lang='kor+eng')
print(g)
return {"data": g}
@app.post("/recognize")
async def read_root(file: UploadFile):
print(file.filename)
content = await file.read()
filename = f"{str(uuid.uuid4())}.jpg"
with open(os.path.join(UPLOAD_DIR, filename), "wb") as fp:
fp.write(content)
savedFilename = os.path.join(UPLOAD_DIR, filename)
receipt = cv2.imread(savedFilename, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(receipt, cv2.COLOR_BGR2GRAY)
rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 20))
gray = cv2.GaussianBlur(gray, (11, 11), 0)
blackhat = cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, rectKernel)
text = pytesseract.image_to_string(blackhat, lang='kor+eng')
return {"data": text}