def reindex_gallery(): logger.debug("Reindexing entire gallery to " + get_gallery_file()) if os.path.isfile(get_gallery_file()): os.remove(get_gallery_file()) allfiles = get_fileservice_files_abs() logger.debug(allfiles) br.br_enroll_n(len(allfiles), allfiles, get_gallery_file())
def reload_gallery(self, request, **kwargs): reindex_gallery() galleryGalPath = get_gallery_file() galGallery = br.br_make_gallery(galleryGalPath) galTemplateList = br.br_load_from_gallery(galGallery) # compare and collect scores ntargets = br.br_num_templates(galTemplateList) logger.debug(str(ntargets) + " templates found in the gallery at " + galleryGalPath) res = str(ntargets) + " templates found in the gallery at " + galleryGalPath for r in range(ntargets): tmpl = br.br_get_template(galTemplateList, r) logger.debug("Filename = " + br.br_get_filename(tmpl)) response = self.create_response(request, res) br.br_free_template_list(galTemplateList) br.br_close_gallery(galGallery) return response
def index_face(image): # NOTE: This commented out code appears to do the exact same as br_enroll # gal = br.br_make_gallery(get_gallery_file()+'[append=true]') # imgbuffer = open(image, 'rb').read() # template = br.br_load_img(imgbuffer, len(imgbuffer)) # tlist = br.br_enroll_template(template) # num = br.br_num_templates(tlist) # # br.br_add_template_list_to_gallery(gal, tlist) # NOTE: This commented out code is the same as br_add_template_list_to_gallery # for x in range(0, num): # t = br.br_get_template(tlist, x) # br.br_set_filename(t, image) # #fn = br.br_get_filename(t) # br.br_add_template_to_gallery(gal, t) # # br.br_free_template(template) # br.br_free_template_list(tlist) # br.br_close_gallery(gal) br.br_enroll(image, get_gallery_file() + '[append=true]')
def index_face(image): # NOTE: This commented out code appears to do the exact same as br_enroll # gal = br.br_make_gallery(get_gallery_file()+'[append=true]') # imgbuffer = open(image, 'rb').read() # template = br.br_load_img(imgbuffer, len(imgbuffer)) # tlist = br.br_enroll_template(template) # num = br.br_num_templates(tlist) # # br.br_add_template_list_to_gallery(gal, tlist) # NOTE: This commented out code is the same as br_add_template_list_to_gallery # for x in range(0, num): # t = br.br_get_template(tlist, x) # br.br_set_filename(t, image) # #fn = br.br_get_filename(t) # br.br_add_template_to_gallery(gal, t) # # br.br_free_template(template) # br.br_free_template_list(tlist) # br.br_close_gallery(gal) br.br_enroll(image, get_gallery_file()+'[append=true]')
def auto_populate(self, request, **kwargs): # method check to avoid bad requests self.method_check(request, allowed=['get']) files = os.listdir('/vida/samples/photos/') res = {'Status': 'Pictures Uploaded: '} # reset the OpenBR Gallery if os.path.isfile(get_gallery_file()): os.remove(get_gallery_file()) ctr = 0 length = files.__len__() with open('/vida/samples/MOCK_DATA.json') as personDB: _personDB = json.load(personDB) db_size = len(_personDB) - 1 # indexing person_index = 0 for file in files: with open('/vida/samples/photos/' + file, 'rb') as f: url = helpers.get_network_ip('eth1') response = requests.post('http://' + url + '/api/v1/fileservice/', data={'index': 'false'}, files={'file': f}, auth=('admin', 'admin')) if response.status_code == 201: # Picture successfully uploaded pictureFilename = json.loads(response._content)['name'] # Separate gender isFemale = False if 'female' in file: isFemale = True while True: if person_index > db_size: # just in case person_index = 0 # *Try* and match up a gender specific picture to the correct gender thisGender = _personDB[person_index]['gender'] if isFemale: if thisGender == 'Male': person_index += 1 elif thisGender == 'Female': break else: break else: if thisGender == 'Female': person_index += 1 elif thisGender == 'Male': break else: break # Get person information uploadJSON = '{"given_name":"' + _personDB[person_index]['given_name'] + '", "street_and_number":"' + _personDB[person_index]['street_and_number'] + '",' uploadJSON += '"family_name":"' + _personDB[person_index]['family_name'] + '", "gender":"' + _personDB[person_index]['gender'] + '", ' if 'fathers_given_name' in _personDB[person_index]: uploadJSON += '"fathers_given_name":"' + _personDB[person_index]['fathers_given_name'] + '", ' if 'mothers_given_name' in _personDB[person_index]: uploadJSON += '"mothers_given_name":"' + _personDB[person_index]['mothers_given_name'] + '", ' uploadJSON += '"age":"' + str(_personDB[person_index]['age']) + '", "date_of_birth":" ' + _personDB[person_index]['date_of_birth'] + '", ' uploadJSON += '"city":"' + _personDB[person_index]['city'] + '", "phone_number":" ' + _personDB[person_index]['phone_number'] + '", ' if 'province_or_state' in _personDB[person_index]: uploadJSON += '"province_or_state":"' + _personDB[person_index]['province_or_state'] + '", ' uploadJSON += '"pic_filename":"' + pictureFilename + '"' uploadJSON += '}' person_index += 1 # move forward in _nameDB headers = {'Content-type':'application/json', 'Content-length':len(uploadJSON), 'Accept':'application/json'} postResponse = requests.post('http://' + url + '/api/v1/person/', data=uploadJSON, headers=headers, auth=('admin', 'admin')) if (postResponse.status_code != 201): raise self.create_response(request, response) res['Status'] += file ctr += 1 if (ctr != length): res['Status'] += ' || ' response = self.create_response(request, res) return response
def obj_create(self, bundle, request=None, **kwargs): # create a new File bundle.obj = FaceSearch() # full_hydrate does the heavy lifting mapping the # POST-ed payload key/values to object attribute/values bundle = self.full_hydrate(bundle) file_data = bundle.data[u"file"].read() # write the file data to a temporary file filename_name, file_extension = os.path.splitext(bundle.data[u"file"].name) destination_file = tempfile.NamedTemporaryFile(suffix=file_extension) destination_file.write(bytearray(file_data)) destination_file.flush() os.fsync(destination_file) # Need this, we were not getting all bytes written to file before proceeding temp_gal = tempfile.NamedTemporaryFile(suffix=".gal") args = ["br", "-algorithm", "FaceRecognition", "-enroll", destination_file.name, temp_gal.name] subprocess.call(args) out_file = tempfile.NamedTemporaryFile(suffix=".csv") galleryGalPath = get_gallery_file() args = args[:3] + ["-compare", galleryGalPath, temp_gal.name, out_file.name] subprocess.call(args) with open(out_file.name, "r") as scores_file: files_line = scores_file.readline().strip().split(",")[1:] scores_line = scores_file.readline().strip().split(",") probe = scores_line[0] score_list = scores_line[1:] scores = [] for f, s in zip(files_line, score_list): logger.debug("%s -> %s: %s" % (probe, f, s)) scores.append((os.path.basename(f), s)) destination_file.close() out_file.close() temp_gal.close() peeps = Person.objects.filter(pic_filename__in=dict(scores).keys()).values() sorted_peeps = [] scores.sort(key=lambda s: s[1], reverse=True) # TODO: Make 15 a parameter - right now we return the top 15 results logger.debug("Building result package") for s in scores[:15]: currfilename = s[0] logger.debug(currfilename) foundPeep = filter(lambda p: p["pic_filename"] == os.path.basename(s[0]), peeps) if foundPeep is not None and len(foundPeep) > 0: sorted_peeps.append(foundPeep[0]) else: logger.info("Found picture with no link person " + currfilename) # bundle the search results bundle.obj.name = bundle.data[u"file"].name bundle.data.pop(u"file", None) bundle.data["meta"] = { "limit": len(peeps), "next": None, "offset": 0, "previous": None, "total_count": len(peeps), } bundle.data["objects"] = sorted_peeps bundle.data["scores"] = scores return bundle
def reindex_gallery(): if os.path.isfile(get_gallery_file()): os.remove(get_gallery_file()) allfiles = get_fileservice_files_abs() br.br_enroll_n(len(allfiles), allfiles, get_gallery_file())
def obj_create(self, bundle, request=None, **kwargs): # create a new File bundle.obj = FaceSearch() # full_hydrate does the heavy lifting mapping the # POST-ed payload key/values to object attribute/values bundle = self.full_hydrate(bundle) file_data = bundle.data[u'file'].read() # write the file data to a temporary file filename_name, file_extension = os.path.splitext(bundle.data[u'file'].name) destination_file = tempfile.NamedTemporaryFile(suffix=file_extension) destination_file.write(bytearray(file_data)) destination_file.flush() os.fsync(destination_file) # Need this, we were not getting all bytes written to file before proceeding temp_gal = tempfile.NamedTemporaryFile(suffix='.gal') args = ['br', '-algorithm', 'FaceRecognition', '-enroll', destination_file.name, temp_gal.name] subprocess.call(args) out_file = tempfile.NamedTemporaryFile(suffix='.csv') galleryGalPath = get_gallery_file() args = args[:3] + ['-compare', galleryGalPath, temp_gal.name, out_file.name] subprocess.call(args) with open(out_file.name, 'r') as scores_file: files_line = scores_file.readline().strip().split(',')[1:] scores_line = scores_file.readline().strip().split(',') probe = scores_line[0] score_list = scores_line[1:] scores= [] for f, s in zip(files_line, score_list): logger.debug('%s -> %s: %s' % (probe, f, s)) scores.append((os.path.basename(f), s)) destination_file.close() out_file.close() temp_gal.close() peeps = Person.objects.filter(pic_filename__in=dict(scores).keys()).values() sorted_peeps = [] scores.sort(key=lambda s: s[1], reverse=True) # TODO: Make 15 a parameter - right now we return the top 15 results logger.debug("Building result package") for s in scores[:15]: currfilename = s[0] logger.debug(currfilename) foundPeep = filter(lambda p: p['pic_filename'] == os.path.basename(s[0]), peeps) if foundPeep is not None and len(foundPeep) > 0: sorted_peeps.append(foundPeep[0]) else: logger.info("Found picture with no link person " + currfilename) # bundle the search results bundle.obj.name = bundle.data[u'file'].name bundle.data.pop(u'file', None) bundle.data['meta'] = { "limit": len(peeps), "next": None, "offset": 0, "previous": None, "total_count": len(peeps) } bundle.data['objects'] = sorted_peeps bundle.data['scores'] = scores return bundle
def obj_create(self, bundle, request=None, **kwargs): # create a new File bundle.obj = FaceSearch() # full_hydrate does the heavy lifting mapping the # POST-ed payload key/values to object attribute/values bundle = self.full_hydrate(bundle) file_data = bundle.data[u'file'].read() filename_name, file_extension = os.path.splitext(bundle.data[u'file'].name) # Load uploaded image with OpenBR facetmpl = br.br_load_img(file_data, len(file_data)) query = br.br_enroll_template(facetmpl) # Open the OpenBR gallery file galleryGalPath = get_gallery_file() galGallery = br.br_make_gallery(galleryGalPath) galTemplateList = br.br_load_from_gallery(galGallery) # compare and collect scores nqueries = br.br_num_templates(query) ntargets = br.br_num_templates(galTemplateList) scoresmat = br.br_compare_template_lists(galTemplateList, query) scores = [] for r in range(ntargets): for c in range(nqueries): # This is not a percentage match, it's a relative score similarity = br.br_get_matrix_output_at(scoresmat, r, c) tmpl = br.br_get_template(galTemplateList, r) # TODO: This doesn't seem to work through PyCharm, but does once deployed # Plus, it's not correlating filenames to templates correctly. #filename = br.br_get_filename(tmpl) #scores.append((os.path.basename(filename), similarity)) scores.append(('replace-me', similarity)) # clean up - no memory leaks br.br_free_template(facetmpl) br.br_free_template_list(query) br.br_free_template_list(galTemplateList) br.br_close_gallery(galGallery) scores.sort(key=lambda s: s[1], reverse=True) peeps = Person.objects.filter(pic_filename__in=dict(scores).keys()).values() # TODO: Make 15 a parameter - right now we return the top 15 results sorted_peeps = [] for s in scores[:15]: sorted_peeps.append(filter(lambda p: p['pic_filename'] == s[0], peeps)[0]) # bundle the search results bundle.obj.name = bundle.data[u'file'].name bundle.data.pop(u'file', None) bundle.data['meta'] = { "limit": len(peeps), "next": None, "offset": 0, "previous": None, "total_count": len(peeps) } bundle.data['objects'] = sorted_peeps bundle.data['scores'] = scores return bundle
def obj_create(self, bundle, request=None, **kwargs): # create a new File bundle.obj = FaceSearch() # full_hydrate does the heavy lifting mapping the # POST-ed payload key/values to object attribute/values bundle = self.full_hydrate(bundle) file_data = bundle.data[u'file'].read() filename_name, file_extension = os.path.splitext( bundle.data[u'file'].name) # Load uploaded image with OpenBR facetmpl = br.br_load_img(file_data, len(file_data)) query = br.br_enroll_template(facetmpl) # Open the OpenBR gallery file galleryGalPath = get_gallery_file() galGallery = br.br_make_gallery(galleryGalPath) galTemplateList = br.br_load_from_gallery(galGallery) # compare and collect scores nqueries = br.br_num_templates(query) ntargets = br.br_num_templates(galTemplateList) scoresmat = br.br_compare_template_lists(galTemplateList, query) scores = [] for r in range(ntargets): for c in range(nqueries): # This is not a percentage match, it's a relative score similarity = br.br_get_matrix_output_at(scoresmat, r, c) tmpl = br.br_get_template(galTemplateList, r) # TODO: This doesn't seem to work through PyCharm, but does once deployed # Plus, it's not correlating filenames to templates correctly. #filename = br.br_get_filename(tmpl) #scores.append((os.path.basename(filename), similarity)) scores.append(('replace-me', similarity)) # clean up - no memory leaks br.br_free_template(facetmpl) br.br_free_template_list(query) br.br_free_template_list(galTemplateList) br.br_close_gallery(galGallery) scores.sort(key=lambda s: s[1], reverse=True) peeps = Person.objects.filter( pic_filename__in=dict(scores).keys()).values() # TODO: Make 15 a parameter - right now we return the top 15 results sorted_peeps = [] for s in scores[:15]: sorted_peeps.append( filter(lambda p: p['pic_filename'] == s[0], peeps)[0]) # bundle the search results bundle.obj.name = bundle.data[u'file'].name bundle.data.pop(u'file', None) bundle.data['meta'] = { "limit": len(peeps), "next": None, "offset": 0, "previous": None, "total_count": len(peeps) } bundle.data['objects'] = sorted_peeps bundle.data['scores'] = scores return bundle