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

首次下载模型中

测试

相关推荐
circuitsosk4 小时前
NL2SQL在工业级场景下的精度优化:Schema Linking + 动态Few-shot实战
人工智能·python·sql·大模型·nl2sql
9000AI4 小时前
9000AI如何工业化生产流量?高质量规模生产与矩阵化饱和覆盖
人工智能
字节数据平台5 小时前
iDA:从 ChatBI 到专业数据分析助手的演进之路
大数据·人工智能·机器学习·数据分析
warpdrivelabs5 小时前
Codex 开源 harness 全面了解
开发语言·人工智能
mit6.8245 小时前
微软如何交付企业级Agent
人工智能
Mininglamp_27185 小时前
明略科技携手海康机器人亮相世界机器人大会,以“Agent+具身“联合进入商业机器人场景
人工智能·科技·机器人·开源·agent·ai agent
2401_894915535 小时前
GEO 优化源码全解析:从搜索引擎到 AI 引擎的底层改写逻辑
java·服务器·前端·数据库·人工智能·分布式·搜索引擎
MobotStone7 小时前
从“听得懂”到“干得了”:工业大模型落地工厂的三层进化路线
人工智能
wangruofeng8 小时前
GLM-5.3-Flash 发布:追平 Opus 4.8 的智力,1/40 的价格,跑在国产芯片上
人工智能·aigc·chatglm (智谱)
皮皮虾❀9 小时前
钉钉AI服务商×教育行业:AI现代产业学院共建一体化实战——从“高校AI教学缺平台”到“校企共建AI产业学院培养数字化人才
人工智能·钉钉