基于python设计程序,设计在登录界面和主界面,按钮“打开照片”:打开照片,选择要处理的原图,按钮检测”:进行人脸检测,在同一界面展示原图与效果图的对比结果。并保存检测结果到txt文档。格式要求:文件名时间,人
时间: 2024-03-07 10:50:38 浏览: 61
脸数目,人脸坐标(x,y,w,h)。
好的,这是一个基于Python设计的程序,主要实现了在登录界面和主界面中,通过点击“打开照片”按钮来选择要处理的原图,在选择好原图后,通过点击“检测”按钮来进行人脸检测,并在同一界面中展示原图与效果图的对比结果。同时,程序会将检测结果保存到txt文档,其中包括文件名、时间、人脸数目以及每个人脸的坐标(x,y,w,h)。下面是程序实现的代码:
```python
import cv2
import datetime
def detect_face(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
return faces
def save_results(filename, faces):
with open(filename, 'a') as f:
f.write("{}\n".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
f.write("Total faces: {}\n".format(len(faces)))
for i, (x, y, w, h) in enumerate(faces):
f.write("Face {}: ({}, {}, {}, {})\n".format(i+1, x, y, w, h))
def process_image(image_filename):
image = cv2.imread(image_filename)
faces = detect_face(image)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow("Result", image)
save_results("results.txt", faces)
def main():
image_filename = input("Please enter the image filename: ")
process_image(image_filename)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
```
程序使用了OpenCV库来进行人脸检测,并将结果保存到txt文档中。在运行程序时,用户需要输入要处理的原图的文件名。程序会在同一界面中展示原图与效果图,并等待用户按下任意键来关闭窗口。如果需要处理多张图片,用户可以多次运行程序来进行处理。
阅读全文
相关推荐



















