Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Tom Pillot
Thymio Game
Commits
f774d250
Commit
f774d250
authored
Jul 03, 2020
by
Tom Pillot
Browse files
Add a window to choose a webcam
parent
5c028f72
Changes
1
Hide whitespace changes
Inline
Side-by-side
game/webcam_chooser.py
0 → 100644
View file @
f774d250
import
cv2
from
PyQt5
import
QtWidgets
as
qtw
from
PyQt5
import
QtCore
as
qtc
from
PyQt5
import
QtGui
as
qtg
class
WebcamWindow
(
qtw
.
QWidget
):
WEBCAM_REFRESH_TIME
=
50
def
__init__
(
self
):
super
().
__init__
()
self
.
setWindowTitle
(
"Choix de la caméra"
)
self
.
webcam_label
=
qtw
.
QLabel
()
self
.
left_button
=
qtw
.
QPushButton
(
"Précédent"
)
self
.
right_button
=
qtw
.
QPushButton
(
"Suivant"
)
self
.
start_button
=
qtw
.
QPushButton
(
"Valider"
)
button_layout
=
qtw
.
QHBoxLayout
()
button_layout
.
addWidget
(
self
.
left_button
)
button_layout
.
addWidget
(
self
.
right_button
)
button_layout
.
addWidget
(
self
.
start_button
)
main_layout
=
qtw
.
QVBoxLayout
()
main_layout
.
addWidget
(
self
.
webcam_label
)
main_layout
.
addLayout
(
button_layout
)
self
.
setLayout
(
main_layout
)
self
.
webcam_timer
=
qtc
.
QTimer
()
self
.
camera_indexes
=
return_camera_indexes
()
self
.
current_index
=
0
self
.
cam
=
cv2
.
VideoCapture
(
self
.
camera_indexes
[
0
])
self
.
left_button
.
clicked
.
connect
(
self
.
show_previous
)
self
.
right_button
.
clicked
.
connect
(
self
.
show_next
)
self
.
webcam_timer
.
timeout
.
connect
(
self
.
show_webcam
)
self
.
webcam_timer
.
start
(
self
.
WEBCAM_REFRESH_TIME
)
def
show_webcam
(
self
):
# Read an image from the webcam and detect the tags it contains
ret
,
frame
=
self
.
cam
.
read
()
if
ret
:
frame
=
cv2
.
resize
(
frame
,
(
640
,
480
))
# Convert frame to QImage
cv2
.
cvtColor
(
frame
,
cv2
.
COLOR_RGB2BGR
,
frame
)
rows
,
cols
,
_
=
frame
.
shape
qimage
=
qtg
.
QImage
(
frame
.
data
,
cols
,
rows
,
qtg
.
QImage
.
Format_RGB888
)
pixmap
=
qtg
.
QPixmap
()
pixmap
.
convertFromImage
(
qimage
,
qtc
.
Qt
.
AutoColor
)
self
.
webcam_label
.
setPixmap
(
pixmap
)
else
:
print
(
"Failed to grab frame."
)
self
.
webcam_timer
.
start
(
self
.
WEBCAM_REFRESH_TIME
)
def
show_next
(
self
):
if
self
.
current_index
<
len
(
self
.
camera_indexes
)
-
1
:
self
.
current_index
+=
1
self
.
change_cam
()
def
show_previous
(
self
):
if
self
.
current_index
>
0
:
self
.
current_index
-=
1
self
.
change_cam
()
def
change_cam
(
self
):
self
.
cam
.
release
()
new_index
=
self
.
camera_indexes
[
self
.
current_index
]
self
.
cam
=
cv2
.
VideoCapture
(
new_index
)
self
.
cam
.
set
(
cv2
.
CAP_PROP_FRAME_WIDTH
,
640
)
self
.
cam
.
set
(
cv2
.
CAP_PROP_FRAME_HEIGHT
,
480
)
def
return_camera_indexes
():
# checks the first 10 indexes.
index
=
0
arr
=
[]
i
=
10
while
i
>
0
:
cap
=
cv2
.
VideoCapture
(
index
)
if
cap
.
read
()[
0
]:
arr
.
append
(
index
)
cap
.
release
()
index
+=
1
i
-=
1
return
arr
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