1. 程式人生 > >通用物體識別小專案

通用物體識別小專案

#用來進行百度雲身份驗證
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()