作者:wangwei8638
细粒度图像识别 (fine-grained image recognition),即 精细化分类 。
识别出物体的大类别(比如:花、草、狗等)较易,但比如区分月季和玫瑰,判断更为精细化的物体分类名称,则难度极大。
最大的挑战在于,同一大类别下不同子类别间的视觉差异极小。因此,精细化分类需要更高的图像分辨率。
百度细粒度图像识别目前支持动物、植物、菜品、地标等,能精准识别超过十万种物体和场景,包含多项高精度的识图能力并提供相应的API服务。https://ai.baidu.com/tech/imagerecognition
一.平台接入
此步骤比较简单,不多阐述。可参照上篇文档:
https://ai.baidu.com/forum/topic/show/943028
二.分析接口文档
1.打开接口说明文档
http://ai.baidu.com/docs#/ImageClassify-API/2c607890
(1)接口描述
该请求用于识别地标,即对于输入的一张图片(可正常解码,且长宽比适宜),输出图片中的地标识别结果。
(2)请求说明
需要用到的信息有:
请求URL:https://aip.baidubce.com/rest/2.0/image-classify/v1/landmark
Header格式:Content-Type:application/x-www-form-urlencoded
请求参数:image, 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式 。注意:图片需要base64编码、去掉编码头后再进行urlencode。
(3)返回示例
{“log_id”: 3450013152046070669, “result”: {“landmark”: “狮身人面像”}}
2.获取accesstoken
@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!')3.准备一张用于识别的图片,存储在本地
三.识别结果 四.源码共享
# -*- coding: utf-8 -*- #!/usr/bin/env python import os import requests import base64 import json from pprint import pprint import time #api_key 为官网获取的AK, secret_key 为官网获取的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/landmark' + '?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() 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: rp_json = rp.json() return rp_json else: print('=> Error! token invalid or network error!') return None #识别地标信息 def detect(self, img_path): f = open(img_path, 'rb') strover = '识别结果:' img_str = base64.b64encode(f.read()) params = {'image': img_str} rp_json = self.get_result(params) result = rp_json['result'] strover += ' 这是:{} \n '.format(result['landmark']) print(strover) if __name__ == '__main__': recognizer = LandmarkRecognizer(api_key, secret_key) img = 'F:\paddle\dibiao\湖州温泉度假酒店.jpeg' recognizer.detect(img)