Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bob.io.base
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
bob
bob.io.base
Commits
3cbca67f
Commit
3cbca67f
authored
11 years ago
by
André Anjos
Browse files
Options
Downloads
Patches
Plain Diff
Bug fixes len() functions; Prevent closed VideoWriters to crash your interpreter
parent
c8300545
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
xbob/io/test_video.py
+95
-0
95 additions, 0 deletions
xbob/io/test_video.py
xbob/io/videoreader.cpp
+8
-4
8 additions, 4 deletions
xbob/io/videoreader.cpp
xbob/io/videowriter.cpp
+47
-5
47 additions, 5 deletions
xbob/io/videowriter.cpp
with
150 additions
and
9 deletions
xbob/io/test_video.py
+
95
−
0
View file @
3cbca67f
...
...
@@ -8,6 +8,7 @@
"""
Runs some video tests
"""
import
os
import
numpy
import
nose.tools
from
.
import
test_utils
...
...
@@ -228,3 +229,97 @@ def test_can_use_array_interface():
for
frame_id
,
frame
in
zip
(
range
(
array
.
shape
[
0
]),
iv
.
__iter__
()):
assert
numpy
.
array_equal
(
array
[
frame_id
,:,:,:],
frame
)
@test_utils.ffmpeg_found
()
def
test_video_reading_after_writing
():
from
.
import
test_utils
tmpname
=
test_utils
.
temporary_filename
(
suffix
=
'
.avi
'
)
from
.
import
VideoWriter
,
VideoReader
try
:
width
=
20
height
=
20
framerate
=
24
outv
=
VideoWriter
(
tmpname
,
height
,
width
,
framerate
)
for
i
in
range
(
0
,
3
):
newframe
=
(
numpy
.
random
.
random_integers
(
0
,
255
,(
3
,
height
,
width
)))
outv
.
append
(
newframe
.
astype
(
'
uint8
'
))
outv
.
close
()
# this should not crash
i
=
VideoReader
(
tmpname
)
nose
.
tools
.
eq_
(
i
.
number_of_frames
,
3
)
nose
.
tools
.
eq_
(
i
.
width
,
width
)
nose
.
tools
.
eq_
(
i
.
height
,
height
)
finally
:
# And we erase both files after this
if
os
.
path
.
exists
(
tmpname
):
os
.
unlink
(
tmpname
)
@test_utils.ffmpeg_found
()
def
test_video_writer_close
():
from
.
import
test_utils
tmpname
=
test_utils
.
temporary_filename
(
suffix
=
'
.avi
'
)
from
.
import
VideoWriter
,
VideoReader
try
:
width
=
20
height
=
20
framerate
=
24
outv
=
VideoWriter
(
tmpname
,
height
,
width
,
framerate
)
for
i
in
range
(
0
,
3
):
newframe
=
(
numpy
.
random
.
random_integers
(
0
,
255
,(
3
,
height
,
width
)))
outv
.
append
(
newframe
.
astype
(
'
uint8
'
))
outv
.
close
()
# this should not crash
nose
.
tools
.
eq_
(
outv
.
filename
,
tmpname
)
nose
.
tools
.
eq_
(
outv
.
width
,
width
)
nose
.
tools
.
eq_
(
outv
.
height
,
height
)
nose
.
tools
.
eq_
(
len
(
outv
),
3
)
nose
.
tools
.
eq_
(
outv
.
number_of_frames
,
len
(
outv
))
nose
.
tools
.
eq_
(
outv
.
frame_rate
,
framerate
)
assert
outv
.
bit_rate
assert
outv
.
gop
finally
:
# And we erase both files after this
if
os
.
path
.
exists
(
tmpname
):
os
.
unlink
(
tmpname
)
@test_utils.ffmpeg_found
()
def
test_closed_video_writer_raises
():
from
.
import
test_utils
tmpname
=
test_utils
.
temporary_filename
(
suffix
=
'
.avi
'
)
from
.
import
VideoWriter
try
:
width
=
20
height
=
20
framerate
=
24
outv
=
VideoWriter
(
tmpname
,
height
,
width
,
framerate
)
for
i
in
range
(
0
,
3
):
newframe
=
(
numpy
.
random
.
random_integers
(
0
,
255
,(
3
,
height
,
width
)))
outv
.
append
(
newframe
.
astype
(
'
uint8
'
))
outv
.
close
()
nose
.
tools
.
assert_raises
(
RuntimeError
,
outv
.
__str__
)
nose
.
tools
.
assert_raises
(
RuntimeError
,
outv
.
__repr__
)
nose
.
tools
.
assert_raises
(
RuntimeError
,
outv
.
append
,
newframe
)
del
outv
finally
:
# And we erase both files after this
if
os
.
path
.
exists
(
tmpname
):
os
.
unlink
(
tmpname
)
This diff is collapsed.
Click to expand it.
xbob/io/videoreader.cpp
+
8
−
4
View file @
3cbca67f
...
...
@@ -135,8 +135,8 @@ PyDoc_STRVAR(s_width_str, "width");
PyDoc_STRVAR
(
s_width_doc
,
"[int] The width of each frame in the video (a multiple of 2)"
);
Py
_ssize_t
PyBobIoVideoReader_
Len
(
PyBobIoVideoReaderObject
*
self
)
{
return
self
->
v
->
numberOfFrames
();
Py
Object
*
PyBobIoVideoReader_
NumberOfFrames
(
PyBobIoVideoReaderObject
*
self
)
{
return
Py_BuildValue
(
"n"
,
self
->
v
->
numberOfFrames
()
)
;
}
PyDoc_STRVAR
(
s_number_of_frames_str
,
"number_of_frames"
);
...
...
@@ -239,7 +239,7 @@ static PyGetSetDef PyBobIoVideoReader_getseters[] = {
},
{
s_number_of_frames_str
,
(
getter
)
PyBobIoVideoReader_
Len
,
(
getter
)
PyBobIoVideoReader_
NumberOfFrames
,
0
,
s_number_of_frames_doc
,
0
,
...
...
@@ -541,6 +541,10 @@ static PyObject* PyBobIoVideoReader_GetItem (PyBobIoVideoReaderObject* self, PyO
}
}
Py_ssize_t
PyBobIoVideoReader_Len
(
PyBobIoVideoReaderObject
*
self
)
{
return
self
->
v
->
numberOfFrames
();
}
static
PyMappingMethods
PyBobIoVideoReader_Mapping
=
{
(
lenfunc
)
PyBobIoVideoReader_Len
,
//mp_lenght
(
binaryfunc
)
PyBobIoVideoReader_GetItem
,
//mp_subscript
...
...
This diff is collapsed.
Click to expand it.
xbob/io/videowriter.cpp
+
47
−
5
View file @
3cbca67f
...
...
@@ -131,8 +131,8 @@ static int PyBobIoVideoWriter_Init(PyBobIoVideoWriterObject* self,
#endif
try
{
self
->
v
=
boost
::
make_shared
<
bob
::
io
::
VideoWriter
>
(
c_filename
,
height
,
width
,
framerate
,
bitrate
,
gop
,
codec_str
,
format_str
,
check
);
self
->
v
=
boost
::
make_shared
<
bob
::
io
::
VideoWriter
>
(
c_filename
,
height
,
width
,
framerate
,
bitrate
,
gop
,
codec_str
,
format_str
,
check
);
}
catch
(
std
::
exception
&
e
)
{
PyErr_SetString
(
PyExc_RuntimeError
,
e
.
what
());
...
...
@@ -170,8 +170,8 @@ PyDoc_STRVAR(s_width_str, "width");
PyDoc_STRVAR
(
s_width_doc
,
"[int] The width of each frame in the video (a multiple of 2)"
);
Py
_ssize_t
PyBobIoVideoWriter_
Len
(
PyBobIoVideoWriterObject
*
self
)
{
return
self
->
v
->
numberOfFrames
();
Py
Object
*
PyBobIoVideoWriter_
NumberOfFrames
(
PyBobIoVideoWriterObject
*
self
)
{
return
Py_BuildValue
(
"n"
,
self
->
v
->
numberOfFrames
()
)
;
}
PyDoc_STRVAR
(
s_number_of_frames_str
,
"number_of_frames"
);
...
...
@@ -187,6 +187,11 @@ PyDoc_STRVAR(s_duration_doc,
"[int] Total duration of this video file in microseconds (long)"
);
PyObject
*
PyBobIoVideoWriter_FormatName
(
PyBobIoVideoWriterObject
*
self
)
{
if
(
!
self
->
v
->
is_opened
())
{
PyErr_Format
(
PyExc_RuntimeError
,
"`%s' for `%s' is closed"
,
Py_TYPE
(
self
)
->
tp_name
,
self
->
v
->
filename
().
c_str
());
return
0
;
}
return
Py_BuildValue
(
"s"
,
self
->
v
->
formatName
().
c_str
());
}
...
...
@@ -195,6 +200,11 @@ PyDoc_STRVAR(s_format_name_doc,
"[str] Short name of the format in which this video file was recorded in"
);
PyObject
*
PyBobIoVideoWriter_FormatLongName
(
PyBobIoVideoWriterObject
*
self
)
{
if
(
!
self
->
v
->
is_opened
())
{
PyErr_Format
(
PyExc_RuntimeError
,
"`%s' for `%s' is closed"
,
Py_TYPE
(
self
)
->
tp_name
,
self
->
v
->
filename
().
c_str
());
return
0
;
}
return
Py_BuildValue
(
"s"
,
self
->
v
->
formatLongName
().
c_str
());
}
...
...
@@ -203,6 +213,11 @@ PyDoc_STRVAR(s_format_long_name_doc,
"[str] Full name of the format in which this video file was recorded in"
);
PyObject
*
PyBobIoVideoWriter_CodecName
(
PyBobIoVideoWriterObject
*
self
)
{
if
(
!
self
->
v
->
is_opened
())
{
PyErr_Format
(
PyExc_RuntimeError
,
"`%s' for `%s' is closed"
,
Py_TYPE
(
self
)
->
tp_name
,
self
->
v
->
filename
().
c_str
());
return
0
;
}
return
Py_BuildValue
(
"s"
,
self
->
v
->
codecName
().
c_str
());
}
...
...
@@ -211,6 +226,11 @@ PyDoc_STRVAR(s_codec_name_doc,
"[str] Short name of the codec in which this video file was recorded in"
);
PyObject
*
PyBobIoVideoWriter_CodecLongName
(
PyBobIoVideoWriterObject
*
self
)
{
if
(
!
self
->
v
->
is_opened
())
{
PyErr_Format
(
PyExc_RuntimeError
,
"`%s' for `%s' is closed"
,
Py_TYPE
(
self
)
->
tp_name
,
self
->
v
->
filename
().
c_str
());
return
0
;
}
return
Py_BuildValue
(
"s"
,
self
->
v
->
codecLongName
().
c_str
());
}
...
...
@@ -264,6 +284,12 @@ PyDoc_STRVAR(s_frame_type_doc,
"[tuple] Typing information to load each frame separatedly"
);
static
PyObject
*
PyBobIoVideoWriter_Print
(
PyBobIoVideoWriterObject
*
self
)
{
if
(
!
self
->
v
->
is_opened
())
{
PyErr_Format
(
PyExc_RuntimeError
,
"`%s' for `%s' is closed"
,
Py_TYPE
(
self
)
->
tp_name
,
self
->
v
->
filename
().
c_str
());
return
0
;
}
return
Py_BuildValue
(
"s"
,
self
->
v
->
info
().
c_str
());
}
...
...
@@ -305,7 +331,7 @@ static PyGetSetDef PyBobIoVideoWriter_getseters[] = {
},
{
s_number_of_frames_str
,
(
getter
)
PyBobIoVideoWriter_
Len
,
(
getter
)
PyBobIoVideoWriter_
NumberOfFrames
,
0
,
s_number_of_frames_doc
,
0
,
...
...
@@ -398,6 +424,12 @@ static PyGetSetDef PyBobIoVideoWriter_getseters[] = {
};
static
PyObject
*
PyBobIoVideoWriter_Repr
(
PyBobIoVideoWriterObject
*
self
)
{
if
(
!
self
->
v
->
is_opened
())
{
PyErr_Format
(
PyExc_RuntimeError
,
"`%s' for `%s' is closed"
,
Py_TYPE
(
self
)
->
tp_name
,
self
->
v
->
filename
().
c_str
());
return
0
;
}
return
# if PY_VERSION_HEX >= 0x03000000
PyUnicode_FromFormat
...
...
@@ -409,6 +441,12 @@ static PyObject* PyBobIoVideoWriter_Repr(PyBobIoVideoWriterObject* self) {
static
PyObject
*
PyBobIoVideoWriter_Append
(
PyBobIoVideoWriterObject
*
self
,
PyObject
*
args
,
PyObject
*
kwds
)
{
if
(
!
self
->
v
->
is_opened
())
{
PyErr_Format
(
PyExc_RuntimeError
,
"`%s' for `%s' is closed"
,
Py_TYPE
(
self
)
->
tp_name
,
self
->
v
->
filename
().
c_str
());
return
0
;
}
/* Parses input arguments in a single shot */
static
const
char
*
const_kwlist
[]
=
{
"frame"
,
0
};
static
char
**
kwlist
=
const_cast
<
char
**>
(
const_kwlist
);
...
...
@@ -497,6 +535,10 @@ static PyMethodDef PyBobIoVideoWriter_Methods[] = {
{
0
}
/* Sentinel */
};
Py_ssize_t
PyBobIoVideoWriter_Len
(
PyBobIoVideoWriterObject
*
self
)
{
return
self
->
v
->
numberOfFrames
();
}
static
PyMappingMethods
PyBobIoVideoWriter_Mapping
=
{
(
lenfunc
)
PyBobIoVideoWriter_Len
,
//mp_lenght
0
,
/* (binaryfunc)PyBobIoVideoWriter_GetItem, //mp_subscript */
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment