Commit 9423b361 authored by Quedeville Antoine's avatar Quedeville Antoine
Browse files

Initial commit

parents
Code/Computed Vectors/*
Code/Data/*
Code/XML Analysis/Files
**/__pycache__/*
**/*.npy
**/*.pyc
**/*.lyx~
window_width = 765
window_height = 450
img_width, img_height = 64, 96
slider_scale_factor = 3
# DONT FORGET TO CHANGE THE DIMENSIONS OF THE INITIAL SIZES IF THE IMAGES CHANGE !!!
slider_bg_width = int(31 * slider_scale_factor)
slider_bg_height = int(15 * slider_scale_factor)
slider_button_width = int (5 * slider_scale_factor)
slider_button_height = int (5 * slider_scale_factor)
slider_text_font = "Helvetica"
slider_text_size = 12
sliders_per_row = 5
sliders_per_column = 40
scroll_step = 3
max_scroll_distance = sliders_per_column * slider_bg_height /2
min_scroll_distance = 0
def mapInterval (x, inita, initb, targeta, targetb):
""" Returns the corresponding x in the linear mapping of [inita, initb] to [targeta, targetb] """
percentage_in_init = (float(x)-inita)/(initb-inita)
return targeta + percentage_in_init * (targetb-targeta)
import Module_Convert_Images_Matrix
import numpy as np
import scipy.stats as stats
PCA_dir = "Computed Vectors/All faces, 64_96/"
PCA_dir = ""
new_X = np.load(PCA_dir + 'newFaceArray.npy')
def getMeanFace():
X = np.load (PCA_dir + 'FaceArray.npy')
return np.mean(X, axis=0)
def computeDifference (vals, vects, sliderValues):
res = np.zeros (len(vects[0]))
for i in range(len(vals)):
res = res + sliderValues[i] * vals[i] * vects[i]
return res
def computeNewImage (meanFace, vals, vects, sliderValues):
""" Function doc """
new_arr = meanFace.astype(np.float64)
new_arr += computeDifference (vals, vects, sliderValues)
return np.array(map (lambda x: max (min(255, x), 0), new_arr))
def getPCAResults ():
""" Function doc """
return np.load (PCA_dir + 'vecteursPropres.npy'), np.load (PCA_dir + 'valeursPropres.npy')
def getIthVals(i):
return new_X[i]
def getRandomInd():
return stats.norm.rvs(size=len(new_X[0]))
import random
import pygame
from PIL import Image
from AppSlider import *
from AppDataAPI import *
class App:
def __init__(self):
self.width = csts.window_width
self.height = csts.window_height
self.sliders = []
self.should_save_img = False
self.should_update_image = False
self.scroll_position = 0
self.vectProp, self.valProp = getPCAResults()
self.meanFace = getMeanFace()
self.currentImage = self.convertNpArrToPygameSurface(self.meanFace)
# Initialize sliders here
for i in range(csts.sliders_per_column):
for j in range(csts.sliders_per_row):
self.sliders.append(Slider(300 + j * csts.slider_bg_width, i * csts.slider_bg_height, i*csts.sliders_per_row + j + 1))
self.surface = pygame.display.set_mode ((self.width, self.height))
pygame.display.set_caption('Projet M8')
def run (self):
pygame.init()
running = True
clock = pygame.time.Clock()
while running:
clock.tick(10)
for event in pygame.event.get():
#print (event)
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONUP or event.type == pygame.KEYUP:
self.should_update_image = False
self.should_update_sliders = False
self.should_save_img = False
for slider in self.sliders:
slider.being_used = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4 or event.button == 5:
#Scroll event
if event.button == 4: #Scroll up
self.scroll_position -= csts.scroll_step
self.scroll_position = max (csts.min_scroll_distance, self.scroll_position)
elif event.button == 5: #Scroll down
self.scroll_position += csts.scroll_step
self.scroll_position = min (csts.max_scroll_distance, self.scroll_position)
elif event.button == 1:
#Check for collisions with the sliders
self.should_update_image = True
for slider in self.sliders:
slider.mouseHit(pygame.mouse.get_pos())
elif event.button == 3:
self.resetSliders()
self.should_update_image = True
elif event.type == pygame.KEYDOWN:
self.should_update_image = True
if event.unicode == '1':
i = random.randint(1, len(self.valProp))
vals = getIthVals(i)
self.setSlidersToValues(vals)
if event.unicode == 'r':
vals = getRandomInd()
self.setSlidersToValues(vals)
if event.unicode == 'p':
self.should_save_img = True
self.update()
self.draw()
pygame.quit()
def update (self):
""" update all the objects of the display """
if self.should_update_image:
sliderValues = [slider.value for slider in self.sliders]
img = computeNewImage (self.meanFace, self.valProp, self.vectProp, sliderValues)
self.currentImage = self.convertNpArrToPygameSurface(img)
for slider in self.sliders:
slider.updateCoordinates(self.scroll_position)
slider.update()
def draw(self):
#Put the drawing of all the objects here
#Drawing the current Face
if self.should_update_image:
self.surface.blit(self.currentImage, (0,0))
for slider in self.sliders:
slider.draw(self.surface)
pygame.display.update()
def setSlidersToValues(self, values):
for i, slider in enumerate(self.sliders):
slider.setSliderToValue (values[i])
def resetSliders (self):
for slider in self.sliders:
slider.reset()
def convertNpArrToPygameSurface (self, NpArr):
image = Module_Convert_Images_Matrix.NpArrToImage(NpArr)
if self.should_save_img:
image.save("test.png")
mode = image.mode
size = image.size
data = image.tobytes()
py_image = pygame.image.fromstring(data, size, mode)
scale_factor = float(self.height)/py_image.get_height()
new_width = int(py_image.get_width() * scale_factor)
new_height = int (py_image.get_height() * scale_factor)
py_image = pygame.transform.scale(py_image, (new_width, new_height))
return py_image
from AppGame import *
g = App()
g.run()
import AppConstants as csts
from AppDataAPI import *
import pygame
pygame.font.init()
WHITE = (255, 255, 255)
class Slider:
#Loading sprites of the slider elements (class constants)
button_sprite = pygame.image.load('AppRessources/Slider_button.png')
bg_sprite = pygame.image.load('AppRessources/Slider_bg.png')
#Scaling of the elements
bg_sprite = pygame.transform.scale (bg_sprite , (csts.slider_bg_width, csts.slider_bg_height))
button_sprite = pygame.transform.scale (button_sprite, (csts.slider_button_width, csts.slider_button_height))
#Loading their dimentions
bg_width = bg_sprite.get_width()
bg_height = bg_sprite.get_height()
button_width = button_sprite.get_width()
button_height = button_sprite.get_height()
#Text constants
font = pygame.font.SysFont(csts.slider_text_font, csts.slider_text_size)
def __init__(self, x, y, axis_nb):
self.value = 0.0
self.text_base = "Axe #" + str (axis_nb)
self.text = self.text_base + " (" + str (self.value) + ")"
self.being_used = False
self.pos_init_x = x
self.pos_init_y = y
self.pos_init_button_x = self.pos_init_x + (self.bg_width - self.button_width)/2
self.pos_init_button_y = self.pos_init_y + (self.bg_height - self.button_height)/2
self.x = self.pos_init_x
self.y = self.pos_init_y
self.button_x = self.pos_init_button_x
self.button_y = self.pos_init_button_y
self.should_be_displayed = self.y < csts.window_height
def update (self):
# Update the value of the slider
if self.being_used:
self.button_x = pygame.mouse.get_pos()[0]
self.button_x = min (self.button_x, self.x + self.bg_width)
self.button_x = max (self.button_x, self.x)
#Recenter the slider on the mouse
self.button_x = self.button_x - self.button_width/2
self.updateValueAccordingToButtonX()
self.text = self.text_base + " (" + str (self.value)[:5] + ")"
def updateCoordinates (self, offset):
self.y = self.pos_init_y - offset
self.button_y = self.pos_init_button_y - offset
self.should_be_displayed = (self.y < csts.window_height) and (self.y > - csts.slider_bg_height)
def draw (self, surface):
if self.should_be_displayed:
# Display the scale & background
surface.blit(self.bg_sprite, (self.x, self.y))
#Display the axis number
txt_img = self.font.render (self.text, 0, WHITE)
surface.blit (txt_img, (self.x + 3, self.y + 3))
#Display the button
surface.blit(self.button_sprite, (self.button_x, self.button_y))
def mouseHit (self, coords):
x = coords[0]
y = coords[1]
if (x > self.button_x) and (x < self.button_x + self.button_width) and (y > self.button_y) and (y < self.button_y + self.button_height):
self.being_used = True
def updateValueAccordingToButtonX (self):
self.value = csts.mapInterval (self.button_x - self.pos_init_button_x, -self.bg_width/2, self.bg_width/2, -2, 2)
def setSliderToValue(self, val):
self.value = val
self.button_x = self.pos_init_button_x + csts.mapInterval (val, -2, 2, -self.bg_width/2, self.bg_width/2)
self.text = self.text_base + " (" + str (self.value)[:5] + ")"
def reset (self):
self.button_x = self.pos_init_button_x
self.value = 0.0
self.text = self.text_base + " (" + str (self.value)[:5] + ")"
import numpy as np
X = np.load('FaceArray.npy')
V = np.load('vecteursPropres.npy')
vals = np.load('valeursPropres.npy')
max_vects = len(V)
X = X - np.mean(X, axis=0)
new_X = np.dot(X, V.T)/vals
np.save('newFaceArray.npy', new_X)
from Module_Convert_Images_Matrix import *
facesToNpArr()
echo "Copying images to Data/Faces..."
cp Color\ FERET\ DB/dvd*/data/thumbnails/*/*.ppm.bz2 "Data/Faces"
echo "Unziping the files..."
bunzip2 Data/Faces/*.ppm.bz2
echo "Copying xml files..."
cp Color\ FERET\ DB/dvd*/data/ground_truths/xml/*/*.xml "Data/IDs"
echo "Done."
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
nb_of_vectors = 200
def ACP (X):
pca = PCA(n_components=nb_of_vectors)
pca.fit(X)
return np.sqrt(pca.explained_variance_), pca.components_, pca.explained_variance_ratio_
def showValPropGraph (valProp, amount):
val = valProp[:amount]
graphdata = np.cumsum(val/np.sum(val)*100)
plt.plot(graphdata)
plt.show()
from PIL import Image
import glob
import numpy as np
import os
from xml.dom import minidom
face_path = "Data/Faces/"
IDs_path = "Data/IDs/"
max_face_angle = 30
width, height = 128, 192
new_width, new_height = 64, 96
def imageToNpArr (pic):
arr = np.array(pic)
arr = arr.ravel()
return arr
def NpArrToImage (arr):
arr = arr.astype(np.uint8)
arr = arr.reshape((new_height, new_width, 3))
pic = Image.fromarray(arr, "RGB")
return pic
def facesToNpArr():
filenames = glob.glob(face_path + "*.ppm")
nb_of_faces = len(filenames)
MAX_FACES = nb_of_faces
X = []
images_in_X = 0
for i, filename in enumerate(filenames):
if (images_in_X >= MAX_FACES) or (i >= nb_of_faces):
break
if isFacingCamera(filename):
pic = Image.open (filename)
pic.thumbnail ((new_width, new_height))
arr = imageToNpArr (pic)
images_in_X += 1
X.append (arr)
print ('Went through ' + str(i) + ' images, final array size : ' + str(len(X)))
np.save ('FaceArray.npy', X)
def isFacingCamera (img_path):
filename = os.path.basename(img_path)
name, _ = os.path.splitext(filename)
xmldoc = minidom.parse(IDs_path + name + '.xml')
pose = xmldoc.getElementsByTagName('Pose')
yaw = float(pose[0].attributes['yaw'].value)
return (yaw > -max_face_angle) and (yaw < max_face_angle)
from Module_ACP import *
X = np.load('FaceArray.npy')
vals, vects, ratios = ACP(X)
np.save('valeursPropres.npy', vals)
np.save('vecteursPropres.npy', vects)
np.save('ratiosValPropres.npy', ratios)
execfile("Create_Face_Array.py")
execfile("Perform_PCA.py")
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
from xml.dom import minidom
import glob
import os
import numpy as np
def extractInfoFromFile(filename):
xmldoc = minidom.parse(filename)
pose = xmldoc.getElementsByTagName('Pose')
yaw = float(pose[0].attributes['yaw'].value)
pitch = float(pose[0].attributes['pitch'].value)
roll = float(pose[0].attributes['roll'].value)
capDate = xmldoc.getElementsByTagName('CaptureDate')[0].childNodes[0].data
d = capDate.split('/')
day, month, year = int(d[0]), int(d[1]), int(d[2])
wearing = xmldoc.getElementsByTagName('Wearing')
glasses = wearing[0].attributes['glasses'].value == 'Yes'
hair = xmldoc.getElementsByTagName('Hair')
beard = hair[0].attributes['beard'].value == 'Yes'
mustache = hair[0].attributes['mustache'].value == 'Yes'
individual = [yaw ,pitch, roll, glasses, beard, mustache, day, month, year]
return individual
def extractInfoFromPeep(filename):
xmldoc = minidom.parse(filename)
gender = xmldoc.getElementsByTagName('Gender')
gender = (gender[0].attributes['value'].value)
YOB = xmldoc.getElementsByTagName('YOB') #Year of birth
YOB = int(YOB[0].attributes['value'].value)
race = xmldoc.getElementsByTagName('Race')
race = (race[0].attributes['value'].value)
data = [gender, YOB, race]
return data
filenames = glob.glob("Files/pics/*.xml")
X = [extractInfoFromFile(filename) for filename in filenames]
np.save('DataPicsArray', X)
filenames = glob.glob("Files/peeps/*.xml")
X = [extractInfoFromPeep(filename) for filename in filenames]
np.save('DataPeepsArray', X)
cp ../Color\ FERET\ DB/dvd*/data/ground_truths/xml/*/*_* Files/pics
cp ../Color\ FERET\ DB/dvd*/data/ground_truths/xml/*/?????.xml Files/peeps
import numpy as np
import matplotlib.pyplot as plt
var = np.load('ratiosValPropres.npy')
var = np.cumsum(var)*100
plt.xlabel('Nombre de vecteurs')
plt.ylabel('% de variance expliquee')
plt.plot(var)
plt.show()
Supports Markdown
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