Skip to content

Commit

Permalink
changed model
Browse files Browse the repository at this point in the history
  • Loading branch information
amirtaherkhani committed Apr 13, 2019
1 parent fab7d4e commit 2a7b73c
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 69 deletions.
Empty file removed src/__init__.py
Empty file.
29 changes: 21 additions & 8 deletions src/align/detect_face.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#from math import floor
import cv2
import os
import numba
import ctypes

def layer(op):
"""Decorator for composable network layers."""
Expand Down Expand Up @@ -289,12 +291,18 @@ def create_mtcnn(sess, model_path):
data = tf.placeholder(tf.float32, (None,48,48,3), 'input')
onet = ONet({'data':data})
onet.load(os.path.join(model_path, 'det3.npy'), sess)

pnet_fun = lambda img : sess.run(('pnet/conv4-2/BiasAdd:0', 'pnet/prob1:0'), feed_dict={'pnet/input:0':img})
rnet_fun = lambda img : sess.run(('rnet/conv5-2/conv5-2:0', 'rnet/prob1:0'), feed_dict={'rnet/input:0':img})
onet_fun = lambda img : sess.run(('onet/conv6-2/conv6-2:0', 'onet/conv6-3/conv6-3:0', 'onet/prob1:0'), feed_dict={'onet/input:0':img})
sess.close()
with tf.device('/cpu:2'):
with tf.Graph().as_default():
tfconfig = tf.ConfigProto(device_count = {"CPU": 4}, allow_soft_placement = True)
tfconfig.intra_op_parallelism_threads = 8
tfconfig.inter_op_parallelism_threads = 8
with tf.Session(config = tfconfig) as sess:
pnet_fun = lambda img : sess.run(('pnet/conv4-2/BiasAdd:0', 'pnet/prob1:0'), feed_dict={'pnet/input:0':img})
rnet_fun = lambda img : sess.run(('rnet/conv5-2/conv5-2:0', 'rnet/prob1:0'), feed_dict={'rnet/input:0':img})
onet_fun = lambda img : sess.run(('onet/conv6-2/conv6-2:0', 'onet/conv6-3/conv6-3:0', 'onet/prob1:0'), feed_dict={'onet/input:0':img})
return pnet_fun, rnet_fun, onet_fun

@numba.jit(nopython=True, parallel=True,fastmath = True)
def detect_face(img, minsize, pnet, rnet, onet, threshold, factor):
"""Detects faces in an image, and returns bounding boxes and points for them.
img: input image
Expand Down Expand Up @@ -643,6 +651,7 @@ def bulk_detect_face(images, detection_window_size_ratio, pnet, rnet, onet, thre


# function [boundingbox] = bbreg(boundingbox,reg)
@numba.jit(nopaython=True)
def bbreg(boundingbox,reg):
"""Calibrate bounding boxes"""
if reg.shape[1]==1:
Expand All @@ -656,7 +665,7 @@ def bbreg(boundingbox,reg):
b4 = boundingbox[:,3]+reg[:,3]*h
boundingbox[:,0:4] = np.transpose(np.vstack([b1, b2, b3, b4 ]))
return boundingbox
@numba.jit(nopython=True, parallel=True,fastmath = True)
def generateBoundingBox(imap, reg, scale, t):
"""Use heatmap to generate bounding boxes"""
stride=2
Expand All @@ -682,8 +691,9 @@ def generateBoundingBox(imap, reg, scale, t):
q2 = np.fix((stride*bb+cellsize-1+1)/scale)
boundingbox = np.hstack([q1, q2, np.expand_dims(score,1), reg])
return boundingbox, reg

# function pick = nms(boxes,threshold,type)
@numba.jit(nopaython=True)
def nms(boxes, threshold, method):
if boxes.size==0:
return np.empty((0,3))
Expand Down Expand Up @@ -717,6 +727,7 @@ def nms(boxes, threshold, method):
return pick

# function [dy edy dx edx y ey x ex tmpw tmph] = pad(total_boxes,w,h)
@numba.jit(nopaython=True)
def pad(total_boxes, w, h):
"""Compute the padding coordinates (pad the bounding boxes to square)"""
tmpw = (total_boxes[:,2]-total_boxes[:,0]+1).astype(np.int32)
Expand Down Expand Up @@ -752,6 +763,7 @@ def pad(total_boxes, w, h):
return dy, edy, dx, edx, y, ey, x, ex, tmpw, tmph

# function [bboxA] = rerec(bboxA)
@numba.jit(nopaython=True)
def rerec(bboxA):
"""Convert bboxA to square."""
h = bboxA[:,3]-bboxA[:,1]
Expand All @@ -761,7 +773,8 @@ def rerec(bboxA):
bboxA[:,1] = bboxA[:,1]+h*0.5-l*0.5
bboxA[:,2:4] = bboxA[:,0:2] + np.transpose(np.tile(l,(2,1)))
return bboxA


@numba.jit(nopaython=True)
def imresample(img, sz):
im_data = cv2.resize(img, (sz[1], sz[0]), interpolation=cv2.INTER_AREA) #@UndefinedVariable
return im_data
Expand Down
6 changes: 3 additions & 3 deletions src/data_processing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
process data and produce valid output for other moduls
"""
process data and produce valid output for other moduls
"""
import copy

Expand Down Expand Up @@ -71,7 +71,7 @@ def detect_faces_bbox(self, image):
minsize = 30
threshold = [self.pnet_threshold, self.rnet_threshold, self.onet_threshold]
factor = 0.709
bounding_boxes, _ = align.detect_face.detect_face(image, minsize, self.pnet, self.rnet, self.onet, threshold,
boundin g_boxes, _ = align.detect_face.detect_face(image, minsize, self.pnet, self.rnet, self.onet, threshold,
factor)
return bounding_boxes

Expand Down
4 changes: 1 addition & 3 deletions src/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
Handles ros operations necessary for using the package.
"""
import os

import roslib
import rospkg
import rospy
from person_recognition.srv import *
from std_msgs.msg import String

from execution import Execution

os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
Expand Down Expand Up @@ -69,7 +67,7 @@ def idle(self):
rospy.spin()
except KeyboardInterrupt:
print("Shutting down person recognition module")

def dict_to_json_str(self, data):
json_data = dict()
for key, value in data.iteritems():
Expand Down
1 change: 0 additions & 1 deletion src/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from data_acquisition import Warehouse
from data_processing import DataProcessing
from model_engineering import ModelEngineering

import roslib
import rospy
import cv2
Expand Down
52 changes: 0 additions & 52 deletions src/gender_detection.py
Original file line number Diff line number Diff line change
@@ -1,52 +0,0 @@
from keras.preprocessing.image import img_to_array
from keras.models import load_model
import numpy as np
import cv2
from keras.models import load_model


class Gender:
def __init__(self, model= "pre_trained_gn/gender_detection.model"):
self.model = load_model(model)
self.classes = ['man', 'woman']

def f_detector(self, frame, bbox):
if frame is None:
print("Could not read input image")
gender = list()
info = {}
if np.shape(bbox)[0] is 0 :
info = {"person": 0, "man": 0, "woman": 0}
return info
else:
for idx, f in enumerate(bbox):
(startx, starty) = f[0], f[1]
(endx, endy) = f[2], f[3],
conf = self.model.predict(
self.crop(frame, startx, starty, endx, endy))[0]
idx = np.argmax(conf)
label = self.classes[idx]
gender.append(label)
info = {"person": np.shape(bbox)[0], "man": gender.count("man"), "woman": gender.count("woman")}
return info, gender

def m_detector(self, img):
if img is None:
print("Could not read input image")
face_crop = cv2.resize(img, (96, 96))
face_crop = face_crop.astype("float") / 255.0
face_crop = img_to_array(face_crop)
face_crop = np.expand_dims(face_crop, axis=0)
conf = self.model.predict(face_crop)[0]
idx = np.argmax(conf)
label = self.classes[idx]
return label


def crop (self, frame, startx, starty, endx, endy):
face_crop = np.copy(frame[int(np.rint(starty)):int(np.rint(endy)), int(np.rint(startx)):int(np.rint(endx))])
face_crop = cv2.resize(face_crop, (96, 96))
face_crop = face_crop.astype("float") / 255.0
face_crop = img_to_array(face_crop)
face_crop = np.expand_dims(face_crop, axis=0)
return face_crop
2 changes: 0 additions & 2 deletions src/model_engineering.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import numpy as np
import tensorflow as tf
from tensorflow.python.platform import gfile
from gender_detection import Gender
from knn import KNN


Expand Down Expand Up @@ -115,7 +114,6 @@ def encode(self, images):
"""
if not self.initialized:
self.initialized = self.initialize()

feed_dict = {self.imgs_ph: images, self.phase_train_ph: False}
emb_array = self.session.run(self.embs_ph, feed_dict=feed_dict)
return emb_array
Expand Down

0 comments on commit 2a7b73c

Please sign in to comment.