Once a cherry photo maxed circle of friends, there is a financial freedom called cherry free!
User cried: I can only buy back a try ......
is coming season cherries and cherries, many people face cherries and cherry silly could not tell, these two look very similar to fruit , but the price difference is huge, imported 60 yuan / kg of "cherry" and 15 yuan / kg in China, "cherry", what's the difference? The naked eye could not tell it to Baidu Ai.
One. Access platform
This step is relatively simple, not much elaboration. Before a document can refer to:
https://ai.baidu.com/forum/topic/show/943028
II. Analysis of interface documentation
1. https://ai.baidu.com/docs#/ImageClassify-API/f0fe4219
(1) Interface description
The request for identifying the fruit and vegetable ingredients, i.e., for an input image (normally decoded, and an appropriate aspect ratio), fruit and vegetable ingredients result output picture.
(2) Description Request
Information need to use are:
请求URL:https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient
Header格式:Content-Type:application/x-www-form-urlencoded
Request parameters: the Image, the image data, base64 encoding, base64 encoding requires no larger than 4M, 15px shortest side at least, the maximum longest side 4096px, support jpg / png / bmp format. Note: The image needs to base64 encoded, the encoding head removed and then be urlencode.
(3) Returning to the example
{'log_id': 7884358602702161307,
'Result': [{ 'name': 'cherries', 'score': 0.60600465536118},
{ 'Name': 'Cherry', 'score': 0.35849434137344},
{ 'Name': 'cherry', 'score': 0.022074541077018},
{ 'Name': 'Black Pearl cherry', 'score': 0.0061983447521925},
{ 'Name': 'black cherry', 'score': 0.0045025632716715}],
'result_num': 5}
2. Get accesstoken
#client_id official net acquisition of AK, client_secret official net acquisition of SK
client_id = [AK Baidu cloud applications]
[SK] Baidu cloud applications client_secret =
#获取token
def get_token():
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
token_content = response.read()
if token_content:
token_info = json.loads(token_content.decode("utf-8"))
token_key = token_info['access_token']
return token_key
三.识别结果
1. cherries
Recognition result: { 'score': 0.60600465536118, 'name': 'cherries'}
2. Cherry
识别结果: {'score': 0.66473871469498, 'name': '大樱桃'}
四.源码共享
# -*- coding: utf-8 -*- #!/usr/bin/env python import os import requests import base64 import json from pprint import pprint import time #client_id 为官网获取的AK, client_secret 为官网获取的SK api_key = '**************' secret_key = '********************' class LandmarkRecognizer(object): def __init__(self, api_key, secret_key): self.access_token = self._get_access_token(api_key=api_key, secret_key=secret_key) self.API_URL = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient' + '?access_token=' \ + self.access_token #获取token @staticmethod def _get_access_token(api_key, secret_key): api = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials' \ '&client_id={}&client_secret={}'.format(api_key, secret_key) rp = requests.post(api) if rp.ok: rp_json = rp.json() # print(rp_json['access_token']) return rp_json['access_token'] else: print('=> Error in get access token!') def get_result(self, params): rp = requests.post(self.API_URL, data=params) if rp.ok: # print('=> Success! got result: ') rp_json = rp.json() pprint(rp_json) return rp_json else: print('=> Error! token invalid or network error!') print(rp.content) return None #果蔬识别 def detect(self, img_path): f = open(img_path, 'rb') strover = '识别结果:' img_str = base64.b64encode(f.read()) params = {'image': img_str} tic = time.clock() rp_json = self.get_result(params) toc = time.clock() result = rp_json['result'] strover += ' {} \n '.format(result[0]) print(strover) print('花费时长: '+'%.2f' %(toc - tic) +' s') if __name__ == '__main__': recognizer = LandmarkRecognizer(api_key, secret_key) img = 'F:\paddle\yt2.jpg' recognizer.detect(img)
作者:wangwei8638