Python recognizes and extracts characters in pictures based on Baidu API


Use the OCR character recognition API of Baidu AI development platform to recognize and extract the characters in the picture. First, you need to register to get the ID and key of the API call. The steps are as follows:

Open Baidu AI open platform and enterConsoleMediumCharacter recognitionApplication (baidu account is required).


Create an application and enterManagement application, write down appid, API key and secrect key, and call the API.



Finally, install the baidu AI interface library of Python

pip install baidu-aip

The following is the code implementation. You need to put all the identified pictures into a folder called picture.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
Created on Tue Jun 12 09:37:38 2018
Using Baidu API to realize picture text recognition
@author: XnCSD

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

def convertimg(picfile, outdir):
    '' 'resize the picture and compress the picture that is too large
    Picfile: picture path
    Outdir: picture output path
    img =
    width, height = img.size
    While (width * height > 4000000): # the compressed image of this value is about 200 K
        width = width // 2
        height = height // 2
    new_img=img.resize((width, height),Image.BILINEAR),os.path.basename(picfile)))
def baiduOCR(picfile, outfile):
    "" "use Baidu API to recognize text and save the extracted text"
    Picfile: picture file name
    Outfile: output file
    filename = path.basename(picfile)
    APP_ Id = '********' # just obtained ID, the same below
    API_KEY = '******'
    SECRECT_KEY = '******'
    client = AipOcr(APP_ID, API_KEY, SECRECT_KEY)
    i = open(picfile, 'rb')
    img =
    Print ("identifying picture: \ T" + filename)
    message = client. Basic general (IMG) # universal character recognition, 50000 times per day free
    #message = client. Basic accurate (IMG) # universal high-precision character recognition, 800 times a day for free
    Print ("recognition succeeded!")
    with open(outfile, 'a+') as fo:
        fo.writelines("+" * 60 + '\n')
        fo. Writelines ("identification picture: \ T" + filename + "\ n" * 2)
        fo. Writelines ("text content: \ n")
        #Output text content
        for text in message.get('words_result'):
            fo.writelines(text.get('words') + '\n')
    Print ("text exported successfully!")

if __name__ == "__main__":
    outfile = 'export.txt'
    outdir = 'tmp'
    if path.exists(outfile):
    if not path.exists(outdir):
    Print ("compressed picture...)
    //Firstly, compress the too large pictures to improve the recognition speed, and save the compressed pictures in the temporary folder
    for picfile in glob.glob("picture/*"):
        convertimg(picfile, outdir)
    Print ("picture recognition...)
    for picfile in glob.glob("tmp/*"):
        baiduOCR(picfile, outfile)
    Print ('picture text extraction finished! Text output result is in% s file. '% outfile)

This is the end of this article about Python recognizing and extracting words from pictures based on Baidu API. For more information about Python Baidu API recognizing pictures and words, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

Recommended Today

Overview of technical differences between loops in JavaScript

Author: gbolahan olagunjuTranslator: front end XiaozhiSource: Blog Dream, dry goods, wechat search[move to the world]Pay attention to this dish brushing wisdom who is still doing dishes in the early morning. This article GitHub has been included. There are complete test sites, materials and my series of articles for the interview of front-line large factories. When […]