Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasxlu committed Dec 9, 2018
1 parent 5f806d4 commit b70f5f2
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ cv/__pycache__/
cv/static/FaceUpload/
dm/__pycache__/
nlp/__pycache__/
__pycache__/
pyWeb/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ with RESTful APIs. The platform is developed and maintained by [@LucasX](https:/
- [x] Age Estimation

## Deployment
1. create a virtual enviroment named ```pyWeb```
1. create a virtual enviroment named ```pyWeb``` follow [this tutorial](https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432712108300322c61f256c74803b43bfd65c6f8d0d0000)
2. install [Django](https://docs.djangoproject.com/en/2.1/intro/install/) and [PyTorch](https://pytorch.org/)
3. install all dependent libraries: ```pip3 install -r requirements.txt```
4. activate Python Web environment: ```source ~/pyWeb/bin/activate pyWeb```
Expand Down
3 changes: 2 additions & 1 deletion XCloud/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,5 @@

STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = ('D:/Users/LucasX/PycharmProjects/XCloud/cv/static', '/root/Project/XCloud/cv/static')
STATICFILES_DIRS = ('D:/Users/LucasX/PycharmProjects/XCloud/cv/static', '/root/Project/XCloud/cv/static',
'D:/Users/29140\PycharmProjects\XCloud\cv\static')
36 changes: 27 additions & 9 deletions cv/controllers.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import json
import os
import time
from random import randint

import numpy as np
import cv2
from django.http import HttpResponse
from mtcnn.mtcnn import MTCNN
from sklearn.externals import joblib

from cv import features

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

URL_PORT = 'http://localhost:8000'


def upload_and_rec(request):
def upload_and_rec_beauty(request):
"""
upload and recognize image
:param request:
Expand Down Expand Up @@ -43,11 +46,13 @@ def upload_and_rec(request):
tik = time.time()
imagepath = URL_PORT + '/static/FaceUpload/' + image.name

beauty = BeautyRecognizer().infer(os.path.join(image_dir, image.name))

result['code'] = 0
result['msg'] = 'success'
result['data'] = {
'imgpath': imagepath,
'beauty': randint(0, 9)
'beauty': beauty
}
result['elapse'] = time.time() - tik

Expand Down Expand Up @@ -78,14 +83,27 @@ def detect_face(img_path):


class BeautyRecognizer:
def __init__(self, pretrained_model):
self.model = None
def __init__(self, pretrained_model='./model/GradientBoostingRegressor.pkl'):
gbr = joblib.load(pretrained_model)
self.model = gbr

def infer(self, img_path):
img = cv2.imread(img_path)
mtcnn_result = detect_face(img_path)
bbox = mtcnn_result['box']
face_region = img[bbox[0] - int(bbox[2] / 2): bbox[0] + int(bbox[2] / 2),
bbox[1] - int(bbox[3] / 2): bbox[1] + int(bbox[3] / 2)]
bbox = mtcnn_result[0]['box']

margin_pixel = 10
face_region = img[bbox[0] - margin_pixel: bbox[0] + bbox[2] + margin_pixel,
bbox[1] - margin_pixel: bbox[1] + bbox[3] + margin_pixel]

ratio = max(face_region.shape[0], face_region.shape[1]) / min(face_region.shape[0], face_region.shape[1])
if face_region.shape[0] < face_region.shape[1]:
face_region = cv2.resize(face_region, (int(ratio * 64), 64))
face_region = face_region[:,
int((face_region.shape[0] - 64) / 2): int((face_region.shape[0] - 64) / 2) + 64]
else:
face_region = cv2.resize(face_region, (64, int(ratio * 64)))
face_region = face_region[int((face_region.shape[1] - 64) / 2): int((face_region.shape[1] - 64) / 2) + 64,
:]

return self.model.infer(face_region)
return self.model.predict(np.array(features.HOG_from_cv(face_region).reshape(1, -1)))[0]
7 changes: 7 additions & 0 deletions cv/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ def RAW(img_path):
return img.reshape(img.shape[0] * img.shape[1])


def RAW_from_cv(img):
img = skimage.color.rgb2gray(img)
img = (img - np.mean(img)) / np.std(img)

return img.reshape(img.shape[0] * img.shape[1])


def HOG_from_cv(img):
"""
extract HOG feature from opencv image object
Expand Down
Binary file added cv/model/GradientBoostingRegressor.pkl
Binary file not shown.
2 changes: 1 addition & 1 deletion cv/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ <h3>From Research to Production</h3>
<div class="row">
<div class="col-lg-12 text-center">
<h2>HMT-Net</h2>
<p class="lead">Analyse Your Face in <span style="color: red;
<p class="lead">Analyze Your Face in <span style="color: red;
font-size:larger">Real-time</span></p>
</div>
</div>
Expand Down
54 changes: 42 additions & 12 deletions cv/train_and_test_fbp_with_ml.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@

import pandas as pd
from mtcnn.mtcnn import MTCNN
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.externals import joblib
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.model_selection import train_test_split

sys.path.append('../')
from cv.features import *

SCUT5500_DIR = "E:\DataSet\CV\SCUT-FBP5500\Images"
LABEL_CSV = "E:\DataSet\CV\SCUT-FBP5500/train_test_files\SCUT-FBP5500-With-Head.csv"
SCUT5500_DIR = "E:\DataSet\Face\SCUT-FBP5500\Images"
SCUT5500_CROP_DIR = "E:\DataSet\Face\SCUT-FBP5500\Crop"
LABEL_CSV = "E:\DataSet\Face\SCUT-FBP5500/train_test_files\SCUT-FBP5500-With-Head.csv"


def prepare_data():
Expand Down Expand Up @@ -54,8 +55,8 @@ def prepare_data():
face_region = cv2.resize(img, (64, 64))

lbp = LBP_from_cv(face_region)
hog = HOG_from_cv(face_region)
ldmk = Geo_from_cv(img)
# hog = HOG_from_cv(face_region)
# ldmk = Geo_from_cv(img)

feature = lbp
features.append(feature)
Expand All @@ -64,21 +65,50 @@ def prepare_data():
return features, lbs


def prepare_data_from_crop():
"""
prepare face datasets from cropped facial regions
:return:
"""
features = []
lbs = []

df = pd.read_csv(LABEL_CSV)
files = df['file']
scores = df['score']

for i in range(len(files)):
img = cv2.imread(os.path.join(SCUT5500_CROP_DIR, files[i]))

face_region = cv2.resize(img, (64, 64))

hog = HOG_from_cv(face_region)
# raw = RAW_from_cv(face_region)
# lbp = LBP_from_cv(face_region)
# ldmk = Geo_from_cv(img)

feature = list(hog)
features.append(feature)
lbs.append(scores[i])

return features, lbs


def train_fbp(X, y):
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)

rfreg = RandomForestRegressor()
print('start training Random Forest Regressor...')
rfreg.fit(X_train, y_train)
gbr = GradientBoostingRegressor()
print('start training regressor...')
gbr.fit(X_train, y_train)

if not os.path.exists('./model'):
os.makedirs('./model')

joblib.dump(rfreg, './model/RandomForestRegressor.pkl')
joblib.dump(gbr, './model/GradientBoostingRegressor.pkl')
print('The regression model has been persisted...')

y_pred = rfreg.predict(X_test)
y_pred = gbr.predict(X_test)

mae_lr = round(mean_absolute_error(y_test, np.array(y_pred).ravel()), 4)
rmse_lr = round(np.math.sqrt(mean_squared_error(np.array(y_test), np.array(y_pred).ravel())), 4)
Expand All @@ -90,5 +120,5 @@ def train_fbp(X, y):


if __name__ == '__main__':
X, y = prepare_data()
X, y = prepare_data_from_crop()
train_fbp(X, y)
2 changes: 1 addition & 1 deletion cv/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def index(request):

def fbp(request):
from cv import controllers
return controllers.upload_and_rec(request)
return controllers.upload_and_rec_beauty(request)


def fbp_view(request):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
jieba==0.39
opencv_python==3.4.0.12
mtcnn==0.0.6
Django==2.1.3
Django==2.1.4
snownlp==0.12.3

0 comments on commit b70f5f2

Please sign in to comment.