Spaces:
Running
Running
Update app.py
Browse filesFix the error due to gradio's version
app.py
CHANGED
|
@@ -1,40 +1,67 @@
|
|
| 1 |
import os
|
| 2 |
os.system('pip install paddlepaddle')
|
| 3 |
os.system('pip install paddleocr')
|
|
|
|
| 4 |
from paddleocr import PaddleOCR, draw_ocr
|
| 5 |
from PIL import Image
|
| 6 |
import gradio as gr
|
| 7 |
-
import torch
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def inference(img, lang):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
title = 'PaddleOCR'
|
| 26 |
description = 'Gradio demo for PaddleOCR. PaddleOCR demo supports Chinese, English, French, German, Korean and Japanese. To use it, simply upload your image and choose a language from the dropdown menu, or click one of the examples to load them. Read more at the links below.'
|
| 27 |
-
article = "<p style='text-align: center'
|
| 28 |
examples = [['example.jpg','en']]
|
| 29 |
css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
|
| 30 |
gr.Interface(
|
| 31 |
inference,
|
| 32 |
-
[gr.
|
| 33 |
-
gr.
|
| 34 |
title=title,
|
| 35 |
description=description,
|
| 36 |
article=article,
|
| 37 |
examples=examples,
|
| 38 |
-
css=css
|
| 39 |
-
|
| 40 |
-
).launch(debug=True)
|
|
|
|
| 1 |
import os
|
| 2 |
os.system('pip install paddlepaddle')
|
| 3 |
os.system('pip install paddleocr')
|
| 4 |
+
import requests
|
| 5 |
from paddleocr import PaddleOCR, draw_ocr
|
| 6 |
from PIL import Image
|
| 7 |
import gradio as gr
|
|
|
|
| 8 |
|
| 9 |
+
def download_image(url, save_path):
|
| 10 |
+
"""
|
| 11 |
+
Download an image from a specified URL and save it to the specified path
|
| 12 |
+
|
| 13 |
+
Args:
|
| 14 |
+
url (str): URL of the image
|
| 15 |
+
save_path (str): Path to save the image
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
bool: True if download is successful, False otherwise
|
| 19 |
+
"""
|
| 20 |
+
try:
|
| 21 |
+
response = requests.get(url, stream=True)
|
| 22 |
+
if response.status_code == 200:
|
| 23 |
+
with open(save_path, 'wb') as file:
|
| 24 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 25 |
+
file.write(chunk)
|
| 26 |
+
print(f"Image successfully downloaded and saved as: {save_path}")
|
| 27 |
+
return True
|
| 28 |
+
else:
|
| 29 |
+
print(f"Download failed, status code: {response.status_code}")
|
| 30 |
+
return False
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"Error occurred during download: {str(e)}")
|
| 33 |
+
return False
|
| 34 |
+
|
| 35 |
+
# Download example image from GitHub
|
| 36 |
+
image_url = "https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/v2.8.0/doc/imgs_en/img_12.jpg"
|
| 37 |
+
download_image(image_url, "example.jpg")
|
| 38 |
|
| 39 |
def inference(img, lang):
|
| 40 |
+
ocr = PaddleOCR(use_angle_cls=True, lang=lang,use_gpu=False)
|
| 41 |
+
img_path = img
|
| 42 |
+
result = ocr.ocr(img_path, cls=True)[0]
|
| 43 |
+
image = Image.open(img_path).convert('RGB')
|
| 44 |
+
boxes = [line[0] for line in result]
|
| 45 |
+
txts = [line[1][0] for line in result]
|
| 46 |
+
scores = [line[1][1] for line in result]
|
| 47 |
+
im_show = draw_ocr(image, boxes, txts, scores,
|
| 48 |
+
font_path='./simfang.ttf')
|
| 49 |
+
im_show = Image.fromarray(im_show)
|
| 50 |
+
im_show.save('result.jpg')
|
| 51 |
+
return 'result.jpg'
|
| 52 |
|
| 53 |
title = 'PaddleOCR'
|
| 54 |
description = 'Gradio demo for PaddleOCR. PaddleOCR demo supports Chinese, English, French, German, Korean and Japanese. To use it, simply upload your image and choose a language from the dropdown menu, or click one of the examples to load them. Read more at the links below.'
|
| 55 |
+
article = "<p style='text-align: center'>Awesome multilingual OCR toolkits based on PaddlePaddle <a href='https://github.com/PaddlePaddle/PaddleOCR'>Github Repo</a></p>"
|
| 56 |
examples = [['example.jpg','en']]
|
| 57 |
css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
|
| 58 |
gr.Interface(
|
| 59 |
inference,
|
| 60 |
+
[gr.Image(type='filepath', label='Input'), gr.Dropdown(choices=['ch', 'en', 'fr', 'german', 'korean', 'japan'], value='en', label='language')],
|
| 61 |
+
gr.Image(type='filepath', label='Output'),
|
| 62 |
title=title,
|
| 63 |
description=description,
|
| 64 |
article=article,
|
| 65 |
examples=examples,
|
| 66 |
+
css=css
|
| 67 |
+
).launch(debug=True)
|
|
|