Python3.5 Windows 7下 利用百度识图来获得图片信息

#!/usr/bin/python

from PIL import Image
from PIL import ImageFilter
import urllib
import requests
import re
import json

# hack CERTIFICATE_VERIFY_FAILED
# https://github.com/mtschirs/quizduellapi/issues/2
import ssl

if hasattr(ssl, '_create_unverified_context'):
    ssl._create_default_https_context = ssl._create_unverified_context


UA = 'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36'

pic_url = "https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=login&rand=sjrand&0.21191171556711197"

'''
def get_img():
    resp = urllib.urlopen(pic_url)
    raw = resp.read()
    with open("./tmp.png", 'wb') as fp:
        fp.write(raw)

    return Image.open("./tmp.jpg")
'''

def get_img(im):
    (WITH,HEIGHT) = im.size
    print("WITH = %d,HEIGHT = %d"%(WITH,HEIGHT))
    left = 0
    top = 0
    right = left + WITH
    bottom = top + HEIGHT

    return im.crop((left, top, right, bottom))


def baidu_image_upload(im):
    url = "http://image.baidu.com/pictureup/uploadshitu?fr=flash&fm=index&pos=upload"

    im.save("./tmp.png")
    raw = open("./tmp.png", 'rb').read()

    files = {
        'fileheight': "0",
        'newfilesize': str(len(raw)),
        'compresstime': "0",
        'Filename': "image.png",
        'filewidth': "0",
        'filesize': str(len(raw)),
        'filetype': 'image/png',
        'Upload': "Submit Query",
        'filedata': ("image.png", raw)
    }

    resp = requests.post(url, files=files, headers={'User-Agent': UA})

    #print("resp.text = %s"%resp.text)
    #  resp.url
    redirect_url = "http://image.baidu.com" + resp.text
    return redirect_url


def baidu_stu_lookup(im):
    redirect_url = baidu_image_upload(im)

    # print redirect_url
    #print("1111111111redirect_url = %s"%redirect_url)
   
    resp = requests.get(redirect_url)

    html = resp.text
    #print("2222222222resp.text = %s"%resp.text)
    return baidu_stu_html_extract(html)


def baidu_stu_html_extract(html):
    pattern = re.compile(r"'multitags':\s*'(.*?)'")
    matches = pattern.findall(html)
    if not matches:
        print("Not match")
        return '[ERROR?]'
    else:
        print("Match")
   
    tags_str = matches[0]
   
    for item in matches:
        print("item = %s"%item)
   
    #print("tags_str = %s"%tags_str)
    result = list(filter(None, tags_str.replace('\t', ' ').split()))

    return '|'.join(result) if result else '[UNKOWN]'


def ocr_question_extract(im):
    # git@github.com:madmaze/pytesseract.git
    global pytesseract
    try:
        import pytesseract
    except:
        print("[ERROR] pytesseract not installed")
        return
   
    (width, height) = im.size
    im = im.crop((0, 0, width, height))
    im = pre_ocr_processing(im)
    #im.show()
    return pytesseract.image_to_string(im, lang='chi_sim').strip()


def pre_ocr_processing(im):
    im = im.convert("RGB")
    (width, height) = im.size

    white = im.filter(ImageFilter.BLUR).filter(ImageFilter.MaxFilter(23))
    grey = im.convert('L')
    impix = im.load()
    whitepix = white.load()
    greypix = grey.load()

    for y in range(height):
        for x in range(width):
            greypix[x, y] = min(255, max(255 + impix[0,0][0] - whitepix[x, y][0],
                         255 + impix[0,0][1] - whitepix[x, y][1],
                         255 + impix[0,0][2] - whitepix[x, y][2]))

    new_im = grey.copy()
    binarize(new_im, 150)
    return new_im

def binarize(im, thresh=120):
    assert 0 < thresh < 255
    assert im.mode == 'L'
    w, h = im.size
    for y in range(0, h):
        for x in range(0, w):
            if im.getpixel((x, y)) < thresh:
                im.putpixel((x, y), 0)
            else:
                im.putpixel((x, y), 255)


if __name__ == '__main__':
    # im = get_img()
    im = Image.open("tmp.jpg")

    '''
    try:
        print('OCR Question:', ocr_question_extract(im))
    except Exception as e:
        print ('<OCR failed>', e)
   '''

    im2 = get_img(im)

    result = baidu_stu_lookup(im2)
    print(result)

将一张名为tmp.jpg的jpeg图片放在代码目录下:

显示结果如下:

E:\Python35\python.exe E:/PycharmProjs/baidushitu/baidushitu.py
WITH = 500,HEIGHT = 440
Match
item = 小鸡  哈多利博美  荷兰猪 豚鼠 鸡雏 鸡蛋孵小鸡
小鸡|哈多利博美|荷兰猪|豚鼠|鸡雏|鸡蛋孵小鸡

Process finished with exit code 0

本来是想用来识别12306抢票的验证码,但百度识图的识别率不高,其他的也试了,现在都不太高,所以就在想想有什么好的值得研究推荐下

转载于:https://my.oschina.net/michaelshu/blog/1510942

识别结果大概是这样 {'result': {'face_num': 1, 'face_list': [{'quality': {'occlusion': {'right_eye': 0, 'left_cheek': 0.1459853947, 'right_cheek': 0.05144193396, 'left_eye': 0.465408802, 'mouth': 0.02919708006, 'chin_contour': 0.01420217194, 'nose': 0}, 'illumination': 116, 'blur': 7.266304692e-06, 'completeness': 1}, 'age': 22, 'face_token': 'dc6f8f9df5d977ea476e2d04acdf5063', 'race': {'type': 'white', 'probability': 0.6173604727}, 'glasses': {'type': 'common', 'probability': 0.9834988713}, 'gender': {'type': 'male', 'probability': 0.655915916}, 'face_probability': 0.9185044169, 'beauty': 51.21487427, 'angle': {'roll': -2.750922441, 'yaw': 28.97134399, 'pitch': 5.202290535}, 'location': {'height': 65, 'top': 112.0704803, 'width': 76, 'left': 76.20765686, 'rotation': -4}, 'face_type': {'type': 'human', 'probability': 0.9992217422}, 'face_shape': {'type': 'oval', 'probability': 0.4419156313}, 'expression': {'type': 'none', 'probability': 0.9999142885}}]}, 'error_msg': 'SUCCESS', 'timestamp': 1537413754, 'cached': 0, 'error_code': 0, 'log_id': 9465840013520} 年龄:22 颜值:51.21487427 表情-type(none:不笑;smile:微笑;laugh:大笑):none 表情-probability(表情置信度,范围【0~1】,0最小、1最大):0.9999142885 脸型-type(square: 正方形 triangle:三角形 oval: 椭圆 heart: 心形 round: 圆形):oval 脸型-probability(置信度,范围【0~1】,代表这是人脸形状判断正确的概率,0最小、1最大):0.4419156313 性别-type(male:男性 female:女性):male 性别-probability(性别置信度,范围【0~1】,0代表概率最小、1代表最大。):0.655915916 是否带眼镜-type(none:无眼镜,common:普通眼镜,sun:墨镜):common 是否带眼镜-probability(眼镜置信度,范围【0~1】,0代表概率最小、1代表最大。):0.9834988713 人种-type(yellow: 黄种人 white: 白种人 black:黑种人 arabs: 阿拉伯人):white 人种-probability(人种置信度,范围【0~1】,0代表概率最小、1代表最大。):0.6173604727 真实人脸/卡通人脸 -type(human: 真实人脸 cartoon: 卡通人脸):human 真实人脸/卡通人脸 -probability(人脸类型判断正确的置信度,范围【0~1】,0代表概率最小、1代表最大。):0.9992217422
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值