-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcameraDisplayModule.py
executable file
·55 lines (44 loc) · 1.48 KB
/
cameraDisplayModule.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
#!/usr/bin/env python3
import os
import robomodules as rm
from messages import *
import cv2
import numpy
import pickle
import pygame
ADDRESS = os.environ.get("BIND_ADDRESS", "localhost")
PORT = os.environ.get("BIND_PORT", 11297)
FREQUENCY = 0
SCREEN_SIZE = 800
class CameraDisplayModule(rm.ProtoModule):
def __init__(self, addr, port):
self.subscriptions = [MsgType.CAMERA_FRAME_MSG]
super().__init__(addr, port, message_buffers, MsgType, FREQUENCY,
self.subscriptions)
self.frame = None
pygame.init()
self.display = pygame.display.set_mode((SCREEN_SIZE, SCREEN_SIZE))
def msg_received(self, msg, msg_type):
# This gets called whenever any message is received
# We receive pickled frames here.
if msg_type == MsgType.CAMERA_FRAME_MSG:
self.frame = msg.cameraFrame
self._display_serialized_image()
def tick(self):
# FREQUENCY is 0, so this will never be called.
return
def _display_serialized_image(self):
if self.frame is None:
print('frame == None')
return
frame = pickle.loads(self.frame)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = numpy.rot90(frame)
frame = pygame.surfarray.make_surface(frame)
self.display.blit(frame, (0, 0))
pygame.display.flip()
def main():
module = CameraDisplayModule(ADDRESS, PORT)
module.run()
if __name__ == "__main__":
main()