Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
bob
bob.db.swan
Commits
ee29015a
Commit
ee29015a
authored
May 28, 2018
by
Amir MOHAMMADI
Browse files
DRY
parent
f692d50a
Pipeline
#20578
passed with stage
in 28 minutes and 49 seconds
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
bob/db/swan/__init__.py
View file @
ee29015a
SWAN_FRAME_SHAPE
=
(
3
,
1280
,
720
)
"Shape of the video frames in the swan database."
from
.query_bio
import
Database
,
SwanAudioBioFile
,
SwanVideoBioFile
...
...
bob/db/swan/common.py
View file @
ee29015a
import
scipy.io.wavfile
from
bob.db.base
import
read_annotation_file
from
bob.io.base
import
load
from
bob.io.video
import
reader
from
bob.bio.video.utils
import
FrameSelector
from
bob.bio.video.database
import
VideoBioFile
import
numpy
as
np
import
subprocess
import
tempfile
from
os.path
import
split
,
splitext
from
.
import
SWAN_FRAME_SHAPE
SITE_MAPPING
=
{
'1'
:
'NTNU'
,
...
...
@@ -78,3 +85,130 @@ def swan_file_metadata(path):
modality
=
MODALITY_MAPPING
[
modality
]
session
=
int
(
session
)
return
client
,
session
,
nrecording
,
device
,
modality
class
SwanFile
(
object
):
"""A base class for SWAN bio files which can handle the metadata."""
def
__init__
(
self
,
**
kwargs
):
super
(
SwanFile
,
self
).
__init__
(
**
kwargs
)
(
self
.
client
,
self
.
session
,
self
.
nrecording
,
self
.
device
,
self
.
modality
)
=
swan_file_metadata
(
self
.
path
)
class
SwanVideoFile
(
VideoBioFile
,
SwanFile
):
"""A base class for SWAN video files"""
def
swap
(
self
,
data
):
# rotate the video or image since SWAN videos are not upright!
return
np
.
swapaxes
(
data
,
-
2
,
-
1
)
def
load
(
self
,
directory
=
None
,
extension
=
None
,
frame_selector
=
FrameSelector
(
selection_style
=
'all'
)):
if
extension
is
None
:
video_path
=
self
.
make_path
(
directory
or
self
.
original_directory
,
extension
)
video
=
load
(
video_path
)
video
=
self
.
swap
(
video
)
return
frame_selector
(
video
)
else
:
return
super
(
SwanVideoFile
,
self
).
load
(
directory
,
extension
,
frame_selector
)
@
property
def
frames
(
self
):
"""Yields the frames of the padfile one by one.
Parameters
----------
padfile : :any:`SwanVideoPadFile`
The high-level pad file
Yields
------
:any:`numpy.array`
A frame of the video. The size is (3, 1280, 720).
"""
vfilename
=
self
.
make_path
(
directory
=
self
.
original_directory
)
video
=
reader
(
vfilename
)
for
frame
in
video
:
yield
self
.
swap
(
frame
)
@
property
def
number_of_frames
(
self
):
"""Returns the number of frames in a video file.
Parameters
----------
padfile : :any:`SwanVideoPadFile`
The high-level pad file
Returns
-------
int
The number of frames.
"""
vfilename
=
self
.
make_path
(
directory
=
self
.
original_directory
)
return
reader
(
vfilename
).
number_of_frames
@
property
def
frame_shape
(
self
):
"""Returns the size of each frame in this database.
Returns
-------
(int, int, int)
The (#Channels, Height, Width) which is (3, 1920, 1080).
"""
return
SWAN_FRAME_SHAPE
@
property
def
annotations
(
self
):
"""Returns the annotations of the current file
Returns
-------
dict
The annotations as a dictionary, e.g.:
``{'0': {'reye':(re_y,re_x), 'leye':(le_y,le_x)}, ...}``
"""
return
read_annotation_file
(
self
.
make_path
(
self
.
annotation_directory
,
self
.
annotation_extension
),
self
.
annotation_type
)
class
SwanAudioFile
(
SwanVideoFile
):
"""A base class that extracts audio from SWAN video files"""
def
load
(
self
,
directory
=
None
,
extension
=
None
):
if
extension
is
None
:
video_path
=
self
.
make_path
(
directory
,
extension
)
rate
,
audio
=
read_audio
(
video_path
)
return
rate
,
np
.
cast
[
'float'
](
audio
)
else
:
return
super
(
SwanAudioFile
,
self
).
load
(
directory
,
extension
)
class
SwanVideoDatabase
(
object
):
"""SwanVideoDatabase"""
def
frames
(
self
,
padfile
):
return
padfile
.
frames
def
number_of_frames
(
self
,
padfile
):
return
padfile
.
number_of_frames
@
property
def
frame_shape
(
self
):
return
SWAN_FRAME_SHAPE
def
update_files
(
self
,
files
):
for
f
in
files
:
f
.
original_directory
=
self
.
original_directory
f
.
annotation_directory
=
self
.
annotation_directory
f
.
annotation_extension
=
self
.
annotation_extension
f
.
annotation_type
=
self
.
annotation_type
return
files
bob/db/swan/query_bio.py
View file @
ee29015a
#!/usr/bin/env python
from
bob.bio.spear.database
import
AudioBioFile
from
bob.bio.video.database
import
VideoBioFile
from
bob.bio.video.utils
import
FrameSelector
import
bob.bio.base
import
bob.io.base
import
bob.io.video
import
numpy
as
np
from
bob.extension
import
rc
from
.common
import
s
wan
_file_metadata
,
read_audio
from
.common
import
S
wan
VideoFile
,
SwanAudioFile
,
SwanVideoDatabase
class
SwanBioFile
(
object
):
"""A base class for SWAN bio files which can handle the metadata."""
def
__init__
(
self
,
**
kwargs
):
super
(
SwanBioFile
,
self
).
__init__
(
**
kwargs
)
(
self
.
client
,
self
.
session
,
self
.
nrecording
,
self
.
device
,
self
.
modality
)
=
swan_file_metadata
(
self
.
path
)
class
SwanAudioBioFile
(
AudioBioFile
,
SwanBioFile
):
class
SwanAudioBioFile
(
AudioBioFile
,
SwanAudioFile
):
"""SwanAudioBioFile are video files actually"""
def
__init__
(
self
,
**
kwargs
):
super
(
SwanAudioBioFile
,
self
).
__init__
(
**
kwargs
)
def
load
(
self
,
directory
=
None
,
extension
=
None
):
if
extension
is
None
:
video_path
=
self
.
make_path
(
directory
,
extension
)
rate
,
audio
=
read_audio
(
video_path
)
return
rate
,
np
.
cast
[
'float'
](
audio
)
else
:
return
super
(
SwanAudioBioFile
,
self
).
load
(
directory
,
extension
)
class
SwanVideoBioFile
(
Vide
oBioFile
,
SwanBi
oFile
):
class
SwanVideoBioFile
(
Swan
VideoFile
):
"""SwanVideoBioFile are video files actually"""
def
__init__
(
self
,
**
kwargs
):
super
(
SwanVideoBioFile
,
self
).
__init__
(
**
kwargs
)
def
swap
(
self
,
data
):
# rotate the video or image since SWAN videos are not upright!
return
np
.
swapaxes
(
data
,
-
2
,
-
1
)
def
load
(
self
,
directory
=
None
,
extension
=
None
,
frame_selector
=
FrameSelector
(
selection_style
=
'all'
)):
if
extension
is
None
:
video_path
=
self
.
make_path
(
directory
or
self
.
original_directory
,
extension
)
video
=
bob
.
io
.
base
.
load
(
video_path
)
video
=
self
.
swap
(
video
)
return
frame_selector
(
video
)
else
:
return
super
(
SwanVideoBioFile
,
self
).
load
(
directory
,
extension
,
frame_selector
)
@
property
def
frames
(
self
):
"""Yields the frames of the biofile one by one.
Yields
------
:any:`numpy.array`
A frame of the video. The size is (3, 1280, 720).
"""
vfilename
=
self
.
make_path
(
directory
=
self
.
original_directory
)
reader
=
bob
.
io
.
video
.
reader
(
vfilename
)
for
frame
in
reader
:
yield
self
.
swap
(
frame
)
@
property
def
number_of_frames
(
self
):
"""Returns the number of frames in a video file.
Returns
-------
int
The number of frames.
"""
vfilename
=
self
.
make_path
(
directory
=
self
.
original_directory
)
return
bob
.
io
.
video
.
reader
(
vfilename
).
number_of_frames
@
property
def
frame_shape
(
self
):
"""Returns the size of each frame in this database.
Returns
-------
(int, int, int)
The (#Channels, Height, Width) which is (3, 1280, 720).
"""
return
(
3
,
1280
,
720
)
class
Database
(
bob
.
bio
.
base
.
database
.
FileListBioDatabase
):
class
Database
(
bob
.
bio
.
base
.
database
.
FileListBioDatabase
,
SwanVideoDatabase
):
"""Wrapper class for the SWAN database for speaker recognition
(http://www.idiap.ch/dataset/swan). This class defines a simple protocol
for training, dev and and by splitting the audio files of the database in
...
...
@@ -128,20 +47,4 @@ class Database(bob.bio.base.database.FileListBioDatabase):
files
=
super
(
Database
,
self
).
objects
(
groups
=
groups
,
protocol
=
protocol
,
purposes
=
purposes
,
model_ids
=
model_ids
,
classes
=
classes
,
**
kwargs
)
for
f
in
files
:
f
.
original_directory
=
self
.
original_directory
return
files
def
frames
(
self
,
padfile
):
return
padfile
.
frames
def
number_of_frames
(
self
,
padfile
):
return
padfile
.
number_of_frames
@
property
def
frame_shape
(
self
):
return
(
3
,
1280
,
720
)
def
load_frames
(
biofile
,
directory
,
extension
):
return
biofile
.
frames
return
self
.
update_files
(
files
)
bob/db/swan/query_pad.py
View file @
ee29015a
...
...
@@ -2,102 +2,20 @@
from
bob.pad.voice.database
import
PadVoiceFile
from
bob.pad.face.database
import
VideoPadFile
from
bob.bio.video.utils
import
FrameSelector
from
bob.pad.base.database
import
FileListPadDatabase
import
bob.io.base
import
bob.io.video
import
numpy
as
np
from
bob.extension
import
rc
from
.common
import
read_audio
from
.query_bio
import
SwanBioFile
from
.common
import
SwanVideoFile
,
SwanAudioFile
,
SwanVideoDatabase
class
SwanAudioPadFile
(
PadVoiceFile
,
Swan
B
ioFile
):
class
SwanAudioPadFile
(
PadVoiceFile
,
Swan
Aud
ioFile
):
"""SwanAudioPadFile are video files actually"""
def
__init__
(
self
,
**
kwargs
):
super
(
SwanAudioPadFile
,
self
).
__init__
(
**
kwargs
)
def
load
(
self
,
directory
=
None
,
extension
=
None
):
if
extension
is
None
:
video_path
=
self
.
make_path
(
directory
,
extension
)
rate
,
audio
=
read_audio
(
video_path
)
return
rate
,
np
.
cast
[
'float'
](
audio
)
else
:
return
super
(
SwanAudioPadFile
,
self
).
load
(
directory
,
extension
)
class
SwanVideoPadFile
(
VideoPadFile
,
SwanBioFile
):
class
SwanVideoPadFile
(
VideoPadFile
,
SwanVideoFile
):
"""SwanVideoPadFile are video files actually"""
def
__init__
(
self
,
**
kwargs
):
super
(
SwanVideoPadFile
,
self
).
__init__
(
**
kwargs
)
def
swap
(
self
,
data
):
# rotate the video or image since SWAN videos are not upright!
return
np
.
swapaxes
(
data
,
-
2
,
-
1
)
def
load
(
self
,
directory
=
None
,
extension
=
None
,
frame_selector
=
FrameSelector
(
selection_style
=
'all'
)):
if
extension
is
None
:
video_path
=
self
.
make_path
(
directory
or
self
.
original_directory
,
extension
)
video
=
bob
.
io
.
base
.
load
(
video_path
)
video
=
self
.
swap
(
video
)
return
frame_selector
(
video
)
else
:
return
super
(
SwanVideoPadFile
,
self
).
load
(
directory
,
extension
,
frame_selector
)
@
property
def
frames
(
self
):
"""Yields the frames of the padfile one by one.
Parameters
----------
padfile : :any:`SwanVideoPadFile`
The high-level pad file
Yields
------
:any:`numpy.array`
A frame of the video. The size is (3, 1280, 720).
"""
vfilename
=
self
.
make_path
(
directory
=
self
.
original_directory
)
reader
=
bob
.
io
.
video
.
reader
(
vfilename
)
for
frame
in
reader
:
yield
self
.
swap
(
frame
)
@
property
def
number_of_frames
(
self
):
"""Returns the number of frames in a video file.
Parameters
----------
padfile : :any:`SwanVideoPadFile`
The high-level pad file
Returns
-------
int
The number of frames.
"""
vfilename
=
self
.
make_path
(
directory
=
self
.
original_directory
)
return
bob
.
io
.
video
.
reader
(
vfilename
).
number_of_frames
@
property
def
frame_shape
(
self
):
"""Returns the size of each frame in this database.
Returns
-------
(int, int, int)
The (#Channels, Height, Width) which is (3, 1920, 1080).
"""
return
(
3
,
1280
,
720
)
class
Database
(
FileListPadDatabase
):
class
Database
(
FileListPadDatabase
,
SwanVideoDatabase
):
"""Wrapper class for the SWAN database for PAD
(http://www.idiap.ch/dataset/swan).
"""
...
...
@@ -126,17 +44,4 @@ class Database(FileListPadDatabase):
files
=
super
(
Database
,
self
).
objects
(
groups
=
groups
,
protocol
=
protocol
,
purposes
=
purposes
,
model_ids
=
model_ids
,
classes
=
classes
,
**
kwargs
)
for
f
in
files
:
f
.
original_directory
=
self
.
original_directory
return
files
def
frames
(
self
,
padfile
):
for
frame
in
padfile
.
frames
:
yield
frame
def
number_of_frames
(
self
,
padfile
):
return
padfile
.
number_of_frames
@
property
def
frame_shape
(
self
):
return
(
3
,
1280
,
720
)
return
self
.
update_files
(
files
)
doc/index.rst
View file @
ee29015a
...
...
@@ -21,5 +21,6 @@ Package Documentation
---------------------
.. automodule:: bob.db.swan
.. automodule:: bob.db.swan.common
.. automodule:: bob.db.swan.query_bio
.. automodule:: bob.db.swan.query_pad
Write
Preview
Supports
Markdown
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