Skip to content

Commit 7ddabbf

Browse files
committed
added IPWEBCAM feature to DLStream
1 parent 068cc0f commit 7ddabbf

File tree

4 files changed

+90
-7
lines changed

4 files changed

+90
-7
lines changed

DeepLabStream.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import click
1919

2020
from utils.configloader import RESOLUTION, FRAMERATE, OUT_DIR, MODEL, MULTI_CAM, STACK_FRAMES, \
21-
ANIMALS_NUMBER, STREAMS, VIDEO
21+
ANIMALS_NUMBER, STREAMS, VIDEO, IPWEBCAM
2222
from utils.poser import load_deeplabcut, get_pose, find_local_peaks_new, calculate_skeletons
2323
from utils.plotter import plot_bodyparts, plot_metadata_frame
2424

@@ -131,6 +131,13 @@ def set_camera_manager():
131131
from utils.generic import VideoManager
132132
manager = VideoManager()
133133
return manager
134+
135+
elif IPWEBCAM:
136+
from utils.generic import WebCamManager
137+
manager = WebCamManager()
138+
return manager
139+
140+
134141
else:
135142
manager_list = []
136143
# loading realsense manager, if installed

settings.ini

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@ FRAMERATE = 30
44
STREAMS = color, depth, infrared
55
OUTPUT_DIRECTORY = /Output
66
MULTIPLE_DEVICES = False
7-
CAMERA_SOURCE = 2
7+
CAMERA_SOURCE = 0
88

99
[DeepLabCut]
10-
DLC_PATH = DLC_PATH
11-
MODEL = MODEL
10+
DLC_PATH = D:\PycharmProjects\DeepLabCut\deeplabcut
11+
MODEL = Mouse_resident_intruder_HomeCage_CLAHE
1212

1313
[Experiment]
14-
EXP_ORIGIN = BASE/CUSTOM
15-
EXP_NAME = CONFIG_NAME
14+
EXP_NAME = FirstExperiment
15+
EXP_ORIGIN = uguh
16+
;EXP_NAME = CONFIG_NAME
1617
RECORD_EXP = True
1718

1819
[Video]
1920
VIDEO_SOURCE = PATH_TO_PRERECORDED_VIDEO
2021
VIDEO = False
22+
23+
[IPWEBCAM]
24+
PORT = 5555
25+
IPWEBCAM = True

utils/configloader.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def get_script_path():
4949
VIDEO_SOURCE = dsc_config['Video'].get('VIDEO_SOURCE')
5050
VIDEO = dsc_config['Video'].getboolean('VIDEO')
5151

52+
#IPWEBCAM
53+
PORT = dsc_config['IPWEBCAM'].get('PORT')
54+
IPWEBCAM = dsc_config['IPWEBCAM'].getboolean('IPWEBCAM')
55+
5256

5357
# experiment
5458
EXP_ORIGIN = dsc_config['Experiment'].get('EXP_ORIGIN')

utils/generic.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88

99
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
1111
import time
1212
import numpy as np
1313

@@ -130,3 +130,70 @@ def get_frames(self) -> tuple:
130130

131131
return color_frames, depth_maps, infra_frames
132132

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

Comments
 (0)