利用百度AI接口实现的图片识别

利用百度AI接口实现的简单的图片文字识别及色情图片识别,需要看百度AI提供的技术文档,锻炼自己看文档开发的能力,不同的应用需要申请不同的id和secret。

import requests as r
import base64
class Orc:
    @classmethod
    def get_zi_token(cls):
        url = "https://aip.baidubce.com/oauth/2.0/token"
        params = {
            'grant_type':'client_credentials',
            'client_id':'百度AI申请的id',
            'client_secret':'百度AI申请的secret'
        }
        res = r.get(url=url,params=params).json()
        access_token = res.get('access_token')
        return access_token

    @classmethod
    def get_zi_info(cls):
        url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
        params = {
            'access_token':cls.get_zi_token()
        }
        headers = {
            'Content-Type':'application/x-www-form-urlencoded'
        }
        with open("\crawler_demo\crawler_four\photos\\6.jpg","rb") as rs:
            img = rs.read()
            img = base64.b64encode(img)
        data = {
            'image':img
        }
        res = r.post(url=url,headers=headers,params=params,data=data).json()
        str_list = res.get("words_result")
        s = ""
        for i in str_list:
            s+=i["words"]+"\n"
        print(s)

    @classmethod
    def get_pic_token(cls):
        url = "https://aip.baidubce.com/oauth/2.0/token"
        params = {
            'grant_type': 'client_credentials',
            'client_id': '百度AI申请的id',
            'client_secret': '百度AI申请的secret'
        }
        res = r.get(url=url, params=params).json()
        access_token = res.get('access_token')
        return access_token

    @classmethod
    def get_pic_info(cls):
        url = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined"
        params = {
            'access_token':cls.get_pic_token()
        }
        headers = {
            'Content-Type':'application/x-www-form-urlencoded'
        }
        with open("G:\BaiZhiPython\crawler_demo\crawler_four\photos\\129.jpg","rb") as rs:
            img = rs.read()
            img = base64.b64encode(img)
        data = {
            'image':img
        }
        res = r.post(url=url,headers=headers,params=params,data=data).json()
        if len(res) == 3:
            conclusion = res["conclusion"]
            print(conclusion)
        else:
            conclusion = res["data"][0]["conclusion"]
            msg = res["data"][0]["msg"]
            print(conclusion,msg)

if __name__ == '__main__':
    Orc.get_zi_info()
    # Orc.get_pic_info()

你可能感兴趣的:(利用百度AI接口实现的图片识别)