曾经一张车厘子的照片刷爆朋友圈,有一种财务自由叫车厘子自由!
网友哭了:我只能买一颗回去尝尝……
又快到了车厘子和樱桃的季节,很多人面对车厘子和大樱桃傻傻分不清楚,这两种水果看起来十分相近,但价格差别巨大,60元/斤的进口“车厘子”和15元/斤的中国“大樱桃”,有啥区别?肉眼分不清楚,交给百度Ai吧。
一.平台接入
此步骤比较简单,不多阐述。可参照之前文档:
https://ai.baidu.com/forum/topic/show/943028
二.分析接口文档
1. https://ai.baidu.com/docs#/ImageClassify-API/f0fe4219
(1)接口描述
该请求用于识别果蔬类食材,即对于输入的一张图片(可正常解码,且长宽比适宜),输出图片中的果蔬食材结果。
(2)请求说明
需要用到的信息有:
请求URL:https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient
Header格式:Content-Type:application/x-www-form-urlencoded
请求参数:image, 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式 。注意:图片需要base64编码、去掉编码头后再进行urlencode。
(3)返回示例
{'log_id': 7884358602702161307,
'result': [{'name': '车厘子', 'score': 0.60600465536118},
{'name': '大樱桃', 'score': 0.35849434137344},
{'name': '樱桃', 'score': 0.022074541077018},
{'name': '黑珍珠樱桃', 'score': 0.0061983447521925},
{'name': '黑樱桃', 'score': 0.0045025632716715}],
'result_num': 5}
2.获取accesstoken
#client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id =【百度云应用的AK】
client_secret =【百度云应用的SK】
#获取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.车厘子
识别结果: {'score': 0.60600465536118, 'name': '车厘子'}
2.大樱桃
识别结果: {'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 += ' {} '.format(result[0]) print(strover) print('花费时长: '+'%.2f' %(toc - tic) +' s') if __name__ == '__main__': recognizer = LandmarkRecognizer(api_key, secret_key) img = 'F:paddleyt2.jpg' recognizer.detect(img)
作者:wangwei8638