-
Notifications
You must be signed in to change notification settings - Fork 0
/
kinect_streamer.py
131 lines (128 loc) · 5.21 KB
/
kinect_streamer.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import os, sys, traceback
import logging
import signal
import numpy as np
import cv2
import rospy
import roslaunch
import message_filters as mfilters
from cv_bridge import CvBridge, CvBridgeError
from sensor_msgs.msg import Image, TimeReference
from math import pi
import time
import class_objects as co
import extract_and_process_rosbag as epr
import classifiers as cs
import moving_object_detection_alg as moda
#import poses_to_actions as p2a
KINECT = None
class Time(object):
def __init__(self):
self.secs = 0
self.nsecs = 0
def signal_handler(sig, frame):
'''
Signal handler for CTRL-C interrupt (SIGINT)
'''
LOG.info('Mean processing time: '+
str(np.sum(np.diff(KINECT.time))/(len(KINECT.time) - 1))+' s')
KINECT.process.stop()
sys.exit(0)
class Kinect(object):
'''Kinect Processing Class'''
def __init__(self):
#Kinect requirements
# Hand action recognition
self.mog2 = moda.Mog2()
self.used_classifier = cs.construct_passive_actions_classifier(train=False, test=False,
visualize=False,
test_against_all=True,
for_app=True)
self.open_kernel = np.ones((5, 5), np.uint8)
self.time = []
####
node = roslaunch.core.Node("kinect2_bridge", "kinect2_bridge")
launch = roslaunch.scriptapi.ROSLaunch()
launch.start()
self.process = launch.launch(node)
if self.process.is_alive():
print 'Starting subscribers to Kinect..'
signal.signal(signal.SIGINT, signal_handler)
self.image_sub = rospy.Subscriber(
"/kinect2/sd/image_depth",Image,self.callback, queue_size=10)
self.image_pub = rospy.Publisher("hand", Image,queue_size=10)
self.class_pub = rospy.Publisher("class", TimeReference,queue_size=10)
self.skel_pub = rospy.Publisher("skeleton", Image,queue_size=10)
rospy.init_node('kinect_stream', anonymous=True)
self.bridge = CvBridge()
###
def callback(self, data):
'''
Callback function, <data> is the depth image
'''
try:
time1 = time.time()
try:
frame = self.bridge.imgmsg_to_cv2(data, desired_encoding="passthrough")
except CvBridgeError as err:
print err
return
mog2_res = self.mog2.run(False,
frame.astype(np.float32))
if mog2_res is None:
return
mask1 = cv2.morphologyEx(mog2_res.copy(), cv2.MORPH_OPEN, self.open_kernel)
check_sum = np.sum(mask1 > 0)
if not check_sum or check_sum == np.sum(frame > 0):
return
_, contours, _ = cv2.findContours(mask1, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
cont_ind = np.argmax([cv2.contourArea(contour) for
contour in contours])
final_mask = np.zeros_like(mask1)
cv2.drawContours(final_mask, contours, cont_ind, 1, -1)
#cv2.imshow('test',mask1*255)
#cv2.waitKey(10)
frame = frame * final_mask
scores_exist,_ = self.used_classifier.run_testing(frame,
online=True)
#DEBUGGING
#cv2.imshow('test',(frame%256).astype(np.uint8))
#cv2.waitKey(10)
time2 = time.time()
self.time.append(np.mean(self.time[-9:]+[(time2-time1)]))
if (self.used_classifier.recognized_classes is not None
and len(self.used_classifier.recognized_classes)>0
and scores_exist):
self.image_pub.publish(self.bridge.cv2_to_imgmsg(
self.used_classifier.frames_preproc.hand_img, "passthrough"))
msg = TimeReference()
try:
msg.source = self.used_classifier.train_classes[
self.used_classifier.recognized_classes[-1]]
except TypeError:
msg.source = self.used_classifier.recognized_classes[-1].name
msg.time_ref = Time()
msg.time_ref.secs = int(1/float(self.time[-1]) if self.time[-1]
else 0)
self.class_pub.publish(msg)
self.skel_pub.publish(self.bridge.cv2_to_imgmsg(
np.atleast_2d(np.array(self.used_classifier.
frames_preproc.skeleton.skeleton,np.int32))))
except Exception as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type,
exc_value,
exc_traceback, limit=10, file=sys.stdout)
LOG = logging.getLogger('__name__')
CH = logging.StreamHandler()
CH.setFormatter(logging.Formatter(
'%(funcName)20s()(%(lineno)s)-%(levelname)s:%(message)s'))
LOG.handlers = []
LOG.addHandler(CH)
LOG.setLevel('INFO')
KINECT = Kinect()
def main():
rospy.spin()
if __name__=='__main__':
main()