-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jeremie Despraz
committed
Mar 3, 2018
0 parents
commit 03033b9
Showing
74 changed files
with
23,303 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class DeepemotionConfig(AppConfig): | ||
name = 'deepemotion' |
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# -*- coding: utf-8 -*- | ||
######## REFs face detection ######## | ||
# https://www.superdatascience.com/opencv-face-detection/ | ||
##################################### | ||
|
||
from keras.models import load_model | ||
from keras import backend as K | ||
import tensorflow as tf | ||
|
||
import os | ||
import numpy as np | ||
import cv2 | ||
|
||
import matplotlib | ||
matplotlib.use('Agg') | ||
import matplotlib.pyplot as plt | ||
|
||
os.environ["CUDA_VISIBLE_DEVICES"] = "" # do not use the GPU | ||
|
||
img_rows, img_cols = 48, 48 | ||
|
||
labels_txt = np.array(['Colère', 'Peur', 'Joie', 'Tristesse', 'Surprise', 'Neutre']) | ||
|
||
num_classes = labels_txt.size | ||
|
||
graph = tf.get_default_graph() | ||
model = load_model(r'/home/jeremie/jeremie/topten/static/model/trained_model') | ||
IMP = np.load(r'/home/jeremie/jeremie/topten/static/model/IMP.npy') | ||
print "model loaded" | ||
|
||
face_cascade = cv2.CascadeClassifier() | ||
face_cascade_name = "/home/jeremie/miniconda2/envs/keras/lib/python2.7/site-packages/cv2/data/haarcascade_frontalface_default.xml" | ||
face_cascade.load(face_cascade_name) | ||
print "face detector loaded" | ||
|
||
def transform_image(im): | ||
im2 = im.astype(np.float32)*1 | ||
im2 -= 127.817 | ||
im2 /= 74.3659 | ||
return im2 | ||
|
||
def cut_face(im, cascade_classifier): | ||
faces = cascade_classifier.detectMultiScale(im) | ||
print(len(faces), "face(s) detected") | ||
if len(faces) != 0: | ||
x, y, w, h = [ v for v in faces[0] ] | ||
return im[y:y+h, x:x+w] | ||
else: | ||
return im*0 | ||
return im | ||
|
||
def reconstruct_image(im): | ||
im2 = im.astype(np.float32)*1 | ||
im2 *= 74.3659 | ||
im2 += 127.817 | ||
return np.squeeze(im2.astype(np.uint8)) | ||
|
||
def transform(fname): | ||
image_path = fname | ||
custom_image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) | ||
custom_image = cut_face(custom_image, face_cascade) | ||
|
||
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) | ||
custom_input = clahe.apply(custom_image) | ||
|
||
sh = custom_input.shape | ||
custom_input = cv2.resize(custom_input, (img_rows, img_cols)) | ||
custom_input = transform_image(custom_input) | ||
custom_input = np.expand_dims(custom_input, axis=0) | ||
custom_input = np.expand_dims(custom_input, axis=0) | ||
with graph.as_default(): | ||
res1 = model.predict(custom_input, verbose=0)[0] | ||
res2 = model.predict(np.fliplr(custom_input), verbose=0)[0] | ||
res = (res1+res2)/2. | ||
k = np.argmax(res) | ||
|
||
input_img = model.layers[0].input | ||
for l in model.layers: | ||
if(l.name == 'last_conv'): | ||
layer_output = l.output | ||
get_acti = K.function([input_img, K.learning_phase()], [layer_output[:,:,:,:]]) | ||
|
||
fig = plt.figure() | ||
img = np.zeros((sh[0], sh[1])) | ||
acti = np.squeeze(get_acti([custom_input,0])[0]) | ||
for i in range(IMP[k].size): | ||
img += IMP[k][i]*cv2.resize(np.abs(acti[i]), (sh[1], sh[0])) | ||
plt.imshow(custom_image, cmap='gray') | ||
plt.imshow(img, alpha=0.4, cmap='bwr') | ||
plt.axis('off') | ||
fig.tight_layout() | ||
fig.savefig("static/temp2.jpeg") | ||
return res[0], labels_txt[k] | ||
|
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<!doctype html> | ||
{% load static %} | ||
<html lang="fr"> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>DeepEmotion</title> | ||
<link rel="stylesheet" type="text/css" href="{% static 'bootstrap-4.0.0-dist/css/bootstrap.min.css' %}"> | ||
<link rel="stylesheet" type="text/css" href="{% static 'css/styles.css' %}"> | ||
</head> | ||
|
||
<body id="body"> | ||
<div class="container-fluid"> | ||
<div class="row justify-content-between"> | ||
<div class="col-4"> | ||
<img src="{% static 'logos/CI4CB_logo.png' %}" class="float-left" alt="Responsive image" width="50%"> | ||
</div> | ||
<div class="col-4"> | ||
<img src="{% static 'logos/HEIG-VD_Logo-83x25_CMJN-ROUGE.jpg' %}" class="float-right" alt="Responsive image" width="75%"> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="jumbotron text-center"> | ||
<div class="container"> | ||
<h1>DeepEmotion</h1> | ||
<h3>Un réseau de neurones capable de lire les émotions sur votre visage</h3> | ||
<form id="theForm" method="POST" enctype="multipart/form-data">{% csrf_token %} | ||
<input id="id_pic" name="url" type=text value="" autofocus="" hidden> | ||
<input id="id_image_data" name="img" type="text" value="" autofocus="" hidden> | ||
<input type='submit' value='Take Snapshot' hidden/> | ||
<button id="button" onCLick='switchForm();' hidden>Dynamic mode</button> | ||
</form> | ||
</div> | ||
</div> | ||
|
||
<div class="container-fluid"> | ||
<div class="row align-items-center"> | ||
<div class="col text-center"> | ||
<h4>Entrée</h4> | ||
<div id="my_camera" class="d-inline"></div> | ||
</div> | ||
<div class="col text-center"> | ||
<h4>Votre visage laisse transparaître l'émotion</h4> | ||
<h2 id="id_emotion">...Cliquez pour le découvrir...</h2> | ||
<h5> Certitude de la prédiction:</h5> | ||
{% for key, value in emotions.items%} | ||
<div class="progress-bar-alt vertical rounded"> | ||
<div class="progress-track"> | ||
<div class="progress-fill" style="background: linear-gradient(to bottom, {{value.1}} 0%, {{value.2}} 100%);"> | ||
<span id="{{value.0}}">0.1%</span> | ||
</div> | ||
</div> | ||
<p class="histlabel">{{key}}</p> | ||
</div> | ||
{% endfor %} | ||
</div> | ||
|
||
<div class="col text-center" id="results"> | ||
<h4>Zones déterminantes *</h4> | ||
<img id="t_pic2" src="{% static 'temp2.jpeg' %}" class="float-center" alt="Responsive image" width="75%"> | ||
</div> | ||
</div> | ||
<div class="row justify-content-end"> | ||
<p id="heatmaplegend">* plus une zone est rouge, plus celle-ci a été déterminante dans la décision du réseau.</p> | ||
</div> | ||
</div> | ||
|
||
<!-- javascript imports --> | ||
<script src="{% static 'js/jquery-3.1.0.min.js' %}"></script> | ||
<script type="text/javascript" src="{% static 'js/html2canvas.min.js' %}"></script> | ||
<script type="text/javascript" src="{% static 'js/jquery_script.js' %}"></script> | ||
<!-- bootstrap specifics --> | ||
<script type="text/javascript" src="{% static 'js/popper.min.js' %}"></script> | ||
<script type="text/javascript" src="{% static 'bootstrap-4.0.0-dist/js/bootstrap.min.js' %}"></script> | ||
<!-- Webcam specific scripts --> | ||
<script type="text/javascript" src="{% static 'js/webcam.min.js' %}"></script> | ||
<script type="text/javascript" src="{% static 'js/webcam_specifics.js' %}"></script> | ||
</body> | ||
</html> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import unicode_literals | ||
from django.shortcuts import render, redirect | ||
from django.templatetags.static import static | ||
from django.http import HttpResponse | ||
import json | ||
|
||
# Create your views here. | ||
|
||
from core import effect | ||
import urllib | ||
import re | ||
from collections import OrderedDict | ||
|
||
def IndexView(request): | ||
prob=[0,0,0,0,0,0] | ||
emotions = OrderedDict() | ||
emotions['Joie']=['happy', '#ff9900', '#ffff99'] | ||
emotions['Colère']=['angry', '#ff0000', '#800000'] | ||
emotions['Tristesse']=['sad', '#0033cc', '#0099cc'] | ||
emotions['Peur']=['scared', '#006600', '#009900'] | ||
emotions['Surprise']=['surprised', '#3366cc', '#66ffff'] | ||
emotions['Neutre']=['neutral', '#669999', '#99ccff'] | ||
if request.is_ajax(): | ||
URL=request.POST.get("url") | ||
screen=request.POST.get("img") | ||
url=urllib.urlopen(URL) | ||
with open('static/temp.jpeg', 'wb') as f: | ||
f.write(url.read()) | ||
prob,label=effect.transform('static/temp.jpeg') | ||
prob=[str(x) for x in prob] | ||
screen=screen.replace("/static/","/home/jeremie/jeremie/topten/static/") | ||
screen=re.sub('<script.*?</script>',"",screen) | ||
screen=re.sub('<div id="camera".*?<\/div>.*?<\/div>.*?<\/div>','<div id="results" style="float:left;margin-top:125px;"> <img id="t_pic2" src="/home/jeremie/jeremie/topten/static/temp.jpeg"> </div>',screen) | ||
f=open("static/out.html","w") | ||
f.write(screen.encode('utf-8')) | ||
f.close() | ||
return HttpResponse(json.dumps({"prob":prob,"label":label}),content_type="application/json") | ||
return render(request, 'deepemotion/index.html',{"emotions":emotions}) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import sys | ||
|
||
if __name__ == "__main__": | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "topten.settings") | ||
try: | ||
from django.core.management import execute_from_command_line | ||
except ImportError: | ||
# The above import may fail for some other reason. Ensure that the | ||
# issue is really that Django is missing to avoid masking other | ||
# exceptions on Python 2. | ||
try: | ||
import django | ||
except ImportError: | ||
raise ImportError( | ||
"Couldn't import Django. Are you sure it's installed and " | ||
"available on your PYTHONPATH environment variable? Did you " | ||
"forget to activate a virtual environment?" | ||
) | ||
raise | ||
execute_from_command_line(sys.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/bash | ||
|
||
source activate keras | ||
python manage.py runserver 0.0.0.0:8000 | ||
|
||
|
Oops, something went wrong.