Skip to content

Commit

Permalink
fix weight relative path in model
Browse files Browse the repository at this point in the history
  • Loading branch information
dandynaufaldi committed Aug 23, 2018
1 parent 17915ae commit 1ae3627
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 7 deletions.
3 changes: 2 additions & 1 deletion model/inceptionv3.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def __init__(self):
base = InceptionV3(
input_shape=(140, 140, 3),
include_top=False,
weights='weight/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5')
weights=os.path.dirname(
__file__)+'/weight/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5')
top_layer = GlobalAveragePooling2D(name='avg_pool')(base.output)
gender_layer = Dense(2, activation='softmax', name='gender_prediction')(top_layer)
age_layer = Dense(101, activation='softmax', name='age_prediction')(top_layer)
Expand Down
3 changes: 2 additions & 1 deletion model/insightface.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ class InsightFace:
def __init__(self):
ctx = mx.gpu(0)
self.image_size = (112, 112)
self.ga_model = get_model(ctx, self.image_size, 'weight/model-r34-age/model,0', 'fc1')
self.ga_model = get_model(ctx, self.image_size, os.path.dirname(
__file__)+'/weight/model-r34-age/model,0', 'fc1')
self.threshold = 1.24
self.det_minsize = 50
self.det_threshold = [0.6, 0.7, 0.8]
Expand Down
7 changes: 3 additions & 4 deletions model/mobilenetv2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import os
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras.applications.mobilenetv2 import MobileNetV2
Expand All @@ -11,10 +12,8 @@ class AgenderNetMobileNetV2(Model):

def __init__(self):
self.input_size = 96
base = MobileNetV2(
input_shape=(96, 96, 3),
include_top=False,
weights='weight/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_96_no_top.h5')
base = MobileNetV2(input_shape=(96, 96, 3), include_top=False, weights=os.path.dirname(
__file__)+'/weight/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_96_no_top.h5')
top_layer = GlobalAveragePooling2D()(base.output)
gender_layer = Dense(2, activation='softmax', name='gender_prediction')(top_layer)
age_layer = Dense(101, activation='softmax', name='age_prediction')(top_layer)
Expand Down
Empty file added model/weight/inceptionv3/.keep
Empty file.
Empty file added model/weight/mobilenetv2/.keep
Empty file.
Empty file added result/.keep
Empty file.
70 changes: 70 additions & 0 deletions utils/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import os
import cv2
import dlib
import numpy as np


def resize_square_image(image: np.ndarray, size: int =140):
"""
Resize image and make it square
Parameters
----------
image : numpy array -> with dtype uint8 and shape (W, H, 3)
Image to be resized
size : int
Size of image to be returned
Returns
----------
resized : numpy array -> with dtype uint8 and shape (size, size, 3)
Resized image
"""
BLACK = [0, 0, 0]
h = image.shape[0]
w = image.shape[1]
if w < h:
border = h-w
image = cv2.copyMakeBorder(
image, 0, 0, border, 0, cv2.BORDER_CONSTANT, value=BLACK)
else:
border = w-h
image = cv2.copyMakeBorder(
image, border, 0, 0, 0, cv2.BORDER_CONSTANT, value=BLACK)
resized = cv2.resize(image, (size, size), interpolation=cv2.INTER_CUBIC)
return resized


def align_faces(image: np.ndarray, padding: float=0.4, size: int=140, predictor_path: str=os.path.dirname(
os.path.dirname(__file__)) + '/model/shape_predictor_5_face_landmarks.dat'):
"""
Get aligned faces from image if face is detected, else just resize
Parameters
----------
image : numpy array -> with dtype uint8 and shape (W, H, 3)
Image to be processed
padding : float
Padding for aligned face
size : int
Size of image to be returned
path : string
Path to dlib facial landmark detector
Returns
----------
result : numpy array -> with dtype uint8 and shape (size, size, 3)
Processed image
"""
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
rects = detector(image, 1)
result = None

# if detect exactly 1 face, get aligned face
if len(rects) == 1:
shape = predictor(image, rects[0])
result = dlib.get_face_chip(image, shape, padding=padding, size=size)

# else use resized full image
else:
result = resize_square_image(image, size)
return result
2 changes: 1 addition & 1 deletion utils/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def update(self):
def elapsed(self):
# return the total number of seconds between the start and
# end interval
return (self._end - self._start).total_seconds()
return (datetime.datetime.now() - self._start).total_seconds()

def fps(self):
# compute the (approximate) frames per second
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 1ae3627

Please sign in to comment.