├── .gitattributes ├── model.pyc ├── utils.pyc ├── image ├── selfi.jpg ├── selfi2.jpg ├── webcam.jpg ├── Capture.jpg ├── friends.jpg ├── olivier.jpg ├── webcam_test.jpg ├── Capture_detected.jpg ├── big_bang_theory2.jpg ├── big_bang_theory4.jpg ├── big_bang_theory5.jpg ├── friends_detected.jpg ├── game-of-thrones.jpg ├── olivier_detected.jpg ├── selfi2_detected.jpg ├── selfi_detected.jpg ├── webcam_detected.jpg ├── how_I_met_your_mother.jpg ├── webcam_test_detected.jpg ├── big_bang_theory2_detected.jpg ├── big_bang_theory4_detected.jpg ├── big_bang_theory5_detected.jpg ├── game-of-thrones_detected.jpg └── how_I_met_your_mother_detected.jpg ├── modelGender.pyc ├── ageWeights └── README.md ├── genderWeights └── README.md ├── freeze_graph └── README.md ├── modelGender.py ├── prediction_age_gender.py ├── README.md ├── model.py ├── LICENSE └── utils.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto -------------------------------------------------------------------------------- /model.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/model.pyc -------------------------------------------------------------------------------- /utils.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/utils.pyc -------------------------------------------------------------------------------- /image/selfi.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/selfi.jpg -------------------------------------------------------------------------------- /image/selfi2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/selfi2.jpg -------------------------------------------------------------------------------- /image/webcam.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/webcam.jpg -------------------------------------------------------------------------------- /modelGender.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/modelGender.pyc -------------------------------------------------------------------------------- /image/Capture.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/Capture.jpg -------------------------------------------------------------------------------- /image/friends.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/friends.jpg -------------------------------------------------------------------------------- /image/olivier.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/olivier.jpg -------------------------------------------------------------------------------- /image/webcam_test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/webcam_test.jpg -------------------------------------------------------------------------------- /image/Capture_detected.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/Capture_detected.jpg -------------------------------------------------------------------------------- /image/big_bang_theory2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/big_bang_theory2.jpg -------------------------------------------------------------------------------- /image/big_bang_theory4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/big_bang_theory4.jpg -------------------------------------------------------------------------------- /image/big_bang_theory5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/big_bang_theory5.jpg -------------------------------------------------------------------------------- /image/friends_detected.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/friends_detected.jpg -------------------------------------------------------------------------------- /image/game-of-thrones.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/game-of-thrones.jpg -------------------------------------------------------------------------------- /image/olivier_detected.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/olivier_detected.jpg -------------------------------------------------------------------------------- /image/selfi2_detected.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/selfi2_detected.jpg -------------------------------------------------------------------------------- /image/selfi_detected.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/selfi_detected.jpg -------------------------------------------------------------------------------- /image/webcam_detected.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/webcam_detected.jpg -------------------------------------------------------------------------------- /image/how_I_met_your_mother.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/how_I_met_your_mother.jpg -------------------------------------------------------------------------------- /image/webcam_test_detected.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/webcam_test_detected.jpg -------------------------------------------------------------------------------- /image/big_bang_theory2_detected.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/big_bang_theory2_detected.jpg -------------------------------------------------------------------------------- /image/big_bang_theory4_detected.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/big_bang_theory4_detected.jpg -------------------------------------------------------------------------------- /image/big_bang_theory5_detected.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/big_bang_theory5_detected.jpg -------------------------------------------------------------------------------- /image/game-of-thrones_detected.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/game-of-thrones_detected.jpg -------------------------------------------------------------------------------- /image/how_I_met_your_mother_detected.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OValery16/gender-age-classification/HEAD/image/how_I_met_your_mother_detected.jpg -------------------------------------------------------------------------------- /ageWeights/README.md: -------------------------------------------------------------------------------- 1 | Fichiers disponible sur le serveur WM 2 | Télécharger les fichiers du dossier gender_classification/ageWeights et les placer dans ce dossier 3 | 4 | 5 | ###OLD 6 | # This folder should contain the weight of the INCEPTION_v3 model. We trained this model to predict this age of a face on a picture. 7 | # You can dowload these files with the following [link](https://drive.google.com/open?id=13krtMcqOFsMUQLGAmpKE-0nMeMB3K3Xu) 8 | # This model is tailored to be used on Tensorflow. 9 | 10 | -------------------------------------------------------------------------------- /genderWeights/README.md: -------------------------------------------------------------------------------- 1 | Fichiers disponible sur le serveur WM 2 | Télécharger les fichiers du dossier gender_classification/genderWeights et les placer dans ce dossier 3 | 4 | ###OLD 5 | This model run on top of the INCEPTION_v3 model. The INCEPTION_v3 modelextract the image features, which are used by our classifier to extract the gender information (male/female). This folder should contain the checkpoint files 6 | You can dowload these files with the following [link](https://drive.google.com/open?id=1a_-_9IqRtBcC0wJjrmnnaFAi4pFrmFLX) 7 | This model is tailored to be used on Tensorflow. 8 | -------------------------------------------------------------------------------- /freeze_graph/README.md: -------------------------------------------------------------------------------- 1 | Fichiers disponible sur le serveur WM 2 | Télécharger les fichiers du dossier gender_classification/freeze_graph et les placer dans ce dossier 3 | 4 | 5 | ###OLD 6 | # This folder should contain the weight of the YOLO_v2 model. We trained this model to detect faces on a picture. 7 | # We imported the weight from Darknet and we retrained the model in order to detect faces on pictures. We use the [FDDB data set](http://vis-www.cs.umass.edu/fddb/) for the training. 8 | # This model is tailored to be used on Keras. 9 | ## The weight are stored in the .h5 file 10 | ## The model is stored in the JSON file 11 | # You can dowload these files with the following [link](https://drive.google.com/open?id=1EmVX6dkIaRJWpM65i1AzDwqshpoAa8la) 12 | -------------------------------------------------------------------------------- /modelGender.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from datetime import datetime 6 | import time 7 | import os 8 | import numpy as np 9 | import tensorflow as tf 10 | #from data import distorted_inputs 11 | import re 12 | from tensorflow.contrib.layers import * 13 | 14 | def genderClassifier(): 15 | 16 | train_inputs=2048 17 | train_labels=2 18 | 19 | 20 | # Setting hyperparameters 21 | learning_rate = 0.01 22 | batch_size = 64 23 | epochs = 50 24 | log_batch_step = 50 25 | 26 | # useful info 27 | n_features = 2048 28 | n_labels = 2 29 | 30 | # Placeholders for input features and labels 31 | inputs = tf.placeholder(tf.float32, (None, n_features)) 32 | labels = tf.placeholder(tf.float32, (None, n_labels)) 33 | 34 | # Setting up weights and bias 35 | weights = tf.Variable(tf.truncated_normal((n_features, n_labels), stddev=0.1), name='weights') 36 | bias = tf.Variable(tf.zeros(n_labels), name='bias') 37 | tf.add_to_collection('vars', weights) 38 | tf.add_to_collection('vars', bias) 39 | 40 | # Setting up operation in fully connected layer 41 | logits = tf.add(tf.matmul(inputs, weights), bias) 42 | prediction = tf.nn.softmax(logits) 43 | tf.add_to_collection('pred', prediction) 44 | 45 | # Defining loss of network 46 | difference = tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits) 47 | loss = tf.reduce_sum(difference) 48 | 49 | # Setting optimiser 50 | optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss) 51 | 52 | # Define accuracy 53 | is_correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(labels, 1)) 54 | accuracy = tf.reduce_mean(tf.cast(is_correct_prediction, tf.float32)) 55 | 56 | saver = tf.train.Saver((weights, bias)) 57 | 58 | return prediction,inputs 59 | -------------------------------------------------------------------------------- /prediction_age_gender.py: -------------------------------------------------------------------------------- 1 | from keras.models import load_model 2 | from keras.models import model_from_json 3 | import tensorflow as tf 4 | from keras import backend as K 5 | from utils import * 6 | import cv2 7 | from build_predicator import * 8 | from utils import * 9 | import matplotlib.pyplot as plt 10 | 11 | 12 | input_size=416 13 | max_box_per_image = 10 14 | anchors=[0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828] 15 | labels=["face"] 16 | image_path = "image/olivier.jpg" 17 | 18 | 19 | # load json and create model 20 | json_file = open('freeze_graph/model.json', 'r') 21 | loaded_model_json = json_file.read() 22 | json_file.close() 23 | loaded_model = model_from_json(loaded_model_json, custom_objects={"tf": tf}) 24 | # load weights into new model 25 | loaded_model.load_weights("freeze_graph/model.h5") 26 | print("Loaded model from disk") 27 | 28 | g1=Graph() 29 | 30 | 31 | if image_path[-4:] == '.mp4': 32 | video_out = image_path[:-4] + '_detected' + image_path[-4:] 33 | 34 | video_reader = cv2.VideoCapture(image_path) 35 | 36 | nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT)) 37 | frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT)) 38 | frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH)) 39 | 40 | fps = video_reader.get(cv2.CAP_PROP_FPS) 41 | 42 | video_writer = cv2.VideoWriter(video_out, 43 | cv2.VideoWriter_fourcc(*'MPEG'), 44 | fps, 45 | (frame_w, frame_h)) 46 | n=0 47 | skip=5 48 | savBox=[] 49 | savPrediction=[] 50 | for i in tqdm(range(1000)): 51 | n=n+1 52 | _, image = video_reader.read() 53 | if i%skip==0: 54 | 55 | image2 = cv2.resize(image, (input_size, input_size)) 56 | 57 | image2 = normalize(image2) 58 | 59 | input_image = image2[:,:,::-1] 60 | input_image = np.expand_dims(input_image, 0) 61 | dummy_array = dummy_array = np.zeros((1,1,1,1,max_box_per_image,4)) 62 | input_data=[input_image, dummy_array] 63 | netout = loaded_model.predict([input_image, dummy_array])[0] 64 | boxes = decode_netout2(netout, labels,anchors) 65 | if len(boxes) != 0: 66 | 67 | listImg=getFacesList(image, boxes) 68 | listPrediction=g1.classify_age(listImg) 69 | image = draw_boxes_v2(image, boxes, labels,listPrediction) 70 | savBox=boxes 71 | savPrediction=listPrediction 72 | video_writer.write(image) 73 | else: 74 | image = draw_boxes_v2(image, savBox, labels,savPrediction) 75 | video_writer.write(image) 76 | 77 | video_reader.release() 78 | video_writer.release() 79 | else: 80 | image = cv2.imread(image_path) 81 | 82 | image2 = cv2.resize(image, (input_size, input_size)) 83 | 84 | image2 = normalize(image2) 85 | 86 | input_image = image2[:,:,::-1] 87 | input_image = np.expand_dims(input_image, 0) 88 | dummy_array = dummy_array = np.zeros((1,1,1,1,max_box_per_image,4)) 89 | input_data=[input_image, dummy_array] 90 | netout = loaded_model.predict([input_image, dummy_array])[0] 91 | boxes = decode_netout2(netout, labels,anchors) 92 | if len(boxes) != 0: 93 | 94 | listImg=getFacesList(image, boxes) 95 | listPrediction=g1.classify_age(listImg) 96 | image = draw_boxes_v2(image, boxes, labels,listPrediction) 97 | print(len(boxes), 'boxes are found') 98 | print(listPrediction) 99 | imgplot = plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) 100 | plt.show() 101 | 102 | cv2.imwrite(image_path[:-4] + '_detected2' + image_path[-4:], image) 103 | 104 | 105 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gender/age classification 2 | 3 | Pour exécuter le script ```python prediction_age_gender.py``` en modifiant le lien de la photo dans ```image_path``` 4 | 5 | ## The architecture of this project is the following: 6 | 7 | * ageWeights: weight for the age model 8 | * build_predicator.py: script to build the age/gender graph (it also call "modelGender.py") 9 | * detected_faces: example of extracted faces (it was mostly done for debugging purpose) 10 | * freeze_graph: the yolo model + the weight (i froze the network) 11 | * genderWeights: gender for the age model (the branch for the gender) 12 | * image: examples of image and their respective output 13 | * log: it 's where I put the tensorboard log 14 | * modelGender.py: script to build the gender graph 15 | * model.py: helper for building the inception model 16 | * prediction_age_gender.py: the main python script 17 | * utils.py: useful functions 18 | * video: examples of video and its output 19 | 20 | ## You can run our predicator with the command: 21 | 22 | ```python prediction_age_gender.py``` 23 | 24 | * You can modify the value of ```image_path``` in ```prediction_age_gender.py``` to run our model on your own picture/video. 25 | 26 | * Note: For the sake of simplicity, I use Keras for the YOLO implementation, but I usetensorflow for the other graphs. 27 | 28 | * Note2: Don't forget to download the weight and to put them in th corresponding folder (ageWeights/freeze_graph/genderWeights) 29 | * This link to each of them is available in the folder 30 | 31 | ## Know prediction errors 32 | 33 | Several prediction errors could happen in the following case: 34 | * The face is too far from the camera 35 | * The picture used in the training mostly includes people that are relatively closed to the camera 36 | * The picture includes many character that are very closed to each other 37 | * The picture used in the training mostly includes people are not too closed for each other 38 | * The face is very closed to the border 39 | * Our system consists in the combinasion of 2 neural networks. The first one extract the faces as a square image, the second one resize them to the correct input size and predicts the corresponding label of each of them. Howeve if the face is closed to the picture's border, the extracted face may not be a square size image, wich force our system to distord the image in order to make it fit in our model. It can lead to pottential errors. 40 | * The lighting condition are very different from our training set 41 | * The picture used in the test set should have similar distribution as the validation set. 42 | 43 | ## Note: 44 | 45 | In deep learning, it important to know that the training set should cover sufficiently the scenarios that you want to score later on. If the classifier sees fully new concepts or contexts it is likely to perform badly. Just a few examples: 46 | 47 | * You train only on images from a constraint environment (say, indoor) and try to score images from a different environment (outdoor). 48 | * You train only on images of a certain make and try to score others. 49 | * Your test images have largely different characteristics, e.g. with respect to illumination, background, color, size, position, etc. 50 | * Your test images contain entirely new concepts. 51 | 52 | As a result, we invite the reader to fine tune our model in case it makes some prediction errors with their test set (see previous section). Another parameter that ca be adjusted is size of the face that is extracted (go to "utils.py" and search for 'getFacesList' and adjust the size of maxDist) 53 | 54 | * Each label is written like gender, (age_interval) 55 | * The exact age cannot be guessed without large training dataset (which we don't have) 56 | * Instead we guess the age intervale 57 | 58 | # The following pictures are example of input/output: 59 | 60 | 61 | 62 | Input | Output 63 | :-------------------------:|:-------------------------: 64 | ![](/image/Capture.jpg?raw=true) | ![](/image/Capture_detected.jpg?raw=true) 65 | ![](/image/image_extracted1.jpg?raw=true) | ![](/image/image_extracted1_detected.jpg?raw=true) 66 | ![](/image/webcam.jpg?raw=true) | ![](/image/webcam_detected.jpg?raw=true) 67 | ![](/image/olivier.jpg?raw=true) | ![](/image/olivier_detected.jpg?raw=true) 68 | ![](/image/webcam_test.jpg?raw=true) | ![](/image/webcam_test_detected.jpg?raw=true) 69 | ![](/image/big_bang_theory4.jpg?raw=true) | ![](/image/big_bang_theory4_detected.jpg?raw=true) 70 | ![](/image/big_bang_theory2.jpg?raw=true) | ![](/image/big_bang_theory2_detected.jpg?raw=true) 71 | ![](/image/big_bang_theory5.jpg?raw=true) | ![](/image/big_bang_theory5_detected.jpg?raw=true) 72 | ![](/image/friends.jpg?raw=true) | ![](/image/friends_detected.jpg?raw=true) 73 | ![](/image/game-of-thrones.jpg?raw=true) | ![](/image/game-of-thrones_detected.jpg?raw=true) 74 | ![](/image/how_I_met_your_mother.jpg?raw=true) | ![](/image/how_I_met_your_mother_detected.jpg?raw=true) 75 | ![](/image/selfi.jpg?raw=true) | ![](/image/selfi_detected.jpg?raw=true) 76 | ![](/image/selfi2.jpg?raw=true) | ![](/image/selfi2_detected.jpg?raw=true) 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /model.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from datetime import datetime 6 | import time 7 | import os 8 | import numpy as np 9 | import tensorflow as tf 10 | #from data import distorted_inputs 11 | import re 12 | from tensorflow.contrib.layers import * 13 | 14 | from tensorflow.contrib.slim.python.slim.nets.inception_v3 import inception_v3_base 15 | 16 | 17 | TOWER_NAME = 'tower' 18 | 19 | 20 | def get_checkpoint(checkpoint_path, requested_step=None, basename='checkpoint'): 21 | if requested_step is not None: 22 | 23 | model_checkpoint_path = '%s/%s-%s' % (checkpoint_path, basename, requested_step) 24 | if os.path.exists(model_checkpoint_path) is None: 25 | print('No checkpoint file found at [%s]' % checkpoint_path) 26 | exit(-1) 27 | print(model_checkpoint_path) 28 | print(model_checkpoint_path) 29 | return model_checkpoint_path, requested_step 30 | 31 | ckpt = tf.train.get_checkpoint_state(checkpoint_path) 32 | if ckpt and ckpt.model_checkpoint_path: 33 | # Restore checkpoint as described in top of this program 34 | print(ckpt.model_checkpoint_path) 35 | global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] 36 | 37 | return ckpt.model_checkpoint_path, global_step 38 | else: 39 | print('No checkpoint file found at [%s]' % checkpoint_path) 40 | exit(-1) 41 | 42 | def _activation_summary(x): 43 | tensor_name = re.sub('%s_[0-9]*/' % TOWER_NAME, '', x.op.name) 44 | tf.summary.histogram(tensor_name + '/activations', x) 45 | tf.summary.scalar(tensor_name + '/sparsity', tf.nn.zero_fraction(x)) 46 | 47 | def inception_v3(nlabels, images, pkeep, is_training): 48 | 49 | batch_norm_params = { 50 | "is_training": is_training, 51 | "trainable": True, 52 | # Decay for the moving averages. 53 | "decay": 0.9997, 54 | # Epsilon to prevent 0s in variance. 55 | "epsilon": 0.001, 56 | # Collection containing the moving mean and moving variance. 57 | "variables_collections": { 58 | "beta": None, 59 | "gamma": None, 60 | "moving_mean": ["moving_vars"], 61 | "moving_variance": ["moving_vars"], 62 | } 63 | } 64 | weight_decay = 0.00004 65 | stddev=0.1 66 | weights_regularizer = tf.contrib.layers.l2_regularizer(weight_decay) 67 | with tf.variable_scope("InceptionV3", "InceptionV3", [images]) as scope: 68 | 69 | with tf.contrib.slim.arg_scope( 70 | [tf.contrib.slim.conv2d, tf.contrib.slim.fully_connected], 71 | weights_regularizer=weights_regularizer, 72 | trainable=True): 73 | with tf.contrib.slim.arg_scope( 74 | [tf.contrib.slim.conv2d], 75 | weights_initializer=tf.truncated_normal_initializer(stddev=stddev), 76 | activation_fn=tf.nn.relu, 77 | normalizer_fn=batch_norm, 78 | normalizer_params=batch_norm_params): 79 | net, end_points = inception_v3_base(images, scope=scope) 80 | with tf.variable_scope("logits"): 81 | shape = net.get_shape() 82 | net = avg_pool2d(net, shape[1:3], padding="VALID", scope="pool") 83 | net = tf.nn.dropout(net, pkeep, name='droplast') 84 | net = flatten(net, scope="flatten") 85 | 86 | with tf.variable_scope('output') as scope: 87 | 88 | weights = tf.Variable(tf.truncated_normal([2048, nlabels], mean=0.0, stddev=0.01), name='weights') 89 | biases = tf.Variable(tf.constant(0.0, shape=[nlabels], dtype=tf.float32), name='biases') 90 | output = tf.add(tf.matmul(net, weights), biases, name=scope.name) 91 | _activation_summary(output) 92 | return output 93 | 94 | 95 | def inception_v3_test(nlabels, images, pkeep, is_training): 96 | 97 | batch_norm_params = { 98 | "is_training": is_training, 99 | "trainable": True, 100 | # Decay for the moving averages. 101 | "decay": 0.9997, 102 | # Epsilon to prevent 0s in variance. 103 | "epsilon": 0.001, 104 | # Collection containing the moving mean and moving variance. 105 | "variables_collections": { 106 | "beta": None, 107 | "gamma": None, 108 | "moving_mean": ["moving_vars"], 109 | "moving_variance": ["moving_vars"], 110 | } 111 | } 112 | weight_decay = 0.00004 113 | stddev=0.1 114 | weights_regularizer = tf.contrib.layers.l2_regularizer(weight_decay) 115 | with tf.variable_scope("InceptionV3", "InceptionV3", [images]) as scope: 116 | 117 | with tf.contrib.slim.arg_scope( 118 | [tf.contrib.slim.conv2d, tf.contrib.slim.fully_connected], 119 | weights_regularizer=weights_regularizer, 120 | trainable=True): 121 | with tf.contrib.slim.arg_scope( 122 | [tf.contrib.slim.conv2d], 123 | weights_initializer=tf.truncated_normal_initializer(stddev=stddev), 124 | activation_fn=tf.nn.relu, 125 | normalizer_fn=batch_norm, 126 | normalizer_params=batch_norm_params): 127 | net, end_points = inception_v3_base(images, scope=scope) 128 | with tf.variable_scope("logits"): 129 | shape = net.get_shape() 130 | net = avg_pool2d(net, shape[1:3], padding="VALID", scope="pool") 131 | net = tf.nn.dropout(net, pkeep, name='droplast') 132 | net = flatten(net, scope="flatten") 133 | 134 | with tf.variable_scope('output') as scope: 135 | 136 | weights = tf.Variable(tf.truncated_normal([2048, nlabels], mean=0.0, stddev=0.01), name='weights') 137 | biases = tf.Variable(tf.constant(0.0, shape=[nlabels], dtype=tf.float32), name='biases') 138 | output = tf.add(tf.matmul(net, weights), biases, name=scope.name) 139 | _activation_summary(output) 140 | return output,net 141 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | import six.moves 6 | from datetime import datetime 7 | import sys 8 | import math 9 | import time 10 | import timeit 11 | #from data import inputs, standardize_image 12 | import numpy as np 13 | import tensorflow as tf 14 | import re 15 | import numpy as np 16 | import os 17 | import xml.etree.ElementTree as ET 18 | import tensorflow as tf 19 | import copy 20 | import cv2 21 | from numpy import array 22 | from distutils.version import LooseVersion 23 | import os 24 | 25 | VERSION_GTE_0_12_0 = LooseVersion(tf.__version__) >= LooseVersion('0.12.0') 26 | 27 | # Name change in TF v 0.12.0 28 | if VERSION_GTE_0_12_0: 29 | standardize_image = tf.image.per_image_standardization 30 | else: 31 | standardize_image = tf.image.per_image_whitening 32 | 33 | class BoundBox: 34 | def __init__(self, x, y, w, h, c = None, classes = None): 35 | self.x = x 36 | self.y = y 37 | self.w = w 38 | self.h = h 39 | 40 | self.c = c 41 | self.classes = classes 42 | 43 | self.label = -1 44 | self.score = -1 45 | 46 | def get_label(self): 47 | if self.label == -1: 48 | self.label = np.argmax(self.classes) 49 | 50 | return self.label 51 | 52 | def get_score(self): 53 | if self.score == -1: 54 | self.score = self.classes[self.get_label()] 55 | 56 | return self.score 57 | 58 | class WeightReader: 59 | def __init__(self, weight_file): 60 | self.offset = 4 61 | self.all_weights = np.fromfile(weight_file, dtype='float32') 62 | 63 | def read_bytes(self, size): 64 | self.offset = self.offset + size 65 | return self.all_weights[self.offset-size:self.offset] 66 | 67 | def reset(self): 68 | self.offset = 4 69 | 70 | def normalize(image): 71 | image = image / 255. 72 | 73 | return image 74 | 75 | def bbox_iou(box1, box2): 76 | x1_min = box1.x - box1.w/2 77 | x1_max = box1.x + box1.w/2 78 | y1_min = box1.y - box1.h/2 79 | y1_max = box1.y + box1.h/2 80 | 81 | x2_min = box2.x - box2.w/2 82 | x2_max = box2.x + box2.w/2 83 | y2_min = box2.y - box2.h/2 84 | y2_max = box2.y + box2.h/2 85 | 86 | intersect_w = interval_overlap([x1_min, x1_max], [x2_min, x2_max]) 87 | intersect_h = interval_overlap([y1_min, y1_max], [y2_min, y2_max]) 88 | 89 | intersect = intersect_w * intersect_h 90 | 91 | union = box1.w * box1.h + box2.w * box2.h - intersect 92 | 93 | return float(intersect) / union 94 | 95 | def interval_overlap(interval_a, interval_b): 96 | x1, x2 = interval_a 97 | x3, x4 = interval_b 98 | 99 | if x3 < x1: 100 | if x4 < x1: 101 | return 0 102 | else: 103 | return min(x2,x4) - x1 104 | else: 105 | if x2 < x3: 106 | return 0 107 | else: 108 | return min(x2,x4) - x3 109 | 110 | def draw_reduced(image, boxes, labels,name): 111 | 112 | i=0 113 | for box in boxes: 114 | i=i+1 115 | maxDist=max((box.w*image.shape[1]),(box.h)* image.shape[0]) 116 | xmin = int((box.x * image.shape[1])- maxDist/2) 117 | xmax = int((box.x * image.shape[1])+ maxDist/2) 118 | ymin = int((box.y * image.shape[0])- maxDist/2) 119 | ymax = int((box.y * image.shape[0])+ maxDist/2) 120 | 121 | cv2.imwrite(name, image[ymin:ymax,xmin:xmax]) 122 | return image 123 | 124 | def draw_boxes(image, boxes, labels): 125 | 126 | i=0 127 | for box in boxes: 128 | i=i+1 129 | xmin = int((box.x - box.w/2) * image.shape[1]) 130 | xmax = int((box.x + box.w/2) * image.shape[1]) 131 | ymin = int((box.y - box.h/2) * image.shape[0]) 132 | ymax = int((box.y + box.h/2) * image.shape[0]) 133 | 134 | 135 | #cv2.putText(image, 136 | # labels[box.get_label()] + ' ' + str(box.get_score()), 137 | # (xmin, ymin - 13), 138 | # cv2.FONT_HERSHEY_SIMPLEX, 139 | # 1e-3 * image.shape[0], 140 | # (0,255,0), 2) 141 | print(labels[box.get_label()] + ' ' +str(box.get_score()) + ' size: '+str(xmax-xmin)+'X'+str(ymax-ymin) ) 142 | cv2.imwrite('/home/olivier/Desktop/yolo-floydhub/test/image_extracted'+str(i)+'.jpg', image[ymin:ymax,xmin:xmax]) 143 | #temp =image[int((box.y* image.shape[1]-64)):int((box.y* image.shape[1]+64)),int((box.x* image.shape[1]-64)):int((box.x* image.shape[1]+64))] 144 | #cv2.imwrite('/home/olivier/Desktop/yolo-floydhub/test/image_extracted'+str(i)+'.jpg', temp) 145 | cv2.rectangle(image, (xmin,ymin), (xmax,ymax), (0,255,0), 3) 146 | #cv2.rectangle(image, (int((box.x* image.shape[1]-64)),int((box.y* image.shape[1]-64))), (int((box.x* image.shape[1]+64)),int((box.y* image.shape[1]+64))), (0,255,0), 3) 147 | return image 148 | 149 | def getFacesList(image, boxes): 150 | imgList=[] 151 | i=0 152 | for box in boxes: 153 | i=i+1 154 | if (len(boxes) ==1): 155 | maxDist=max((box.w*image.shape[1]),(box.h)* image.shape[0])*1.4 156 | else: 157 | maxDist=max((box.w*image.shape[1]),(box.h)* image.shape[0])*1.05 158 | xmin = int((box.x * image.shape[1])- maxDist/2) 159 | xmax = int((box.x * image.shape[1])+ maxDist/2) 160 | ymin = int((box.y * image.shape[0])- maxDist/2) 161 | ymax = int((box.y * image.shape[0])+ maxDist/2) 162 | 163 | height, width = image.shape[:2] 164 | #adjustment 165 | if (xmin<0): 166 | print("xmin<0") 167 | dif=xmin 168 | xmin=max(xmin,0) 169 | ymin=int(round(ymin-dif/2)) 170 | ymax=int(round(ymax+dif/2)) 171 | if (ymin<0): 172 | print("ymin<0",) 173 | dif=ymin 174 | ymin=max(ymin,0) 175 | xmin=int(round(xmin-dif/2)) 176 | xmax=int(round(xmax+dif/2)) 177 | print("ymin<0 xmin="+str(xmin)+"xmax"+str(xmax)+"dist1 ="+str(xmax-xmin)+"dist2="+str(ymax-ymin)) 178 | if (xmax>width): 179 | print("xmax>width") 180 | dif=width-xmax 181 | xmax=min(xmax,width ) 182 | ymin=int(round(ymin-dif/2)) 183 | ymax=int(round(ymax+dif/2)) 184 | if (ymax>height): 185 | print("xmax>height") 186 | dif=width-ymax 187 | ymin=min(ymax,height ) 188 | xmin=int(round(xmin-dif/2)) 189 | xmax=int(round(xmax+dif/2 )) 190 | 191 | 192 | #xmax=min(xmax,width ) 193 | #ymin=max(ymin,0) 194 | #ymax=min(ymax,height ) 195 | 196 | #print("width: "+str(width)+" height: "+str(height)+" xmin: "+str(xmin)+ " xmax: "+str(xmax)+" ymin: "+str(ymin)+ " ymax: "+str(ymax) ) 197 | 198 | path = os.path.dirname(os.path.abspath(__file__))+'/detected_faces/image_extracted'+str(i)+'.jpg' 199 | cv2.imwrite(path, image[ymin:ymax,xmin:xmax]) 200 | imgList.append(path) 201 | 202 | return imgList 203 | 204 | def draw_boxes_v2(image, boxes, labels,listPrediction): 205 | 206 | i=0 207 | for box in boxes: 208 | xmin = int((box.x - box.w/2) * image.shape[1]) 209 | xmax = int((box.x + box.w/2) * image.shape[1]) 210 | ymin = int((box.y - box.h/2) * image.shape[0]) 211 | ymax = int((box.y + box.h/2) * image.shape[0]) 212 | 213 | cv2.rectangle(image, (xmin,ymin), (xmax,ymax), (0,255,0), 3) 214 | temp=array( listPrediction[i][3]) 215 | if temp[0]>temp[1]: 216 | gender="Female" 217 | else: 218 | gender="Male" 219 | 220 | (gender +' ' + str(listPrediction[i][1]) +' '+ str(listPrediction[i][0])) 221 | 222 | a=int((xmin+xmax)/2)-10 223 | cv2.putText(image,gender +' ' + str(listPrediction[i][1]), 224 | (xmin, ymax+25), 225 | fontFace=cv2.FONT_HERSHEY_TRIPLEX, 226 | fontScale=1e-3 *3* image.shape[0], 227 | color=(0, 255, 0)) 228 | i=i+1 229 | cv2.cvtColor(image, cv2.COLOR_BGR2RGB) 230 | 231 | #del listPrediction 232 | del gender 233 | del box 234 | 235 | 236 | return image 237 | 238 | def decode_netout(netout, obj_threshold, nms_threshold, anchors, nb_class): 239 | grid_h, grid_w, nb_box = netout.shape[:3] 240 | 241 | boxes = [] 242 | 243 | # decode the output by the network 244 | netout[..., 4] = sigmoid(netout[..., 4]) 245 | netout[..., 5:] = netout[..., 4][..., np.newaxis] * softmax(netout[..., 5:]) 246 | netout[..., 5:] *= netout[..., 5:] > obj_threshold 247 | 248 | for row in range(grid_h): 249 | for col in range(grid_w): 250 | for b in range(nb_box): 251 | # from 4th element onwards are confidence and class classes 252 | classes = netout[row,col,b,5:] 253 | 254 | if np.sum(classes) > 0: 255 | # first 4 elements are x, y, w, and h 256 | x, y, w, h = netout[row,col,b,:4] 257 | 258 | x = (col + sigmoid(x)) / grid_w # center position, unit: image width 259 | y = (row + sigmoid(y)) / grid_h # center position, unit: image height 260 | w = anchors[2 * b + 0] * np.exp(w) / grid_w # unit: image width 261 | h = anchors[2 * b + 1] * np.exp(h) / grid_h # unit: image height 262 | confidence = netout[row,col,b,4] 263 | 264 | box = BoundBox(x, y, w, h, confidence, classes) 265 | 266 | boxes.append(box) 267 | 268 | # suppress non-maximal boxes 269 | for c in range(nb_class): 270 | sorted_indices = list(reversed(np.argsort([box.classes[c] for box in boxes]))) 271 | 272 | for i in xrange(len(sorted_indices)): 273 | index_i = sorted_indices[i] 274 | 275 | if boxes[index_i].classes[c] == 0: 276 | continue 277 | else: 278 | for j in xrange(i+1, len(sorted_indices)): 279 | index_j = sorted_indices[j] 280 | 281 | if bbox_iou(boxes[index_i], boxes[index_j]) >= nms_threshold: 282 | boxes[index_j].classes[c] = 0 283 | 284 | # remove the boxes which are less likely than a obj_threshold 285 | boxes = [box for box in boxes if box.get_score() > obj_threshold] 286 | 287 | return boxes 288 | 289 | def decode_netout2( netout,labels,anchors, obj_threshold=0.4, nms_threshold=0.3): 290 | grid_h, grid_w, nb_box = netout.shape[:3] 291 | nb_class=len(labels) 292 | boxes = [] 293 | 294 | # decode the output by the network 295 | netout[..., 4] = sigmoid(netout[..., 4]) 296 | netout[..., 5:] = netout[..., 4][..., np.newaxis] * softmax(netout[..., 5:]) 297 | netout[..., 5:] *= netout[..., 5:] > obj_threshold 298 | 299 | for row in range(grid_h): 300 | for col in range(grid_w): 301 | for b in range(nb_box): 302 | # from 4th element onwards are confidence and class classes 303 | classes = netout[row,col,b,5:] 304 | 305 | if np.sum(classes) > 0: 306 | # first 4 elements are x, y, w, and h 307 | x, y, w, h = netout[row,col,b,:4] 308 | 309 | x = (col + sigmoid(x)) / grid_w # center position, unit: image width 310 | y = (row + sigmoid(y)) / grid_h # center position, unit: image height 311 | w = anchors[2 * b + 0] * np.exp(w) / grid_w # unit: image width 312 | h = anchors[2 * b + 1] * np.exp(h) / grid_h # unit: image height 313 | confidence = netout[row,col,b,4] 314 | 315 | box = BoundBox(x, y, w, h, confidence, classes) 316 | 317 | boxes.append(box) 318 | 319 | # suppress non-maximal boxes 320 | for c in range(nb_class): 321 | sorted_indices = list(reversed(np.argsort([box.classes[c] for box in boxes]))) 322 | 323 | for i in xrange(len(sorted_indices)): 324 | index_i = sorted_indices[i] 325 | 326 | if boxes[index_i].classes[c] == 0: 327 | continue 328 | else: 329 | for j in xrange(i+1, len(sorted_indices)): 330 | index_j = sorted_indices[j] 331 | 332 | if bbox_iou(boxes[index_i], boxes[index_j]) >= nms_threshold: 333 | boxes[index_j].classes[c] = 0 334 | 335 | # remove the boxes which are less likely than a obj_threshold 336 | boxes = [box for box in boxes if box.get_score() > obj_threshold] 337 | 338 | return boxes 339 | 340 | def sigmoid(x): 341 | return 1. / (1. + np.exp(-x)) 342 | 343 | def softmax(x, axis=-1, t=-100.): 344 | x = x - np.max(x) 345 | 346 | if np.min(x) < t: 347 | x = x/np.min(x)*t 348 | 349 | e_x = np.exp(x) 350 | 351 | return e_x / e_x.sum(axis, keepdims=True) 352 | 353 | 354 | 355 | RESIZE_AOI = 256 356 | RESIZE_FINAL = 227 357 | 358 | # Modifed from here 359 | # http://stackoverflow.com/questions/3160699/python-progress-bar#3160819 360 | class ProgressBar(object): 361 | DEFAULT = 'Progress: %(bar)s %(percent)3d%%' 362 | FULL = '%(bar)s %(current)d/%(total)d (%(percent)3d%%) %(remaining)d to go' 363 | 364 | def __init__(self, total, width=40, fmt=DEFAULT, symbol='='): 365 | assert len(symbol) == 1 366 | 367 | self.total = total 368 | self.width = width 369 | self.symbol = symbol 370 | self.fmt = re.sub(r'(?P%\(.+?\))d', 371 | r'\g%dd' % len(str(total)), fmt) 372 | 373 | self.current = 0 374 | 375 | def update(self, step=1): 376 | self.current += step 377 | percent = self.current / float(self.total) 378 | size = int(self.width * percent) 379 | remaining = self.total - self.current 380 | bar = '[' + self.symbol * size + ' ' * (self.width - size) + ']' 381 | 382 | args = { 383 | 'total': self.total, 384 | 'bar': bar, 385 | 'current': self.current, 386 | 'percent': percent * 100, 387 | 'remaining': remaining 388 | } 389 | six.print_('\r' + self.fmt % args, end='') 390 | 391 | def done(self): 392 | self.current = self.total 393 | self.update(step=0) 394 | print('') 395 | 396 | # Read image files 397 | class ImageCoder(object): 398 | 399 | def __init__(self): 400 | # Create a single Session to run all image coding calls. 401 | config = tf.ConfigProto(allow_soft_placement=True) 402 | self._sess = tf.Session(config=config) 403 | 404 | # Initializes function that converts PNG to JPEG data. 405 | self._png_data = tf.placeholder(dtype=tf.string) 406 | image = tf.image.decode_png(self._png_data, channels=3) 407 | self._png_to_jpeg = tf.image.encode_jpeg(image, format='rgb', quality=100) 408 | 409 | # Initializes function that decodes RGB JPEG data. 410 | self._decode_jpeg_data = tf.placeholder(dtype=tf.string) 411 | self._decode_jpeg = tf.image.decode_jpeg(self._decode_jpeg_data, channels=3) 412 | self.crop = tf.image.resize_images(self._decode_jpeg, (RESIZE_AOI, RESIZE_AOI)) 413 | 414 | def png_to_jpeg(self, image_data): 415 | return self._sess.run(self._png_to_jpeg, 416 | feed_dict={self._png_data: image_data}) 417 | 418 | def decode_jpeg(self, image_data): 419 | image = self._sess.run(self.crop, #self._decode_jpeg, 420 | feed_dict={self._decode_jpeg_data: image_data}) 421 | 422 | assert len(image.shape) == 3 423 | assert image.shape[2] == 3 424 | return image 425 | 426 | 427 | def _is_png(filename): 428 | """Determine if a file contains a PNG format image. 429 | Args: 430 | filename: string, path of the image file. 431 | Returns: 432 | boolean indicating if the image is a PNG. 433 | """ 434 | return '.png' in filename 435 | 436 | def make_multi_image_batch(filenames, coder): 437 | """Process a multi-image batch, each with a single-look 438 | Args: 439 | filenames: list of paths 440 | coder: instance of ImageCoder to provide TensorFlow image coding utils. 441 | Returns: 442 | image_buffer: string, JPEG encoding of RGB image. 443 | """ 444 | 445 | images = [] 446 | 447 | for filename in filenames: 448 | with tf.gfile.FastGFile(filename, 'rb') as f: 449 | image_data = f.read() 450 | # Convert any PNG to JPEG's for consistency. 451 | if _is_png(filename): 452 | print('Converting PNG to JPEG for %s' % filename) 453 | image_data = coder.png_to_jpeg(image_data) 454 | 455 | image = coder.decode_jpeg(image_data) 456 | 457 | crop = tf.image.resize_images(image, (RESIZE_FINAL, RESIZE_FINAL)) 458 | image = standardize_image(crop) 459 | images.append(image) 460 | image_batch = tf.stack(images) 461 | return image_batch 462 | 463 | def make_multi_crop_batch(filename, coder): 464 | """Process a single image file. 465 | Args: 466 | filename: string, path to an image file e.g., '/path/to/example.JPG'. 467 | coder: instance of ImageCoder to provide TensorFlow image coding utils. 468 | Returns: 469 | image_buffer: string, JPEG encoding of RGB image. 470 | """ 471 | 472 | # Read the image file. 473 | with tf.gfile.FastGFile(filename, 'rb') as f: 474 | image_data = f.read() 475 | 476 | # Convert any PNG to JPEG's for consistency. 477 | if _is_png(filename): 478 | print('Converting PNG to JPEG for %s' % filename) 479 | image_data = coder.png_to_jpeg(image_data) 480 | 481 | image = coder.decode_jpeg(image_data) 482 | crops = [] 483 | print('Running multi-cropped image') 484 | h = image.shape[0] 485 | w = image.shape[1] 486 | hl = h - RESIZE_FINAL 487 | wl = w - RESIZE_FINAL 488 | 489 | crop = tf.image.resize_images(image, (RESIZE_FINAL, RESIZE_FINAL)) 490 | crops.append(standardize_image(crop)) 491 | crops.append(tf.image.flip_left_right(crop)) 492 | 493 | corners = [ (0, 0), (0, wl), (hl, 0), (hl, wl), (int(hl/2), int(wl/2))] 494 | for corner in corners: 495 | ch, cw = corner 496 | cropped = tf.image.crop_to_bounding_box(image, ch, cw, RESIZE_FINAL, RESIZE_FINAL) 497 | crops.append(standardize_image(cropped)) 498 | flipped = tf.image.flip_left_right(cropped) 499 | crops.append(standardize_image(flipped)) 500 | 501 | image_batch = tf.stack(crops) 502 | return image_batch 503 | 504 | --------------------------------------------------------------------------------