日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
GUI布局Tkinter完善Python小項目

 本次 Python 小項目主要功能:調(diào)用電腦攝像頭實現(xiàn)拍照,并使用百度 API 接口實現(xiàn)圖像識別。

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比武都網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式武都網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋武都地區(qū)。費用合理售后完善,10多年實體公司更值得信賴。

上次完成了API的封裝,這次完成GUI的布局。具體成品如下所示。

拍照保存圖片采用的是opencv中的imwrite方法,具體的示例查看上上篇文章。

Tkinter 布局邏輯中最推薦使用的Grid布局。實現(xiàn)機制是將Widget邏輯上分割成表格,在指定的位置放置想要的Widget就可以了。

Grid布局參數(shù)說明

具體main.py代碼如下。

 
 
 
 
  1. """ 
  2. @Author:Runsen 
  3. @WeChat:RunsenLiu 
  4. @微信公眾號:Python之王 
  5. @CSDN:https://blog.csdn.net/weixin_44510615 
  6. @Github:https://github.com/MaoliRUNsen 
  7. @Date:2020/11/29 
  8. """ 
  9. import time 
  10. import cv2 as cv  # pip install opencv-python 
  11. import tkinter as tk 
  12. from tkinter import ttk  # 下拉框依賴庫 
  13. from tkinter import scrolledtext  # 滾動文本框依賴庫 
  14. from tkinter import N,E,S,W 
  15. # 引入Baidu_API類 (上次文章) 
  16. from baidu_api import Baidu_API 
  17.  
  18. # 拍照 
  19. def take_a_photo(): 
  20.     # 調(diào)用筆記本內(nèi)置攝像頭,所以參數(shù)為0,如果有其他的攝像頭可以調(diào)整參數(shù)為1,2 
  21.     cap = cv.VideoCapture(0) 
  22.     img_path = str(int(time.time())) + '.jpg' 
  23.     while True: 
  24.         # 從攝像頭讀取圖片 
  25.         sucess, img = cap.read() 
  26.         # 轉(zhuǎn)為灰度圖片 
  27.         # gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)# 
  28.         # 顯示攝像頭 
  29.         cv.imshow('----------please enter "s" to take a picture----------', img) 
  30.         # 保持畫面的持續(xù),無限期等待輸入 
  31.         k = cv.waitKey(1) 
  32.         if k == 27: 
  33.             # 通過esc鍵退出攝像 
  34.             cv.destroyAllWindows() 
  35.             break 
  36.         elif k == ord("s"): 
  37.             # 通過s鍵保存圖片,并退出。 
  38.             cv.imwrite(img_path, img) 
  39.             cv.destroyAllWindows() 
  40.             break 
  41.     # 關(guān)閉攝像頭 
  42.     cap.release() 
  43.     # 打印日志 
  44.     scr.insert(tk.END, '[{}]拍攝成功...\n'.format(time.strftime('%Y-%m-%d %H:%M:%S'))) 
  45.     # 返回圖像 
  46.     return img_path 
  47.  
  48. # ----------圖形界面各個組件功能的設(shè)計---------- 
  49. # 清除窗口日志 
  50. def clear_the_window(): 
  51.     scr.delete(1.0, tk.END) 
  52.  
  53. # 退出軟件 
  54. def exit(): 
  55.     win.quit() 
  56.  
  57. # 下拉框選項選擇 
  58. def select_ttk(event): 
  59.     global numberChosen 
  60.     # 顏值評分 
  61.     if numberChosen.current() == 1: 
  62.         # 獲取圖像 
  63.         img_path = take_a_photo() 
  64.  
  65.         try: 
  66.             # 向API發(fā)送圖像并獲取信息 
  67.             score, age, gender, race = Baidu_API().face_detect(img_path=img_path) 
  68.  
  69.             # 打印日志 
  70.             scr.insert(tk.END, '[{}]年齡「{}」性別「{}」人種「{}」\n'.format(time.strftime('%Y-%m-%d %H:%M:%S'), age, gender, race)) 
  71.             scr.insert(tk.END, '[{}]顏值評分為:{}/100 分\n'.format(time.strftime('%Y-%m-%d %H:%M:%S'), score)) 
  72.         except: 
  73.             scr.insert(tk.END, '[{}]{}'.format(time.strftime(time.strftime('%Y-%m-%d %H:%M:%S')), 
  74.                                                Baidu_API().face_detect(img_path=img_path))) 
  75.     # 手勢識別 
  76.     if numberChosen.current() == 2: 
  77.         scr.insert(tk.END, '[{}]請將您的手勢放置攝像頭前...\n'.format(time.strftime('%Y-%m-%d %H:%M:%S'))) 
  78.         time.sleep(0.1) 
  79.         img_path = take_a_photo() 
  80.         try: 
  81.             classname_en, classname_zh = Baidu_API().gesture(img_path=img_path) 
  82.             scr.insert(tk.END, 
  83.                        '[{}]手勢大意:{}({})\n'.format(time.strftime('%Y-%m-%d %H:%M:%S'), classname_zh, classname_en)) 
  84.         except: 
  85.             scr.insert(tk.END, 
  86.                        '[{}]{}\n'.format(time.strftime('%Y-%m-%d %H:%M:%S'), Baidu_API().gesture(img_path=img_path))) 
  87.     # 智能人臉摳圖 
  88.     if numberChosen.current() == 3: 
  89.         scr.insert(tk.END, '智能人臉摳圖\n'.format(time.strftime('%Y-%m-%d %H:%M:%S'))) 
  90.         img_path = take_a_photo() 
  91.         out_path = str(int(time.time())) + '.jpg' 
  92.         try: 
  93.             Baidu_API().body_seg(img_path=img_path, out_path=out_path) 
  94.             scr.insert(tk.END, '完成智能人臉摳圖') 
  95.         except: 
  96.             scr.insert(tk.END, '[{}]{}\n'.format(time.strftime('%Y-%m-%d %H:%M:%S'), 
  97.                                                  Baidu_API().body_seg(img_path=img_path, out_path=None))) 
  98.  
  99.  
  100. # -------------創(chuàng)建窗口-------------- 
  101. win = tk.Tk() 
  102. win.title('客官先關(guān)注微信公眾號:Python之王!') 
  103. win.geometry('600x300') 
  104.  
  105. # ------------窗口組件設(shè)計----------- 
  106. # grid中的參數(shù):column, columnspan, in, ipadx, ipady, padx, pady, row, rowspan,sticky 
  107.  
  108. # 下拉框組件 
  109. number = tk.StringVar 
  110. numberChosen = ttk.Combobox(win, textvariable=number) 
  111. numberChosen['value'] = ('please select', '給我的顏值打個分吧!', '識別一下我的手勢', '智能人臉摳圖') 
  112.  
  113. numberChosen.current(0)  # 設(shè)置默認值為第一個,即默認下拉框中的內(nèi)容 
  114.  
  115. numberChosen.grid(row=1, column=1, rowspan=1, sticky=N + E + S + W) 
  116. # 下拉框觸發(fā)動作 (綁定點擊事件) 
  117. numberChosen.bind('<>', select_ttk) 
  118.  
  119. # 清除按鈕組件 
  120. tk.Button(win, cnf={'text': 'clear', 'command': clear_the_window}).grid(row=1, column=2, ipadx=1, sticky=N + E + S + W) 
  121.  
  122. # 退出按鈕組件 
  123. tk.Button(win, cnf={'text': 'exit', 'command': exit}).grid(row=1, column=3, ipadx=1, sticky=N + E + S + W) 
  124.  
  125. # 滾動文本框組件 
  126. scr = scrolledtext.ScrolledText(win) 
  127. scr.grid(row=2, column=1, columnspan=3, rowspan=1) 
  128.  
  129. # 使窗口一直顯示 
  130. win.mainloop() 

最后使用Pyinstaller打包即可。

Java 一次編譯到處運行,Python沒有這么好本事,Python有一個pyinstaller可以打包exe,在window平臺下運行,這也是Python非常不好的方面,而且打包出來的占用內(nèi)存非常的大

安裝:pip install pyinstaller。Pyinstaller具體參數(shù)如下所示。

注意點:有的時候在代碼最后面加上input(),這樣打開exe不會一散而過。由于上面代碼本身就是窗口一直顯示,無需加上input()。

在打包時候,并沒有提示錯誤,可以順利打包成 exe 文件。但是在運行打包好的軟件時,會提示找不到模塊,本人遇到的是找不到第三方模塊,例如 cv2 。這時候需要在打包時指定 -p 參數(shù),后面跟上 python 目錄下的第三方庫模板目錄路徑 site-packages ,再打包就成功了。

cd 到代碼的目錄執(zhí)行 pyinstaller main.py -F -p F:\anaconda\Lib\site-packages如果Pyinstaller打包報錯numpy.core.multiarray failed to import,這是numpy和opencv的不兼容,可以降低numpy的版本。


網(wǎng)站名稱:GUI布局Tkinter完善Python小項目
轉(zhuǎn)載來于:http://www.dlmjj.cn/article/dpecgsc.html