本文来源吾爱破解论坛
本帖最后由 我叫周奔跑 于 2019-9-29 20:13 编辑
大噶好,我系python新手村选手。
这两天工作很闲,趁闲暇学下python,python调用百度的图像技术做菜品识别。
GUI是用QTdesigner写的,稍微有点冗长,我也是为了练习下使用方法。【logo如果不喜欢可以删了,毕竟我知道太难看了】
很初级的选手,请各位大佬不吝赐教,接受各种建议和意见。
一起学习的也可以联系我,有时候为了一个小问题,绞一下午的脑汁也搞不定,然后论坛上一问,发现全是低级问题。
1,QTdesigner生成的form代码:
[Python] 纯文本查看 复制代码
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'window.ui' # # Created by: PyQt5 UI code generator 5.8.2 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(594, 422) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(255, 0, 0)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Light, brush) brush = QtGui.QBrush(QtGui.QColor(255, 0, 0)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Light, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Light, brush) MainWindow.setPalette(palette) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.select_image = QtWidgets.QPushButton(self.centralwidget) self.select_image.setGeometry(QtCore.QRect(140, 70, 111, 41)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(170, 0, 0)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 170, 0)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(170, 0, 0)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 170, 0)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 170, 0)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush) self.select_image.setPalette(palette) self.select_image.setObjectName("select_image") self.textEdit_findmsg = QtWidgets.QTextEdit(self.centralwidget) self.textEdit_findmsg.setGeometry(QtCore.QRect(100, 140, 341, 31)) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(11) font.setBold(True) font.setWeight(75) self.textEdit_findmsg.setFont(font) self.textEdit_findmsg.setObjectName("textEdit_findmsg") self.funcBtn_food = QtWidgets.QRadioButton(self.centralwidget) self.funcBtn_food.setGeometry(QtCore.QRect(30, 70, 89, 16)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(0, 0, 127)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 0, 127)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) self.funcBtn_food.setPalette(palette) self.funcBtn_food.setObjectName("funcBtn_food") self.funcBtn_star = QtWidgets.QRadioButton(self.centralwidget) self.funcBtn_star.setGeometry(QtCore.QRect(30, 90, 89, 16)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(0, 0, 127)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 0, 127)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) self.funcBtn_star.setPalette(palette) self.funcBtn_star.setObjectName("funcBtn_star") self.funcBtn_animal = QtWidgets.QRadioButton(self.centralwidget) self.funcBtn_animal.setGeometry(QtCore.QRect(30, 110, 89, 16)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(0, 0, 127)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 0, 127)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) self.funcBtn_animal.setPalette(palette) self.funcBtn_animal.setObjectName("funcBtn_animal") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(40, 10, 511, 41)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) self.label.setPalette(palette) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(22) font.setBold(True) font.setWeight(75) self.label.setFont(font) self.label.setTextFormat(QtCore.Qt.AutoText) self.label.setOpenExternalLinks(False) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(40, 150, 51, 20)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) self.label_2.setPalette(palette) self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(self.centralwidget) self.label_3.setGeometry(QtCore.QRect(20, 190, 71, 20)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) self.label_3.setPalette(palette) self.label_3.setObjectName("label_3") self.textEdit_caroli = QtWidgets.QTextEdit(self.centralwidget) self.textEdit_caroli.setGeometry(QtCore.QRect(100, 190, 341, 31)) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(11) font.setBold(True) font.setWeight(75) self.textEdit_caroli.setFont(font) self.textEdit_caroli.setObjectName("textEdit_caroli") self.label_4 = QtWidgets.QLabel(self.centralwidget) self.label_4.setGeometry(QtCore.QRect(60, 240, 31, 20)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) self.label_4.setPalette(palette) self.label_4.setObjectName("label_4") self.textEdit_descr = QtWidgets.QTextEdit(self.centralwidget) self.textEdit_descr.setGeometry(QtCore.QRect(100, 240, 341, 161)) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(11) font.setBold(True) font.setWeight(75) self.textEdit_descr.setFont(font) self.textEdit_descr.setObjectName("textEdit_descr") self.label_6 = QtWidgets.QLabel(self.centralwidget) self.label_6.setGeometry(QtCore.QRect(460, 250, 121, 31)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) self.label_6.setPalette(palette) self.label_6.setObjectName("label_6") self.label_7 = QtWidgets.QLabel(self.centralwidget) self.label_7.setGeometry(QtCore.QRect(460, 280, 121, 31)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) self.label_7.setPalette(palette) self.label_7.setObjectName("label_7") self.label_8 = QtWidgets.QLabel(self.centralwidget) self.label_8.setGeometry(QtCore.QRect(460, 310, 121, 31)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) self.label_8.setPalette(palette) self.label_8.setObjectName("label_8") self.label_9 = QtWidgets.QLabel(self.centralwidget) self.label_9.setGeometry(QtCore.QRect(460, 340, 121, 31)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) self.label_9.setPalette(palette) self.label_9.setObjectName("label_9") self.label_10 = QtWidgets.QLabel(self.centralwidget) self.label_10.setGeometry(QtCore.QRect(460, 370, 121, 31)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) self.label_10.setPalette(palette) self.label_10.setObjectName("label_10") MainWindow.setCentralWidget(self.centralwidget) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) self.select_image.clicked.connect(MainWindow.pushbutton_click) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.select_image.setText(_translate("MainWindow", "选择图片")) self.funcBtn_food.setText(_translate("MainWindow", "菜品识别")) self.funcBtn_star.setText(_translate("MainWindow", "明星识别")) self.funcBtn_animal.setText(_translate("MainWindow", "动物识别")) self.label.setText(_translate("MainWindow", "初涉Python之百度API之乱七八糟调用")) self.label_2.setText(_translate("MainWindow", "菜品名字:")) self.label_3.setText(_translate("MainWindow", "卡路里/100g:")) self.label_4.setText(_translate("MainWindow", "介绍:")) self.label_6.setText(_translate("MainWindow", "作者:我叫周奔跑")) self.label_7.setText(_translate("MainWindow", "发布日期:2019.9.27")) self.label_8.setText(_translate("MainWindow", "发布功能:菜品识别")) self.label_9.setText(_translate("MainWindow", "版权所有,严禁转载;")) self.label_10.setText(_translate("MainWindow", "如有雷同,就是抄的。"))
2.主程序module:
[Python] 纯文本查看 复制代码
import requests import json import base64 from tkinter import filedialog import sys from pic2word.form_name_food import Ui_MainWindow from PyQt5.QtWidgets import QMainWindow from PyQt5 import QtWidgets def get_token(): #素质三连(二) url='https://aip.baidubce.com/oauth/2.0/token' #申请token的URL data={} data['grant_type']='client_credentials' #这个必须有 data['client_id']='***************' #百度该应用的APP—ID data['client_secret']='*********' #相应的secret response=requests.post(url, data) #发送post请求 content=response.content.decode('utf-8') content=json.loads(content) print('您的access_token为 : \r',content['access_token']) return content['access_token'] def base64_food(image): #将图片解码成base64 f1 = open(image, 'rb') f1_64 = base64.b64encode(f1.read()) f1.close() f1_64=f1_64.decode() return f1_64 def foodread(image): #下面是网页访问常用素质三连 url='https://aip.baidubce.com/rest/2.0/image-classify/v2/dish'+"?access_token=" + get_token() data={'image':base64_food(image),"top_num":5,'filter_threshold':0.95,'baike_num':1} headers={'Content-Type':'application/x-www-form-urlencoded'} response=requests.post(url, data, headers=headers) req_con1 = response.content.decode('utf-8') req_dict = json.JSONDecoder().decode(req_con1) #得到解码的结果 return req_dict class mywindow(QtWidgets.QMainWindow,Ui_MainWindow): def __init__(self): super(mywindow,self).__init__() self.setupUi(self) def pushbutton_click(self): image = filedialog.askopenfilename() #打开文件夹选择文件,返回文件名 result=foodread(image) result=result['result'][0] #返回的一个列表,第一条置信度最大,选择第一条 calorie=result['calorie'] #选择字典内容 name=result['name'] name=str(name) calorie=str(calorie) print(result['baike_info']) if result['baike_info']: #有的菜的返回里面没有百科,则不能进行后续操作,用if来防止闪退 description=result['baike_info']['description'] description=str(description) self.textEdit_descr.setText(description) self.textEdit_findmsg.setText(name) self.textEdit_caroli.setText(calorie) if __name__=='__main__': app=QtWidgets.QApplication(sys.argv) mainwindow=QMainWindow window=mywindow() window.show() sys.exit(app.exec_())
3.结果:
没什么实质性用处....
用pyinstaller生成的EXE...体积太大了,我用蓝奏云发了。
因为方便,其他打包的方法有体积浓缩的我还没了解。
exe:
https://www.lanzous.com/i6hgemf
我在看小甲鱼的视频,其他新手一起学习交流的话可以加我,有问题可以相互讨论下,因为身边没一个用python语言的。。
菜品识别.png (36.17 KB, 下载次数: 3)
下载附件 保存到相册
2019-9-29 20:01 上传
菜品识别
版权声明:
本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。
- 上一篇: python贪吃蛇源码
- 下一篇: 【新人贴】基于scrapy框架爬取全部股票数据