Spaces:
Running
Running
Update app.py
Browse filesFix the error due to gradio's version
12345678910111213141516171819202122232425262728293031323334353637383940import osos.system('pip install paddlepaddle')os.system('pip install paddleocr')from paddleocr import PaddleOCR, draw_ocrfrom PIL import Imageimport gradio as grimport torchtorch.hub.download_url_to_file('https://i.imgur.com/aqMBT0i.jpg', 'example.jpg')def inference(img, lang):ocr = PaddleOCR(use_angle_cls=True, lang=lang,use_gpu=False)img_path = img.nameresult = ocr.ocr(img_path, cls=True)[0]image = Image.open(img_path).convert('RGB')boxes = [line[0] for line in result]txts = [line[1][0] for line in result]scores = [line[1][1] for line in result]im_show = draw_ocr(image, boxes, txts, scores,font_path='simfang.ttf')im_show = Image.fromarray(im_show)im_show.save('result.jpg')return 'result.jpg'title = 'PaddleOCR'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.'article = "<p style='text-align: center'><a href='https://www.paddlepaddle.org.cn/hub/scene/ocr'>Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)</a> | <a href='https://github.com/PaddlePaddle/PaddleOCR'>Github Repo</a></p>"examples = [['example.jpg','en']]css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"gr.Interface(inference,[gr.inputs.Image(type='file', label='Input'),gr.inputs.Dropdown(choices=['ch', 'en', 'fr', 'german', 'korean', 'japan'], type="value", default='en', label='language')],gr.outputs.Image(type='file', label='Output'),title=title,description=description,article=article,examples=examples,css=css,enable_queue=True).launch(debug=True)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667import osos.system('pip install paddlepaddle')os.system('pip install paddleocr')import requestsfrom paddleocr import PaddleOCR, draw_ocrfrom PIL import Imageimport gradio as grdef download_image(url, save_path):"""Download an image from a specified URL and save it to the specified pathArgs:url (str): URL of the imagesave_path (str): Path to save the imageReturns:bool: True if download is successful, False otherwise"""try:response = requests.get(url, stream=True)if response.status_code == 200:with open(save_path, 'wb') as file:for chunk in response.iter_content(chunk_size=8192):file.write(chunk)print(f"Image successfully downloaded and saved as: {save_path}")return Trueelse:print(f"Download failed, status code: {response.status_code}")return Falseexcept Exception as e:print(f"Error occurred during download: {str(e)}")return False# Download example image from GitHubimage_url = "https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/v2.8.0/doc/imgs_en/img_12.jpg"download_image(image_url, "example.jpg")def inference(img, lang):ocr = PaddleOCR(use_angle_cls=True, lang=lang,use_gpu=False)img_path = imgresult = ocr.ocr(img_path, cls=True)[0]image = Image.open(img_path).convert('RGB')boxes = [line[0] for line in result]txts = [line[1][0] for line in result]scores = [line[1][1] for line in result]im_show = draw_ocr(image, boxes, txts, scores,font_path='./simfang.ttf')im_show = Image.fromarray(im_show)im_show.save('result.jpg')return 'result.jpg'title = 'PaddleOCR'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.'article = "<p style='text-align: center'>Awesome multilingual OCR toolkits based on PaddlePaddle <a href='https://github.com/PaddlePaddle/PaddleOCR'>Github Repo</a></p>"examples = [['example.jpg','en']]css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"gr.Interface(inference,[gr.Image(type='filepath', label='Input'), gr.Dropdown(choices=['ch', 'en', 'fr', 'german', 'korean', 'japan'], value='en', label='language')],gr.Image(type='filepath', label='Output'),title=title,description=description,article=article,examples=examples,css=css).launch(debug=True)
app.py+47-20