Exemple #1
0
    def subjectDelete(self, request, context):
        ''' Delete subjects from this service. '''
        try:
            start = time.time()

            response = fsd.EnrollmentDeleteResponse()

            gallery_name = request.gallery_name
            subject_id = request.subject_id
            #gallery_key = request.gallery_key

            delete_count = self.gallery_worker.subjectDelete(
                gallery_name, subject_id)

            stop = time.time()
            notes = "%d records deleted." % (delete_count)
            global LOG_FORMAT
            print((LOG_FORMAT % (pv.timestamp(), stop - start,
                                 "subjectDelete()", notes, context.peer())))

            response.delete_count = delete_count

            return response

        except:
            traceback.print_exc()
            raise
Exemple #2
0
    def echo(self, request, context):
        try:
            start = time.time()

            #face_records = request.records

            #worker_result = self.workers.apply_async(worker_score,[request])
            #dist_mat = worker_result.get()
            stop = time.time()

            size = "?X?"
            #speed = "? per second"
            try:
                size = "%dX%d" % (len(request.rows), len(request.rows[0].data))
                #speed = "%0.1f per second"%(len(dist_mat.rows)*len(dist_mat.rows[0].data)/(stop-start))
            except:
                pass
            notes = ""
            notes += "Matrix %s" % (size)

            global LOG_FORMAT
            print((LOG_FORMAT % (pv.timestamp(), stop - start, "echo()", notes,
                                 context.peer())))

            return request

        except:
            traceback.print_exc()
            raise
Exemple #3
0
    def galleryDelete(self, request, context):
        ''' Delete a gallery. '''
        result = self.passthrough(inspect.currentframe().f_code.co_name,
                                  request)
        if result:
            return Empty()
        try:
            start = time.time()

            result = GalleryList()
            gallery_name = request.gallery_name

            deleted_faces = self.gallery_worker.deleteGallery(gallery_name)

            stop = time.time()
            notes = "%d faces removed." % (deleted_faces, )

            global LOG_FORMAT
            print((LOG_FORMAT % (pv.timestamp(), stop - start,
                                 "galleryDelete()", notes, context.peer())))

            return result
        except:
            traceback.print_exc()
            raise
Exemple #4
0
    def extract(self, request, context):
        ''' Extracts faces from the image for the given face detections. '''
        try:
            start = time.time()

            #display = False
            img = pt.image_proto2np(request.image)
            face_records = request.records

            worker_result = self.workers.apply_async(worker_extract,
                                                     [img, face_records])
            face_records_list = worker_result.get()

            notes = "Image Size %s" % (img.shape, )

            notes += ", Faces %s" % (len(face_records_list.face_records), )

            stop = time.time()

            global LOG_FORMAT
            print((LOG_FORMAT % (pv.timestamp(), stop - start, "extract()",
                                 notes, context.peer())))

            return face_records_list

        except:
            traceback.print_exc()
            raise
Exemple #5
0
    def galleryList(self, request, context):
        ''' List the galleries hosted by this service. '''
        result = self.passthrough(inspect.currentframe().f_code.co_name,
                                  request)
        if result:
            return result

        try:
            start = time.time()

            result = GalleryList()
            #gallery_name = request.enroll_gallery

            count = 0
            for gallery_name in self.gallery_worker.galleryNames():

                item = result.galleries.add()
                item.gallery_name = gallery_name
                item.face_count = self.gallery_worker.size(gallery_name)
                #print("Name:",gallery_name)
                #print("Size:",len(GALLERIES[gallery_name]))
                #print()
                count += 1

            stop = time.time()
            notes = "%d galleries returned." % (count, )
            global LOG_FORMAT
            print((LOG_FORMAT % (pv.timestamp(), stop - start, "galleryList()",
                                 notes, context.peer())))

            return result
        except:
            traceback.print_exc()
            raise
Exemple #6
0
    def detect(self, request, context):
        ''' Runs a face detector and return rectangles. '''
        try:

            start = time.time()
            mat = pt.image_proto2np(request.image)

            #skimage.io.imsave("test.png",mat)
            options = request.detect_options
            notes = "Image Size %s" % (mat.shape, )

            # scale down for speed
            for _ in range(options.downsample):
                mat = cv2.pyrDown(mat)

            worker_result = self.workers.apply_async(worker_detect,
                                                     [mat, options])
            face_records_list = worker_result.get()

            # scale up the results
            for face in face_records_list.face_records:
                for _ in range(options.downsample):
                    face.detection.location.x *= 2
                    face.detection.location.y *= 2
                    face.detection.location.width *= 2
                    face.detection.location.height *= 2

            notes += ", Detections %s" % (len(
                face_records_list.face_records), )

            for face in face_records_list.face_records:
                face.source = request.source
                face.frame = request.frame
                face.subject_id = request.subject_id
                face.name = request.subject_name

            #date = request.image.date
            #time_ = request.image.time
            #module = request.image.module
            #camera = request.image.camera
            #event = request.image.event
            #image_num = request.image.image_num

            stop = time.time()
            global LOG_FORMAT
            print(
                (LOG_FORMAT % (pv.timestamp(), stop - start, "detect()", notes,
                               context.peer())))

            #print (face_records_list)
            return face_records_list

        except:
            traceback.print_exc()
            raise
Exemple #7
0
    def enrollmentList(self, request, context):
        ''' List the faces enrolled in this '''
        start = time.time()

        gallery_name = request.gallery_name
        result = self.gallery_worker.enrollmentList(gallery_name)

        stop = time.time()
        notes = "%d records returned." % (len(result.face_records), )
        global LOG_FORMAT
        print((LOG_FORMAT % (pv.timestamp(), stop - start, "enrollmentList()",
                             notes, context.peer())))

        return result
Exemple #8
0
    def enroll(self, request, context):
        ''' Enrolls the faces in the gallery. '''
        try:
            start = time.time()

            gallery_name = request.enroll_gallery

            global GALLERIES, STORAGE

            if gallery_name not in GALLERIES:
                print("Creating gallery", gallery_name)
                GALLERIES[gallery_name] = {}

                path = os.path.join(self.gallery_storage, gallery_name + '.h5')
                STORAGE[gallery_name] = h5py.File(path)

            count = 0
            for face in request.records.face_records:
                face_id = faro.generateFaceId(face)
                face.gallery_key = face_id
                #face_id = "-".join(face_id.split('/'))
                #print('face_id:',face_id)
                count += 1
                GALLERIES[gallery_name][face_id] = face
                if face_id in STORAGE[gallery_name]:
                    del STORAGE[gallery_name][
                        face_id]  # delete so it can be replaced.
                STORAGE[gallery_name][face_id] = np.bytes_(
                    face.SerializeToString())
                STORAGE[gallery_name].flush()

            #response = fsd.ErrorMessage()

            stop = time.time()
            notes = "Enrolled %d faces into gallery '%s'.  Gallery size = %d." % (
                count, gallery_name, len(GALLERIES[gallery_name]))
            global LOG_FORMAT
            print((LOG_FORMAT %
                   (pv.timestamp(), stop - start, "enroll()", notes)))

            return request.records
        except:
            traceback.print_exc()
            raise
Exemple #9
0
    def detect(self, request, context):
        ''' Runs a face detector and return rectangles. '''
        try:

            start = time.time()
            mat = pt.image_proto2np(request.image)
            options = request.detect_options
            notes = "Image Size %s" % (mat.shape, )
            #print('time_check AA:',time.time()-start)
            worker_result = self.workers.apply_async(worker_detect,
                                                     [mat, options])
            #print('time_check BB:',time.time()-start)
            face_records_list = worker_result.get()

            notes += ", Detections %s" % (len(
                face_records_list.face_records), )
            #print('time_check CC:',time.time()-start)

            for face in face_records_list.face_records:
                face.source = request.source
                face.frame = request.frame
                face.subject_id = request.subject_id
                face.name = request.subject_name

            #date = request.image.date
            #time_ = request.image.time
            #module = request.image.module
            #camera = request.image.camera
            #event = request.image.event
            #image_num = request.image.image_num

            stop = time.time()
            global LOG_FORMAT
            print((LOG_FORMAT %
                   (pv.timestamp(), stop - start, "detect()", notes)))

            #print (face_records_list)
            return face_records_list

        except:
            traceback.print_exc()
            raise
Exemple #10
0
    def status(self, request, context):
        ''' Returns the status of the service. '''
        try:
            start = time.time()

            worker_result = self.workers.apply_async(worker_status, [])
            status_message = worker_result.get()
            status_message.worker_count = len(self.workers._pool)
            # print('Status Request', '<',status_message,'>')
            # print(context.peer())
            notes = None
            stop = time.time()
            global LOG_FORMAT
            print(
                (LOG_FORMAT % (pv.timestamp(), stop - start, "status()", notes,
                               context.peer())))

            return status_message
        except:
            traceback.print_exc()
            raise
Exemple #11
0
    def enroll(self, request, context):
        ''' Enrolls the faces in the gallery. '''
        result = self.passthrough(inspect.currentframe().f_code.co_name,
                                  request)
        if result:
            return request.records
        else:
            try:
                start = time.time()

                gallery_name = request.enroll_gallery

                replacements = 0
                count = 0

                for face in request.records.face_records:
                    face_id = faro.generateFaceId(face)
                    face.gallery_key = face_id

                    enrolled, replaced = self.gallery_worker.addFaceToGallery(
                        gallery_name, face_id, face)

                    count += enrolled
                    replacements += replaced

                stop = time.time()
                notes = "Enrolled %d faces into gallery '%s' with %d replacements.  Gallery size = %d." % (
                    count, gallery_name, replacements,
                    self.gallery_worker.size(gallery_name))
                global LOG_FORMAT
                print((LOG_FORMAT % (pv.timestamp(), stop - start, "enroll()",
                                     notes, context.peer())))

                return request.records
            except:
                traceback.print_exc()
                raise
Exemple #12
0
    def search(self, request, context):
        '''
        Search a gallery for a face.
        
        request - should be a SearchRequest protobuf.
        
        returns: SearchResponse protobuf
        '''

        #global STORAGE

        try:
            start = time.time()
            #result = fsd.SearchResponse()
            #result.probes.CopyFrom(request.probes)

            # Collect the options
            search_gallery = request.search_gallery
            probes = request.probes
            max_results = request.max_results
            threshold = request.threshold
            matched = 0

            if len(probes.face_records
                   ) > 0:  # if there are no probes then skip the search

                if self.gallery_worker.isSearchable():
                    self.gallery_worker.generateIndex(search_gallery)
                    probes = self.gallery_worker.search(
                        search_gallery, probes, max_results, threshold)

                else:
                    gallery = self.gallery_worker.getAllFaceRecords(
                        search_gallery)

                    score_request = fsd.ScoreRequest()
                    score_request.face_probes.CopyFrom(probes)
                    score_request.face_gallery.CopyFrom(gallery)

                    # Compute the scores matrix
                    scores = self.score(score_request, context)
                    scores = pt.matrix_proto2np(scores)

                    for p in range(scores.shape[0]):
                        #probe = probes.face_records[p]
                        #out = result.probes.face_records[p].search_results
                        matches = []
                        for g in range(scores.shape[1]):
                            score = scores[p, g]
                            if score > threshold:
                                continue
                            matches.append([score, gallery.face_records[g]])

                        matches.sort(key=lambda x: x[0])

                        if max_results > 0:
                            matches = matches[:max_results]
                            matched += 1

                        for score, face in matches:
                            probes.face_records[
                                p].search_results.face_records.add().CopyFrom(
                                    face)
                            probes.face_records[p].search_results.face_records[
                                -1].score = score

            # Count the matches
            count = len(probes.face_records)

            # Compute speed
            stop = time.time()

            gcount = self.gallery_worker.size(search_gallery)
            total_faces = count * gcount
            speed = total_faces / (stop - start)
            notes = "Processed %d probes. Searched %0.1f faces per second." % (
                count, speed)

            print(
                (LOG_FORMAT % (pv.timestamp(), stop - start, "search()", notes,
                               context.peer())))
            return probes
        except:
            traceback.print_exc()
            raise
Exemple #13
0
    def search(self, request, context):
        '''
        Search a gallery for a face.
        
        request - should be a SearchRequest protobuf.
        
        returns: SearchResponse protobuf
        '''
        try:
            start = time.time()
            #result = fsd.SearchResponse()
            #result.probes.CopyFrom(request.probes)

            # Collect the options
            search_gallery = request.search_gallery
            probes = request.probes
            max_results = request.max_results
            threshold = request.threshold

            if search_gallery not in GALLERIES:
                raise ValueError("Unknown gallery: " + search_gallery)

            gallery = fsd.FaceRecordList()
            for key in GALLERIES[search_gallery]:
                face = GALLERIES[search_gallery][key]
                gallery.face_records.add().CopyFrom(face)

            score_request = fsd.ScoreRequest()
            score_request.face_probes.CopyFrom(probes)
            score_request.face_gallery.CopyFrom(gallery)

            # Compute the scores matrix
            scores = self.score(score_request, None)
            scores = pt.matrix_proto2np(scores)

            matched = 0
            for p in range(scores.shape[0]):
                #probe = probes.face_records[p]
                #out = result.probes.face_records[p].search_results
                matches = []
                for g in range(scores.shape[1]):
                    score = scores[p, g]
                    if score > threshold:
                        continue
                    matches.append([score, gallery.face_records[g]])

                matches.sort(key=lambda x: x[0])

                if max_results > 0:
                    matches = matches[:max_results]
                    matched += 1

                for score, face in matches:
                    probes.face_records[p].search_results.face_records.add(
                    ).CopyFrom(face)
                    probes.face_records[p].search_results.face_records[
                        -1].score = score

            # Count the matches
            count = len(probes.face_records)
            notes = "Processed %d probes. %d matched." % (count, matched)
            stop = time.time()
            print((LOG_FORMAT %
                   (pv.timestamp(), stop - start, "search()", notes)))
            return probes
        except:
            traceback.print_exc()
            raise
Exemple #14
0
    def search(self, request, context):
        '''
        Search a gallery for a face.
        
        request - should be a SearchRequest protobuf.
        
        returns: SearchResponse protobuf
        '''
        try:
            start = time.time()
            result = fsd.SearchResponse()

            #print('search',request)
            name = request.gallery_name
            probes = request.probes
            max_results = request.max_results
            threshold = request.threshold
            
            if name not in GALLERIES:
                raise ValueError("Unknown gallery: "+name)
            
            gallery = fsd.FaceRecordList()
            for key in GALLERIES[name]:
                face = GALLERIES[name][key]
                gallery.face_records.add().CopyFrom(face)
                
            #print('gallery size',len(gallery.face_records))

            score_request = fsd.ScoreRequest()
            score_request.face_probes.CopyFrom(probes)
            score_request.face_gallery.CopyFrom(gallery)
            
            scores = self.score(score_request,None)
                 
            #print('-------------- scores ------------')
            #print(threshold)
            #print(scores)
            scores = pt.matrix_proto2np(scores)
            
            
            for row in range(scores.shape[0]):
                out = result.matches.add()
                matches = []
                for col in range(scores.shape[1]):
                    score = scores[row,col]
                    if score > threshold:
                        continue
                    matches.append( [ score, gallery.face_records[col] ] )
                matches.sort(key=lambda x: x[0])
                #print('matches',matches)
                for score,face in matches:
                    out.face_records.add().CopyFrom(face)
                    out.scores.append(score)
            #for each in request.face_records:
            #    self.galleries[name].addTemplate( each )
                
                
            notes = "Returned %d Results"%(len(result.matches))
            stop = time.time()
            print(( LOG_FORMAT%(pv.timestamp(),stop-start,"search()",notes)))
            return result
        except:
            traceback.print_exc()
            raise