Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
54e7f087
Commit
54e7f087
authored
Oct 11, 2015
by
Pavel Rojtberg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add python sample to test/ showcase new cap_v4l2 features
parent
838947bb
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
64 additions
and
0 deletions
+64
-0
video_v4l2.py
samples/python2/video_v4l2.py
+64
-0
No files found.
samples/python2/video_v4l2.py
0 → 100644
View file @
54e7f087
#!/usr/bin/env python
'''
VideoCapture sample showcasing some features of the Video4Linux2 backend
Sample shows how VideoCapture class can be used to control parameters
of a webcam such as focus or framerate.
Also the sample provides an example how to access raw images delivered
by the hardware to get a grayscale image in a very efficient fashion.
Keys:
ESC - exit
g - toggle optimized grayscale conversion
'''
import
cv2
def
decode_fourcc
(
v
):
v
=
int
(
v
)
return
""
.
join
([
chr
((
v
>>
8
*
i
)
&
0xFF
)
for
i
in
range
(
4
)])
font
=
cv2
.
FONT_HERSHEY_SIMPLEX
color
=
(
0
,
255
,
0
)
cap
=
cv2
.
VideoCapture
(
0
)
cap
.
set
(
cv2
.
CAP_PROP_AUTOFOCUS
,
False
)
cv2
.
namedWindow
(
"Video"
)
convert_rgb
=
True
fps
=
int
(
cap
.
get
(
cv2
.
CAP_PROP_FPS
))
focus
=
int
(
cap
.
get
(
cv2
.
CAP_PROP_FOCUS
))
*
100
cv2
.
createTrackbar
(
"FPS"
,
"Video"
,
fps
,
30
,
lambda
v
:
cap
.
set
(
cv2
.
CAP_PROP_FPS
,
v
))
cv2
.
createTrackbar
(
"Focus"
,
"Video"
,
focus
,
100
,
lambda
v
:
cap
.
set
(
cv2
.
CAP_PROP_FOCUS
,
v
/
100
))
while
True
:
status
,
img
=
cap
.
read
()
fourcc
=
decode_fourcc
(
cap
.
get
(
cv2
.
CAP_PROP_FOURCC
))
fps
=
cap
.
get
(
cv2
.
CAP_PROP_FPS
)
if
not
bool
(
cap
.
get
(
cv2
.
CAP_PROP_CONVERT_RGB
)):
if
fourcc
==
"MJPG"
:
img
=
cv2
.
imdecode
(
img
,
cv2
.
IMREAD_GRAYSCALE
)
elif
fourcc
==
"YUYV"
:
img
=
cv2
.
cvtColor
(
img
,
cv2
.
COLOR_YUV2GRAY_YUYV
)
else
:
print
(
"unsupported format"
)
break
cv2
.
putText
(
img
,
"Mode: {}"
.
format
(
fourcc
),
(
15
,
40
),
font
,
1.0
,
color
)
cv2
.
putText
(
img
,
"FPS: {}"
.
format
(
fps
),
(
15
,
80
),
font
,
1.0
,
color
)
cv2
.
imshow
(
"Video"
,
img
)
k
=
cv2
.
waitKey
(
1
)
if
k
==
27
:
break
elif
k
==
ord
(
"g"
):
convert_rgb
=
not
convert_rgb
cap
.
set
(
cv2
.
CAP_PROP_CONVERT_RGB
,
convert_rgb
)
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