-
Notifications
You must be signed in to change notification settings - Fork 43
/
camera_window_test.py
84 lines (60 loc) · 2.68 KB
/
camera_window_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import pygame
import pygame.camera
import pygame_gui
"""
Uses Pygame Camera module to display a webcam in a window
"""
class CameraWindow(pygame_gui.elements.UIWindow):
def __init__(self,
rect: pygame.Rect,
camera_name,
ui_manager: pygame_gui.core.interfaces.IUIManagerInterface):
super().__init__(rect, ui_manager, window_display_title=camera_name, resizable=True)
self.camera = None
self.camera = pygame.camera.Camera(camera_name, (640, 480))
self.camera.start()
print(self.camera.get_controls())
cam_rect = pygame.Rect((0, 0), self.get_container().rect.size)
self.cam_image = pygame_gui.elements.UIImage(relative_rect=cam_rect,
image_surface=self.camera.get_image(),
manager=self.ui_manager,
container=self,
anchors={'left': 'left',
'right': 'right',
'top': 'top',
'bottom': 'bottom'})
def update(self, time_delta: float):
super().update(time_delta)
if self.camera is not None:
self.cam_image.set_image(pygame.transform.smoothscale(self.camera.get_image(),
self.get_container().rect.size))
pygame.init()
pygame.camera.init()
print(pygame.camera.get_backends())
print(pygame.camera.list_cameras())
pygame.display.set_caption('Quick Start')
window_surface = pygame.display.set_mode((800, 600))
manager = pygame_gui.UIManager((800, 600), 'data/themes/camera_theme.json')
background = pygame.Surface((800, 600))
background.fill(manager.ui_theme.get_colour('dark_bg'))
cam_window_pos = [10, 10]
num_connected_cameras = 1
cam_names = pygame.camera.list_cameras()
for cam_name in cam_names:
cam_window_rect = pygame.Rect(0, 0, 400, 300)
cam_window_rect.topleft = cam_window_pos
CameraWindow(cam_window_rect, cam_name, manager)
cam_window_pos = (cam_window_pos[0] + 420,
cam_window_pos[1])
clock = pygame.time.Clock()
is_running = True
while is_running:
time_delta = clock.tick(60)/1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
manager.process_events(event)
manager.update(time_delta)
window_surface.blit(background, (0, 0))
manager.draw_ui(window_surface)
pygame.display.update()