def obj_create(self, bundle, request=None, **kwargs): # create a new File bundle.obj = FileItem() # full_hydrate does the heavy lifting mapping the # POST-ed payload key/values to object attribute/values bundle = self.full_hydrate(bundle) filename_name, file_extension = os.path.splitext( bundle.data[u'file'].name) file_data = bundle.data[u'file'].read() file_sha1 = hashlib.sha1(file_data).hexdigest() if file_extension: filename = '{}{}'.format(file_sha1, file_extension) else: filename = file_sha1 bundle.obj.name = filename # add entry to the facesearch.ImageTemplate # image_template should be passed the openbr template instad of binary data of the image itself tmpl = br.br_load_img(file_data, len(file_data)) image_template, created = ImageTemplate.objects.get_or_create( filename=filename, image_template=file_data) if created: print 'added new image template' else: print 'already had same image template' fn = helpers.get_filename_absolute(filename) with open(helpers.get_filename_absolute(filename), 'wb+') as destination_file: destination_file.write(file_data) destination_file.flush() os.fsync(destination_file) # make thumbnail on server from PIL import ImageFile ImageFile.LOAD_TRUNCATED_IMAGES = True image = Image.open(destination_file.name) # 40% the size of the original image # (aspect ratio could still be off, could look into that) original_width, original_height = image.size THUMB_SIZE = (original_width * 0.4, original_height * 0.4) image.thumbnail(THUMB_SIZE, Image.ANTIALIAS) file_thumbnail_name = helpers.get_fileservice_dir( ) + "/" + file_sha1 + "_thumb" + file_extension image_type = file_extension[1:] if image_type.lower() == 'jpg': image_type = 'jpeg' image.save(file_thumbnail_name, image_type) # index the file in openbr if u'index' not in bundle.data or 'false' != bundle.data[u'index']: index_face(helpers.get_filename_absolute(filename)) # remove the file object passed in so that the response is more concise about what this file will be referred to bundle.data.pop(u'file', None) return bundle
def obj_create(self, bundle, request=None, **kwargs): # create a new File bundle.obj = FileItem() # full_hydrate does the heavy lifting mapping the # POST-ed payload key/values to object attribute/values bundle = self.full_hydrate(bundle) filename_name, file_extension = os.path.splitext(bundle.data[u'file'].name) file_data = bundle.data[u'file'].read() file_sha1 = hashlib.sha1(file_data).hexdigest() if file_extension: filename = '{}{}'.format(file_sha1, file_extension) else: filename = file_sha1 bundle.obj.name = filename # add entry to the facesearch.ImageTemplate # image_template should be passed the openbr template instad of binary data of the image itself tmpl = br.br_load_img(file_data, len(file_data)) image_template, created = ImageTemplate.objects.get_or_create( filename=filename, image_template=file_data) if created: print 'added new image template' else: print 'already had same image template' fn = helpers.get_filename_absolute(filename) with open(helpers.get_filename_absolute(filename), 'wb+') as destination_file: destination_file.write(file_data) destination_file.flush() os.fsync(destination_file) # make thumbnail on server from PIL import ImageFile ImageFile.LOAD_TRUNCATED_IMAGES = True image = Image.open(destination_file.name) # 40% the size of the original image # (aspect ratio could still be off, could look into that) original_width, original_height = image.size THUMB_SIZE = (original_width * 0.4, original_height * 0.4) image.thumbnail(THUMB_SIZE, Image.ANTIALIAS) file_thumbnail_name = helpers.get_fileservice_dir() + "/" + file_sha1 + "_thumb" + file_extension image_type = file_extension[1:] if image_type.lower() == 'jpg': image_type = 'jpeg' image.save(file_thumbnail_name, image_type) # index the file in openbr if u'index' not in bundle.data or 'false' != bundle.data[u'index']: index_face(helpers.get_filename_absolute(filename)) # remove the file object passed in so that the response is more concise about what this file will be referred to bundle.data.pop(u'file', None) return bundle
def test_getfilename(self, request, **kwargs): self.method_check(request, allowed=["post"]) res = [] # create a new File filename_name = u"/tmp/testimage1.jpg" file_data = open(filename_name, "rb").read() facetmpl = br.br_load_img(file_data, len(file_data)) print "Setting file name on template to " + os.path.basename(filename_name) logger.debug("Setting file name on template to " + os.path.basename(filename_name)) br.br_set_filename(facetmpl, os.path.basename(filename_name)) print "Retrieving file name from template" logger.debug("Retriving file name from template") filename = br.br_get_filename(facetmpl) logger.debug("Filename=" + filename) print filename response = self.create_response(request, res) return response
def test_getfilename(self, request, **kwargs): self.method_check(request, allowed=['post']) res = [] # create a new File filename_name = u'/tmp/testimage1.jpg' file_data = open(filename_name, 'rb').read() facetmpl = br.br_load_img(file_data, len(file_data)) print "Setting file name on template to " + os.path.basename(filename_name) logger.debug("Setting file name on template to " + os.path.basename(filename_name)) br.br_set_filename(facetmpl, os.path.basename(filename_name)) print "Retrieving file name from template" logger.debug("Retriving file name from template") filename = br.br_get_filename(facetmpl) logger.debug("Filename=" + filename) print filename 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() 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