def score(self, probe, gallery): ''' ''' request = fsd.ScoreRequest() # Copy the templates into the request for face_rec in probe: request.template_probes.templates.add().CopyFrom(face_rec.template) for face_rec in gallery: request.template_gallery.templates.add().CopyFrom( face_rec.template) # Run the computation on the server dist_mat = self.service_stub.score(request, None) return pt.matrix_proto2np(dist_mat)
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
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
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