def betaface_recognition(image_dict): """ Given a message contained a cropped head, send it to BetaFace API for face recognition. This can take anywhere from 1 to 10 seconds. """ try: api = BetaFaceAPI() # Get image from message, save it to disk and # pass it on to BetaFace API image = base64_to_image(image_dict['head_image']['image'], int(image_dict['head_image']['width']), int(image_dict['head_image']['height'])) temp_path = random_file_name('jpg', "FR_") logger.info("Generated random path to save image: %s" % temp_path) image = image.rotate(180) image.save(temp_path) logger.info("Saved image to disk at path %s" % temp_path) matches = api.recognize_faces(temp_path, 'amilab.ro') logger.info("Received recognized faces from BetaFace API %r" % matches) #os.remove(str(temp_path)) #logger.info("Removed image from disk (%s)" % str(temp_path)) return matches except: logger.exception("Failed to recognize faces via BetaFace API") return {}
def train_api_with_members(): """ Trains the BetaFace API with all the faces of the existing AmI lab members. """ client = BetaFaceAPI() members = get_ami_lab_members() for member in members: member_headshots = get_headshots_for_member(member) for headshot in member_headshots: client.upload_face(headshot, member)
class UpgradeFaceSamples(PDU): QUEUE = 'upgrade-face-samples' def __init__(self, **kwargs): super(UpgradeFaceSamples, self).__init__() self.api = BetaFaceAPI() def save_image_to_file(self, buffer, width, height, path): image_buffer = array.array('B', buffer).tostring() image_to_file = Image.frombuffer("RGB", (width, height), image_buffer) image_to_file.save(path) def process_message(self, message): """ Sends the face sample to BetaFaceAPI. Right now, these samples are coming from the face recognition module, whenever the recognition confidence is really high. """ person_name = message['person_name'] head_image = message['head_image'] image = head_image['image'] width = int(head_image['width']) height = int(head_image['height']) # save image to file path = "/tmp/%s.jpg" % uuid.uuid4() self.save_image_to_file(image, width, height, path) self.logger.info("Saved face sample to file %s" % path) # upload image to BetaFace API self.api.upload_face(path, person_name) self.logger.info("Fed face sample %s as an example for %s" %\ (path, person_name)) os.remove(str(path)) self.logger.info("Removed temporary file %s from disk" % str)
def __init__(self, **kwargs): super(UpgradeFaceSamples, self).__init__() self.api = BetaFaceAPI()
import re from pybetaface.api import BetaFaceAPI default_name = "fakemeuser" namespace = "fakeme" client = BetaFaceAPI() def get_info_from_pic(path, name=None): if name is None: name = default_name info = client.upload_face(path, name + '@' + namespace) img_uid = info['img_uid'] person_info = client.get_image_info(img_uid) raw = person_info['raw_content'] m = re.search( ".*<name>age</name><value>([0-9]+)</value>.*<name>gender</name><value>(female|male)</value>.*<name>race</name><value>(.*)(</value>)", raw) age = m.group(1) gender = m.group(2) race = m.group(3).split('<')[0] return {'url': path, 'age': age, 'gender': gender, 'race': race} def generate_picture(path): max_pic = 6 matches = client.recognize_faces(path, namespace) similar_pictures = {} for i, (same_pic_name, confidence) in enumerate(matches.items()): if similar_pictures.__len__() < max_pic: if not same_pic_name.__contains__(default_name):