#用来进行百度云身份验证
from aip import AipImageClassify
def client():
""" 你的 APPID AK SK """
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
return AipImageClassify(APP_ID, API_KEY, SECRET_KEY)
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
#上传本地照片,进行检测
import Client
def get_result(path):
image = Client.get_file_content(path)
client = Client.client()
options = {}
options["baike_num"] = 1
""" 带参数调用通用物体识别 """
res = client.advancedGeneral(image, options)
if res['result'][0][ 'baike_info']:
return res['result'][0]['baike_info']['description']
else:
return '没有检测到'
#用tkinter编写界面,并且加载图片,调用上面两个模块运行
from tkinter import *
from PIL import Image, ImageTk
from tkinter.filedialog import askopenfilename
from objectIdentify import *
from objectIdentify import get_result
class Im:
def __init__(self):
self.text = Text(tk, width=40, height=30)
self.t = Text(tk, width=40, height=30)
self.scroll = Scrollbar()
def start(self):
tk.geometry('620x500')
Button(tk, text='上传图片', command=im.choiceImg).place(x=310, y=0)
def choiceImg(self):
msg = ''
path = askopenfilename(title='选择文件')
print(path)
load = Image.open(path)
render = ImageTk.PhotoImage(load)
self.text.image_create(END, image=render) # 用这个方法创建一个图片对象,并插入到“END”的位置
img = Label(image=render)
img.image = render
self.text.place(x=0, y=50)
msg += get_result(path)
self.showResult(msg)
def showResult(self, msg):
# 将滚动条填充
self.scroll.pack(side=RIGHT, fill=Y) # side是滚动条放置的位置,上下左右。fill是将滚动条沿着y轴填充
self.t.pack(side=LEFT, fill=Y) # 将文本框填充进wuya窗口的左侧,
# 将滚动条与文本框关联
self.scroll.config(command=self.t.yview) # 将文本框关联到滚动条上,滚动条滑动,文本框跟随滑动
self.t.config(yscrollcommand=self.scroll.set) # 将滚动条关联到文本框
self.t.insert(END, msg)
self.t.insert(END, '\n')
self.t.insert(END, '\n')
self.t.insert(END, '\n')
self.t.place(x=300, y=50)
if __name__=='__main__':
tk = Tk()
im = Im()
im.start()
tk.mainloop()