Spaces:
Sleeping
Sleeping
tungedng2710
commited on
Upload folder using huggingface_hub
Browse files- README.md +58 -9
- app.py +65 -0
- requirements.txt +92 -0
README.md
CHANGED
@@ -1,13 +1,62 @@
|
|
1 |
---
|
2 |
-
title: TonAI
|
3 |
-
emoji: 📉
|
4 |
-
colorFrom: green
|
5 |
-
colorTo: green
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 4.44.0
|
8 |
app_file: app.py
|
9 |
-
|
10 |
-
|
|
|
11 |
---
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: TonAI-OCR
|
|
|
|
|
|
|
|
|
|
|
3 |
app_file: app.py
|
4 |
+
sdk: gradio
|
5 |
+
colorTo: purple
|
6 |
+
sdk_version: 4.31.2
|
7 |
---
|
8 |
|
9 |
+
# TonAI Text Recognition
|
10 |
+
### A simple OCR Python library
|
11 |
+
|
12 |
+
![ONNX](https://a11ybadges.com/badge?logo=onnx) ![Python](https://a11ybadges.com/badge?logo=python) ![PyPI](https://a11ybadges.com/badge?logo=pypi)
|
13 |
+
|
14 |
+
Text detection with text recognition
|
15 |
+
![](stuffs/demo2.jpg)
|
16 |
+
|
17 |
+
Vietnam military vehicle license plate
|
18 |
+
![](stuffs/ocr_plate.jpg)
|
19 |
+
|
20 |
+
## Installation
|
21 |
+
install via PyPi
|
22 |
+
```
|
23 |
+
pip install ton-ocr
|
24 |
+
```
|
25 |
+
## Usage example
|
26 |
+
|
27 |
+
```
|
28 |
+
import cv2
|
29 |
+
import numpy as np
|
30 |
+
from ton_ocr import TonOCRPipeline
|
31 |
+
|
32 |
+
image_path = "stuffs/example.jpg"
|
33 |
+
image = cv2.imread(image_path)
|
34 |
+
ocr = TonOCRPipeline()
|
35 |
+
results = ocr.predict(image)
|
36 |
+
|
37 |
+
for result in results:
|
38 |
+
bbox = result.box # text bounding box
|
39 |
+
text = result.text # text string
|
40 |
+
score = result.score # OCR's confidence
|
41 |
+
img = result.img # cropped text image
|
42 |
+
|
43 |
+
# Draw the bounding polygon
|
44 |
+
points = np.array(bbox, np.int32)
|
45 |
+
points = points.reshape((-1, 1, 2))
|
46 |
+
color = (0, 255, 255)
|
47 |
+
is_closed = True
|
48 |
+
thickness = 2
|
49 |
+
cv2.polylines(image, [points], is_closed, color, thickness)
|
50 |
+
|
51 |
+
# Add OCR text to the image
|
52 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
53 |
+
font_scale = 1
|
54 |
+
text_color = (0, 0, 255)
|
55 |
+
text_thickness = 2
|
56 |
+
first_point = tuple(points[0][0])
|
57 |
+
cv2.putText(image, text, first_point, font, font_scale, text_color, text_thickness)
|
58 |
+
|
59 |
+
cv2.imshow('Image Window', image)
|
60 |
+
cv2.waitKey(0)
|
61 |
+
cv2.destroyAllWindows()
|
62 |
+
```
|
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
import warnings
|
5 |
+
from ton_ocr import TonOCRPipeline
|
6 |
+
|
7 |
+
# Suppress all warnings
|
8 |
+
warnings.filterwarnings("ignore")
|
9 |
+
|
10 |
+
def clear_image():
|
11 |
+
return None, None
|
12 |
+
|
13 |
+
def ocr_image(image):
|
14 |
+
ocr = TonOCRPipeline()
|
15 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
16 |
+
results = ocr.predict(image)
|
17 |
+
|
18 |
+
for result in results:
|
19 |
+
bbox = result.box # text bounding box
|
20 |
+
text = result.text # text string
|
21 |
+
score = result.score # OCR's confidence
|
22 |
+
img = result.img # cropped text image
|
23 |
+
|
24 |
+
# Draw the bounding polygon
|
25 |
+
points = np.array(bbox, np.int32)
|
26 |
+
points = points.reshape((-1, 1, 2))
|
27 |
+
color = (0, 255, 255)
|
28 |
+
is_closed = True
|
29 |
+
thickness = 2
|
30 |
+
cv2.polylines(image, [points], is_closed, color, thickness)
|
31 |
+
|
32 |
+
# Add OCR text to the image
|
33 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
34 |
+
font_scale = 1
|
35 |
+
text_color = (0, 0, 255)
|
36 |
+
text_thickness = 2
|
37 |
+
first_point = tuple(points[0][0])
|
38 |
+
cv2.putText(image, text, first_point, font, font_scale, text_color, text_thickness)
|
39 |
+
|
40 |
+
# Convert image from BGR to RGB for Gradio
|
41 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
42 |
+
return image
|
43 |
+
|
44 |
+
# Create the Gradio Blocks interface
|
45 |
+
with gr.Blocks(theme=gr.Theme.from_hub("ParityError/Interstellar")) as demo:
|
46 |
+
gr.Markdown("# TonAI OCR - Nhận diện chữ trong ảnh")
|
47 |
+
with gr.Row():
|
48 |
+
with gr.Column():
|
49 |
+
image_input = gr.Image(sources = ['upload', 'clipboard'],
|
50 |
+
label="Input Image")
|
51 |
+
with gr.Row():
|
52 |
+
with gr.Column():
|
53 |
+
submit_button = gr.Button("Submit")
|
54 |
+
with gr.Column():
|
55 |
+
clear_button = gr.Button("Clear")
|
56 |
+
with gr.Column():
|
57 |
+
image_output = gr.Image(type="numpy", label="Output Image")
|
58 |
+
submit_button.click(fn=ocr_image, inputs=image_input, outputs=image_output)
|
59 |
+
clear_button.click(fn=clear_image, outputs=[image_input, image_output])
|
60 |
+
|
61 |
+
# Launch the interface
|
62 |
+
demo.launch(server_name="0.0.0.0",
|
63 |
+
server_port=7862,
|
64 |
+
favicon_path="stuffs/favicon.png",
|
65 |
+
max_threads=99)
|
requirements.txt
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiofiles==23.2.1
|
2 |
+
annotated-types==0.7.0
|
3 |
+
anyio==4.4.0
|
4 |
+
backports.tarfile==1.2.0
|
5 |
+
build==1.2.2
|
6 |
+
certifi==2024.8.30
|
7 |
+
cffi==1.17.1
|
8 |
+
charset-normalizer==3.3.2
|
9 |
+
click==8.1.7
|
10 |
+
coloredlogs==15.0.1
|
11 |
+
contourpy==1.3.0
|
12 |
+
cryptography==43.0.1
|
13 |
+
cycler==0.12.1
|
14 |
+
docutils==0.21.2
|
15 |
+
exceptiongroup==1.2.2
|
16 |
+
fastapi==0.115.0
|
17 |
+
ffmpy==0.4.0
|
18 |
+
filelock==3.16.1
|
19 |
+
flatbuffers==24.3.25
|
20 |
+
fonttools==4.53.1
|
21 |
+
fsspec==2024.9.0
|
22 |
+
gradio==4.44.0
|
23 |
+
gradio_client==1.3.0
|
24 |
+
h11==0.14.0
|
25 |
+
httpcore==1.0.5
|
26 |
+
httpx==0.27.2
|
27 |
+
huggingface-hub==0.25.0
|
28 |
+
humanfriendly==10.0
|
29 |
+
idna==3.10
|
30 |
+
importlib_metadata==8.5.0
|
31 |
+
importlib_resources==6.4.5
|
32 |
+
jaraco.classes==3.4.0
|
33 |
+
jaraco.context==6.0.1
|
34 |
+
jaraco.functools==4.0.2
|
35 |
+
jeepney==0.8.0
|
36 |
+
Jinja2==3.1.4
|
37 |
+
keyring==25.3.0
|
38 |
+
kiwisolver==1.4.7
|
39 |
+
markdown-it-py==3.0.0
|
40 |
+
MarkupSafe==2.1.5
|
41 |
+
matplotlib==3.9.2
|
42 |
+
mdurl==0.1.2
|
43 |
+
more-itertools==10.5.0
|
44 |
+
mpmath==1.3.0
|
45 |
+
nh3==0.2.18
|
46 |
+
numpy==2.1.1
|
47 |
+
onnxruntime-gpu==1.19.2
|
48 |
+
opencv-python==4.10.0.84
|
49 |
+
orjson==3.10.7
|
50 |
+
packaging==24.1
|
51 |
+
pandas==2.2.2
|
52 |
+
pillow==10.4.0
|
53 |
+
pkginfo==1.10.0
|
54 |
+
protobuf==5.28.1
|
55 |
+
pyclipper==1.3.0.post5
|
56 |
+
pycparser==2.22
|
57 |
+
pydantic==2.9.2
|
58 |
+
pydantic_core==2.23.4
|
59 |
+
pydub==0.25.1
|
60 |
+
Pygments==2.18.0
|
61 |
+
pyparsing==3.1.4
|
62 |
+
pyproject_hooks==1.1.0
|
63 |
+
python-dateutil==2.9.0.post0
|
64 |
+
python-multipart==0.0.9
|
65 |
+
pytz==2024.2
|
66 |
+
PyYAML==6.0.2
|
67 |
+
readme_renderer==44.0
|
68 |
+
requests==2.32.3
|
69 |
+
requests-toolbelt==1.0.0
|
70 |
+
rfc3986==2.0.0
|
71 |
+
rich==13.8.1
|
72 |
+
ruff==0.6.5
|
73 |
+
SecretStorage==3.3.3
|
74 |
+
semantic-version==2.10.0
|
75 |
+
shapely==2.0.6
|
76 |
+
shellingham==1.5.4
|
77 |
+
six==1.16.0
|
78 |
+
sniffio==1.3.1
|
79 |
+
starlette==0.38.5
|
80 |
+
sympy==1.13.2
|
81 |
+
tomli==2.0.1
|
82 |
+
tomlkit==0.12.0
|
83 |
+
ton_ocr==0.2.1
|
84 |
+
tqdm==4.66.5
|
85 |
+
twine==5.1.1
|
86 |
+
typer==0.12.5
|
87 |
+
typing_extensions==4.12.2
|
88 |
+
tzdata==2024.1
|
89 |
+
urllib3==2.2.3
|
90 |
+
uvicorn==0.30.6
|
91 |
+
websockets==12.0
|
92 |
+
zipp==3.20.2
|