def __init__(self): self.labels = ["male", "female"] self.mean = (78.4263377603, 87.7689143744, 114.895847746) self.f_detector = FDetector() self.web_util = web() self.f_detector.set_detector("hog") self.__set_detector()
from visionlib.face.detection import FDetector import cv2 import argparse # Configre the parser. parser = argparse.ArgumentParser() parser.add_argument("img_path", help="Path to image") parser.add_argument("--enable-gpu", help="Set to true to enable gpu support", dest="enable_gpu", default=False, type=bool) args = parser.parse_args() # Instantiating the required classes. detector = FDetector() # Read the image img = cv2.imread(args.img_path) detector.set_detector("hog") # Apply face detection and show image d_img, boxes, conf = detector.detect_face(img, show=True, enable_gpu=args.enable_gpu) for box, conf in zip(boxes, conf): print(box, conf)
"--face-model", help="Model to use for detecting face", dest="face_model", default="hog", ) parser.add_argument( "--enable-gpu", help="Set to true to enable gpu support", dest="enable_gpu", default=False, type=bool, ) args = parser.parse_args() # Instantiating the required class fdetector = FDetector() kdetector = KDetector() # Read the image img = cv2.imread(args.img_path) # Set the detectors kdetector.set_detector(args.key_model) fdetector.set_detector(args.face_model) # Detect face and the points fimg, boxes, conf = fdetector.detect_face(img, enable_gpu=True) points = kdetector.detect_keypoints(img, rects=boxes) # Draw the points pimg = kdetector.draw_points(fimg, points, show=True)
from visionlib.face.detection import FDetector import cv2 import argparse # Configre the parser. parser = argparse.ArgumentParser() parser.add_argument("vid_path", help="Path to image") parser.add_argument("--enable-gpu", help="Set to true to enable gpu support", dest="enable_gpu", default=False, type=bool) args = parser.parse_args() # Instantiating the required classes. detector = FDetector() detector.set_detector("dnn") # Read the video and apply face detection. detection = detector.vdetect_face(args.vid_path, show=True, enable_gpu=args.enable_gpu) for img, box, conf in detection: print(box, conf)
def setUp(self): self.detectors = ["mtcnn", "haar", "hog", "dnn"] self.face_detector = FDetector() self.group_img_path = "assets/face.jpg" self.test_img = cv2.imread(self.group_img_path) logging.basicConfig(level=logging.INFO)
class GDetector: """ This class contains all functions to detect gender of a given face . . . Methods: detect_gender(): Used to detect gender from an face. Returns Predicted Gender and confidence. """ def __init__(self): self.labels = ["male", "female"] self.mean = (78.4263377603, 87.7689143744, 114.895847746) self.f_detector = FDetector() self.web_util = web() self.f_detector.set_detector("hog") self.__set_detector() def __set_detector(self): model_name = 'gender_net.caffemodel' model_url = 'https://github.com/arunponnusamy/cvlib-files/releases/download/v0.1/gender_net.caffemodel' proto_name = 'gender_deploy.prototxt' proto_url = 'https://download.cvlib.net/config/gender_detection/gender_deploy.prototxt' self.model = self.web_util.download_file(model_url, model_name) self.proto = self.web_util.download_file(proto_url, proto_name) self.model = cv2.dnn.readNet(self.proto, self.model) def detect_gender(self, img=None, enable_gpu=False): '''This method is used to detect gender from an image. Args: img (numpy array) This argument must the output which similar to opencv's imread method's output. enable_gpu (bool) : Set to True if You want to use gpu for prediction. Returns: str : Returns the predicted gender. int : Returns the confidence for the predicted gender. ''' if img != []: blob = cv2.dnn.blobFromImage(img, 1.0, (227, 227), self.mean, swapRB=False) if enable_gpu: self.model.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA) self.model.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA) self.model.setInput(blob) preds = self.model.forward() preds = preds[0] b_pred = self.labels[np.argmax(preds)] b_confidence = preds[np.argmax(preds)] return (b_pred, b_confidence) else: print("img is none")
from visionlib.gender.detection import GDetector from visionlib.utils.imgutils import Image import cv2 import argparse # Configre the parser. parser = argparse.ArgumentParser() parser.add_argument("img_path", help="Path to image") parser.add_argument("--enable-gpu", help="Set to true to enable gpu support", dest="enable_gpu", default=False, type=bool) args = parser.parse_args() # Instantiating the required classes. Fdetector = FDetector() Gdetector = GDetector() im_utils = Image() # Read the image img = cv2.imread(args.img_path) Fdetector.set_detector("hog") # Detect the Faces d_img, boxes, conf = Fdetector.detect_face(img) for box in boxes: # Get the face by cropping c_img = im_utils.crop(d_img, box) # Apply Gender Detection gender = Gdetector.detect_gender(c_img, enable_gpu=args.enable_gpu) # format the label label = "{}: {:.2f}%".format(gender[0], gender[1] * 100) # Put padding for rendering the label
help="The model to use for detection", dest="model") parser.add_argument("--enable-gpu", help="Set to true to enable gpu support", dest="enable_gpu", default=False, type=bool) parser.add_argument("--cam", help="Set to True if you are using webcam", dest="cam", default=False, type=bool) args = parser.parse_args() # Instantiating the required classes. detector = FDetector() Gdetector = GDetector() im_utils = Image() detector.set_detector(args.model) # Read the video detection = detector.vdetect_face(args.vid_path, cam=args.cam) run = True while run: # Get frames and bounding boxes of faces for img, boxes, conf in detection: for box in boxes: # Get the face by cropping c_img = im_utils.crop(img, box) # Apply Gender Detection gender = Gdetector.detect_gender(c_img, enable_gpu=args.enable_gpu)
from visionlib.utils.imgutils import Image import cv2 import argparse # Configre the parser. parser = argparse.ArgumentParser() parser.add_argument("vid_path", help="Path to image") parser.add_argument("--enable-gpu", help="Set to true to enable gpu support", dest="enable_gpu", default=False, type=bool) args = parser.parse_args() # Instantiating the required classes. detector = FDetector() Gdetector = GDetector() im_utils = Image() detector.set_detector("dnn") # Read the video detection = detector.vdetect_face(args.vid_path) run = True while run: # Get frames and bounding boxes of faces for img, boxes, conf in detection: for box in boxes: # Get the face by cropping c_img = im_utils.crop(img, box) # Apply Gender Detection gender = Gdetector.detect_gender(c_img, enable_gpu=args.enable_gpu)