调用百度图像识别接口进行相应图像识别

目标:

1.学习使用百度AI开放平台进行语音识别与语音合成

       百度AI有两种开发方式:REST, SDK;    本文使用的是SDK python进行开发

        文档地址:https://ai.baidu.com/docs#/ImageClassify-Python-SDK/top

2.将学到的内容:异常捕获,tkinter显示图像,多线程的使用

  异常捕获十分方便程序开发时,当未每一个函数添加抛出异常时,可以方便判定错误出现的函数

 图像显示:Canvas实现

多线程使用:参考文档 https://blog.csdn.net/qq_42233538/article/details/86659085

3.生成exe格式可执行文件

  pyinstaller -F xxx.py 参考文档 https://blog.csdn.net/qq_35203425/article/details/78568141

4.python-Tkinter图形界面开发

  1.按钮Button   2.文本框 Text  3.容器 LabelFrame 4.复选框 Checkbutton 5.画布Canvas

问题点:

1.Canvas绘制图像时坐标的指定

 

界面:
 

代码:

from tkinter import *
from tkinter.filedialog import askopenfilename,askdirectory  #加载文件路径
import base64
import time,os
import numpy as np
from PIL import Image, ImageTk
import cv2
import math
import threading
import matplotlib.pyplot as plt
from aip import AipImageClassify  #导入图像识别SDK接口
class Image_Identification():
    def __init__(self):
        """ 创建图形识别对象,你的 APPID AK SK """
        self.APP_ID = '17845246'
        self.API_KEY = 'M49UHtOLaGTQhwXuOdmXrq4h'
        self.SECRET_KEY = 'vgzmhcFrjgA9vry4bFsyVgGVL4eWov82'
        self.client = AipImageClassify(self.APP_ID, self.API_KEY, self.SECRET_KEY)
        self.myWindow = Tk()
        self.var_1=StringVar()
        self.thread_lock=threading.Lock()
        # self.img = self.open_image()
    def open_image(self):
        # print("成功打开图像")
        try:
            self.path_name=askopenfilename(title="查找文件的路径", filetypes=[("图片格式", "*.jpg;*.jpeg;*.png;*.bmp")])
            with open(self.path_name,"rb") as fp:
                image=fp.read()
                return image
                # time.sleep(1)
        except Exception as error:
            print("图像读取错误{}".format(error))
            return []
    def show_image(self,img):
        self.canvas.delete('tag1')
        self.canvas.delete('tag2')
        """ 如果有可选参数 """
        face_dict={"true":1,"false":0}
        with_face=self.var_1.get()
        options = {}
        options["with_face"] = face_dict[with_face]
        try:
            # img=self.open_image()
            # print(img.shape)
            result=self.client.objectDetect(img, options)
            # print(result)

        except Exception as errs:
            print("主体识别错误{}".format(errs))
        else:
            if 'error_code' in result:
                raise Exception(result['error_msg'])
            else:
                l, t, w, h = result['result']['left'], result['result']['top'], result['result']['width'], \
                             result['result']['height']
                # print(l, t, w, h )
                img_show=Image.open(self.path_name)
                img_shape=np.array(img_show)
                '''
                img_w,img_h,_=img_shape.shape
                print(img_w,img_h)
                if img_w/380>=img_h/350:
                    img_show = img_show.resize(( int(img_h*380/img_w),380))  #resize(h,w)
                    l_r,t_r,w_r,h_r=[i/(img_w / 380) for i in [l, t, w, h ]]
                    d_xc=190-l_r-w_r/2
                    d_yc=img_h*380/(img_w*2)-t_r-h_r/2
                else:
                    img_show = img_show.resize((350,int(img_w * 350 / img_h)))
                    l_r, t_r, w_r, h_r = [i / (img_h/ 350) for i in [l, t, w, h]]
                    d_xc = img_w * 350 / (img_h*2) - l_r - w_r / 2
                    d_yc = 175 - t_r - h_r / 2

                img_show=ImageTk.PhotoImage(img_show)
                # self.canvas.create_image(190,175,image=img_show)
                # self.canvas.create_rectangle(190+d_xc-w_r/2, 175+d_yc-h_r/2,190+d_xc+w_r/2, 175+d_yc+h_r/2  ,width=2)
                self.canvas.create_image(100,100, image=img_show,anchor="nw",tag="tag1")
                self.canvas.create_rectangle(100+l_r,10+t_r,100+l_r+w_r,10+t_r+h_r,width = 2,outline="red",tag="tag2")
                self.canvas.image=img_show
                '''
                img_h, img_w, _ = img_shape.shape  #返回为图像高度,宽度
                # print(img_h, img_w)
                if img_w / 380 >= img_h / 350:
                    # img_show = img_show.resize((int(img_h * 380 / img_w), 380))  # resize(h,w)
                    img_show = img_show.resize( (380,int(img_h * 380 / img_w)))
                    l_r, t_r, w_r, h_r = [i / (img_w / 380) for i in [l, t, w, h]]
                    d_xc = 190 - l_r - w_r / 2
                    d_yc = img_h* 380 / (img_w * 2) - t_r - h_r / 2
                else:
                    img_show = img_show.resize(( int(img_w * 350 / img_h),350))
                    l_r, t_r, w_r, h_r = [i / (img_h / 350) for i in [l, t, w, h]]
                    d_xc = img_w* 350 / (img_h * 2) - l_r - w_r / 2
                    d_yc = 175 - t_r - h_r / 2
                # print(d_xc,d_yc)
                img_show = ImageTk.PhotoImage(img_show)
                self.canvas.create_image(190,175,image=img_show, tag="tag1")
                self.canvas.create_rectangle(190-d_xc-w_r/2, 175-d_yc-h_r/2,190-d_xc+w_r/2, 175-d_yc+h_r/2 ,width=3,outline="red", tag="tag2")
                # self.canvas.create_image(0, 0, image=img_show, anchor="nw", tag="tag1")
                # self.canvas.create_rectangle( l_r,  t_r,  l_r + w_r, t_r + h_r, width=2,outline="red", tag="tag2")

                self.canvas.image = img_show

    def object_recognition(self,img):
        # self.thread_lock.acquire()
        self.text_r2.delete(1.0, END)
        # self.thread_lock.release()
        """ 如果有可选参数 """
        options = {}
        options["baike_num"] = 1
        try:
            # img = self.open_image()
            """ 带参数调用通用物体识别 """
            result = self.client.advancedGeneral(img, options)
            # print(result)
        except Exception as errs:
            print("主体识别错误{}".format(errs))
        else:
            if 'error_code' in result:
                raise Exception(result['error_msg'])
            else:
                key=result['result'][0]['keyword']
                if 'description' in result['result'][0]['baike_info']:
                    description = result['result'][0]['baike_info']['description']
                    string="{}\n{}".format(key,description)
                else:
                    string = "{}\n".format(key)
                self.text_r2.insert("insert",string)
    def dishes_ident(self,img):
        self.thread_lock.acquire()
        self.text_r1.delete(1.0,END)
        self.thread_lock.release()
        """ 如果有可选参数 """
        options = {}
        options["top_num"] = 3
        options["filter_threshold"] = "0.7"
        options["baike_num"] = 5
        try:
            # img = self.open_image()
            """ 带参数调用通用物体识别 """
            result = self.client.dishDetect(img, options)
            # print(result)
        except Exception as errs:
            print("菜品识别错误{}".format(errs))
        else:
            if 'error_code' in result:
                raise Exception(result['error_msg'])
            else:
                string=""
                for data in result['result']:
                    if data["has_calorie"] == "true":
                        string+="\n菜品名称:{}\n卡路里:{}\n置信度:{}\n".format(data["name"],data["calorie"] ,data["probability"])
                    else:
                        string += "\n菜品名称:{}\n置信度:{}\n".format(data["name"],data["probability"])

                # self.text_r1.insert("insert",string)
                self.thread_lock.acquire()
                self.text_r1.insert("insert", string)
                self.thread_lock.release()
    def model_ident(self,img):
        self.thread_lock.acquire()
        self.text_r1.delete(1.0, END)
        self.thread_lock.release()
        """ 如果有可选参数 """
        options = {}
        options["top_num"] = 3
        options["baike_num"] = 5
        try:
            # img = self.open_image()
            """ 带参数调用通用物体识别 """
            result = self.client.carDetect(img, options)
            # print(result)
        except Exception as errs:
            print("车辆识别错误{}".format(errs))
        else:
            if 'error_code' in result:
                raise Exception(result['error_msg'])
            else:
                string = ""
                for data in result['result']:
                    string += "\n车型:{}\n年份:{}\n置信度:{}\n".format(data["name"], data['year'], data['score'])
                # self.text_r1.insert("insert", string)
                self.thread_lock.acquire()
                self.text_r1.insert("insert", string)
                self.thread_lock.release()
    def trademark_ident(self,img):
        self.thread_lock.acquire()
        self.text_r1.delete(1.0, END)
        self.thread_lock.release()
        """ 如果有可选参数 """
        options = {}
        options["custom_lib"] = "false"
        try:
            # img = self.open_image()
            """ 带参数调用通用物体识别 """
            result = self.client.logoSearch(img, options)
            # print(result)
        except Exception as errs:
            print("商标识别错误{}".format(errs))
        else:
            if 'error_code' in result:
                raise Exception(result['error_msg'])
            else:
                string = ""
                for data in result['result']:
                    string += "\n商标:{}\n置信度:{}\n".format(data["name"], data['probability'])
                # self.text_r1.insert("insert", string)
                self.thread_lock.acquire()
                self.text_r1.insert("insert", string)
                self.thread_lock.release()
    def animal_ident(self,img):
        self.thread_lock.acquire()
        self.text_r1.delete(1.0, END)
        self.thread_lock.release()
        """ 如果有可选参数 """
        options = {}
        options["top_num"] = 3
        options["baike_num"] = 5
        try:
            # img = self.open_image()
            """ 带参数调用通用物体识别 """
            result = self.client.animalDetect(img, options)
            # print(result)
        except Exception as errs:
            print("动物识别错误{}".format(errs))
        else:
            if 'error_code' in result:
                raise Exception(result['error_msg'])
            else:
                string = ""
                for data in result['result']:
                    string += "\n动物:{}\n置信度:{}\n".format(data["name"], data['score'])
                self.thread_lock.acquire()
                self.text_r1.insert("insert", string)
                self.thread_lock.release()
    def plant_ident(self,img):
        self.thread_lock.acquire()
        self.text_r1.delete(1.0, END)
        self.thread_lock.release()
        # print("植物识别")
        """ 如果有可选参数 """
        options = {}
        options["baike_num"] = 5
        try:
            # img = self.open_image()
            """ 带参数调用通用物体识别 """
            result = self.client.plantDetect(img, options)
            # print(result)
        except Exception as errs:
            print("植物识别错误{}".format(errs))
        else:
            if 'error_code' in result:
                raise Exception(result['error_msg'])
            else:
                string = ""
                for data in result['result']:
                    string += "\n动物:{}\n置信度:{}\n".format(data["name"], data['score'])
                self.thread_lock.acquire()
                self.text_r1.insert("insert", string)
                self.thread_lock.release()
    def button1(self):
        try:
            self.img=self.open_image()
            thread1=threading.Thread(target=self.show_image,args=[self.img,])
            thread2 = threading.Thread(target=self.object_recognition, args=[self.img, ])
            thread1.start()
            thread2.start()

        except Exception as errs:
            print("通用物体识别/图像主体错误:{}".format(errs))
    def button2(self):
        try:
            thread3=threading.Thread(target=self.dishes_ident,args=[self.img])
            thread3.start()
        except Exception as errs:
            print("菜品识别错误:{}".format(errs))
    def button3(self):
        try:
            thread4 = threading.Thread(target=self.model_ident, args=[self.img])
            thread4.start()
        except Exception as errs:
            print("车型识别错误:{}".format(errs))
    def button4(self):
        try:
            thread5 = threading.Thread(target=self.trademark_ident, args=[self.img])
            thread5.start()
        except Exception as errs:
            print("商标识别错误:{}".format(errs))
    def button5(self):
        try:
            thread6 = threading.Thread(target=self.animal_ident, args=[self.img])
            thread6.start()
        except Exception as errs:
            print("动物识别错误:{}".format(errs))
    def button6(self):
        try:
            thread7 = threading.Thread(target=self.plant_ident, args=[self.img])
            thread7.start()
        except Exception as errs:
            print("植物识别错误:{}".format(errs))
    def interface(self):
        # 制作界面
        self.myWindow.title('图像识别示例程序')  # 修改窗口标题
        self.myWindow.geometry("680x420+400+200")  # 修改窗口大小 width*height+xoffset+yoffset
        # 左侧界面
        # button_l=Button(self.myWindow, text='打开图像', command=self.show_image, width=8, height=1)
        button_l = Button(self.myWindow, text='打开图像', command=self.button1, width=8, height=1)
        button_l.grid(row=0, column=0, sticky='EW',pady=5, padx=5,rowspan=1, columnspan=1)
        checkbutton_l=Checkbutton(self.myWindow, text='检测人脸', variable=self.var_1, onvalue='true', offvalue='false')
        checkbutton_l.grid(row=0, column=1, sticky='EW',pady=5, padx=5,rowspan=1, columnspan=1)
        self.var_1.set('true')
        labelframe_l=LabelFrame(self.myWindow, width=380, height=380)
        labelframe_l.grid(row=1, column=0, sticky='EW',pady=5, padx=5,rowspan=20, columnspan=6)
        self.canvas=Canvas(labelframe_l, background='white', width=380, height=350)
        self.canvas.grid()
        # 右侧界面
        button_r1 = Button(self.myWindow, text='菜品识别', command=self.button2, width=10, height=1)
        button_r1.grid(row=1, column=6, sticky='EW', pady=0, padx=5, rowspan=1, columnspan=1)
        button_r2 = Button(self.myWindow, text='车型识别', command=self.button3, width=10, height=1)
        button_r2.grid(row=1, column=7, sticky='EW', pady=0, padx=5, rowspan=1, columnspan=1)
        button_r3 = Button(self.myWindow, text='商标识别', command=self.button4, width=10, height=1)
        button_r3.grid(row=1, column=8, sticky='EW', pady=0, padx=5, rowspan=1, columnspan=1)
        button_r4 = Button(self.myWindow, text='动物识别', command=self.button5, width=8, height=1)
        button_r4.grid(row=2, column=6, sticky='EW', pady=0, padx=5, rowspan=1, columnspan=1)
        button_r5 = Button(self.myWindow, text='植物识别', command=self.button6, width=8, height=1)
        button_r5.grid(row=2, column=7, sticky='EW', pady=0, padx=5, rowspan=1, columnspan=1)
        self.text_r1=Text(self.myWindow,  width=30, height=9)
        self.text_r1.grid(row=3, column=6, sticky='EW', pady=4, padx=5, rowspan=8, columnspan=3)
        self.text_r2 = Text(self.myWindow, width=30, height=9)
        self.text_r2.grid(row=12, column=6, sticky='EW', pady=4, padx=5, rowspan=9, columnspan=3)

        # 启动主窗口的消息循环
        self.myWindow.mainloop()
if __name__ == '__main__':
    image_ident=Image_Identification()
    image_ident.interface()
发布了109 篇原创文章 · 获赞 22 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_42233538/article/details/103321588