-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
296 lines (237 loc) · 11.3 KB
/
main.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python
# Created by Martin Vasilkovski on 21.07.2019
##########################################################################
from __future__ import print_function
import roslib
#roslib.load_manifest('my_package')
import sys
import rospy
import cv2
from std_msgs.msg import String, Int16MultiArray, MultiArrayDimension
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import cv2 as cv
import Drawing
import Preprocessing
import Tracking
import BufferStartUp
import time
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt
#THIS IS ABOUT THE LAYOUT
'''
a = np.array([[[537, 264]], [[559, 221]], [[533, 157]], [[495, 148]], [[504, 225]], [[519 ,272]]])
msg = Int16MultiArray()
a1 = np.reshape(a, len(a)*len(a[0][0]))#, order='F')
msg.data = a1
# This is almost always zero there is no empty padding at the start of your data
msg.layout.data_offset = 0
# create two dimensions in the dim array
msg.layout.dim = [MultiArrayDimension(), MultiArrayDimension()]
# dim[0] is the vertical dimension of your matrix
msg.layout.dim[0].label = "axes"
msg.layout.dim[0].size = len(a[0][0])
msg.layout.dim[0].stride = len(a)*len(a[0][0])
# dim[1] is the horizontal dimension of your matrix
msg.layout.dim[1].label = "vertices"
msg.layout.dim[1].size = len(a)
msg.layout.dim[1].stride = len(a)
print(msg)
'''
initial_flag = False
mydatetime = datetime.now()
now = mydatetime.strftime('%m%d%H%M')
Buffer_images = []
Buffer = {}
p0 = []
ratio = 0.4
################################### 1 #################################################################
############# Definition of location of video. Initialize video object to read this video
contour = []
contour1 = []
vid1 = '' #/home/martin/Desktop/'
namevid = 'a1' #'videoLeft1'
format = '.mp4'
#vid = vid1 + namevid + format
#cap = cv.VideoCapture(vid)
#############################################################################################################
############ To read one fram from the video
#############################################################################################################
#ret, old_frame1 = cap.read()
#############################################################################################################
class image_converter:
def __init__(self):
self.image_pub = rospy.Publisher("tracking",Image)
self.contour_pub = rospy.Publisher("/AC_Path_tracked",Int16MultiArray)
self.bridge = CvBridge()
self.image_sub = rospy.Subscriber("/stereo/left/image_rect_color",Image,self.callback)
self.contour_sub = rospy.Subscriber("/AC_Path",Int16MultiArray,self.callback1)
def callback1(self, msg):
global contour
global contour1
contour_t = msg.data
#print(contour1)
#print(type(contour1))
#print(len(contour1))
contour = np.zeros((len(contour_t)/2,2))
contour1 = []
for i in range(len(contour_t)/2):
contour[i][0] = contour_t[i*2]*ratio
contour[i][1] = contour_t[i*2+1]*ratio
contour1.append((contour_t[i*2]*ratio, contour_t[i*2+1]*ratio))
contour = np.array(contour)
#contour1 = np.asarray(contour1, dtype=np.float32)
#print(contour)
#print(type(contour))
#print(len(contour))
def callback(self,data):
global initial_flag
global contour
#global contour1
global Buffer
global Buffer_images
global old_gray
global totalF
global Score
global p0
global pts
global jac
global trackers
global threshR
global pts1
global temp
#cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
old_frame1 = self.bridge.imgmsg_to_cv2(data, "bgr8")
#plt.imshow(old_frame1)
#plt.show()
if not initial_flag:
######################################################### REFERENCE FOR TABBING
old_frame = cv.resize(old_frame1, None, fx=ratio, fy=ratio)
#plt.imshow(old_frame)
#plt.show()
#old_frame_c = old_frame.copy()
means = []
pts1 = contour #Drawing.Draw(old_frame)
print("PTS 1 : ")
#print(pts1)
print("PTS CONTOUR1 : ")
#print(contour1)
#print("=========================================")
#print(pts1)
#print("=========================================")
old_gray, old_gray1, temp, gray_orig = Preprocessing.Track_init(old_frame, contour1)
# old_gray = frame rescaled, processed, with edge mask
# old_gray1 = frame rescaled, processed, with edge mask and with mask over ROI
# temp = frame rescaled, processed, cropped over boundingbox of ROI. Template for model
# gray_orig = frame rescaled, processed
#plt.imshow(old_gray1)
#plt.show()
Buffer, Buffer_images = BufferStartUp.start(Buffer, Buffer_images, temp)
# Buffer = buffer data
# Buffer_images = list of model images
p0, pts = Tracking.init(old_gray, pts1) # p0 are detected features, pts are vertices of the polygons
# p0 = kaze features found
# pts = input argument of vertices/points of the contour is reshaped and parsed for numpy
#print(p0)
#print(pts)
############# Initialize various variables to be used
#############################################################################################################
totalF = len(p0) # total number of features found/detected at (re-)initialization
Score = 1 # jaccard score of most recent (re-)initialization
threshR = 10 # tracker ransacReprojThreshold
jac = [] # jaccard score history
trackers = [] # list of active trackers
img_array = [] # not used
times = [] # execution times
initial_flag = True
else:
########################################## 2 ############################################################
############# Just a small test to see if the input is an image, and if it is non NULL
try:
frame1 = cv.resize(old_frame1, None, fx=ratio, fy=ratio)
# resize by predefined ratio
#frame_orig = frame1.copy()
# copy
except:
print("")
start = time.time()
frame_gray, frame_o, frame_proc, temp = Preprocessing.Track(frame1)
# frame_gray = frame rescaled, processed, with edge mask
# frame_o = original input frame is being sent back
# frame_proc = frame rescaled, processed
# temp = frame rescaled, processed, cropped over boundingbox of ROI. Template for model. Returned regardless of edge.
######### This function called "Tracking.do" does the tracking and reinitialization. There is a LOT of code inside.
# What Trackers.do returns
# img = most recent acquired frame with tracked contour drawn on top
# p0 = kaze features found
# pts = input argument of vertices/points of the contour is reshaped and parsed for numpy
# Buffer = buffer data
# Buffer_images = list of model images
# TotalF = total number of features found/detected at (re-)initialization
# Score = jaccard score of most recent (re-)initialization
# jac = jaccard score history
# trackers = trackers that are active and have been tracked from last frame to current frame
img, p0, pts, Buffer, Buffer_images, totalF, Score, jac, trackers = \
Tracking.do(old_gray, frame_gray, frame_proc, temp, Buffer, Buffer_images, frame_o, totalF, Score, p0, pts,
jac, trackers, threshR)
# Arguments used for Tracking.do
# old_gray = image/frame from previous loop cycle. It is frame_gray from old loop
# frame_gray = current mose recent frame rescaled, processed, with edge mask
# frame_proc = current most recent frame rescaled, processed
# temp = current most recent frame rescaled, processed, cropped over boundingbox of ROI. Template for model. Returned regardless of edge.
# Buffer = buffer data
# Buffer_images = list of model images
# frame_o = current most recent original input frame is being sent back
# totalF = total number of features detected at (re-)intialization. Used as reference to see how much of the initial trackers have been lost/found
# Score = jaccard score of most recent (re-)initialization
# p0 = kaze features found
# pts = input argument of vertices/points of the contour is reshaped and parsed for numpy
# jac = jaccard score history
# trackers = trackers that have been tracked to the last frame, from the one before it. Active trackers that we want to try to find in this loop
# threshR = tracker ransacReprojThreshold
########################################## 3 ############################################################
#############################################################################################################
#############################################################################################################
########### Just computes current fps, and prints it on the image.
t = 1/(time.time() - start)
cv.putText(img, 'FPS: ' + str(t), (0, 30), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
old_gray = frame_gray.copy()
msg = Int16MultiArray()
msg.data = np.reshape(pts, len(pts) * len(pts[0][0])) # , order='F')
'''
# preparation of point message to be published
a1l = len(pts)
a1l0 = len(pts[0][0])
a1 = np.reshape(pts, a1l * a1l0) # , order='F')
msg.data = a1
# This is almost always zero there is no empty padding at the start of your data
msg.layout.data_offset = 0
# create two dimensions in the dim array
msg.layout.dim = [MultiArrayDimension(), MultiArrayDimension()]
# dim[0] is the vertical dimension of your matrix
msg.layout.dim[0].label = "axes"
msg.layout.dim[0].size = a1l0
msg.layout.dim[0].stride = a1l * a1l0
# dim[1] is the horizontal dimension of your matrix
msg.layout.dim[1].label = "vertices"
msg.layout.dim[1].size = a1l
msg.layout.dim[1].stride = a1l
'''
#except CvBridgeError as e:
# print(e)
try:
self.contour_pub.publish(msg)
self.image_pub.publish(self.bridge.cv2_to_imgmsg(img, "bgr8"))
except CvBridgeError as e:
print(e)
def main(args):
ic = image_converter()
rospy.init_node('image_converter', anonymous=True)
try:
rospy.spin()
except KeyboardInterrupt:
print("Shutting down")
cv2.destroyAllWindows()
if __name__ == '__main__':
main(sys.argv)