Python基于百度API识别并提取图片中文字

作者:XnCSD 时间:2023-02-06 13:51:25 

利用百度 AI 开发平台的 OCR 文字识别 API 识别并提取图片中的文字。首先需注册获取 API 调用的 ID 和 key,步骤如下:

打开百度AI开放平台,进入控制台中的文字识别应用(需要有百度账号)。

Python基于百度API识别并提取图片中文字

创建一个应用,并进入管理应用,记下 AppID, API Key, Secrect Key,调用 API需用到。

Python基于百度API识别并提取图片中文字

Python基于百度API识别并提取图片中文字

最后安装 python 的百度ai接口的的库


pip install baidu-aip

以下是代码实现,需将所有识别的图片放进名为 picture 的文件夹。


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 12 09:37:38 2018
利用百度api实现图片文本识别
@author: XnCSD
"""

import glob
from os import path
import os
from aip import AipOcr
from PIL import Image

def convertimg(picfile, outdir):
   '''调整图片大小,对于过大的图片进行压缩
   picfile:    图片路径
   outdir:    图片输出路径
   '''
   img = Image.open(picfile)
   width, height = img.size
   while(width*height > 4000000):  # 该数值压缩后的图片大约 两百多k
       width = width // 2
       height = height // 2
   new_img=img.resize((width, height),Image.BILINEAR)
   new_img.save(path.join(outdir,os.path.basename(picfile)))

def baiduOCR(picfile, outfile):
   """利用百度api识别文本,并保存提取的文字
   picfile:    图片文件名
   outfile:    输出文件
   """
   filename = path.basename(picfile)

APP_ID = '******' # 刚才获取的 ID,下同
   API_KEY = '******'
   SECRECT_KEY = '******'
   client = AipOcr(APP_ID, API_KEY, SECRECT_KEY)

i = open(picfile, 'rb')
   img = i.read()
   print("正在识别图片:\t" + filename)
   message = client.basicGeneral(img)   # 通用文字识别,每天 50 000 次免费
   #message = client.basicAccurate(img)   # 通用文字高精度识别,每天 800 次免费
   print("识别成功!")
   i.close();

with open(outfile, 'a+') as fo:
       fo.writelines("+" * 60 + '\n')
       fo.writelines("识别图片:\t" + filename + "\n" * 2)
       fo.writelines("文本内容:\n")
       # 输出文本内容
       for text in message.get('words_result'):
           fo.writelines(text.get('words') + '\n')
       fo.writelines('\n'*2)
   print("文本导出成功!")
   print()

if __name__ == "__main__":

outfile = 'export.txt'
   outdir = 'tmp'
   if path.exists(outfile):
       os.remove(outfile)
   if not path.exists(outdir):
       os.mkdir(outdir)
   print("压缩过大的图片...")
   // 首先对过大的图片进行压缩,以提高识别速度,将压缩的图片保存与临时文件夹中
   for picfile in glob.glob("picture/*"):
       convertimg(picfile, outdir)
   print("图片识别...")
   for picfile in glob.glob("tmp/*"):
       baiduOCR(picfile, outfile)
       os.remove(picfile)
   print('图片文本提取结束!文本输出结果位于 %s 文件中。' % outfile)
   os.removedirs(outdir)

来源:https://blog.csdn.net/XnCSD/article/details/80786793

标签:Python,识别,图片文字
0
投稿

猜你喜欢

  • oracle初始化参数设置

    2010-07-31 12:47:00
  • MySql存储过程和游标的使用实例

    2024-01-13 09:50:11
  • python中用ggplot绘制画图实例讲解

    2023-07-04 07:25:16
  • vue嵌套组件传参实例分享

    2024-06-07 15:23:11
  • 在 Django/Flask 开发服务器上使用 HTTPS

    2023-02-21 22:15:33
  • python使用requests实现发送带文件请求功能

    2023-11-03 14:23:13
  • pymysql模块的操作实例

    2024-01-28 16:27:45
  • ubuntu下简单配置mysql数据库

    2009-07-31 09:17:00
  • Python:type、object、class与内置类型实例

    2023-09-27 08:51:27
  • 利用Python操作消息队列RabbitMQ的方法教程

    2022-12-11 21:52:16
  • Python中的引用和拷贝实例解析

    2022-02-26 00:59:13
  • asp截取指定英汉混合字符串_支持中文

    2011-04-19 10:39:00
  • splice slice区别

    2024-04-18 10:32:12
  • 如何使用python传入不确定个数参数

    2023-10-27 22:56:36
  • linux系统中重置mysql的root密码

    2024-01-14 06:16:45
  • 详解python单元测试框架unittest

    2022-05-31 23:16:58
  • python opencv摄像头的简单应用

    2023-01-17 14:25:56
  • MySQL数据备份之mysqldump的使用方法

    2024-01-17 05:25:46
  • 一道sql面试题附答案

    2024-01-18 04:50:33
  • Python使用read_csv读数据遇到分隔符问题的2种解决方式

    2022-01-13 13:30:47
  • asp之家 网络编程 m.aspxhome.com