Commit 20492cc6 authored by Richard Torenvliet's avatar Richard Torenvliet

Update server to use the ibug reconstruction method

parent 67f1b14e
...@@ -248,7 +248,7 @@ def build_texture_feature_vectors( ...@@ -248,7 +248,7 @@ def build_texture_feature_vectors(
x, y, w_slice, h_slice = mean_points.get_bounding_box() x, y, w_slice, h_slice = mean_points.get_bounding_box()
for i, f in enumerate(files[:10]): for i, f in enumerate(files):
image, points = get_image_with_points(f) image, points = get_image_with_points(f)
Points = AAMPoints( Points = AAMPoints(
......
...@@ -18,7 +18,7 @@ class IBUGPoints(aam.AAMPoints): ...@@ -18,7 +18,7 @@ class IBUGPoints(aam.AAMPoints):
SHAPE = (68, 2) SHAPE = (68, 2)
"""IBUG datapoints abstraction""" """IBUG datapoints abstraction"""
def __init__(self, filename=None, points_list=None): def __init__(self, filename=None, image=None, points_list=None):
""" """
Args: Args:
filename: optional image file filename: optional image file
...@@ -30,7 +30,11 @@ class IBUGPoints(aam.AAMPoints): ...@@ -30,7 +30,11 @@ class IBUGPoints(aam.AAMPoints):
self.filename = filename self.filename = filename
if self.filename: if self.filename:
self.__get_image() if image is None:
self.__get_image()
else:
self.image = image
self.detector = landmarks.Detector() self.detector = landmarks.Detector()
points_list = self.detector.detect_shape(self.image)[0] points_list = self.detector.detect_shape(self.image)[0]
points_list = np.asarray(points_list, dtype=np.float32) points_list = np.asarray(points_list, dtype=np.float32)
...@@ -114,7 +118,7 @@ def get_points(files): ...@@ -114,7 +118,7 @@ def get_points(files):
points = [] points = []
total_files = len(files) total_files = len(files)
for i, filename in enumerate(files[:10]): for i, filename in enumerate(files):
t1 = time() t1 = time()
ibug = IBUGPoints(filename=filename) ibug = IBUGPoints(filename=filename)
points.append(ibug.get_points()) points.append(ibug.get_points())
......
import json import json
import traceback
import os.path import os.path
import base64 import base64
from glob import glob from glob import glob
...@@ -7,6 +8,7 @@ import cv2 ...@@ -7,6 +8,7 @@ import cv2
import numpy as np import numpy as np
from tornado import websocket, web, ioloop, autoreload from tornado import websocket, web, ioloop, autoreload
import pca import pca
from datasets import imm from datasets import imm
from reconstruction import reconstruction from reconstruction import reconstruction
...@@ -30,8 +32,8 @@ class ImageWebSocketHandler(websocket.WebSocketHandler): ...@@ -30,8 +32,8 @@ class ImageWebSocketHandler(websocket.WebSocketHandler):
self.asf = glob('{}/*.asf'.format(FACE_DB)) self.asf = glob('{}/*.asf'.format(FACE_DB))
# todo get from settings # todo get from settings
model_texture_file = '{}/pca_texture_model.npy'.format(FILES_DIR) model_texture_file = '{}/pca_ibug_texture_model.npy'.format(FILES_DIR)
model_shape_file = '{}/pca_shape_model.npy'.format(FILES_DIR) model_shape_file = '{}/pca_ibug_shape_model.npy'.format(FILES_DIR)
self.shape_model = pca.PCAModel(model_shape_file) self.shape_model = pca.PCAModel(model_shape_file)
self.texture_model = pca.PCAModel(model_texture_file) self.texture_model = pca.PCAModel(model_texture_file)
...@@ -63,6 +65,8 @@ class ImageWebSocketHandler(websocket.WebSocketHandler): ...@@ -63,6 +65,8 @@ class ImageWebSocketHandler(websocket.WebSocketHandler):
image_as_background = message.get('background_image', True) image_as_background = message.get('background_image', True)
shape_components = message.get('shape_components', 58) shape_components = message.get('shape_components', 58)
shape_eigenvalues_multiplier = message.get('shape_eigenvalues') shape_eigenvalues_multiplier = message.get('shape_eigenvalues')
#image = message.get('image')
#input_image = base64.b64decode(image)
shape_eigenvalues_multiplier = np.asarray( shape_eigenvalues_multiplier = np.asarray(
shape_eigenvalues_multiplier, dtype=np.float32 shape_eigenvalues_multiplier, dtype=np.float32
...@@ -70,13 +74,13 @@ class ImageWebSocketHandler(websocket.WebSocketHandler): ...@@ -70,13 +74,13 @@ class ImageWebSocketHandler(websocket.WebSocketHandler):
logger.info('using %s shape_components', shape_components) logger.info('using %s shape_components', shape_components)
asf_filename = self.asf[image_index] image_filename = self.images[image_index]
dataset_module = import_dataset_module(args.shape_type) dataset_module = import_dataset_module('ibug')
input_points = imm.IMMPoints(filename=asf_filename) input_points = dataset_module.factory(filename=image_filename)
input_image = input_points.get_image() input_image = input_points.get_image()
mean_points = dataset_module.factory(points_list=shape_model.mean_values) mean_points = dataset_module.factory(points_list=self.shape_model.mean_values)
mean_points.get_scaled_points(input_image.shape) mean_points.get_scaled_points(input_image.shape)
# set dst image to an empty image if value is None # set dst image to an empty image if value is None
...@@ -86,7 +90,7 @@ class ImageWebSocketHandler(websocket.WebSocketHandler): ...@@ -86,7 +90,7 @@ class ImageWebSocketHandler(websocket.WebSocketHandler):
else: else:
dst_image = input_image dst_image = input_image
output_points = imm.IMMPoints( output_points = dataset_module.factory(
points_list=input_points.get_points() points_list=input_points.get_points()
) )
...@@ -99,8 +103,7 @@ class ImageWebSocketHandler(websocket.WebSocketHandler): ...@@ -99,8 +103,7 @@ class ImageWebSocketHandler(websocket.WebSocketHandler):
reconstruction.reconstruct_shape( reconstruction.reconstruct_shape(
output_points, output_points,
self.shape_model, self.shape_model,
shape_Vt=shape_Vt, # overwrite by scaled Vt shape_Vt=shape_Vt # overwrite by scaled Vt
n_components=shape_components
) )
reconstruction.reconstruct_texture( reconstruction.reconstruct_texture(
...@@ -131,6 +134,7 @@ class ImageWebSocketHandler(websocket.WebSocketHandler): ...@@ -131,6 +134,7 @@ class ImageWebSocketHandler(websocket.WebSocketHandler):
for m in message.keys(): for m in message.keys():
try: try:
handler = getattr(self, self.handlers[m]) handler = getattr(self, self.handlers[m])
print handler
handler(message[m]) handler(message[m])
except (AttributeError, KeyError) as e: except (AttributeError, KeyError) as e:
msg = 'no handler for {}'.format(m) msg = 'no handler for {}'.format(m)
...@@ -140,6 +144,7 @@ class ImageWebSocketHandler(websocket.WebSocketHandler): ...@@ -140,6 +144,7 @@ class ImageWebSocketHandler(websocket.WebSocketHandler):
msg = 'no handler for {}'.format(m) msg = 'no handler for {}'.format(m)
print(msg, e) print(msg, e)
self.__return_error(msg) self.__return_error(msg)
traceback.print_exc()
def on_close(self): def on_close(self):
print("WebSocket closed") print("WebSocket closed")
......
...@@ -9,7 +9,8 @@ import logging.config ...@@ -9,7 +9,8 @@ import logging.config
import os import os
LANDMARK_DETECTOR_PATH = 'data/shape_predictor_68_face_landmarks.dat' dir_path = os.path.dirname(os.path.realpath(__file__))
LANDMARK_DETECTOR_PATH = dir_path + '/../data/shape_predictor_68_face_landmarks.dat'
#logging.basicConfig(level=logging.INFO, #logging.basicConfig(level=logging.INFO,
# format='%(asctime)s %(levelname)s %(name)s: %(message)s') # format='%(asctime)s %(levelname)s %(name)s: %(message)s')
......
...@@ -20,15 +20,43 @@ const ImageLoaderComponent = Ember.Component.extend({ ...@@ -20,15 +20,43 @@ const ImageLoaderComponent = Ember.Component.extend({
return this.get('params')[1]; return this.get('params')[1];
}), }),
convertFilenameToBase64: function(fileUrl, resolve) {
var image = new Image();
image.crossOrigin = 'Anonymous';
image.onload = function() {
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
var dataURL;
canvas.height = this.height;
canvas.width = this.width;
ctx.drawImage(this, 0, 0);
dataURL = canvas.toDataURL('image/jpeg');
console.log(dataUrl);
resolve(dataURL);
canvas = null;
};
image.src = fileUrl;
},
actions: { actions: {
/** /**
* Update the current face given the current index * Update the current face given the current index
*/ */
updateCurrentFace() { updateCurrentFace() {
var face = this.get('faces').objectAt(this.get('image_index')); var face = this.get('faces').objectAt(this.get('image_index'));
var filename = face.get('filename');
//var that = this;
//var requestConversion = new Ember.RSVP.Promise(function(resolve, reject) {
// that.convertFilenameToBase64(filename, resolve);
//});
//requestConversion.then((base64Image) => {
this.set('current_face', face); this.set('current_face', face);
this.set('current_face_filename', ENV.APP.staticURL + face.get('filename')); //this.set('current_face_base64', base64Image);
this.set('current_face_filename', ENV.APP.staticURL + filename);
} }
} }
}); });
......
...@@ -8,7 +8,9 @@ const ThreeComponent = Ember.Component.extend({ ...@@ -8,7 +8,9 @@ const ThreeComponent = Ember.Component.extend({
scene: null, scene: null,
willRender() { willRender() {
if (this.scene) { return; } if (this.scene) {
return;
}
var scene = new THREE.Scene(); var scene = new THREE.Scene();
var gui = new dat.GUI(); var gui = new dat.GUI();
......
...@@ -79,7 +79,6 @@ export default Ember.Controller.extend({ ...@@ -79,7 +79,6 @@ export default Ember.Controller.extend({
getReconstruction: Ember.observer( getReconstruction: Ember.observer(
'image_index', 'background_image', 'image_index', 'background_image',
'shape_components', 'shape_eigenvalues', function() { 'shape_components', 'shape_eigenvalues', function() {
console.log('updating');
this.send('getReconstruction'); this.send('getReconstruction');
}), }),
...@@ -91,6 +90,7 @@ export default Ember.Controller.extend({ ...@@ -91,6 +90,7 @@ export default Ember.Controller.extend({
getImage(faceModel) { getImage(faceModel) {
this.set('loading', true); this.set('loading', true);
var filename = faceModel.get('filename'); var filename = faceModel.get('filename');
const socket = this.get('socketRef'); const socket = this.get('socketRef');
socket.send( socket.send(
...@@ -101,6 +101,7 @@ export default Ember.Controller.extend({ ...@@ -101,6 +101,7 @@ export default Ember.Controller.extend({
getReconstruction() { getReconstruction() {
this.set('loading', true); this.set('loading', true);
const socket = this.get('socketRef'); const socket = this.get('socketRef');
console.log(this.get('current_face_base64'));
socket.send( socket.send(
JSON.stringify({ JSON.stringify({
...@@ -138,7 +139,7 @@ export default Ember.Controller.extend({ ...@@ -138,7 +139,7 @@ export default Ember.Controller.extend({
this.send('getReconstruction'); this.send('getReconstruction');
}, },
resetShapeEigenValues(gui) { resetShapeEigenValues(/*gui*/) {
this.initShapeEigenValues(15); this.initShapeEigenValues(15);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment