目标:

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.shapeprint(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/2d_yc=img_h*380/(img_w*2)-t_r-h_r/2else: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 / 2d_yc = 175 - t_r - h_r / 2img_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 / 2d_yc = img_h* 380 / (img_w * 2) - t_r - h_r / 2else: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 / 2d_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_showdef object_recognition(self,img):# self.thread_lock.acquire()self.text_r2.delete(1.0, END)# self.thread_lock.release()""" 如果有可选参数 """options = {}options["baike_num"] = 1try:# 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"] = 3options["filter_threshold"] = "0.7"options["baike_num"] = 5try:# 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"] = 3options["baike_num"] = 5try:# 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"] = 3options["baike_num"] = 5try:# 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"] = 5try:# 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()

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

  1. 关于调用百度AI接口进行图片识别的实现(C#)

    前言 近期由于项目需要对图片的文字进行图像识别,查看了百度的AI接口,可以通过调用百度的AI识别接口进行实现: 一.注册准备 1.在IE上打开下面的网站,点击右上角的注册按钮,注册一个百度账号:百度智 ...

  2. php调用天气预报接口,PHP调用百度天气接口API实现查询实时天气

    现在,不用守着晚上7点半的时间去看第二天的天气预报,只要你有手机,有网络,便可以轻松查询实时天气,可你知道怎么用PHP实现的吗?本文将带大家学习一种调用百度天气接口的方式,直接在PHP上查看实时天气, ...

  3. php 百度天气接口api接口,PHP调用百度天气接口API实现查询实时天气

    现在,不用守着晚上7点半的时间去看第二天的天气预报,只要你有手机,有网络,便可以轻松查询实时天气,可你知道怎么用PHP实现的吗?本文将带大家学习一种调用百度天气接口的方式,直接在PHP上查看实时天气, ...

  4. 调用百度AI接口实现图片文字识别

    一.准备阶段 进入百度AI网址点击这里跳转 ,点击导航栏的开放能力 ---- 文字识别 ---- 通用文字识别,进入文字识别OCR界面. 在文字识别ORC界面点击 技术文档 进入帮助文档. 在左侧可以 ...

  5. 调用百度ai接口实现图片文字识别详解

    调用百度ai接口实现图片文字识别详解 首先先介绍一下这篇博文是干嘛的,为了不浪费大家时间.公司最近和短视频公司合作,需要监控app的截图上的文字是否符合规范,也就是确保其没有违规的文字.到网上找了一些 ...

  6. 如何用python调用百度云接口实例

    python可以通过python+Opencv来实现很多图片变换之类的工作,因为OpenCV库的功能可以说是相当强大,很多功能都可以完成.但是实现起来需要自己造轮子,所以很费时间和精力,我们可以直接学 ...

  7. Python调用百度AI接口用以实现在线语音合成 并创建简单的GUI界面 生成可执行应用程序

    调用百度AI接口用以实现在线语音合成 并创建简单的GUI界面 生成可执行应用程序 1. 实验环境 Python3.8.Pycharm.window10 2.实验过程 1.首先登陆百度智能云:https ...

  8. JS调用百度api接口——实现简单的百度页面

    描述: JS调用百度api接口--实现简单的百度页面 效果: 实现: css文件: @charset "utf-8"; /* CSS Document */ *{margin: 0 ...

  9. vue调用百度定位接口实现地理定位

    其实HTML5中有个 Geolocation API 可以获得用户的地理位置,不过它只能返回用户位置的经度和纬度. 下面是一个简单示例: var x=document.getElementById(& ...

  10. php调用人脸识别接口,PHP调用百度AI接口人脸识别-语音技术-文本识别-图像识别...

    Rain-AI系统基于百度人工智能接口二次开发 有问题的可以加入这个群:226520254,大家一起交流 说明:这个代码是原生PHP开发的,在6个月前我为了效率和安全性能使用框架进行重写,测试站里的是 ...

最新文章

  1. Brute Force算法介绍及C++实现
  2. 如何用TF Object Detection API训练交通信号灯检测神经网络?
  3. 解决oninput在输入中文时,会获取拼音的问题
  4. PAT甲级1086 Tree Traversals Again:[C++题解]二叉树中序序列、栈、求后序遍历
  5. Windows平台九点提升权限终极技巧
  6. CentOS 初体验二: 切换语言,连接互联网
  7. 《敏捷敬业度》作者访谈
  8. LeetCode 1089. 复写零
  9. pb通过对象名称调用对象_信号线名称自动关联Simulink信号对象
  10. linux技能点七 shell
  11. TIA Portal
  12. shell 第一次练习
  13. paip.svn使用最佳实践
  14. vs2010操作office2010
  15. 原生JS实现canvas移动端电子签名板/画板
  16. 深度学习:从零开始构造一个识别猫狗图片的卷积网络
  17. mysql官网默认账号缪_Mysql账户设置_增删改查_表操作
  18. Redis 跳跃表实现原理 时间复杂度分析
  19. 双窗格文件管理器:Commander One PRO Mac中文版
  20. 云起实验室:零基础入门Serverless:一键部署红白机小游戏

热门文章

  1. 学术工具之学科信息门户大全
  2. 用python画小兔子_python 实现兔子生兔子示例
  3. CAN(FD)数据记录诊断仪-CANLog-VCI
  4. Vector、ArrayList 、LinkedList比较
  5. 【题解】Luogu P2214 [USACO14MAR]哞哞哞Mooo Moo
  6. word编辑目录页码----去掉前后横线的问题
  7. python笔记更新(函数)
  8. 在线C语言编译器/解释器
  9. 江苏淳客网络在第五届“创响江苏 赢在南京”创业大赛中获得一等奖!
  10. throws,throw,try···catch···finally