如何通过实现自己的CNN并从头开始训练来构建人脸检测应用程序?

2024-10-01 22:30:37 发布

您现在位置:Python中文网/ 问答频道 /正文

应用程序仅拍摄一些图像,并围绕存在的面绘制一些矩形。 我使用了一些用Python和keras制作的CNN进行图像分类Face和Noface这里是我使用的图像类型link。我唯一的问题是,我不太明白如何使用CNN来检测图像中的多张脸,我试图验证图像的每一帧,但这需要很多时间,而且一点都不好。我可以训练一个模型来实际返回我的脸的位置吗?或者我必须做一个搜索脸的算法吗? 谢谢,任何帮助都会很合适的


Tags: 模型图像算法应用程序类型时间绘制link
2条回答

看看YOLO算法,它在图像上使用网格,并输出边界框,为您提供对象的位置

您可以使用mtcnn检测图像中的所有人脸。其代码如下所示

from mtcnn import MTCNN
import os
import cv2
detector = MTCNN()
dest_dir=r'C:\Temp\people\storage\cropped' # specify where to save the images
filename=r'C:\Temp\people\storage\34.png' # specify the file name full path
try:
    img=cv2.imread(filename) # filename must be full path to the image
    shape=img.shape # will cause an exception if image was not read properly
    data=detector.detect_faces(img)                
    if data ==[]:
        print ('no faces were detected for file ', filename)
    else:
        for i, faces  in enumerate(data):
            box= faces['box']
            if box != []:
                box[0]= 0 if box[0]<0 else box[0]
                box[1]= 0 if box[1]<0 else box[1]            
                cropped_img=img[box[1]: box[1]+box[3],box[0]: box[0]+ box[2]]               
                fname=os.path.split(filename)[1]
                index=fname.rfind('.')
                fileid=fname[:index]
                fext=fname[index:]
                fname=fileid + '-' +str(i) + fext                
                save_path=os.path.join(dest_dir,fname )                
                cv2.imwrite(save_path, cropped_img)
except:
    print(' an error occurred')

相关问题 更多 >

    热门问题