Home  >  Article  >  Backend Development  >  How to use Gradio and EasyOCR to build a web application for online text recognition in Python

How to use Gradio and EasyOCR to build a web application for online text recognition in Python

王林
王林forward
2023-05-18 15:58:061572browse

1. What is Gradio

Gradio is an open source Python library for building machine learning and data science demonstrations and web applications.

Official website: https://www.gradio.app/

Gradio is suitable for:

  • Demonstration customers/collaborators/users/students Machine learning model.

  • Deployment Quickly create models through automatic sharing links and get feedback on model performance.

  • Troubleshooting Work interactively with your models during development using built-in manipulation and interpretation tools.

Install the gradient library

pip install gradio -i https://pypi.tuna.tsinghua.edu.cn/simple

2. Preparation work for EasyOCR

To use EasyOCR, you need to install pytorch. You can also use easyocr secretly. Drive the installation of torch related libraries.

pip install easyocr  -i https://pypi.tuna.tsinghua.edu.cn/simple

Because EasyOCR uses the trained algorithm, after installing the above library, you need to go to the official website https://www.jaided.ai/easyocr/modelhub/ to download the corresponding trained model file. Mainly the following three files, and extract the files to the C:\Users\Administrator.EasyOCR\model directory. Administrator is the login user name, modify it according to your own situation.

How to use Gradio and EasyOCR to build a web application for online text recognition in Python

How to use Gradio and EasyOCR to build a web application for online text recognition in Python

If you don’t encounter the following two problems, it’s best to solve them according to the method.

Note 1: If there are multiple python environments and an installation error occurs, you can add the user parameter to install to the user directory.

pip install easyocr -i https://pypi.tuna.tsinghua.edu.cn/simple --user

Note 2: If the following prompt appears:

OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
This is because the torch package contains a file named libiomp5md.dll, which is different from the Anaconda environment There is some kind of conflict with the same file, so one needs to be deleted. I renamed libiomp5md.dll under \Anaconda3\Library\bin\ to libiomp5md_old.dll.

3. Use Gradio and easyocr to build a Web application for online text recognition

After completing the above preparations, it’s time to witness the miracle.

import gradio as gr
import easyocr
import cv2
reader = easyocr.Reader(['ch_sim','en'])
def img2txt(image):
    img = r"C:\text.jpg"
    cv2.imwrite(img, image)
    img_read = cv2.imread(img)
    res = reader.readtext(img_read)
    print('识别结果为:',res)
    txt = ''
    if len(res)>0:
        for i in res:
            txt += i[1]
    return txt


interface = gr.Interface(fn=img2txt, inputs="image", outputs="text")
interface.launch()

The following picture is shown after running:

How to use Gradio and EasyOCR to build a web application for online text recognition in Python

Upload a picture and try the effect, as shown below:

How to use Gradio and EasyOCR to build a web application for online text recognition in Python

The above is the detailed content of How to use Gradio and EasyOCR to build a web application for online text recognition in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete