Spaces:
Runtime error
Runtime error
Ziyue Li
commited on
Commit
•
2089314
1
Parent(s):
ff70fbc
add app
Browse files- .gitignore +188 -0
- Receipt_scanner.ipynb +0 -0
- app.py +105 -0
- requirements.txt +6 -0
.gitignore
ADDED
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# Created by https://www.toptal.com/developers/gitignore/api/macos,python,jupyternotebooks
|
3 |
+
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,python,jupyternotebooks
|
4 |
+
|
5 |
+
### JupyterNotebooks ###
|
6 |
+
# gitignore template for Jupyter Notebooks
|
7 |
+
# website: http://jupyter.org/
|
8 |
+
|
9 |
+
.ipynb_checkpoints
|
10 |
+
*/.ipynb_checkpoints/*
|
11 |
+
|
12 |
+
# IPython
|
13 |
+
profile_default/
|
14 |
+
ipython_config.py
|
15 |
+
|
16 |
+
# Remove previous ipynb_checkpoints
|
17 |
+
# git rm -r .ipynb_checkpoints/
|
18 |
+
|
19 |
+
### macOS ###
|
20 |
+
# General
|
21 |
+
.DS_Store
|
22 |
+
.AppleDouble
|
23 |
+
.LSOverride
|
24 |
+
|
25 |
+
# Icon must end with two \r
|
26 |
+
Icon
|
27 |
+
|
28 |
+
|
29 |
+
# Thumbnails
|
30 |
+
._*
|
31 |
+
|
32 |
+
# Files that might appear in the root of a volume
|
33 |
+
.DocumentRevisions-V100
|
34 |
+
.fseventsd
|
35 |
+
.Spotlight-V100
|
36 |
+
.TemporaryItems
|
37 |
+
.Trashes
|
38 |
+
.VolumeIcon.icns
|
39 |
+
.com.apple.timemachine.donotpresent
|
40 |
+
|
41 |
+
# Directories potentially created on remote AFP share
|
42 |
+
.AppleDB
|
43 |
+
.AppleDesktop
|
44 |
+
Network Trash Folder
|
45 |
+
Temporary Items
|
46 |
+
.apdisk
|
47 |
+
|
48 |
+
### Python ###
|
49 |
+
# Byte-compiled / optimized / DLL files
|
50 |
+
__pycache__/
|
51 |
+
*.py[cod]
|
52 |
+
*$py.class
|
53 |
+
|
54 |
+
# C extensions
|
55 |
+
*.so
|
56 |
+
|
57 |
+
# Distribution / packaging
|
58 |
+
.Python
|
59 |
+
build/
|
60 |
+
develop-eggs/
|
61 |
+
dist/
|
62 |
+
downloads/
|
63 |
+
eggs/
|
64 |
+
.eggs/
|
65 |
+
lib/
|
66 |
+
lib64/
|
67 |
+
parts/
|
68 |
+
sdist/
|
69 |
+
var/
|
70 |
+
wheels/
|
71 |
+
share/python-wheels/
|
72 |
+
*.egg-info/
|
73 |
+
.installed.cfg
|
74 |
+
*.egg
|
75 |
+
MANIFEST
|
76 |
+
|
77 |
+
# PyInstaller
|
78 |
+
# Usually these files are written by a python script from a template
|
79 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
80 |
+
*.manifest
|
81 |
+
*.spec
|
82 |
+
|
83 |
+
# Installer logs
|
84 |
+
pip-log.txt
|
85 |
+
pip-delete-this-directory.txt
|
86 |
+
|
87 |
+
# Unit test / coverage reports
|
88 |
+
htmlcov/
|
89 |
+
.tox/
|
90 |
+
.nox/
|
91 |
+
.coverage
|
92 |
+
.coverage.*
|
93 |
+
.cache
|
94 |
+
nosetests.xml
|
95 |
+
coverage.xml
|
96 |
+
*.cover
|
97 |
+
*.py,cover
|
98 |
+
.hypothesis/
|
99 |
+
.pytest_cache/
|
100 |
+
cover/
|
101 |
+
|
102 |
+
# Translations
|
103 |
+
*.mo
|
104 |
+
*.pot
|
105 |
+
|
106 |
+
# Django stuff:
|
107 |
+
*.log
|
108 |
+
local_settings.py
|
109 |
+
db.sqlite3
|
110 |
+
db.sqlite3-journal
|
111 |
+
|
112 |
+
# Flask stuff:
|
113 |
+
instance/
|
114 |
+
.webassets-cache
|
115 |
+
|
116 |
+
# Scrapy stuff:
|
117 |
+
.scrapy
|
118 |
+
|
119 |
+
# Sphinx documentation
|
120 |
+
docs/_build/
|
121 |
+
|
122 |
+
# PyBuilder
|
123 |
+
.pybuilder/
|
124 |
+
target/
|
125 |
+
|
126 |
+
# Jupyter Notebook
|
127 |
+
|
128 |
+
# IPython
|
129 |
+
|
130 |
+
# pyenv
|
131 |
+
# For a library or package, you might want to ignore these files since the code is
|
132 |
+
# intended to run in multiple environments; otherwise, check them in:
|
133 |
+
# .python-version
|
134 |
+
|
135 |
+
# pipenv
|
136 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
137 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
138 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
139 |
+
# install all needed dependencies.
|
140 |
+
#Pipfile.lock
|
141 |
+
|
142 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
143 |
+
__pypackages__/
|
144 |
+
|
145 |
+
# Celery stuff
|
146 |
+
celerybeat-schedule
|
147 |
+
celerybeat.pid
|
148 |
+
|
149 |
+
# SageMath parsed files
|
150 |
+
*.sage.py
|
151 |
+
|
152 |
+
# Environments
|
153 |
+
.env
|
154 |
+
.venv
|
155 |
+
env/
|
156 |
+
venv/
|
157 |
+
ENV/
|
158 |
+
env.bak/
|
159 |
+
venv.bak/
|
160 |
+
|
161 |
+
# Spyder project settings
|
162 |
+
.spyderproject
|
163 |
+
.spyproject
|
164 |
+
|
165 |
+
# Rope project settings
|
166 |
+
.ropeproject
|
167 |
+
|
168 |
+
# mkdocs documentation
|
169 |
+
/site
|
170 |
+
|
171 |
+
# mypy
|
172 |
+
.mypy_cache/
|
173 |
+
.dmypy.json
|
174 |
+
dmypy.json
|
175 |
+
|
176 |
+
# Pyre type checker
|
177 |
+
.pyre/
|
178 |
+
|
179 |
+
# pytype static type analyzer
|
180 |
+
.pytype/
|
181 |
+
|
182 |
+
# Cython debug symbols
|
183 |
+
cython_debug/
|
184 |
+
|
185 |
+
# End of https://www.toptal.com/developers/gitignore/api/macos,python,jupyternotebooks
|
186 |
+
|
187 |
+
# image
|
188 |
+
*.JPG
|
Receipt_scanner.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import re
|
3 |
+
import pytesseract
|
4 |
+
import numpy as np
|
5 |
+
import gradio as gr
|
6 |
+
import pandas as pd
|
7 |
+
from pytesseract import Output
|
8 |
+
from matplotlib import pyplot as plt
|
9 |
+
|
10 |
+
|
11 |
+
# get grayscale image
|
12 |
+
def get_grayscale(image):
|
13 |
+
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
14 |
+
|
15 |
+
# noise removal
|
16 |
+
def remove_noise(image):
|
17 |
+
return cv2.medianBlur(image,5)
|
18 |
+
|
19 |
+
#thresholding
|
20 |
+
def thresholding(image, thresh_hold=0, which='ostu'):
|
21 |
+
if which == 'ostu':
|
22 |
+
return cv2.threshold(image, thresh_hold, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
|
23 |
+
elif which == 'simple':
|
24 |
+
_, img = cv2.threshold(image,thresh_hold,255,cv2.THRESH_BINARY)
|
25 |
+
return img
|
26 |
+
elif which == 'adaptive':
|
27 |
+
return cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
|
28 |
+
|
29 |
+
#dilation
|
30 |
+
def dilate(image):
|
31 |
+
kernel = np.ones((5,5),np.uint8)
|
32 |
+
return cv2.dilate(image, kernel, iterations = 1)
|
33 |
+
|
34 |
+
#erosion
|
35 |
+
def erode(image):
|
36 |
+
kernel = np.ones((5,5),np.uint8)
|
37 |
+
return cv2.erode(image, kernel, iterations = 1)
|
38 |
+
|
39 |
+
#opening - erosion followed by dilation
|
40 |
+
def opening(image):
|
41 |
+
kernel = np.ones((5,5),np.uint8)
|
42 |
+
return cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
|
43 |
+
|
44 |
+
#canny edge detection
|
45 |
+
def canny(image):
|
46 |
+
return cv2.Canny(image, 100, 200)
|
47 |
+
|
48 |
+
#skew correction
|
49 |
+
def deskew(image):
|
50 |
+
coords = np.column_stack(np.where(image > 0))
|
51 |
+
angle = cv2.minAreaRect(coords)[-1]
|
52 |
+
if angle < -45:
|
53 |
+
angle = -(90 + angle)
|
54 |
+
else:
|
55 |
+
angle = -angle
|
56 |
+
(h, w) = image.shape[:2]
|
57 |
+
center = (w // 2, h // 2)
|
58 |
+
M = cv2.getRotationMatrix2D(center, angle, 1.0)
|
59 |
+
rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
|
60 |
+
return rotated
|
61 |
+
|
62 |
+
#template matching
|
63 |
+
def match_template(image, template):
|
64 |
+
return cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)
|
65 |
+
|
66 |
+
def show_cvimg(img, figsize=(15, 15)):
|
67 |
+
fig, ax = plt.subplots(dpi=80, figsize=figsize)
|
68 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
69 |
+
ax.imshow(img)
|
70 |
+
|
71 |
+
|
72 |
+
def extract_purchased_items(txt):
|
73 |
+
pat = '.*\s*(FB|FA|NB)'
|
74 |
+
p = re.compile("(.*) (\d+[,\.\/\:\']*\d+) (FB|FA|NB)")
|
75 |
+
|
76 |
+
txts = txt.split('\n')
|
77 |
+
items = []
|
78 |
+
not_parsed = []
|
79 |
+
for t in txts:
|
80 |
+
if re.match(pat, t):
|
81 |
+
result = p.search(t)
|
82 |
+
if result is not None:
|
83 |
+
items.append({'item': result.group(1),
|
84 |
+
'price': re.sub('[,\.\/\:\']', '.', result.group(2)),
|
85 |
+
'type': result.group(3)
|
86 |
+
})
|
87 |
+
else:
|
88 |
+
not_parsed.append({'not parsed': t})
|
89 |
+
return pd.DataFrame(items), pd.DataFrame(not_parsed)
|
90 |
+
|
91 |
+
|
92 |
+
def parse_receipt(img, **kwargs):
|
93 |
+
# preprocessing
|
94 |
+
gray = get_grayscale(img)
|
95 |
+
thresh = thresholding(gray, **kwargs)
|
96 |
+
|
97 |
+
# ocr
|
98 |
+
custom_config = r'--oem 3 --psm 6'
|
99 |
+
txt = pytesseract.image_to_string(thresh, config=custom_config)
|
100 |
+
|
101 |
+
return extract_purchased_items(txt)
|
102 |
+
|
103 |
+
|
104 |
+
iface = gr.Interface(fn=parse_receipt, inputs="image", outputs=["dataframe", "dataframe"])
|
105 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
opencv-python
|
2 |
+
pytesseract
|
3 |
+
numpy
|
4 |
+
pandas
|
5 |
+
gradio
|
6 |
+
matplotlib
|