Package: face-detectors
Face DetectorsThis repo contains various types of face detection techniques. All the face detection techniques are fine tunned and optimized out of the box to work the best with any resolution images and takes no time to get startedKey features:Easy to understand and setupEasy to manageRequires very less or no tuning for any resolution imageNo need to download models, they're automatically maintainedUses ultralight face detection models that is very fast on CPU aloneGet very good speed and accuracy on CPU aloneAll detectors share same parameters and methods, makes it easier to switch and goDetectors:Hog detectorCNN detectorCaffemodel detectorUltraLight 320 detectorUltraLight 640 detector( More on the way...)Quick usageLike said setup and usage is very simple and easy.Import the detector you want,Initialize it,Get predictsExamplefromface_detectorsimportUltralight320Detectorfromface_detectors.utilsimportannotate_imagedetector=Ultralight320Detector()image=cv2.imread("image.png")faces=detector.detect_faces(image)image=annotate_image(image,faces,width=3)cv2.imshow("view",image)cv2.waitKey(100000)PerformanceEvery detector has different types of features and can be used for different purposes for example detecting only one face we can use hog withnumber_of_times_to_upsample=1or caffemodel, we can also use models but other models like Ultralight models are good for multiple and small face detections.(The following is test on MacBook Pro 2.3 GHz Quad-Core Intel Core i5 with 8 GB 2133 MHz LPDDR3)DetectorIMAGE 1 (ms)IMAGE 2 (ms)IMAGE 3 (ms)IMAGE 4 (ms)Caffe Model0.03340.03270.03140.0344CNN0.52160.13710.43390.2264Hog0.09700.45210.08470.0548UltraLight(320px)0.01280.02030.01280.0149UltraLight(640px)0.03470.03910.04300.0384The below is IMAGE 2 resultView complete comparisonDocumentationBriefly describing face-detectors package that are all the detectors and utility functions.CaffeModel DetectorCaffemodel is very light weight model that uses less resources to perform detections that is created by caffe (Convolutional Architecture for Fast Feature Embedding).importcv2fromface_detectorsimportCaffemodelDetectorfromface_detectors.utilsimportannotate_imagevid=cv2.VideoCapture(0)detector=CaffemodelDetector()whileTrue:rect,frame=vid.read()ifnotrect:breakbbox=detector.detect_faces(frame)frame=annotate_image(frame,bbox)cv2.imshow("Caffe Model Detection",frame)cv2.waitKey(1)Configurable options for CaffeModel detector.Syntax:CaffemodelDetector(**options)OptionsDescriptionconvert_colorTakes OpenCV COLOR codes to convert the images. Defaults to cv2.COLOR_BGR2RGBconfidenceConfidence score is used to refrain from making predictions when it is not above a sufficient threshold. Defaults to 0.5scaleScales the image for faster output (No need to set this manually, scale will be determined automatically if no value is given)meanScalar with mean values which are subtracted from channels. Values are intended to be in (mean-R, mean-G, mean-B) order if image has BGR ordering and swapRB is true. Defaults to (104.0, 177.0, 123.0).scalefactorMultiplier for images values. Defaults to 1.0.cropFlag which indicates whether image will be cropped after resize or not. Defaults to False.swapRBFlag which indicates that swap first and last channels in 3-channel image is necessary. Defaults to False.transposeTranspose image. Defaults to False.resizeSpatial size for output image. Default is (300, 300)Useful methods for this detector:detect_faces(image)This method will return coordinates for all the detected faces of the given imageOptionsDescriptionimageimage in numpy array formatdetect_faces_keypoints(image, get_all=false)This method will return coordinates for all the detected faces along with their facial keypoints of the given image. Keypoints are detected using dlib's new shape_predictor_68_face_landmarks_GTX.dat` model.Note: Generating keypoints might take more time if compared withdetect_facesmethodOptionsDescriptionimageImage in numpy array formatget_allWeather to get all facial keypoints or the main (chin, nose, eyes, mouth)CNN DetectorCNN (Convolutional Neural Network) might not be a light weight model but it is good at detecting faces from all angles. This detector is a hight level wrapper arounddlib::cnn_face_detection_model_v1that is fine tuned to improve overall performance and accuracy.importcv2fromface_detectorsimportCNNDetectorfromface_detectors.utilsimportannotate_imagevid=cv2.VideoCapture(0)detector=CNNDetector()whileTrue:rect,frame=vid.read()ifnotrect:breakbbox=detector.detect_faces(frame)frame=annotate_image(frame,bbox)cv2.imshow("CNN Detection",frame)cv2.waitKey(1)Configurable options for CNNDetector detector.Syntax:CNNDetector(**options)OptionsDescriptionconvert_colorTakes OpenCV COLOR codes to convert the images. Defaults to cv2.COLOR_BGR2RGBnumber_of_times_to_upsampleUp samples the image number_of_times_to_upsample before running the basic detector. By default is 1.confidenceConfidence score is used to refrain from making predictions when it is not above a sufficient threshold. Defaults to 0.5scaleScales the image for faster output (No need to set this manually, scale will be determined automatically if no value is given)detect_faces(image)This method will return coordinates for all the detected faces of the given imageOptionsDescriptionimageimage in numpy array formatdetect_faces_keypoints(image, get_all=false)This method will return coordinates for all the detected faces along with their facial keypoints of the given image. Keypoints are detected using dlib's newshape_predictor_68_face_landmarks_GTX.datmodel.Note: Generating keypoints might take more time if compared withdetect_facesmethodOptionsDescriptionimageImage in numpy array formatget_allWeather to get all facial keypoints or the main (chin, nose, eyes, mouth)Hog DetectorThis detector uses Histogram of Oriented Gradients (HOG) and Linear SVM classifier for face detection. It is also combined with an image pyramid and a sliding window detection scheme.HogDetectoris a high level client over dlib's hog face detector and is fine tuned to make it more optimized in both speed and accuracy.If you want to detect faster withHogDetectorand don't care about number of detections then setnumber_of_times_to_upsample=1in the options, it will detect less fasces in less time, mainly used for real time one face detection.importcv2fromface_detectorsimportHogDetectorfromface_detectors.utilsimportannotate_imagevid=cv2.VideoCapture(0)detector=HogDetector()whileTrue:rect,frame=vid.read()ifnotrect:breakbbox=detector.detect_faces(frame)frame=annotate_image(frame,bbox)cv2.imshow("Hog Detection",frame)cv2.waitKey(1)Configurable options for HogDetector detector.Syntax:HogDetector(**options)OptionsDescriptionconvert_colorTakes OpenCV COLOR codes to convert the images. Defaults to cv2.COLOR_BGR2RGBnumber_of_times_to_upsampleUp samples the image number_of_times_to_upsample before running the basic detector. By default is 2.confidenceConfidence score is used to refrain from making predictions when it is not above a sufficient threshold. Defaults to 0.5scaleScales the image for faster output (No need to set this manually, scale will be determined automatically if no value is given)detect_faces(image)This method will return coordinates for all the detected faces of the given imageOptionsDescriptionimageimage in numpy array formatdetect_faces_keypoints(image, get_all=false)This method will return coordinates for all the detected faces along with their facial keypoints of the given image. Keypoints are detected using dlib's newshape_predictor_68_face_landmarks_GTX.datmodel.Note: Generating keypoints might take more time if compared withdetect_facesmethodOptionsDescriptionimageImage in numpy array formatget_allWeather to get all facial keypoints or the main (chin, nose, eyes, mouth)Ultra Light Detection (320px)Ultra Light detection model is what the name says, it a very light weight, accuracy with impressive speed which is pre-trained on 320x240 sized images and only excepts 320x240 sized images but don't worryUltralight320Detectordetector will do all for you.importcv2fromface_detectorsimportUltralight320Detectorfromface_detectors.utilsimportannotate_imagevid=cv2.VideoCapture(0)detector=Ultralight320Detector()whileTrue:rect,frame=vid.read()ifnotrect:breakbbox=detector.detect_faces(frame)frame=annotate_image(frame,bbox)cv2.imshow("Ultra 320 Detection",frame)cv2.waitKey(1)Configurable options for Ultralight320Detector detector.Syntax:Ultralight320Detector(**options)OptionsDescriptionconvert_colorTakes OpenCV COLOR codes to convert the images. Defaults to cv2.COLOR_BGR2RGBmeanMetric used to measure the performance of models doing detection tasks. Defaults to [127, 127, 127].confidenceConfidence score is used to refrain from making predictions when it is not above a sufficient threshold. Defaults to 0.5scaleScales the image for faster output (No need to set this manually, scale will be determined automatically if no value is given)cacheIt uses same model for all the created sessions. Default is Truedetect_faces(image)This method will return coordinates for all the detected faces of the given imageOptionsDescriptionimageimage in numpy array formatdetect_faces_keypoints(image, get_all=false)This method will return coordinates for all the detected faces along with their facial keypoints of the given image. Keypoints are detected using dlib's newshape_predictor_68_face_landmarks_GTX.datmodel.Note: Generating keypoints might take more time if compared withdetect_facesmethodOptionsDescriptionimageImage in numpy array formatget_allWeather to get all facial keypoints or the main (chin, nose, eyes, mouth)Ultra Light Detection (640px)Ultra Light detection model is what the name says, it a very light weight, accuracy with impressive speed which is pre-trained on 640x480 sized images and only excepts 640x480 sized images but don't worryUltralight640Detectordetector will do all for you.This detector will be more accurate than 320 sized ultra light model (Ultralight320Detector) but might take a little more time.importcv2fromface_detectorsimportUltralight640Detectorfromface_detectors.utilsimportannotate_imagevid=cv2.VideoCapture(0)detector=Ultralight640Detector()whileTrue:rect,frame=vid.read()ifnotrect:breakbbox=detector.detect_faces(frame)frame=annotate_image(frame,bbox)cv2.imshow("Ultra 640 Detection",frame)cv2.waitKey(1)Configurable options for Ultralight640Detector detector.Syntax:Ultralight640Detector(**options)OptionsDescriptionconvert_colorTakes OpenCV COLOR codes to convert the images. Defaults to cv2.COLOR_BGR2RGBmeanMetric used to measure the performance of models doing detection tasks. Defaults to [127, 127, 127].confidenceConfidence score is used to refrain from making predictions when it is not above a sufficient threshold. Defaults to 0.5scaleScales the image for faster output (No need to set this manually, scale will be determined automatically if no value is given)cacheIt uses same model for all the created sessions. Default is Truedetect_faces(image)This method will return coordinates for all the detected faces of the given imageOptionsDescriptionimageimage in numpy array formatdetect_faces_keypoints(image, get_all=false)This method will return coordinates for all the detected faces along with their facial keypoints of the given image. Keypoints are detected using dlib's newshape_predictor_68_face_landmarks_GTX.datmodel.Note: Generating keypoints might take more time if compared withdetect_facesmethodOptionsDescriptionimageImage in numpy array formatget_allWeather to get all facial keypoints or the main (chin, nose, eyes, mouth)Annotate Image FunctionAnnotates the given image with the payload returned by any of the detectors and returns a well annotated image with boxes and keypoints on the faces.Configurable options for annotate_image function.Syntax:annotate_image(**options)OptionsDescriptionimageGive image for annotation in numpy.Array formatfacesPayload returned by detector.detect_faces or detector.detect_faces_keypointsbox_rgbRGB color for rectangle to be of. Defaults to (100, 0, 255).keypoints_rgbRGB color for keypoints to be of. Defaults to (150, 0, 255).widthWidth of annotations. Defaults to 2License