图像识别(含通用物体和场景识别,图像主体检测,动、植物识别)
作者头像
  • 泽熙洲
  • 2022-06-11 23:00:51 7

本文介绍了一种使用百度智能云图像识别接口的方法,该接口具有多种功能,包括识别菜品、Logo、果蔬等。如果你对如何使用百度接口感兴趣,可以参考我在其他文章中的详细介绍。

一、完整代码

```python

!/usr/bin/python

作者:老刘

创建日期:2020年6月6日

编码:UTF-8

import requests import base64 import pprint

获取访问令牌

def getaccesstoken(): host = 'https://aip.baidubce.com/oauth/2.0/token?granttype=clientcredentials&clientid=【官网获取的AK】&clientsecret=【官网获取的SK】' response = requests.get(host) if response: return response.json()['access_token']

通用物体和场景识别

def advancedgeneral(imgpath): requesturl = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advancedgeneral" with open(imgpath, 'rb') as f: img = base64.b64encode(f.read()) params = {"image": img} accesstoken = getaccesstoken() requesturl += "?accesstoken=" + accesstoken headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(requesturl, data=params, headers=headers) if response: pprint.pprint(response.json())

图像主体检测

def objectdetection(imgpath): requesturl = "https://aip.baidubce.com/rest/2.0/image-classify/v1/objectdetect" with open(imgpath, 'rb') as f: img = base64.b64encode(f.read()) params = {"image": img, "withface": 1} accesstoken = getaccesstoken() requesturl += "?accesstoken=" + accesstoken headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(request_url, data=params, headers=headers) if response: print(response.json())

动物识别

def animalrecognition(imgpath): requesturl = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal" with open(imgpath, 'rb') as f: img = base64.b64encode(f.read()) params = {"image": img} accesstoken = getaccesstoken() requesturl += "?accesstoken=" + accesstoken headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(request_url, data=params, headers=headers) if response: print(response.json())

植物识别

def plantrecognition(imgpath): requesturl = "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant" with open(imgpath, 'rb') as f: img = base64.b64encode(f.read()) params = {"image": img} accesstoken = getaccesstoken() requesturl += "?accesstoken=" + accesstoken headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(request_url, data=params, headers=headers) if response: print(response.json())

if name == 'main': imgpath = 'u=2580636683,3144235846&fm=26&gp=0.jpg' # 1. 通用物体和场景识别 # advancedgeneral('111.jpg') # 2. 图像主体检测 # objectdetection(imgpath) # 3. 动物识别 # animalrecognition(imgpath) # 4. 植物识别 plantrecognition(imgpath) ```

二、运行结果

1. 通用物体和场景识别

运行结果: json { "log_id": 470629798833798022, "result": [ { "keyword": "北京天安门", "root": "建筑-传统建筑", "score": 0.943181 }, { "keyword": "城楼", "root": "建筑-传统建筑", "score": 0.75112 }, { "keyword": "都市夜景", "root": "建筑-建筑夜景", "score": 0.414575 }, { "keyword": "街道", "root": "建筑-其他", "score": 0.219592 }, { "keyword": "轿车", "root": "交通工具-汽车", "score": 0.011774 } ], "result_num": 5 }

2. 图像主体检测

运行结果: json { "log_id": 6487470709418459622, "result": { "width": 775, "top": 17, "left": 3, "height": 278 } }

3. 动物识别

运行结果: json { "log_id": 1653180371969692999, "result": [ { "name": "松鼠", "score": "0.98465" }, { "name": "岩松鼠", "score": "0.00457777" }, { "name": "灰鼠", "score": "0.00147658" }, { "name": "金花鼠", "score": "0.000845074" }, { "name": "花栗鼠", "score": "0.000815643" }, { "name": "赤腹松鼠", "score": "0.000759463" } ] } json { "log_id": 5028727426341370887, "result": [ { "name": "非洲象", "score": "0.962111" }, { "name": "亚洲象", "score": "0.0314206" }, { "name": "野象", "score": "0.00213204" }, { "name": "猛犸象", "score": "6.82637e-05" }, { "name": "水牛", "score": "2.94292e-05" }, { "name": "狮子", "score": "2.70095e-05" } ] }

4. 植物识别

运行结果: json { "log_id": 6306910600402241223, "result": [ { "name": "荷花玉兰", "score": 0.7407493591308594 }, { "name": "玉兰", "score": 0.11518511176109314 }, { "name": "山玉兰", "score": 0.031238427385687828 } ] }

希望以上内容对你有所帮助。

    本文来源:图灵汇
责任编辑: : 泽熙洲
声明:本文系图灵汇原创稿件,版权属图灵汇所有,未经授权不得转载,已经协议授权的媒体下载使用时须注明"稿件来源:图灵汇",违者将依法追究责任。
    分享
识别图像物体主体场景植物通用检测
    下一篇