Skip to content

Commit 8e7d797

Browse files
committed
uncoupled webcam.py for simplified requirements.txt
1 parent 6e03680 commit 8e7d797

File tree

4 files changed

+90
-76
lines changed

4 files changed

+90
-76
lines changed

DeepLabStream.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import numpy as np
1818
import pandas as pd
1919

20-
from utils.generic import VideoManager, WebCamManager, GenericManager
20+
from utils.generic import VideoManager, GenericManager
21+
2122
from utils.configloader import RESOLUTION, FRAMERATE, OUT_DIR, MODEL_NAME, MULTI_CAM, STACK_FRAMES, \
2223
ANIMALS_NUMBER, FLATTEN_MA, STREAMS, STREAMING_SOURCE, MODEL_ORIGIN, CROP, CROP_X, CROP_Y
2324
from utils.plotter import plot_bodyparts,plot_metadata_frame
@@ -145,6 +146,7 @@ def select_camera_manager():
145146
pylon_manager = PylonManager()
146147
manager_list.append(pylon_manager)
147148

149+
148150
def check_for_cameras(camera_manager):
149151
"""
150152
Helper method to get cameras, connected to that camera manager
@@ -167,17 +169,21 @@ def check_for_cameras(camera_manager):
167169

168170
MANAGER_SOURCE = {
169171
'video': VideoManager,
170-
'ipwebcam': WebCamManager,
171172
'camera': select_camera_manager
172173
}
173174

175+
# loading WebCam manager, if installed
176+
if find_spec("pyzmq") is not None:
177+
from utils.webcam import WebCamManager
178+
MANAGER_SOURCE['ipwebcam'] = WebCamManager()
179+
174180
# initialize selected manager
175181
camera_manager = MANAGER_SOURCE.get(STREAMING_SOURCE)()
176182
if camera_manager is not None:
177183
return camera_manager
178184
else:
179185
raise ValueError(f'Streaming source {STREAMING_SOURCE} is not a valid option. \n'
180-
f'Please choose from "video", "camera" or "ipwebcam".')
186+
f'Please choose from "video", "camera" or "ipwebcam". Make sure that if you are using "ipwebcam" you installed the additional dependencies.')
181187

182188
@property
183189
def cameras(self):

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ pandas==1.1.4
1111
matplotlib==3.3.4
1212
scikit-image==0.17.2
1313
scipy==1.5.4
14-
pyzmq==20.0.0

utils/generic.py

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66
Licensed under GNU General Public License v3.0
77
"""
88
import time
9-
import base64
10-
119
import cv2
1210
import numpy as np
13-
import zmq
1411

15-
from utils.configloader import CAMERA_SOURCE, VIDEO_SOURCE, RESOLUTION, FRAMERATE, PORT, REPEAT_VIDEO
12+
from utils.configloader import CAMERA_SOURCE, VIDEO_SOURCE, RESOLUTION, FRAMERATE, REPEAT_VIDEO
1613

1714
class MissingFrameError(Exception):
1815
"""Custom expection to be raised when frame is not received. Should be caught in app.py and deeplabstream.py
@@ -157,72 +154,4 @@ def get_frames(self) -> tuple:
157154
return color_frames, depth_maps, infra_frames
158155

159156

160-
class WebCamManager(GenericManager):
161-
162-
def __init__(self):
163-
"""
164-
Binds the computer to a ip address and starts listening for incoming streams.
165-
Adapted from StreamViewer.py https://github.com/CT83/SmoothStream
166-
"""
167-
super().__init__()
168-
self._context = zmq.Context()
169-
self._footage_socket = self._context.socket(zmq.SUB)
170-
self._footage_socket.bind('tcp://*:' + PORT)
171-
self._footage_socket.setsockopt_string(zmq.SUBSCRIBE, np.unicode(''))
172-
173-
self._camera = None
174-
self._camera_name = "webcam"
175-
self.initial_wait = False
176-
self.last_frame_time = time.time()
177-
178-
@ staticmethod
179-
def string_to_image(string):
180-
"""
181-
Taken from https://github.com/CT83/SmoothStream
182-
"""
183-
184-
img = base64.b64decode(string)
185-
npimg = np.fromstring(img, dtype=np.uint8)
186-
return cv2.imdecode(npimg, 1)
187-
188-
def get_frames(self) -> tuple:
189-
"""
190-
Collect frames for camera and outputs it in 'color' dictionary
191-
***depth and infrared are not used here***
192-
:return: tuple of three dictionaries: color, depth, infrared
193-
"""
194-
195-
color_frames = {}
196-
depth_maps = {}
197-
infra_frames = {}
198-
199-
if self._footage_socket:
200-
ret = True
201-
else:
202-
ret = False
203-
self.last_frame_time = time.time()
204-
if ret:
205-
# if not self.initial_wait:
206-
# cv2.waitKey(1000)
207-
# self.initial_wait = True
208-
# receives frame from stream
209-
image = self._footage_socket.recv_string()
210-
# converts image from str to image format that cv can handle
211-
image = self.string_to_image(image)
212-
image = cv2.resize(image, RESOLUTION)
213-
color_frames[self._camera_name] = image
214-
running_time = time.time() - self.last_frame_time
215-
if running_time <= 1 / FRAMERATE:
216-
sleepy_time = int(np.ceil(1000/FRAMERATE - running_time / 1000))
217-
cv2.waitKey(sleepy_time)
218-
219-
else:
220-
raise MissingFrameError('No frame was received from the webcam stream. Make sure that you started streaming on the host machine.')
221157

222-
return color_frames, depth_maps, infra_frames
223-
224-
def enable_stream(self, resolution, framerate, *args):
225-
"""
226-
Not used for webcam streaming over network
227-
"""
228-
pass

utils/webcam.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from utils.generic import GenericManager, MissingFrameError
2+
import time
3+
import base64
4+
5+
import cv2
6+
import numpy as np
7+
import zmq
8+
9+
from utils.configloader import RESOLUTION, FRAMERATE, PORT
10+
11+
12+
class WebCamManager(GenericManager):
13+
14+
def __init__(self):
15+
"""
16+
Binds the computer to a ip address and starts listening for incoming streams.
17+
Adapted from StreamViewer.py https://github.com/CT83/SmoothStream
18+
"""
19+
super().__init__()
20+
self._context = zmq.Context()
21+
self._footage_socket = self._context.socket(zmq.SUB)
22+
self._footage_socket.bind('tcp://*:' + PORT)
23+
self._footage_socket.setsockopt_string(zmq.SUBSCRIBE, np.unicode(''))
24+
25+
self._camera = None
26+
self._camera_name = "webcam"
27+
self.initial_wait = False
28+
self.last_frame_time = time.time()
29+
30+
@ staticmethod
31+
def string_to_image(string):
32+
"""
33+
Taken from https://github.com/CT83/SmoothStream
34+
"""
35+
36+
img = base64.b64decode(string)
37+
npimg = np.fromstring(img, dtype=np.uint8)
38+
return cv2.imdecode(npimg, 1)
39+
40+
def get_frames(self) -> tuple:
41+
"""
42+
Collect frames for camera and outputs it in 'color' dictionary
43+
***depth and infrared are not used here***
44+
:return: tuple of three dictionaries: color, depth, infrared
45+
"""
46+
47+
color_frames = {}
48+
depth_maps = {}
49+
infra_frames = {}
50+
51+
if self._footage_socket:
52+
ret = True
53+
else:
54+
ret = False
55+
self.last_frame_time = time.time()
56+
if ret:
57+
# if not self.initial_wait:
58+
# cv2.waitKey(1000)
59+
# self.initial_wait = True
60+
# receives frame from stream
61+
image = self._footage_socket.recv_string()
62+
# converts image from str to image format that cv can handle
63+
image = self.string_to_image(image)
64+
image = cv2.resize(image, RESOLUTION)
65+
color_frames[self._camera_name] = image
66+
running_time = time.time() - self.last_frame_time
67+
if running_time <= 1 / FRAMERATE:
68+
sleepy_time = int(np.ceil(1000/FRAMERATE - running_time / 1000))
69+
cv2.waitKey(sleepy_time)
70+
71+
else:
72+
raise MissingFrameError('No frame was received from the webcam stream. Make sure that you started streaming on the host machine.')
73+
74+
return color_frames, depth_maps, infra_frames
75+
76+
def enable_stream(self, resolution, framerate, *args):
77+
"""
78+
Not used for webcam streaming over network
79+
"""
80+
pass

0 commit comments

Comments
 (0)