Example #1
0
    def echo(self, mat):
        '''
        '''
        request = pt.matrix_np2proto(mat)

        # Run the computation on the server
        dist_mat = self.service_stub.echo(request, None)

        return pt.matrix_proto2np(dist_mat)
Example #2
0
    def score(self, score_request):
        '''Compare templates to produce scores.'''
        score_type = self.scoreType()
        result = geo.Matrix()

        # Check that this is a known score type
        if score_type not in [fsd.SERVER]:
            raise NotImplementedError("Score type <%s> not implemented." %
                                      (score_type, ))

        # Check to make sure the probe and gallery records are correct
        if len(score_request.template_probes.templates) == 0:
            raise ValueError("no probe templates were found in the arguments.")

        if len(score_request.template_gallery.templates) == 0:
            raise ValueError(
                "no gallery templates were found in the arguments.")

        #THIS IS NOT NECESSAY AS WE ARE ALWAYS COPYING THE TEMPLATES AND NOT USING FACE RECORD -> REFER TO
        #FUNCTION in FaceClient.py
        '''
        if min(len(score_request.face_probes.face_records),len(score_request.template_probes.templates)) != 0:
            raise ValueError("probes argument cannot have both face_probes and template_probes defined.")
        if max(len(score_request.face_probes.face_records),len(score_request.template_probes.templates)) == 0:
            raise ValueError("no probe templates were found in the arguments.")
        if min(len(score_request.face_gallery.face_records),len(score_request.template_gallery.templates)) != 0:
            raise ValueError("gallery argument cannot have both face_gallery and template_gallery defined.")
        if max(len(score_request.face_gallery.face_records),len(score_request.template_gallery.templates)) == 0:
            raise ValueError("no gallery templates were found in the arguments.")
        '''

        #This is the first attempt at computing similarity scores. This is definitely not the fastest approach.
        #Also , this is going to be restricted by  memory. The whole similarity matrix be be held in memory.
        #So for large datasets this might pose a problem

        if score_type == fsd.SERVER:
            #rows = probe images
            #cols = gallery images
            sim_mat = np.zeros((len(score_request.template_probes.templates),
                                len(score_request.template_gallery.templates)),
                               dtype=np.float32)

            roc_probe_template = roc.roc_template()
            roc_gallery_template = roc.roc_template()
            #roc_gallery_template_array = roc.new_roc_template_array(len(score_request.template_gallery.templates))
            sm_metric = roc.new_roc_similarity()
            for p in range(0, len(score_request.template_probes.templates)):
                self._rocUnFlatten(
                    score_request.template_probes.templates[p].buffer,
                    roc_probe_template)
                #print roc_probe_template
                for g in range(0,
                               len(score_request.template_gallery.templates)):
                    #print(p,g)
                    #if p == 0:
                    #    roc_gallery_template = roc.roc_template()
                    #    self._rocUnFlatten(score_request.template_gallery.templates[g].buffer,roc_gallery_template)
                    #    roc.roc_template_array_setitem(roc_gallery_template_array,g,roc_gallery_template)
                    #roc_gallery_template = roc.roc_template()
                    self._rocUnFlatten(
                        score_request.template_gallery.templates[g].buffer,
                        roc_gallery_template)
                    #roc.roc_compare_templates(roc_probe_template, roc.roc_template_array_getitem(roc_gallery_template_array,g), sm_metric)
                    roc.roc_compare_templates(roc_probe_template,
                                              roc_gallery_template, sm_metric)
                    sim_mat[p, g] = roc.roc_similarity_value(sm_metric)
                    #roc.roc_free_template(roc_gallery_template)
            roc.delete_roc_similarity(sm_metric)
            roc.roc_free_template(roc_probe_template)
            roc.roc_free_template(roc_gallery_template)
            #for i in range(len(score_request.template_gallery.templates)):
            #print(i)
            #    roc.roc_ensure(roc.roc_free_template(roc.roc_template_array_getitem(roc_gallery_template_array, i)))

        else:
            NotImplementedError("ScoreType %s is not implemented." %
                                (score_type, ))

        #RankOne returns a similarity score of -1 if it compares with an invalid template
        #Threfore find all -1's in the matrix and replace it with a 0

        sim_mat[sim_mat == -1.0] = 0.0
        #converting the simialrity matrix to distance matrix by subtracting with 1
        dist_mat = 1.0 - sim_mat
        # Return the result
        return pt.matrix_np2proto(dist_mat)
Example #3
0
    def score(self, score_request):
        '''Compare templates to produce scores.'''
        score_type = self.scoreType()
        result = geo.Matrix()

        # Check that this is a known score type
        if score_type not in [fsd.L1, fsd.L2, fsd.NEG_DOT]:
            raise NotImplementedError("Score type <%s> not implemented." %
                                      (score_type, ))

        # Check to make sure the probe and gallery records are correct
        if min(len(score_request.face_probes.face_records),
               len(score_request.template_probes.templates)) != 0:
            raise ValueError(
                "probes argument cannot have both face_probes and template_probes defined."
            )
        if max(len(score_request.face_probes.face_records),
               len(score_request.template_probes.templates)) == 0:
            raise ValueError("no probe templates were found in the arguments.")
        if min(len(score_request.face_gallery.face_records),
               len(score_request.template_gallery.templates)) != 0:
            raise ValueError(
                "gallery argument cannot have both face_gallery and template_gallery defined."
            )
        if max(len(score_request.face_gallery.face_records),
               len(score_request.template_gallery.templates)) == 0:
            raise ValueError(
                "no gallery templates were found in the arguments.")

        # Generate probe and gallery matrices
        if len(score_request.face_probes.face_records) > len(
                score_request.template_probes.templates):
            probe_mat = [
                pt.vector_proto2np(face_rec.template.data)
                for face_rec in score_request.face_probes.face_records
            ]
        else:
            probe_mat = [
                pt.vector_proto2np(template.data)
                for template in score_request.template_probes.templates
            ]
        probe_mat = np.array(probe_mat, dtype=np.float32)

        if len(score_request.face_gallery.face_records) > len(
                score_request.template_gallery.templates):
            gal_mat = [
                pt.vector_proto2np(face_rec.template.data)
                for face_rec in score_request.face_gallery.face_records
            ]
        else:
            gal_mat = [
                pt.vector_proto2np(template.data)
                for template in score_request.template_gallery.templates
            ]
        gal_mat = np.array(gal_mat, dtype=np.float32)

        # Compute the distance
        if score_type == fsd.L1:
            dist_mat = spat.distance_matrix(probe_mat, gal_mat, 1)
        elif score_type == fsd.L2:
            dist_mat = spat.distance_matrix(probe_mat, gal_mat, 2)
        elif score_type == fsd.NEG_DOT:
            dist_mat = -np.dot(probe_mat, gal_mat.T)
        else:
            NotImplementedError("ScoreType %s is not implemented." %
                                (score_type, ))

        # Return the result
        return pt.matrix_np2proto(dist_mat)
Example #4
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]