Beispiel #1
0
def get_estimate_age(url):
    try:
        image = url_to_image(url)
    except urllib.error.HTTPError:
        return False
    except urllib.error.URLError:
        return False
    except:
        return False

    agender = PyAgender()
    faces = agender.detect_genders_ages(image)
    if len(faces) > 0:
        return round(faces[0]['age'])
    else:
        return 0
Beispiel #2
0
    def __init__(self, cascadePath=None, odir=None):
        '''
        Constructor for PixelCore
        '''
        self.cascPath = os.path.join(os.getcwd(), 'models', 'haarcascades',
                                     'haarcascade_frontalface_extended.xml')

        resnetwts = os.path.join(cascadePath, 'models', 'weights.18-4.06.hdf5')
        if cascadePath:
            self.cascPath = os.path.join(os.path.abspath(cascadePath),
                                         'models', 'haarcascades',
                                         'haarcascade_frontalface_alt.xml')

        print('Loading HAAR-Cascade file: %s' % (self.cascPath))
        self.faceCascade = cv2.CascadeClassifier(self.cascPath)
        self.face_size = 32
        self.agender = PyAgender()
def gender_detect(path):
    '''
    gender_detect: takes in an image file, uses OpenCV and 
    PyAgender to detected the gender of the individual in the 
    live image

    Parameters
    ----------
    file: file name + location where file is stored

    Returns
    -------
    Prediction string (i.e. "male")
    '''
    agender = PyAgender()
    face = agender.detect_genders_ages(cv2.imread(path))
    gender = face[0]['gender']
    if gender >= 0.5:
        return 'Female'
    else:
        return 'Male'
Beispiel #4
0
def detect(image):
    agenda = PyAgender()
    ans = agenda.detect_genders_ages(image)
    return ans
def age_estimation(foto):
    agender = PyAgender()
    faces = agender.detect_genders_ages(cv2.imread(foto))
    age = faces[0]['age']
    return round(age)
        age_before_morph[idx] = age_estimation(i)
        # print(age_after_morph)
        # print(face_distance_after_morph)
        # print(actual_age)
        stop = time.time()
        print("Time: ", stop-start)
    np.savez(save_name, age_after_morph=age_after_morph,age_before_morph=age_before_morph, actual_age=actual_age, face_dist_after = face_distance_after_morph, face_dist_before=face_distance_before_morph)
   # Plots_print()
    #return age_after_morph, face_distance_after_morph, actual_age, face_distance_before_morph, age_before_morph

def read_npz_after_morph(file):
    x = np.load(file, mmap_mode='r')

    actual_age = x['actual_age'] # Array with actual ages
    age_after_morph = x['age_after_morph'] # Array with detected ages after morphing
    age_before_morph = x['age_before_morph'] # Array with detected ages before morphing
    face_dist_after = x['face_dist_after'] # Face distance after morphing
    face_dist_before = x['face_dist_before'] # Face distance before morphing
    
    return actual_age, age_after_morph, age_before_morph, face_dist_after, face_dist_before


agender = PyAgender()
path = "../SiblingsDB/SiblingsDB_preprocessed/"
#after_morphing("/Users/ellen/Documents/GitHub/Age-Obfuscation-Morphing/averages/average20_30_N3(1).png", path)
#after_morphing("/Users/ellen/Documents/GitHub/Age-Obfuscation-Morphing/averages/average30_40_N3(0).png", path)
# after_morphing("/Users/ellen/Documents/GitHub/Age-Obfuscation-Morphing/averages/average40_50_N3(0).png", path)
# after_morphing("/Users/ellen/Documents/GitHub/Age-Obfuscation-Morphing/averages/average50_60_N3(0).png", path)
after_morphing("../averages/average60_70_handpickedN2.png", path, agender)

def img_clss():
    new_file_id=uuid.uuid1() 
    try:
        data_file = request.files['img']
        file_name = data_file.name
        data_file.save(f'{os.getcwd()}/images/{data_file.filename}')
        data_file.close()    
        img_file = os.listdir(f'{os.getcwd()}/images/')       
        print('Image ',img_file[0])
        
        agender = PyAgender() 
        path = os.path.join(f'{os.getcwd()}/images/', img_file[0])
        print('Path ', path)
        faces = agender.detect_genders_ages(cv2.imread(path))
        print('Faces ',faces)
        if faces[0]['gender'] < 0.5:
            faces[0]['gender'] = 'male'
        else:
            faces[0]['gender'] = 'female'
        faces[0]['age'] = str(faces[0]['age'] ).split('.')[0]
        print(faces[0])
        resp = make_response(faces[0])
        resp.headers.set('Content-Type','multipart/form-data')
        print('Response ', resp)
        return resp

    except Exception as e:
        print(e)
        try:
            # Camera capture
            path1 = f'{os.getcwd()}/images/'
            image_string = str(request.form.get('file_string'))
            image_newstring = image_string.replace("data:image/jpeg;base64,", "") 
            imgdata = base64.b64decode(image_newstring)
            filename = os.path.join(path1 , new_file_id.hex+'.jpg')
            with open(filename, 'wb') as f:
                f.write(imgdata)
                f.close()
            img_file = os.listdir(f'{os.getcwd()}/images/')
            print('Image ',img_file[0])
            
            agender = PyAgender() 
            path = os.path.join(f'{os.getcwd()}/images/', img_file[0])
            print('Path ',path)
            faces = agender.detect_genders_ages(cv2.imread(path))
            print('Faces ' ,faces)
            if faces[0]['gender'] < 0.5:
                faces[0]['gender'] = 'male'
            else:
                faces[0]['gender'] = 'female'
            faces[0]['age'] = str(faces[0]['age'] ).split('.')[0]
            print(faces[0])
            resp = make_response(faces[0])
            resp.headers.set('Content-Type','multipart/form-data')
            target_dir=f'{os.getcwd()}/images/'
            with os.scandir(target_dir) as entries:
                for entry in entries:
                    time.sleep(1)
                    if entry.is_file() or entry.is_symlink():
                        os.remove(entry)
            print('Response ', resp)
            return resp
            
        except Exception as e:
            print(e)
            print("Something went wrong with your image. Kindly try with different image")
       
    finally:
        target_dir=f'{os.getcwd()}/images/'
        with os.scandir(target_dir) as entries:
            for entry in entries:
                time.sleep(1)
                print(entry)
                if entry.is_file() or entry.is_symlink():
                    os.remove(entry)
        print("Image Deleted")
import face_recognition
import pickle
import sqlite3
import sys
import os
import cv2
from pyagender import PyAgender

res = PyAgender().detect_genders_ages(cv2.imread(sys.argv[1]))
print('Age: {}'.format(res[0]['age']))
print('Gender: {}'.format(res[0]['gender']))

filename = sys.argv[1]
known_image = face_recognition.load_image_file(filename)
known_encoding = face_recognition.face_encodings(known_image)[0]

con = sqlite3.connect('comparator.db')
cur = con.cursor()

## write faces
# cur.execute('create table if not exists faces (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(50), face blob)')
# name_in_db = os.path.splitext(os.path.basename(filename))[0]
# sql = 'insert into faces (name, face) values (?, ?)'
# cur.execute(sql, (name_in_db, pickle.dumps(known_encoding)))
# con.commit()

## recognize faces
cur.execute('select name, face from faces')
face_encodings = cur.fetchall()
for i in range (0, len(face_encodings)):
    fe = pickle.loads(face_encodings[i][1])
Beispiel #9
0
 def __init__(self):
     self.agender = PyAgender()
Beispiel #10
0
 def __init__(self):
     self._detector = MTCNN()
     self._fa = FaceAligner()
     self._image_utils = Image_Utils()
     self._agender = PyAgender()
     self._gap_db = Gap_Db()
Beispiel #11
0
# -*- coding: utf-8 -*-
"""
Created on Tue Dec  1 11:36:51 2020

@author: murie
"""
#import os, fnmatch, facemorpher
from pyagender import PyAgender
import cv2

agender = PyAgender()  # see available options in __init__() src

faces = agender.detect_genders_ages(
    cv2.imread(
        "C:/Users/murie/Documents/school/master/1B/Introduction2Biometrics/project_git/datasets/test"
    ))
image = agender.detect_genders_ages(cv2.imread("acaro.png"))