|
7 | 7 | """ |
8 | 8 |
|
9 | 9 | import cv2 |
10 | | -from utils.configloader import CAMERA_SOURCE, VIDEO_SOURCE, RESOLUTION, FRAMERATE |
| 10 | +from utils.configloader import CAMERA_SOURCE, VIDEO_SOURCE, RESOLUTION, FRAMERATE, PORT |
11 | 11 | import time |
12 | 12 | import numpy as np |
13 | 13 |
|
@@ -130,3 +130,70 @@ def get_frames(self) -> tuple: |
130 | 130 |
|
131 | 131 | return color_frames, depth_maps, infra_frames |
132 | 132 |
|
| 133 | + |
| 134 | +class WebCamManager(GenericManager): |
| 135 | + |
| 136 | + def __init__(self): |
| 137 | + """ |
| 138 | + Binds the computer to a ip address and starts listening for incoming streams. |
| 139 | + Adapted from StreamViewer.py https://github.com/CT83/SmoothStream |
| 140 | + """ |
| 141 | + import zmq |
| 142 | + self._context = zmq.Context() |
| 143 | + self._footage_socket = self._context.socket(zmq.SUB) |
| 144 | + self._footage_socket.bind('tcp://*:' + PORT) |
| 145 | + self._footage_socket.setsockopt_string(zmq.SUBSCRIBE, np.unicode('')) |
| 146 | + |
| 147 | + self._manager_name = "generic" |
| 148 | + self._camera = None |
| 149 | + self._camera_name = "webcam" |
| 150 | + self.initial_wait = False |
| 151 | + self.last_frame_time = time.time() |
| 152 | + |
| 153 | + def string_to_image(self, string): |
| 154 | + """ Taken from https://github.com/CT83/SmoothStream""" |
| 155 | + |
| 156 | + import numpy as np |
| 157 | + import cv2 |
| 158 | + import base64 |
| 159 | + img = base64.b64decode(string) |
| 160 | + npimg = np.fromstring(img, dtype=np.uint8) |
| 161 | + return cv2.imdecode(npimg, 1) |
| 162 | + |
| 163 | + def get_frames(self) -> tuple: |
| 164 | + """ |
| 165 | + Collect frames for camera and outputs it in 'color' dictionary |
| 166 | + ***depth and infrared are not used here*** |
| 167 | + :return: tuple of three dictionaries: color, depth, infrared |
| 168 | + """ |
| 169 | + |
| 170 | + color_frames = {} |
| 171 | + depth_maps = {} |
| 172 | + infra_frames = {} |
| 173 | + |
| 174 | + if self._footage_socket: |
| 175 | + ret = True |
| 176 | + else: |
| 177 | + ret = False |
| 178 | + self.last_frame_time = time.time() |
| 179 | + if ret: |
| 180 | + # if not self.initial_wait: |
| 181 | + # cv2.waitKey(1000) |
| 182 | + # self.initial_wait = True |
| 183 | + # receives frame from stream |
| 184 | + image = self._footage_socket.recv_string() |
| 185 | + # converts image from str to image format that cv can handle |
| 186 | + image = self.string_to_image(image) |
| 187 | + image = cv2.resize(image, RESOLUTION) |
| 188 | + color_frames[self._camera_name] = image |
| 189 | + running_time = time.time() - self.last_frame_time |
| 190 | + if running_time <= 1 / FRAMERATE: |
| 191 | + sleepy_time = int(np.ceil(1000/FRAMERATE - running_time / 1000)) |
| 192 | + cv2.waitKey(sleepy_time) |
| 193 | + return color_frames, depth_maps, infra_frames |
| 194 | + |
| 195 | + def enable_stream(self, resolution, framerate, *args): |
| 196 | + """ |
| 197 | + Not used for webcam streaming over network |
| 198 | + """ |
| 199 | + pass |
0 commit comments