Home>

Reference site

I have a face detection&stamp program with reference to the above site, but I had one question.
I didn't understand it even after checking it, so I asked a question.

I don't understand the meaning of the number in the second argument of dlib.get_frontal_face_detector ().
Could you tell me?

(Hereafter detector.py in the site)

#coding: utf-8
import json
import math
import os
import cv2
import dlib

def main ():
    ## input path and read image
    img_path = "../data/img/" + os.listdir ("../ data/img /") [0]
    img_filename = os.path.basename (img_path)
    img_ext = img_filename.split (".") [-1]
    img = cv2.imread (img_path, cv2.IMREAD_COLOR)
    if img is None:
        print ("Could not read input image")
        exit ()

    ## instance detector/predictor
    detector = dlib.get_frontal_face_detector ()
    predictor = dlib.shape_predictor ("predictor/shape_predictor_68_face_landmarks.dat")

    ## detect faces
## I don't understand the meaning of the second argument below! !! !! !!
    faces = detector (img, 1)
    if len (faces) == 0:
        print ("Could not detect any faces")
        exit ()

    ## compute face properties
    face_properties = {}
    for i, f in enumerate (faces):
        ## get 68 points
        points = predictor (img, f) .parts ()
        ## Left/Right temples
        t_L = points [0]
        t_R = points [16]
        ## calc properties
        face_width = math.sqrt ((t_L.x --t_R.x) ** 2 + (t_L.y --t_R.y) ** 2)
        face_center = [(t_L.x + t_R.x)/2, (t_L.y + t_L.y)/2]
        degree_acw = -1 * math.degrees (math.atan2 ((t_R.y --t_L.y), (t_R.x --t_L.x)))
        ## add result
        face_properties [i] = {
            "stamp": "01.png",
            "center": face_center,
            "width": face_width,
            "angle": degree_acw
        }
    ## save
    with open ("../ data/json /" + img_filename.replace (img_ext, "json"), "w") as f:
        json.dump (face_properties, f, ensure_ascii = False, indent = 4, separators = (',',':'))

if __name__ == "__main__":
    main ()
  • Answer # 1

    It is a parameter called upsample_num_times.
    It seems that the original image is upsampled that number of times before it is applied to the detector.
    http://dlib.net/python/index.html

    The higher the number, the smaller the area that can be detected, but the larger the processing time and memory required.
    The value of the default parameter is 0, and the smallest area detected at that time is 80x80. It seems that 1 is 40x40 and 2 is 20x20.
    https://qiita.com/nonbiri15/items/9561c8194ba0b2041bd0 # About the minimum detection size of face detection with dlib