使用 Gradio 和 Hugging Face 在 Lines 下使用 Python 代码构建文本提取器应用程序

霞舞
发布: 2024-10-30 21:24:40
转载
488人浏览过

使用 gradio 和 hugging face 在 lines 下使用 python 代码构建文本提取器应用程序

原帖:https://baxin.netlify.app/build-text-extractor-python-under-30-lines/

从图像中提取文本,称为光学字符识别 (ocr),对于文档处理、数据提取和可访问性应用程序来说是一项很有价值的功能。在本指南中,我们将使用 python 库创建一个 ocr 应用程序,例如用于 ocr 的 pytesseract、用于图像处理的 pillow 以及用于构建交互式 ui 的 gradio。我们将在 hugging face spaces 上部署此应用程序。

先决条件

开始之前,您需要一个 hugging face 帐户并对 docker 有基本的了解。

分步指南

第 1 步:创建一个拥抱脸部空间

  1. 导航到 hugging face spaces:登录 hugging face 并转到“spaces”部分。
  2. 创建一个新空间
    • 点击“新空间”。
    • 为您的空间命名(例如,图像文本提取器)。
    • 选择gradio作为sdk并设置可见性(公共或私有)。
    • 点击“创建空间”。

第 2 步:创建 dockerfile

要在具有所需系统依赖项的 hugging face spaces 上部署(例如用于 ocr 的 tesseract),我们需要一个用于配置环境的 dockerfile。

立即学习Python免费学习笔记(深入)”;

创建一个包含以下内容的 dockerfile:

# use an official python runtime as a parent image
from python:3.12
env pip_root_user_action=ignore

# set the working directory in the container
workdir $home/app

# install system dependencies
run apt-get update && apt-get install -y
run apt-get install -y tesseract-ocr
run apt-get install -y libtesseract-dev
run apt-get install -y libgl1-mesa-glx
run apt-get install -y libglib2.0-0
run pip install --upgrade pip

# copy requirements and install dependencies
copy requirements.txt requirements.txt
run pip install --no-cache-dir -r requirements.txt

# copy the app code
copy app.py ./

# expose the port for gradio
expose 7860

# run the application
cmd ["python", "app.py"]
登录后复制

第 3 步:创建 ocr 应用程序

  1. 创建一个名为 app.py 的文件,其中包含以下内容:
import gradio as gr
import pytesseract
from pil import image
import os

def extract_text(image_path):
    if not image_path:
        return "no image uploaded. please upload an image."

    if not os.path.exists(image_path):
        return f"error: file not found at {image_path}"

    try:
        img = image.open(image_path)
        text = pytesseract.image_to_string(img)
        return text if text.strip() else "no text detected in the image."
    except exception as e:
        return f"an error occurred: {str(e)}"

iface = gr.interface(
    fn=extract_text,
    inputs=gr.image(type="filepath", label="upload an image"),
    outputs=gr.textbox(label="extracted text"),
    title="image text extractor",
    description="upload an image and extract text from it using ocr."
)

iface.launch(server_name="0.0.0.0", server_port=7860)
登录后复制
  1. 创建requirements.txt文件来指定依赖项:
gradio
pytesseract
Pillow
登录后复制

此设置包括:

  • 图像上传:gr.image(type="filepath") 允许用户将图像作为文件路径上传,由 pytesseract 处理。
  • 文本提取:pytesseract.image_to_string 从图像中提取文本。
  • 用户界面:gradio 生成一个简单的 ui,供用户上传图像和查看提取的文本。

第 4 步:将所有文件推送到拥抱面部空间

创建所有文件后,将它们推送到您的拥抱空间

以上就是使用 Gradio 和 Hugging Face 在 Lines 下使用 Python 代码构建文本提取器应用程序的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号