图片 OCR 文字提取 (Python + AI 模型(ModelScope))

模型地址:https://www.modelscope.cn/models

OCR 文字识别(ocr_recognition

创建虚拟环境

bash 复制代码
conda create -n ai_tool python=3.10 -y

进入环境

bash 复制代码
activate ai_tool

安装依赖

bash 复制代码
pip install torch torchvision torchaudio opencv-python pillow streamlit modelscope pyinstaller addict datasets huggingface_hub==0.24.0 simplejson sortedcontainers yapf scipy ftfy regex tqdm pyarrow pandas -i https://pypi.tuna.tsinghua.edu.cn/simple

编写 ocr_tool.py 脚本

bash 复制代码
# ==============================================
# 图片OCR文字提取 EXE 工具(ModelScope 模型)
# 一键打包,双击运行,无环境依赖
# ==============================================
import streamlit as st
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
import cv2
import numpy as np

# 页面配置
st.set_page_config(page_title="OCR文字提取", page_icon="📝")
st.title("📝 图片OCR文字提取工具")
st.subtitle("支持:证件/发票/截图/文档/手写体")

# 缓存模型(只加载一次,速度更快)
@st.cache_resource
def load_ocr_model():
    # 阿里达摩院 最通用、最准的OCR模型
    return pipeline(
        task=Tasks.ocr_recognition,
        model='damo/cv_convnextTiny_ocr-recognition-general_damo'
    )

# 加载模型
ocr = load_ocr_model()

# 上传图片
img_file = st.file_uploader("上传图片", type=["png", "jpg", "jpeg"])

if img_file is not None:
    # 显示图片
    st.image(img_file, caption="上传的图片", use_column_width=True)

    # 点击提取按钮
    if st.button("🔍 开始提取文字"):
        with st.spinner("正在识别中..."):
            # 读取图片
            img = cv2.imdecode(np.frombuffer(img_file.read(), np.uint8), cv2.IMREAD_COLOR)
            
            # OCR识别
            result = ocr(img)
            text_list = result['text']  # 提取所有文字
            
            # 拼接文字
            final_text = "\n".join(text_list)
            
            # 展示结果
            st.success("✅ 识别完成!")
            st.text_area("提取的文字(可直接复制)", final_text, height=300)

启动脚本

bash 复制代码
streamlit run ocr_tool.py

首次下载模型中

测试

相关推荐
自动跟随2 小时前
UWB自动跟随技术全栈解析:从定位算法到“位控一体化“
java·网络·人工智能
AiMagicGaGa3 小时前
被大模型 API 拿捏效率的研发日常
人工智能·aigc
Thecozzy3 小时前
写文档教 AI 用代码
开发语言·python
Hanniel3 小时前
装饰器 (中): 进阶篇,解锁框架级玩法
开发语言·python
康哥爱编程3 小时前
鸿蒙应用开发之应用如何实现腾讯云对象存储?
python·云计算·腾讯云
云道轩3 小时前
腾讯云上有性能比较强的英伟达GPU
人工智能·云计算·腾讯云·英伟达gpu
常宇杏起在3 小时前
AI安全专项:AI密码技术的应用与安全防护
人工智能·安全
不惑_3 小时前
腾讯云WorkBuddy实战, 全场景智能体工作搭子,这只龙虾真能帮你干活吗
人工智能·云计算·腾讯云·openclaw
wanmeijuhao3 小时前
腾讯云 AI 代码助手编程挑战赛 + 构建开发板垃圾图片识别AI对话的Copilot
人工智能·腾讯云·copilot·腾讯云ai代码助手