自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

GUI布局Tkinter完善Python小項目

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

[[356025]]

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

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

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

Tkinter 布局邏輯中最推薦使用的Grid布局。實現(xiàn)機(jī)制是將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è)置默認(rèn)值為第一個,即默認(rèn)下拉框中的內(nèi)容 
  114.  
  115. numberChosen.grid(row=1, column=1, rowspan=1, sticky=N + E + S + W) 
  116. # 下拉框觸發(fā)動作 (綁定點擊事件) 
  117. numberChosen.bind('<<ComboboxSelected>>', 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的版本。

 

責(zé)任編輯:姜華 來源: Python之王
相關(guān)推薦

2020-12-01 09:15:35

GUI

2023-05-09 08:24:13

PythonTkinterGUI編程

2023-11-30 15:02:34

Python

2023-11-23 13:10:24

Python框架

2023-11-27 19:42:56

Python GUI編程

2022-01-07 10:13:07

Tkinter代碼Kivy

2024-04-15 16:14:57

2020-12-08 22:05:37

Python編程語言開發(fā)

2025-03-24 10:55:53

2021-10-20 18:21:18

項目技術(shù)開發(fā)

2023-09-08 07:54:01

TkinterPython

2011-05-04 15:17:35

愛普生微型打印機(jī)

2025-02-18 08:30:00

GUIPythontkinter

2015-10-22 15:37:41

DockerDocker生態(tài)云服務(wù)

2023-11-29 07:30:08

Python用戶界面

2011-06-07 14:44:22

布線弱電

2014-11-05 12:17:57

移動醫(yī)療

2013-06-19 08:52:48

Unity3D

2011-07-08 14:51:05

2009-01-11 09:06:59

SonicWALLSNWLUTM
點贊
收藏

51CTO技術(shù)棧公眾號