图片 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

首次下载模型中

测试

相关推荐
Cosolar7 分钟前
当 Harness 标准化后,工程师还剩什么?
人工智能·后端·agent
Wellijohn15 分钟前
大模型推理为什么这么贵?从 KV Cache 到投机解码的一线优化实录
人工智能·性能优化·llm
打工仔折腾 AI28 分钟前
L3 静态源码取证|工程评测:python-oracledb 驱动评估中的样本偏差陷阱
开发语言·人工智能·后端·python·性能优化
无奈何杨30 分钟前
我把一张健身 Excel 表,做成了一个能聊天的健身助手
人工智能
Wellijohn31 分钟前
从 RAG 到 Agent:大模型应用为什么「演示很惊艳,上线就翻车」
人工智能·llm·agent
IvanCodes1 小时前
LangChain(15):LangSmith 应用评估与回归测试
python·langchain·agent
土星云SaturnCloud2 小时前
DirectMHP 边缘部署实战(下):土星云SE110S 三机型 28.98ms 实测——模型编译、精度验证与七条工程化经验
人工智能·边缘计算·lightstereo
炸薯条!2 小时前
Python基础学习:文件操作详解(打开、读取、写入与路径)
java·服务器·python
stereohomology2 小时前
解锁和提升特定型号NVIDia显卡性能的开源代码
人工智能·算力·解锁·nvidia显卡
master3362 小时前
基于 Spring AI + Spring Cloud 构建 Web 版 AI Chat:多模型接入、流式对话与全链路验证
人工智能·spring·spring cloud