效果:
全篇分为以下几个部分:
目录
一、进入百度AI开放平台创建动物识别应用
二、用代码获取access_token
三、实现动物识别的功能实现
四、动物识图UI_demo
连接:动物识别_拍照识别动物-百度AI开放平台
登陆后操作步骤如图:
创建完成后如图所示:
此处的API Key和Secret Key是之后需要用上的内容
调用API首先要获取access_token,它通过API Key和Secret Key获得
实现代码如下:
import requests
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【此处填API Key】&client_secret=【此处填Secret Key】'
response = requests.get(host)
if response:
print(response.json())
运行后可在结果中找到access_token的值
记录下access_token的值
代码如下:
request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal"
# 二进制方式打开图片文件
f = open('test7.png', 'rb') #这里是你的图片路径
img = base64.b64encode(f.read())
f.close()
params = {"image":img,"top_num": 3} #返回前3个概率比较大的
access_token = '这里放access_token的值'
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
print (response.json())
运行结果:
由此便实现了百度AI动物识图API的调用了
下面是完整的代码实现
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
import requests
import base64
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
#MainWindow.setWindowIcon(QIcon('..\icons\zhenyanglog.png'))
MainWindow.resize(1025, 600)
MainWindow.setFixedSize(1025, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 0, 1001, 31))
self.label.setObjectName("label")
self.line = QtWidgets.QFrame(self.centralwidget)
self.line.setGeometry(QtCore.QRect(10, 25, 1011, 21))
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(10, 40, 91, 61))
self.pushButton.setObjectName("pushButton")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(440, 54, 91, 31))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(852, 510, 131, 51))
self.pushButton_5.setObjectName("pushButton_5")
font3 = QFont()
font3.setPointSize(17)
self.pushButton_5.setFont(font3)
self.pushButton.setEnabled(False)
#self.pushButton_2.setEnabled(False)
#self.pushButton_3.setEnabled(False)
# 按钮关联函数
self.pushButton.clicked.connect(self.ClickButton1)
self.pushButton_4.clicked.connect(self.openImage)
self.pushButton_5.clicked.connect(self.clear)
self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_4.setGeometry(QtCore.QRect(530, 54, 241, 31))
self.lineEdit_4.setObjectName("lineEdit_4")
self.line_2 = QtWidgets.QFrame(self.centralwidget)
self.line_2.setGeometry(QtCore.QRect(10, 95, 1011, 21))
self.line_2.setFrameShape(QtWidgets.QFrame.HLine)
self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_2.setObjectName("line_2")
self.line_5 = QtWidgets.QFrame(self.centralwidget)
self.line_5.setGeometry(QtCore.QRect(-20, 109, 60, 486))
self.line_5.setFrameShape(QtWidgets.QFrame.VLine)
self.line_5.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_5.setObjectName("line_5")
self.line_6 = QtWidgets.QFrame(self.centralwidget)
self.line_6.setGeometry(QtCore.QRect(-10, 34, 38, 71))
self.line_6.setFrameShape(QtWidgets.QFrame.VLine)
self.line_6.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_6.setObjectName("line_6")
self.line_7 = QtWidgets.QFrame(self.centralwidget)
self.line_7.setGeometry(QtCore.QRect(1000, 34, 38, 71))
self.line_7.setFrameShape(QtWidgets.QFrame.VLine)
self.line_7.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_7.setObjectName("line_7")
self.line_8 = QtWidgets.QFrame(self.centralwidget)
self.line_8.setGeometry(QtCore.QRect(970, 0, 98, 37))
self.line_8.setFrameShape(QtWidgets.QFrame.VLine)
self.line_8.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_8.setObjectName("line_8")
self.line_9 = QtWidgets.QFrame(self.centralwidget)
self.line_9.setGeometry(QtCore.QRect(-10, 0, 38, 33))
self.line_9.setFrameShape(QtWidgets.QFrame.VLine)
self.line_9.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_9.setObjectName("line_9")
self.line_10 = QtWidgets.QFrame(self.centralwidget)
self.line_10.setGeometry(QtCore.QRect(10, 10, 1011, 46))
self.line_10.setFrameShape(QtWidgets.QFrame.HLine)
self.line_10.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_10.setObjectName("line_10")
self.line_11 = QtWidgets.QFrame(self.centralwidget)
self.line_11.setGeometry(QtCore.QRect(10, -9, 1011, 22))
self.line_11.setFrameShape(QtWidgets.QFrame.HLine)
self.line_11.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_11.setObjectName("line_11")
self.line_13 = QtWidgets.QFrame(self.centralwidget)
self.line_13.setGeometry(QtCore.QRect(10, 95, 801, 31))
self.line_13.setFrameShape(QtWidgets.QFrame.HLine)
self.line_13.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_13.setObjectName("line_13")
self.line_15 = QtWidgets.QFrame(self.centralwidget)
self.line_15.setGeometry(QtCore.QRect(10, 566, 801, 61))
self.line_15.setFrameShape(QtWidgets.QFrame.HLine)
self.line_15.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_15.setObjectName("line_15")
self.line_16 = QtWidgets.QFrame(self.centralwidget)
self.line_16.setGeometry(QtCore.QRect(790, 110, 41, 486))
self.line_16.setFrameShape(QtWidgets.QFrame.VLine)
self.line_16.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_16.setObjectName("line_16")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(35, 120, 750, 460))
self.label_3.setStyleSheet("font:28px;\n"
"border-style:solid;\n"
"border-width:1px;\n"
"border-color:rgb(45, 45, 45);\n"
"\n"
"")
self.label_3.setText("")
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(820, 112, 191, 31))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(820, 170, 191, 41))
font = QtGui.QFont()
font.setPointSize(11)
self.label_5.setFont(font)
self.label_5.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label_5.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.label_5.setWordWrap(True)
self.label_5.setObjectName("label_5")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(822, 210, 181, 41))
font = QtGui.QFont()
font.setPointSize(10)
self.lineEdit.setFont(font)
self.lineEdit.setObjectName("lineEdit")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(820, 280, 191, 41))
font = QtGui.QFont()
font.setPointSize(11)
self.label_6.setFont(font)
self.label_6.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label_6.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.label_6.setWordWrap(True)
self.label_6.setObjectName("label_6")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(822, 320, 181, 41))
font = QtGui.QFont()
font.setPointSize(10)
self.lineEdit_2.setFont(font)
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_7 = QtWidgets.QLabel(self.centralwidget)
self.label_7.setGeometry(QtCore.QRect(820, 390, 191, 41))
font = QtGui.QFont()
font.setPointSize(11)
self.label_7.setFont(font)
self.label_7.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label_7.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.label_7.setWordWrap(True)
self.label_7.setObjectName("label_7")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(822, 430, 181, 41))
font = QtGui.QFont()
font.setPointSize(10)
self.lineEdit_3.setFont(font)
self.lineEdit_3.setObjectName("lineEdit_3")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def clear(self):
self.lineEdit.clear()
self.lineEdit_2.clear()
self.lineEdit_3.clear()
def ClickButton1(self):
self.start1()
#self.distance()
#系统目录方法
def initUI(self, Qmodelidx):
self.label_3.clear()
# 选择本地图片上传
def openImage(self):
global imgNamepath # 这里为了方便别的地方引用图片路径,将其设置为全局变量
imgNamepath, imgType = QFileDialog.getOpenFileName(self.centralwidget, "选择图片",
".",
"All Files(*);;*.jpg;;*.png")
# 通过文件路径获取图片文件,并设置图片长宽为label控件的长、宽
print(imgNamepath)
img = QtGui.QPixmap(imgNamepath) # .scaled(self.label_3.width(), self.label_3.height())
# 在label控件上显示选择的图片
self.label_3.setPixmap(img)
self.label_3.setScaledContents(True)
# 显示所选图片的路径
self.lineEdit_4.setText(imgNamepath)
self.pushButton.setEnabled(True)
#识别
def start1(self):
request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal"
# 二进制方式打开图片文件
print(imgNamepath)
f = open(imgNamepath, 'rb')
img = base64.b64encode(f.read())
f.close()
params = {"image": img, "top_num": 3} # 返回前3个概率比较大的
access_token = '此处填你的access_token'
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
print(response.json())
str_re = str(response.json())
str1 = str_re.split("'",29)[5] #百分比
str2 = str_re.split("'", 29)[9] #结果1
str3 = str_re.split("'", 29)[13] # 百分比
str4 = str_re.split("'", 29)[17] # 结果2
str5 = str_re.split("'", 29)[21] # 百分比
str6 = str_re.split("'", 29)[25] # 结果3
self.lineEdit.setText(str2 + " 概率:" + str(str1))
self.lineEdit_2.setText(str4 + " 概率:" + str(str3))
self.lineEdit_3.setText(str6 + " 概率:" + str(str5))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "图像识别"))
self.label.setText(_translate("MainWindow", "识别UI界面"))
self.pushButton.setText(_translate("MainWindow", "识别图像"))
self.pushButton_5.setText(_translate("MainWindow", "清空数据"))
self.label_4.setText(_translate("MainWindow", "数据展示:"))
self.label_5.setText(_translate("MainWindow", "识别结果1:"))
self.label_6.setText(_translate("MainWindow", "识别结果2:"))
self.label_7.setText(_translate("MainWindow", "识别结果3:"))
self.pushButton_4.setText(_translate("MainWindow", "选择图片"))
import sys
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())