欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python 利用百度API識(shí)別圖片文字(多線程版)

 更新時(shí)間:2020年12月14日 15:48:43   作者:凹凸曼大人  
這篇文章主要介紹了python 利用百度API識(shí)別圖片文字(多線程版),幫助大家更好的利用python進(jìn)行機(jī)器識(shí)別,感興趣的朋友可以了解下
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 12 09:37:38 2018
利用百度api實(shí)現(xiàn)圖片文本識(shí)別
@author: XnCSD
"""

import glob
from os import path
import os
from aip import AipOcr
from PIL import Image
from queue import Queue
import threading
import datetime

def convertimg(picfile, outdir):
  '''調(diào)整圖片大小,對(duì)于過大的圖片進(jìn)行壓縮
  picfile:  圖片路徑
  outdir:  圖片輸出路徑
  '''
  img = Image.open(picfile)
  width, height = img.size
  while (width * height > 4000000): # 該數(shù)值壓縮后的圖片大約 兩百多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):
  """利用百度api識(shí)別文本,并保存提取的文字
  picfile:  圖片文件名
  outfile:  輸出文件
  """
  while not ts_queue.empty():
    picfile = ts_queue.get()
    filename = path.basename(picfile)
    outfile = 'D:\Study\pythonProject\scrapy\IpProxy\port_zidian.txt'
    APP_ID = '' # 剛才獲取的 ID,下同
    API_KEY = ''
    SECRECT_KEY = ''
    client = AipOcr(APP_ID, API_KEY, SECRECT_KEY)

    i = open(picfile, 'rb')
    img = i.read()
    print("正在識(shí)別圖片:\t" + filename)
    message = client.basicGeneral(img) # 通用文字識(shí)別,每天 50 000 次免費(fèi)
    # message = client.basicAccurate(img)  # 通用文字高精度識(shí)別,每天 800 次免費(fèi)
    #print("識(shí)別成功!")
    i.close()
    try:
      filename1 = filename.split('.')[0]
      filename1 = ''.join(filename1)
      with open(outfile, 'a+') as fo:
        for text in message.get('words_result'):
          fo.writelines('\'' + filename1 + '\'' + ':' + text.get('words') + ',')
          fo.writelines('\n')
        # fo.writelines("+" * 60 + '\n')
        # fo.writelines("識(shí)別圖片:\t" + filename + "\n" * 2)
        # fo.writelines("文本內(nèi)容:\n")
        # # 輸出文本內(nèi)容
        # for text in message.get('words_result'):
        #   fo.writelines(text.get('words') + '\n')
        # fo.writelines('\n' * 2)
      os.remove(filename)
      print("識(shí)別成功!")
    except:
      print('識(shí)別失敗')



    print("文本導(dǎo)出成功!")
    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("壓縮過大的圖片...")
  # 首先對(duì)過大的圖片進(jìn)行壓縮,以提高識(shí)別速度,將壓縮的圖片保存與臨時(shí)文件夾中
  try:
    for picfile in glob.glob(r"D:\Study\pythonProject\scrapy\IpProxy\端口\*"):
      convertimg(picfile, outdir)
    print("圖片識(shí)別...")
    for picfile in glob.glob("tmp/*"):
      ts_queue.put(picfile)
      #baiduOCR(picfile, outfile)
      #os.remove(picfile)
    print('圖片文本提取結(jié)束!文本輸出結(jié)果位于文件中。' )
    #os.removedirs(outdir)
    return ts_queue
  except:
    print('失敗')

if __name__ == "__main__":

  start = datetime.datetime.now().replace(microsecond=0)
  t = 'tmp'
  s = duqu_tupian(t)
  threads = []
  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('刪除耗時(shí):' + str(end - start))

速度快,準(zhǔn)確率99百分,100里必回出錯(cuò)一張。

實(shí)測,識(shí)別1500張圖片,還是小圖片驗(yàn)證碼大小,高清,用時(shí)30秒,不能識(shí)別150張,出錯(cuò)14張左右。  但總體快,不會(huì)出現(xiàn)亂碼啥的。

以上就是python 利用百度API識(shí)別圖片文字(多線程版)的詳細(xì)內(nèi)容,更多關(guān)于python 識(shí)別圖片文字的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python PaddleOCR模型訓(xùn)練及使用超詳細(xì)教程

    Python PaddleOCR模型訓(xùn)練及使用超詳細(xì)教程

    OCR英文全稱是Optical Character Recognition,中文叫做光學(xué)字符識(shí)別,是通過掃描等光學(xué)技術(shù)與計(jì)算機(jī)技術(shù)結(jié)合的方式直接從影像中提取各類數(shù)據(jù),省去人工錄入,節(jié)約成本,這篇文章主要介紹了Python PaddleOCR模型訓(xùn)練及使用超詳細(xì)教程,需要的朋友可以參考下
    2024-06-06
  • Python全角與半角之間相互轉(zhuǎn)換的方法總結(jié)

    Python全角與半角之間相互轉(zhuǎn)換的方法總結(jié)

    全角與半角轉(zhuǎn)換在處理漢語語料中會(huì)經(jīng)常出現(xiàn),這里分別說明漢字、數(shù)字、字母的unicode編碼范圍,下面這篇文章主要給大家介紹了關(guān)于Python全角與半角之間相互轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • Python 列表的基本操作介紹

    Python 列表的基本操作介紹

    這篇文章主要介紹了Python 列表的基本操作,下面文章圍繞Python 列表的相關(guān)資料展開文章的詳細(xì)內(nèi)容,,需要的朋友可以參考一下,希望對(duì)大家有所幫助
    2021-11-11
  • python實(shí)現(xiàn)決策樹分類算法代碼示例

    python實(shí)現(xiàn)決策樹分類算法代碼示例

    決策樹分類算法是最為常見的一種分類算法,通過屬性劃分來建立一棵決策樹,測試對(duì)象通過在樹上由頂向下搜索確定所屬的分類,下面這篇文章主要給大家介紹了關(guān)于python實(shí)現(xiàn)決策樹分類算法的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • 使用PYTHON解析Wireshark的PCAP文件方法

    使用PYTHON解析Wireshark的PCAP文件方法

    今天小編就為大家分享一篇使用PYTHON解析Wireshark的PCAP文件方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • 淺析Python中的多重繼承

    淺析Python中的多重繼承

    這篇文章主要介紹了Python中的多重繼承,是Python學(xué)習(xí)中的基本知識(shí),代碼基于Python2.x版本,需要的朋友可以參考下
    2015-04-04
  • Python自動(dòng)化開發(fā)學(xué)習(xí)之三級(jí)菜單制作

    Python自動(dòng)化開發(fā)學(xué)習(xí)之三級(jí)菜單制作

    這篇文章主要為大家詳細(xì)介紹了Python自動(dòng)化開發(fā)學(xué)習(xí)之三級(jí)菜單的制作方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 淺談Python類的單繼承相關(guān)知識(shí)

    淺談Python類的單繼承相關(guān)知識(shí)

    本文給大家介紹面向?qū)ο笕刂焕^承Inheritance的相關(guān)知識(shí),通過示例代碼給大家介紹了繼承、貓類、狗類不用寫代碼,直接繼承了父類的屬性和方法,具體實(shí)現(xiàn)代碼跟隨小編一起看看吧
    2021-05-05
  • Python?類和對(duì)象詳細(xì)介紹

    Python?類和對(duì)象詳細(xì)介紹

    這篇文章主要介紹了Python?類和對(duì)象詳細(xì)介紹,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下
    2022-06-06
  • python分割文件的常用方法

    python分割文件的常用方法

    這篇文章主要介紹了python分割文件的常用方法,包括指定分割大小、按行分割與分割合并等技巧,非常實(shí)用,需要的朋友可以參考下
    2014-11-11

最新評(píng)論