基于python设计程序,设计在登录界面和主界面,按钮“打开照片”:打开照片,选择要处理的原图,按钮"检测”:进行人脸检测。并保存检测结果到txt文档。格式要求:文件名时间,人脸数量等。
时间: 2024-03-07 21:50:06 浏览: 23
好的,我可以提供一份基于 Python 的代码示例,供您参考:
```python
import tkinter as tk
from tkinter import filedialog
import cv2
import time
def open_image():
global img_path
img_path = filedialog.askopenfilename(title="选择要处理的照片", filetypes=[("图片文件", "*.jpg;*.png;*.bmp")])
def detect_faces():
global img_path
if img_path:
img = cv2.imread(img_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
if faces is not None:
face_count = len(faces)
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
result = f"文件名:{img_path}\n时间:{timestamp}\n人脸数量:{face_count}\n\n"
with open("result.txt", "a") as f:
f.write(result)
else:
print("未检测到人脸!")
# 创建登录界面
login_window = tk.Tk()
login_window.title("登录界面")
# 创建主界面
main_window = tk.Tk()
main_window.title("主界面")
# 创建登录界面的组件
login_button = tk.Button(login_window, text="登录", command=main_window.deiconify)
login_button.pack()
# 创建主界面的组件
open_button = tk.Button(main_window, text="打开照片", command=open_image)
open_button.pack()
detect_button = tk.Button(main_window, text="检测", command=detect_faces)
detect_button.pack()
# 隐藏主界面
main_window.withdraw()
# 运行程序
login_window.mainloop()
```
在这段代码中,我们使用了 Python 的 tkinter 模块来创建登录界面和主界面,使用 tkinter.filedialog 模块来实现打开照片的功能,使用 OpenCV 模块来实现人脸检测的功能,使用 Python 的文件操作函数来实现将检测结果保存到 txt 文档中的功能。
具体来说,我们在 `open_image` 函数中使用了 `filedialog.askopenfilename` 函数来打开照片,并将其路径保存到全局变量 `img_path` 中。在 `detect_faces` 函数中,我们首先使用 OpenCV 的 `cv2.imread` 函数读取照片,然后将其转换成灰度图像,并使用 `cv2.CascadeClassifier` 函数加载人脸检测器。接着,我们使用 `detectMultiScale` 函数检测人脸,并计算出人脸数量。最后,我们使用 Python 的时间模块获取当前时间,将检测结果写入 txt 文档中。
在主程序中,我们创建了登录界面和主界面,并使用 `main_window.withdraw()` 函数将主界面隐藏起来,只有在登录成功后才会显示。在登录界面中,我们创建了一个登录按钮,点击后可以打开主界面。在主界面中,我们创建了一个打开照片的按钮和一个检测按钮,点击后可以执行相应的功能。
请注意,这只是一个简单的示例程序,您需要根据自己的具体需求和技能水平进行修改和完善。