Example #1
0
    def detect(self,img,face_records,options):
        '''Run a face detector and return rectangles.'''
        
        # TODO: Make this an option
        detection_threshold = options.threshold
        if options.best:
            detection_threshold = 0.01
        
        
        #thresh=.5
        #hier_thresh=.5
        #nms=.45

        thresh=self.t1
        hier_thresh=self.t2
        #nms=self.t_nms
        
        start = time.time()
        
        # Reformat the image for darknet
        img = (img/255.0).astype(np.float32)
        img = np.transpose(img,(2,0,1)).copy()
        
        c,h,w = img.shape
        
        im = IMAGE(w,h,c,img.ctypes.data_as(ctypes.POINTER(ctypes.c_float)))
        
        num = c_int(0)
        pnum = pointer(num)
        self.predict_image(self.net, im)
        dets = self.get_network_boxes(self.net, im.w, im.h, thresh, hier_thresh, None, 0, pnum)
        num = pnum[0]
        if (detection_threshold > 0.0): self.do_nms_obj(dets, num, self.meta.classes, detection_threshold);

        # print('time checkBB:',time.time()-start)
    
        res = []
        for j in range(num):
            for i in range(self.meta.classes):
                if dets[j].prob[i] > 0:
                    b = dets[j].bbox
                    res.append((self.meta.names[i], dets[j].prob[i], (b.x, b.y, b.w, b.h)))
        res = sorted(res, key=lambda x: -x[1])
        
        #self.free_image(im)
        self.free_detections(dets, num)
                
        k = 0
        for each in res:
            name,prob,rect = each

            face_record = face_records.face_records.add()
            face_record.detection.score = prob
            face_record.detection.location.CopyFrom(pt.rect_val2proto(rect[0]-0.5*rect[2],rect[1]-0.5*rect[3],rect[2],rect[3]))
            face_record.detection.detection_id = k
            face_record.detection.detection_class = name
            k += 1

        
        return                
Example #2
0
    def detect(self, img, face_records, options):
        '''Run a face detector and return rectangles.'''

        detection_threshold = options.threshold
        print('Detection Threshold', detection_threshold)
        # TODO: Make this an option
        if options.best:
            detection_threshold = -1.5

        # Run the detector on the image
        dets = self.detector(img, 1)

        # Now process each face we found and add a face to the records list.
        for k, d in enumerate(dets):
            face_record = face_records.face_records.add()
            face_record.detection.score = d.confidence
            face_record.detection.location.CopyFrom(
                pt.rect_val2proto(d.rect.left(), d.rect.top(), d.rect.width(),
                                  d.rect.height()))
            face_record.detection.detection_id = k
            face_record.detection.detection_class = "FACE_%d" % k
            shape = self.shape_pred(img, d.rect)
            for i in range(len(shape.parts())):
                loc = shape.parts()[i]
                landmark = face_record.landmarks.add()
                landmark.landmark_id = "point_%02d" % i
                landmark.location.x = loc.x
                landmark.location.y = loc.y

        if options.best:
            face_records.face_records.sort(key=lambda x: -x.detection.score)
            while len(face_records.face_records) > 1:
                del face_records.face_records[-1]
Example #3
0
def filterDetectBest(face_records, im, best):
    # TODO: This is a temporary fix.
    if best and len(face_records.face_records) > 1:
        print(
            "WARNING: detector service does not seem to support best mode.  Too many faces returned."
        )
        face_records.face_records.sort(key=lambda x: -x.detection.score)
        #print(detections.detections)
        while len(face_records.face_records) > 1:
            del face_records.face_records[-1]

        assert len(face_records.face_records) == 1

    if best and len(face_records.face_records) == 0:
        print(
            "WARNING: detector service does not seem to support best mode.  No faces returned."
        )

        # in this case select the center of the image
        det = face_records.face_records.add().detection
        h, w = im.shape[:2]
        s = 0.8 * min(w, h)
        det.location.CopyFrom(
            pt.rect_val2proto(0.5 * w - 0.5 * s, 0.5 * h - 0.5 * s, s, s))
        det.score = -1.0

        det.detection_id = 1

        assert len(face_records.face_records) == 1

    return face_records
Example #4
0
    def detect(self, img, face_records, options):
        '''Run a face detector and return rectangles.'''
        print('Running Face Detector For ArchFace')

        #print(options.threshold)
        # Run the detector on the image
        dets, lpts = self.detector.detect(img,
                                          threshold=options.threshold,
                                          scale=1)
        # Now process each face we found and add a face to the records list.
        for idx in range(0, dets.shape[0]):
            face_record = face_records.face_records.add()
            face_record.detection.score = dets[idx, -1:]
            ulx, uly, lrx, lry = dets[idx, :-1]
            #create_square_bbox = np.amax(abs(lrx-ulx) , abs(lry-uly))
            face_record.detection.location.CopyFrom(
                pt.rect_val2proto(ulx, uly, abs(lrx - ulx), abs(lry - uly)))
            face_record.detection.detection_id = idx
            face_record.detection.detection_class = "FACE_%d" % idx
            lmark = face_record.landmarks.add()
            lmarkloc = lpts[idx]
            for ldx in range(0, lmarkloc.shape[0]):
                lmark.landmark_id = "point_%02d" % ldx
                lmark.location.x = lmarkloc[ldx][0]
                lmark.location.y = lmarkloc[ldx][1]

        if options.best:
            face_records.face_records.sort(key=lambda x: -x.detection.score)

            while len(face_records.face_records) > 1:
                del face_records.face_records[-1]

        print('Done Running Face Detector For ArchFace')
Example #5
0
    def detect(self, img, face_records, options):
        '''Run a face detector and return rectangles.'''
        #print('Running Face Detector For ArchFace')
        img = img[:, :, ::
                  -1]  #convert from rgb to bgr . There is a reordering from bgr to RGB internally in the detector code.
        dets, lpts = self.detector.detect(img,
                                          threshold=options.threshold,
                                          scale=1)
        #print('Number of detections ', dets.shape[0])
        # Now process each face we found and add a face to the records list.
        for idx in range(0, dets.shape[0]):
            face_record = face_records.face_records.add()
            try:  # todo: this seems to be different depending on mxnet version
                face_record.detection.score = dets[idx, -1:]
            except:
                face_record.detection.score = dets[idx, -1:][0]
            ulx, uly, lrx, lry = dets[idx, :-1]
            #create_square_bbox = np.amax(abs(lrx-ulx) , abs(lry-uly))
            face_record.detection.location.CopyFrom(
                pt.rect_val2proto(ulx, uly, abs(lrx - ulx), abs(lry - uly)))
            face_record.detection.detection_id = idx
            face_record.detection.detection_class = "FACE_%d" % idx
            #lmark = face_record.landmarks.add()
            lmarkloc = lpts[idx]
            for ldx in range(0, lmarkloc.shape[0]):
                lmark = face_record.landmarks.add()
                lmark.landmark_id = "point_%02d" % ldx
                lmark.location.x = lmarkloc[ldx][0]
                lmark.location.y = lmarkloc[ldx][1]

        if options.best:
            face_records.face_records.sort(key=lambda x: -x.detection.score)

            while len(face_records.face_records) > 1:
                del face_records.face_records[-1]
Example #6
0
    def detect(self,img,face_records,options):
        '''Run a face detector and return rectangles.'''
        
        # TODO: Make this an option
        detection_threshold = options.threshold
        if options.best:
            detection_threshold = -1.5
        
        # Run the detector on the image
        dets, scores, idx = self.detector.run(img, 1, detection_threshold)


        # Now process each face we found and add a face to the records list.
        for k, d in enumerate(dets):
            face_record = face_records.face_records.add()
            face_record.detection.score = scores[k]
            face_record.detection.location.CopyFrom(pt.rect_val2proto(d.left(), d.top(), d.width(), d.height()))
            face_record.detection.detection_id = k
            face_record.detection.detection_class = "FACE_%d"%idx[k]

        if options.best:
            face_records.face_records.sort(key = lambda x: -x.detection.score)
            while len(face_records.face_records) > 1:
                del face_records.face_records[-1]
Example #7
0
    def detect(self, img, face_records, options):
        detected_templates = self._detect(img, options)

        for i in range(0, self.num_faces):
            curr_template = roc.roc_template_array_getitem(
                detected_templates, i)
            if curr_template.algorithm_id & roc.ROC_INVALID or curr_template.algorithm_id == 0:
                continue
            else:
                face_record = face_records.face_records.add()
                face_record.detection.score = curr_template.detection.confidence
                xc, yc, w, h = curr_template.detection.x, curr_template.detection.y, curr_template.detection.width, curr_template.detection.height
                x = int(xc - (w * 0.5))
                y = int(yc - (w * 0.5))
                face_record.detection.location.CopyFrom(
                    pt.rect_val2proto(x, y, w, h))
                face_record.detection.detection_id = i
                face_record.detection.detection_class = "FACE"
                face_record.template.buffer = self._rocFlatten(curr_template)

        #Free all the roc stuff
        for i in range(0, self.num_faces):
            roc.roc_free_template(
                roc.roc_template_array_getitem(detected_templates, i))
Example #8
0
    def detect(self, img, face_records, options):
        '''Run a face detector and return rectangles.'''

        # Load the model
        mat = img
        #print('options<',options,'>')
        #thresh = options.threshold

        #result_id = 0

        #im = mat #cv2.imread(pathname)
        timer = pv.Timer()

        # Run the network in the worker processes...
        dets = self.runNetwork(mat, options)

        #dets = as_result.get()

        dets[:, 2] = dets[:, 2] - dets[:, 0] + 1
        dets[:, 3] = dets[:, 3] - dets[:, 1] + 1
        timer.mark("End Detection")

        #print('dets')
        # Now process each face we found and add a face to the records list.
        for k, d in enumerate(dets):
            face_record = face_records.face_records.add()
            face_record.detection.score = d[4]
            face_record.detection.location.CopyFrom(
                pt.rect_val2proto(d[0], d[1], d[2], d[3]))
            face_record.detection.detection_id = k
            face_record.detection.detection_class = "FACE"

        if options.best:
            face_records.face_records.sort(key=lambda x: -x.detection.score)
            while len(face_records.face_records) > 1:
                del face_records.face_records[-1]
Example #9
0
    def detect(self, img, face_records, options):
        '''Run a face detector and return rectangles.'''
        #print('Running Face Detector For ArchFace')
        img = img[:, :, ::
                  -1]  #convert from rgb to bgr . There is a reordering from bgr to RGB internally in the detector code.

        faces = self.app.get(img)

        #print('Number of detections ', dets.shape[0])

        # Now process each face we found and add a face to the records list.
        idx = -1
        for face in faces:
            idx += 1
            face_record = face_records.face_records.add()

            face_record.detection.score = face.det_score
            ulx, uly, lrx, lry = face.bbox
            #create_square_bbox = np.amax(abs(lrx-ulx) , abs(lry-uly))
            face_record.detection.location.CopyFrom(
                pt.rect_val2proto(ulx, uly, abs(lrx - ulx), abs(lry - uly)))
            face_record.detection.detection_id = idx
            face_record.detection.detection_class = "FACE"
            #lmark = face_record.landmarks.add()
            for ldx in range(0, face.kps.shape[0]):
                lmark = face_record.landmarks.add()
                lmark.landmark_id = "point_%02d" % ldx
                lmark.location.x = face.kps[ldx][0]
                lmark.location.y = face.kps[ldx][1]

            normed_embedding = face.normed_embedding

            face_record.template.data.CopyFrom(
                pt.vector_np2proto(normed_embedding))

            if hasattr(face, 'landmark_2d_106'):
                attribute = face_record.attributes.add()
                attribute.key = 'landmark_2d_106'
                attribute.matrix.CopyFrom(
                    pt.matrix_np2proto(face.landmark_2d_106))

            if hasattr(face, 'landmark_3d_68'):
                attribute = face_record.attributes.add()
                attribute.key = 'landmark_3d_68'
                attribute.matrix.CopyFrom(
                    pt.matrix_np2proto(face.landmark_3d_68))

            if hasattr(face, 'sex'):
                attribute = face_record.attributes.add()
                attribute.key = 'sex'
                attribute.text = face.sex

            if hasattr(face, 'age'):
                attribute = face_record.attributes.add()
                attribute.key = 'age'
                attribute.fvalue = face.age

        if options.best:
            face_records.face_records.sort(key=lambda x: -x.detection.score)

            while len(face_records.face_records) > 1:
                del face_records.face_records[-1]