def identification(): ## 1:N search json_request = request.get_json() if 'query' in json_request.keys(): # initialize br = init_brpy(br_loc="/usr/local/lib") br.br_initialize_default() br.br_set_property('algorithm', 'FaceRecognition') br.br_set_property('enrollAll', 'true') # get query image = BytesIO(b64decode(json_request.get('query'))).read() # image template imagetmpl1 = br.br_load_img(image, len(image)) # enroll query = br.br_enroll_template(imagetmpl1) # get filenames files = [x[2] for x in walk('/images')] # search scores = [] for filename in files[0]: # get target image2 = open('/images/' + filename).read() # image template imagetmpl2 = br.br_load_img(image2, len(image2)) # enroll target = br.br_enroll_template(imagetmpl2) # score matrix scoresmat = br.br_compare_template_lists(target, query) # result result = float(br.br_get_matrix_output_at(scoresmat, 0, 0)) # cache score scores.append({'filename': filename, 'result': result}) # sort matches scores.sort(key=lambda x: x['result'], reverse=True) # retreive top 5 matches result = scores[:5] # clean up templates br.br_free_template(imagetmpl1) br.br_free_template_list(target) br.br_free_template(imagetmpl2) br.br_free_template_list(query) # finalize br.br_finalize() return jsonify({ 'mimetype': 'application/json', 'status': 200, 'request': request.url, 'response': [{ 'scores': result }] }) else: return jsonify({ 'error': { 'message': 'Request must contain a query' }, 'status': 400, 'request': request.url })
def verification(): ## 1:1 verification json_request = request.get_json() if all(image in json_request.keys() for image in ['query', 'target']): # initialize br = init_brpy(br_loc="/usr/local/lib") br.br_initialize_default() br.br_set_property('algorithm', 'FaceRecognition') # get images image1 = BytesIO(b64decode(json_request.get('query'))).read() image2 = BytesIO(b64decode(json_request.get('target'))).read() # image templates imagetmpl1 = br.br_load_img(image1, len(image1)) imagetmpl2 = br.br_load_img(image2, len(image2)) # enroll query = br.br_enroll_template(imagetmpl1) target = br.br_enroll_template(imagetmpl2) # score matrix scoresmat = br.br_compare_template_lists(target, query) # result result = float(br.br_get_matrix_output_at(scoresmat, 0, 0)) # clean up templates br.br_free_template(imagetmpl1) br.br_free_template_list(target) br.br_free_template(imagetmpl2) br.br_free_template_list(query) # finalize br.br_finalize() return jsonify({ 'mimetype': 'application/json', 'status': 200, 'request': request.url, 'response': [{ 'score': result }] }) else: return jsonify({ 'error': { 'message': 'Request must contain a query and target' }, 'status': 400, 'request': request.url })
def gender_estimation(): ## Gender estimation json_request = request.get_json() if 'query' in json_request.keys(): # initialize br = init_brpy(br_loc="/usr/local/lib") br.br_initialize_default() br.br_set_property('algorithm', 'GenderEstimation') # get image image = BytesIO(b64decode(json_request.get('query'))).read() # write image to disk with open('/tmp/image.bytes', 'wb') as fl: fl.write(image) # enroll image br.br_enroll('/tmp/image.bytes', '/tmp/matrix.csv') # get data lines = open('/tmp/matrix.csv', 'r').readlines() # result result = 0 if lines[1].split(',')[10] == 'Male' else 1 # finalize br.br_finalize() return jsonify({ 'mimetype': 'application/json', 'status': 200, 'request': request.url, 'response': [{ 'score': result }] }) else: return jsonify({ 'error': { 'message': 'Request must contain a query' }, 'status': 400, 'request': request.url })
from brpy import init_brpy br = init_brpy() br.br_initialize_default() br.br_set_property('algorithm', 'FaceRecognition') br.br_set_property('enrollAll', 'true')
def find_weight(main_person, rel_person, min_score): mcomparision_list = [] mtmpl_list = [] rcomparision_list = [] rtmpl_list = [] pass_score = min_score weight = 0 mimage_dir = imgreference_directory_path(main_person) rimage_dir = imgreference_directory_path(rel_person) downloads_dir = download_directory_path(main_person) br = init_brpy(br_loc='/usr/local/lib') #Default openbr lib location. br.br_initialize_default() br.br_set_property('algorithm','FaceRecognition') #Algorithm to compare faces br.br_set_property('enrollAll','false') #Be sure directories exists dir_exists(mimage_dir) dir_exists(rimage_dir) dir_exists(downloads_dir) #Add images to br directories = [mimage_dir] br_add_images(directories,mtmpl_list,mcomparision_list,br) #Add images to br directories = [rimage_dir] br_add_images(directories,rtmpl_list,rcomparision_list,br) #Compare images for root, dirs, files in os.walk(downloads_dir, topdown=False): for name in files: image = open(os.path.join(root, name)).read() tmpl = br.br_load_img(image, len(image)) targets = br.br_enroll_template(tmpl) ntargets = br.br_num_templates(targets) # compare and collect scores # compare with all images scores = [] for query, nquery in mcomparision_list: scoresmat = br.br_compare_template_lists(targets, query) for r in range(ntargets): for c in range(nquery): scores.append(br.br_get_matrix_output_at(scoresmat, r, c)) if scores : scores.sort() maxscore = float("-inf") for score in scores: if(score > maxscore): maxscore = score #compare with pass score if maxscore >= pass_score: #if match, check if other person is also in photo scores = [] for query, nquery in rcomparision_list: scoresmat = br.br_compare_template_lists(targets, query) for r in range(ntargets): for c in range(nquery): scores.append(br.br_get_matrix_output_at(scoresmat, r, c)) if scores: scores.sort() maxscore = float("-inf") for score in scores: if(score > maxscore): maxscore = score #compare with pass score if maxscore >= pass_score: #Both are in photo, so we add weight weight = weight + 1 # clean up - no memory leaks br.br_free_template(tmpl) br.br_free_template_list(targets) return weight
def main(): br = init_brpy() br.br_initialize_default() br.br_set_property('algorithm', 'FaceRecognition') br.br_set_property('enrollAll', 'true') enrollFaces(pictureDest)
#!/usr/bin/python import sys from os import listdir from os.path import isfile, join from subprocess import Popen, PIPE from brpy import init_brpy br = init_brpy(br_loc='/usr/local/lib') br.br_initialize_default() br.br_set_property('algorithm', 'FaceRecognition') myfile = open("output.txt", "w") picpaths = [f for f in listdir('.') if isfile(join('.', f))] i = 0 for a in picpaths: for b in picpaths: # p = Popen(['br', '-algorithm','FaceRecognition', '-pairwiseCompare', a, b], stdin=None, stdout=PIPE, stderr=PIPE) # output, err = p.communicate() # rc = p.returncode output = "" myfile.write(a + ',' + b + ',') br.br_compare(a, b, "output.txt") myfile.write('\n') i += 1 if i > 10: break if i > 10: break myfile.close()
#!/usr/bin/python import sys from os import listdir from os.path import isfile, join from subprocess import Popen, PIPE from brpy import init_brpy br = init_brpy(br_loc='/usr/local/lib') br.br_initialize_default() br.br_set_property('algorithm', 'FaceRecognition') myfile = open("output.txt", "w") picpaths = [f for f in listdir('.') if isfile(join('.', f))] i = 0 for a in picpaths: for b in picpaths: # p = Popen(['br', '-algorithm','FaceRecognition', '-pairwiseCompare', a, b], stdin=None, stdout=PIPE, stderr=PIPE) # output, err = p.communicate() # rc = p.returncode output = "" myfile.write(a+','+b+',') br.br_compare(a, b, "output.txt") myfile.write('\n') i += 1 if i > 10: break if i > 10: break myfile.close()
def main(argv): br = init_brpy() br.br_initialize_default() br.br_set_property('algorithm','FaceRecognition') # also made up # br.br_set_property('enrollAll','true') inputfile = '' outputfile = '' dataLocation = '' try: opts, args = getopt.getopt(argv,"hi:d",["ifile=","dfile="]) except getopt.GetoptError: print 'test.py -i <inputfile> -o <outputfile>' sys.exit(2) for opt, arg in opts: if opt == '-h': print 'test.py -i <inputfile> -o <outputfile>' sys.exit() elif opt in ("-i", "--ifile"): inputfile = arg elif opt in ("-d", "--dfile"): dataLocation = '~/openbr/data/LFW/img/' lines = [line.rstrip('\n') for line in open(inputfile)] lines = lines[1:] scoreMat = [] trueFalseMat = [] numTrue = 0 numFalse = 0 i = 0 scoreOutput = 'file1,file2,score,true\n' for line in lines : components = line.split("\t"); im1Path = '' im2Path = '' isTrue = 0 if len(components) == 3 : trueFalseMat.append(1) isTrue = 1 numTrue +=1 folder = components[0] im1num = components[1].zfill(4) im2num = components[2].zfill(4) im1Path = dataLocation + folder + '/' + folder + '_' + im1num + '.jpg' im2Path = dataLocation + folder + '/' + folder + '_' + im2num + '.jpg' elif len(components) == 4: trueFalseMat.append(0) isTrue = 0 numFalse += 1 folder1 = components[0] folder2 = components[2] im1num = components[1].zfill(4) im2num = components[3].zfill(4) im1Path = dataLocation + folder1 + '/' + folder1 + '_' + im1num + '.jpg' im2Path = dataLocation + folder2 + '/' + folder2 + '_' + im2num + '.jpg' im1 = open(im1Path, 'rb').read() im2 = open(im2Path, 'rb').read() p1 = subprocess.Popen(["br","-algorithm", "FaceRecognition" ,"-compare", im1Path, im2Path], stdout=subprocess.PIPE) score = float(p1.communicate()[0]) if score < -100: score = -10 elif score > 100: score = 10 p1.stdout.close() scoreOutput = scoreOutput+im1Path+','+im2Path+','+ str(score) + str(isTrue) + '\n' # templ1 = br.br_load_img(im1, len(im1)) # templ2 = br.br_load_img(im2, len(im2)) # query1 = br.br_enroll_template(templ1) # query2 = br.br_enroll_template(templ2) # score = br.br_compare_template_lists(query1,query2) # br.br_pairwise_compare(query1,query2,1) scoreMat.append(score) print i i+=1 medianScore = numpy.median(scoreMat) minScore = min(scoreMat) maxScore = max(scoreMat) outScoreFile = open('match_scores.csv','a'); outScoreFile.write(scoreOutput) DET = [] outString = 'Thresh, True Accept,False Accept,True Reject,False Reject\n' print 'found all scores' sortedScores = sorted(scoreMat) arr = numpy.arange((minScore),(maxScore),((maxScore)-(minScore))/1000) for x in numpy.arange((minScore),(maxScore),((maxScore)-(minScore))/1000): trueAccept = 0.0 falseAccept = 0.0 falseReject = 0.0 trueReject = 0.0 for i in xrange(0,len(scoreMat)): if scoreMat[i] < x and trueFalseMat[i] == 1: falseReject += 1.0 elif scoreMat[i] >= x and trueFalseMat[i] == 0: falseAccept += 1.0 elif scoreMat[i] >= x and trueFalseMat[i] == 1: trueAccept += 1.0 elif scoreMat[i] < x and trueFalseMat[i] == 0: trueReject += 1.0 falseAccept /= (len(scoreMat)+0.0) falseReject /= (len(scoreMat)+0.0) trueAccept /= (len(scoreMat)+0.0) trueReject /= (len(scoreMat)+0.0) outString = outString + str(x)+ ','+ str(trueAccept) + ',' + str(falseAccept) + ',' + str(trueReject) + ',' + str(falseReject)+'\n' DET.append([falseAccept,falseReject]) print DET outFile = open('DETPoints.csv','a'); outFile.write(outString)