简体   繁体   中英

How can I build a face detection aplication by implementing my own CNN and train it from scratch?

The aplication only takes some images and draws some rectangles around the faces that are present there. I used some CNNs made in Python with keras for image classification Face and NonFace here are the types of images that I used link . My only problem is that I don't quite understand how to use a CNN to detect MULTIPLE faces from an image, I tried to verify every frame of an image but that takes to much time and it's not good at all. Can I train a model to actually return the location of my face or do I have to make an algorithm that searches for the faces? Thanks, any help would be much apreaciated.

Have look at the YOLO algorithm, it uses a grid on the image and outputs bounding boxes, giving you location of the object.

you can detect all the faces in an image using mtcnn. The code for that is shown below

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM