Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
py-3d-face-reconstruction
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Richard Torenvliet
py-3d-face-reconstruction
Commits
acb7592b
Commit
acb7592b
authored
Apr 13, 2016
by
Richard Torenvliet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add reconstruction with the possibility of changing the highest eigenvalues
parent
a54d0723
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
160 additions
and
51 deletions
+160
-51
makefile
makefile
+4
-0
src/aam.py
src/aam.py
+31
-0
src/aam_test.py
src/aam_test.py
+41
-0
src/imm_points.py
src/imm_points.py
+4
-7
src/main.py
src/main.py
+45
-38
src/pca.py
src/pca.py
+35
-6
No files found.
makefile
View file @
acb7592b
...
...
@@ -9,3 +9,7 @@ data/imm_face_db: data/imm_face_db.tar.gz
data/imm_face_db.tar.gz
:
(
cd
data
;
wget http://www.imm.dtu.dk/~aam/datasets/imm_face_db.tar.gz
)
test
:
py.test
-f
src/
*
_test.py
src/aam.py
0 → 100644
View file @
acb7592b
import
numpy
as
np
def
build_mean_aam
(
imm_points
):
""" construct a mean from a matrix of x,y values
Args:
imm_points(numpy array) that follows the following structure:
observations:
0. [[x_0_0, y_0_0], ... , [x_0_m, y_0_m]],
1. [[x_1_0, y_1_0], ... , [x_1_m, y_1_m]],
2. [[x_2_0, y_2_0], ... , [x_2_m, y_2_m]],
3. [[x_3_0, y_3_0], ... , [x_3_m, y_3_m]]
.... .... .m...
n. [[x_4_0, y_4_0], ... , [x_n_m, y_n_m]]
Returns mean_values (numpy array)
This vector containts the mean values of the corresponding column, like so:
0. [[x_0_0, y_0_0], ... , [x_0_k, y_0_k]],
1. [[x_1_0, y_1_0], ... , [x_1_k, y_1_k]],
2. [[x_2_0, y_2_0], ... , [x_2_k, y_2_k]],
3. [[x_3_0, y_3_0], ... , [x_3_k, y_3_k]]
.... .... .....
n. [[x_4_0, y_4_0], ... , [x_n_k, y_n_k]]
mean. [[x_mean_0, y_mean_0], ... [x_mean_n, y_mean_n]]
"""
return
np
.
mean
(
imm_points
,
axis
=
0
)
src/aam_test.py
0 → 100644
View file @
acb7592b
import
numpy
as
np
from
aam
import
build_mean_aam
def
test_build_mean_aan
():
imm_points
=
np
.
array
([
[[
1
,
2
],
[
2
,
4
]],
[[
2
,
3
],
[
3
,
6
]],
])
expected
=
np
.
array
([
[
1.5
,
2.5
],
[
2.5
,
5.
]
])
mean
=
build_mean_aam
(
imm_points
)
np
.
testing
.
assert_array_equal
(
mean
,
expected
)
def
test_zero_mean_aan
():
imm_points
=
np
.
array
([
[[
1
,
2
],
[
2
,
4
]],
[[
2
,
3
],
[
3
,
6
]],
])
expected
=
np
.
array
([
[[
-
0.5
,
-
0.5
],
[
-
0.5
,
-
1.0
]],
[[
0.5
,
0.5
],
[
0.5
,
1.0
]],
])
mean
=
build_mean_aam
(
imm_points
)
zero_mean
=
imm_points
-
mean
# test that zero mean has indeed zero mean
np
.
testing
.
assert_array_equal
(
np
.
mean
(
zero_mean
,
axis
=
0
),
np
.
zeros
((
2
,
2
))
)
np
.
testing
.
assert_array_equal
(
zero_mean
,
expected
)
src/imm_points.py
View file @
acb7592b
...
...
@@ -55,12 +55,11 @@ class IMMPoints():
cv2
.
FONT_HERSHEY_SIMPLEX
,
.
5
,
(
100
,
0
,
255
))
cv2
.
circle
(
img
,
tuple
(
p
),
3
,
color
=
(
0
,
255
,
100
))
def
show_on_img
(
self
,
img
):
def
show_on_img
(
self
,
img
,
window_name
=
'image'
):
self
.
draw_triangles
(
img
,
self
.
points
)
cv2
.
imshow
(
'image'
,
img
)
cv2
.
waitKey
(
0
)
cv2
.
imshow
(
window_name
,
img
)
def
show
(
self
):
def
show
(
self
,
window_name
=
'image'
):
"""show the image and datapoints on the image"""
assert
(
len
(
self
.
points
)
>
0
)
assert
(
len
(
self
.
filename
)
>
0
)
...
...
@@ -68,9 +67,7 @@ class IMMPoints():
img
=
cv2
.
imread
(
'data/imm_face_db/'
+
self
.
filename
)
self
.
draw_triangles
(
img
,
self
.
points
)
cv2
.
imshow
(
'image'
,
img
)
cv2
.
waitKey
(
0
)
cv2
.
imshow
(
window_name
,
img
)
def
add_parser_options
():
...
...
src/main.py
View file @
acb7592b
from
pca
import
pca
import
numpy
as
np
import
copy
import
argparse
import
cv2
import
numpy
as
np
from
pca
import
pca
,
reconstruct
from
aam
import
build_mean_aam
from
imm_points
import
IMMPoints
def
nothing
(
_
):
pass
def
add_parser_options
():
parser
=
argparse
.
ArgumentParser
(
description
=
'IMMPoints tool'
)
...
...
@@ -16,39 +24,12 @@ def add_parser_options():
return
parser
def
build_mean_aam
(
imm_points
):
""" construct a mean from a matrix of x,y values
Args:
imm_points(numpy array)that follows the follwing structure:
observations:
0. [[x_0_0, y_0_0], ... , [x_0_m, y_0_m]],
1. [[x_1_0, y_1_0], ... , [x_1_m, y_1_m]],
2. [[x_2_0, y_2_0], ... , [x_2_m, y_2_m]],
3. [[x_3_0, y_3_0], ... , [x_3_m, y_3_m]]
.... .... .m...
n. [[x_4_0, y_4_0], ... , [x_n_m, y_n_m]]
Returns mean_values (numpy array)
This vector containts the mean values of the corresponding column, like so:
0. [[x_0_0, y_0_0], ... , [x_0_k, y_0_k]],
1. [[x_1_0, y_1_0], ... , [x_1_k, y_1_k]],
2. [[x_2_0, y_2_0], ... , [x_2_k, y_2_k]],
3. [[x_3_0, y_3_0], ... , [x_3_k, y_3_k]]
.... .... .....
n. [[x_4_0, y_4_0], ... , [x_n_k, y_n_k]]
def
init_eigenvalue_trackbars
(
n_components
,
s
):
cv2
.
namedWindow
(
'eigenvalues'
)
mean. [[x_mean_0, y_mean_0], ... [x_mean_n, y_mean_n]]
"""
mean_values
=
[]
for
i
in
range
(
n_components
):
cv2
.
createTrackbar
(
'{}'
.
format
(
i
),
'eigenvalues'
,
0
,
1000
,
nothing
)
for
i
in
range
(
imm_points
.
shape
[
1
]):
mean_values
.
append
(
np
.
mean
(
imm_points
[:,
i
],
axis
=
0
))
return
np
.
array
(
mean_values
)
if
__name__
==
'__main__'
:
parser
=
add_parser_options
()
...
...
@@ -63,10 +44,36 @@ if __name__ == '__main__':
# imm.show()
imm_points
=
np
.
array
(
imm_points
)
mean_values
=
build_mean_aam
(
np
.
array
(
imm_points
))
V
,
S
=
pca
(
imm_points
,
mean_values
)
U
,
s
,
Vt
=
pca
(
imm_points
,
mean_values
)
index
=
0
cv2
.
namedWindow
(
'index'
)
cv2
.
createTrackbar
(
'index'
,
'index'
,
index
,
len
(
args
.
asf
)
-
1
,
nothing
)
n_components
=
5
init_eigenvalue_trackbars
(
n_components
,
s
)
s_copy
=
copy
.
copy
(
s
)
while
True
:
projection
=
reconstruct
(
U
,
s_copy
,
Vt
,
n_components
)
X_reconstructed
=
projection
[
index
].
reshape
((
58
,
2
))
+
mean_values
imm
=
IMMPoints
(
points
=
X_reconstructed
)
img
=
np
.
full
((
480
,
640
,
3
),
255
,
np
.
uint8
)
imm
.
show_on_img
(
img
)
for
i
in
range
(
n_components
):
s_copy
[
i
]
=
s
[
i
]
*
(
cv2
.
getTrackbarPos
(
str
(
i
),
'eigenvalues'
)
/
10.0
)
index
=
cv2
.
getTrackbarPos
(
'index'
,
'index'
)
imm
=
IMMPoints
(
filename
=
args
.
asf
[
index
])
imm
.
show
(
window_name
=
'original'
)
k
=
cv2
.
waitKey
(
1
)
&
0xFF
if
k
==
27
:
break
# show immpoints
imm
=
IMMPoints
(
points
=
mean_values
)
img
=
np
.
full
((
480
,
640
,
3
),
255
,
np
.
uint8
)
imm
.
show_on_img
(
img
)
cv2
.
destroyAllWindows
()
src/pca.py
View file @
acb7592b
import
numpy
as
np
def
preprocess
(
data
):
def
flatten_feature_vectors
(
data
):
"""
Flattens the feature vectors inside.
Example:
input:
[
[[1, 2], [3, 4], [5, 6]],
...
[[1, 2], [3, 4], [5, 6]]
]
output:
[
[1, 2, 3, 4, 5, 6],
...
[1, 2, 3, 4, 5, 6]
]
Args:
data (numpy array): array of feature vectors
return:
array: (numpy array): array flattened feature vectors
"""
flattened
=
[]
y
,
x
,
dim
=
data
.
shape
...
...
@@ -12,13 +36,18 @@ def preprocess(data):
return
np
.
array
(
flattened
)
def
pca
(
data
,
mean_values
,
n_components
):
def
pca
(
data
,
mean_values
):
# subtract mean
zero_mean
=
data
-
mean_values
X
=
preproces
s
(
zero_mean
)
X
=
flatten_feature_vector
s
(
zero_mean
)
observations
,
dims
=
X
.
shape
_
,
dim
=
X
.
shape
U
,
S
,
V
=
np
.
linalg
.
svd
(
X
)
U
,
s
,
V
=
np
.
linalg
.
svd
(
X
,
full_matrices
=
False
)
return
V
[:
n_components
],
S
[:
n_components
]
return
U
,
s
,
V
def
reconstruct
(
U
,
s
,
Vt
,
n_components
):
return
np
.
dot
(
U
[:,
:
n_components
],
np
.
dot
(
np
.
diag
(
s
[:
n_components
]),
Vt
[:
n_components
]))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment