python利用pytesseract 实现本地识别图片文字

作者:凹凸曼大人 时间:2021-03-08 19:20:34 


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import glob
from os import path
import os
import pytesseract
from PIL import Image
from queue import Queue
import threading
import datetime
import cv2

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(ts_queue):
 while not ts_queue.empty():
   picfile = ts_queue.get()
   filename = path.basename(picfile)
   outfile = 'D:\Study\pythonProject\scrapy\IpProxy\port_zidian.txt'
   img = cv2.imread(picfile, cv2.IMREAD_COLOR)
   print("正在识别图片:\t" + filename)
   message = pytesseract.image_to_string(img,lang = 'eng')
   message = message.replace(' ', '')
   message = message.replace('\n', '')
   # message = client.basicAccurate(img)  # 通用文字高精度识别,每天 800 次免费
   #print("识别成功!"))
   try:
     filename1 = filename.split('.')[0]
     filename1 = ''.join(filename1)
     with open(outfile, 'a+') as fo:
       fo.writelines('\'' + filename1 + '\'' + ':' + message + ',')
       fo.writelines('\n')
       # 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)
     os.remove(filename)
     print("识别成功!")
   except:
     print('识别失败')

print("文本导出成功!")
   print()
def duqu_tupian(dir):
 ts_queue = Queue(10000)

outdir = dir
 # if path.exists(outfile):
 #   os.remove(outfile)
 if not path.exists(outdir):
   os.mkdir(outdir)
 print("压缩过大的图片...")
 # 首先对过大的图片进行压缩,以提高识别速度,将压缩的图片保存与临时文件夹中
 try:
   for picfile in glob.glob(r"D:\Study\pythonProject\scrapy\IpProxy\tmp\*"):
     convertimg(picfile, outdir)
   print("图片识别...")
   for picfile in glob.glob("tmp1/*"):
     ts_queue.put(picfile)
     #baiduOCR(picfile, outfile)
     #os.remove(picfile)
   print('图片文本提取结束!文本输出结果位于文件中。' )
   #os.removedirs(outdir)
   return ts_queue
 except:
   print('失败')

if __name__ == "__main__":

start = datetime.datetime.now().replace(microsecond=0)
 t = 'tmp1'
 s = duqu_tupian(t)
 threads = []
 try:
   for i in range(100):
     t = threading.Thread(target=baiduOCR, name='th-' + str(i), kwargs={'ts_queue': s})
     threads.append(t)
   for t in threads:
     t.start()
   for t in threads:
     t.join()
   end = datetime.datetime.now().replace(microsecond=0)
   print('删除耗时:' + str(end - start))
 except:
   print('识别失败')

实测速度慢,但用了多线程明显提高了速度,但准确度稍低,同样高清图片,90百分识别率。还时不时出现乱码文字,乱空格,这里展现不了,自己实践吧,重点免费的,随便识别,通向100张图片,用时快6分钟了,速度慢了一倍,但是是免费的,挺不错的了。

来源:https://www.cnblogs.com/aotumandaren/p/14128765.html

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

猜你喜欢

  • Python实现简单查找最长子串功能示例

    2023-01-14 01:20:33
  • python 自动化偷懒的四个实用操作

    2023-11-19 08:49:48
  • IE中jscript/javascript的条件编译

    2007-10-03 14:03:00
  • Dreamweaver实现flash透明背景

    2008-05-04 09:35:00
  • Python使用signal定时结束AsyncIOScheduler任务的问题

    2022-12-19 21:28:11
  • Oracle数据仓库的分层管理器解决方案开发者网络Oracle

    2010-07-16 13:08:00
  • python中的插入排序的简单用法

    2023-09-30 07:57:51
  • 打印出python 当前全局变量和入口参数的所有属性

    2022-09-01 07:06:51
  • 详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud

    2023-11-10 04:55:49
  • 如何限制上传文件的大小?

    2010-06-09 18:47:00
  • python快排算法详解

    2023-08-24 04:17:08
  • 用户界面设计中“状态”和“动作”的表达

    2011-01-06 12:36:00
  • python PIL模块的基本使用

    2022-08-28 16:35:19
  • 判断 iframe 是否加载完成的完美方法

    2009-09-24 13:35:00
  • Python利用networkx画图绘制Les Misérables人物关系

    2021-03-31 07:41:54
  • MySQL Basis 常用命令

    2010-11-11 11:59:00
  • 通过事务日志解决SQL Server常见四大故障(二)

    2009-03-25 13:51:00
  • 详解Python中的多线程编程

    2023-09-17 00:34:08
  • Python 连接字符串(join %)

    2021-01-13 23:30:46
  • 使用Python编写爬虫的基本模块及框架使用指南

    2021-08-21 17:45:19
  • asp之家 网络编程 m.aspxhome.com