Skip to content

Commit 83b8347

Browse files
committed
Fix cfg handling
1 parent bdcda1b commit 83b8347

File tree

3 files changed

+155
-60
lines changed

3 files changed

+155
-60
lines changed

examples/python/RVC4/FeatureTracker/feature_detector_from_host_camera.py

+50-21
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,29 @@ def drawFeatures(frame, features):
1212
for feature in features:
1313
# descriptor = feature.descriptor
1414
# print(descriptor)
15-
if feature.age < 2:
16-
cv2.circle(frame, (int(feature.position.x), int(feature.position.y)), circleRadius, greenColor, -1, cv2.LINE_AA, 0)
17-
else:
15+
if feature.age > 2:
1816
cv2.circle(frame, (int(feature.position.x), int(feature.position.y)), circleRadius, pointColor, -1, cv2.LINE_AA, 0)
17+
# elif feature.age < 255:
18+
# cv2.circle(frame, (int(feature.position.x), int(feature.position.y)), circleRadius, (0, 0, feature.age), -1, cv2.LINE_AA, 0)
19+
else:
20+
cv2.circle(frame, (int(feature.position.x), int(feature.position.y)), circleRadius, greenColor, -1, cv2.LINE_AA, 0)
1921

2022
class HostCamera(dai.node.ThreadedHostNode):
2123
def __init__(self):
2224
dai.node.ThreadedHostNode.__init__(self)
2325
self.output = dai.Node.Output(self)
2426
def run(self):
2527
# Create a VideoCapture object
26-
cap = cv2.VideoCapture(0)
28+
cap = cv2.VideoCapture("/home/nebojsa/Documents/depthai-device-kb/external/depthai-core/examples/python/models/construction_vest.mp4")
29+
# cap = cv2.VideoCapture(0)
2730
if not cap.isOpened():
2831
pipeline.stop()
2932
raise RuntimeError("Error: Couldn't open host camera")
3033
while self.isRunning():
3134
# frame = depthImg
3235
ret, frame = cap.read()
36+
# frame = cv2.imread("/home/nebojsa/Downloads/image.jpg")
37+
frame = cv2.resize(frame, (640, 640), interpolation = cv2.INTER_LINEAR)
3338
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
3439
imgFrame = dai.ImgFrame()
3540
imgFrame.setData(frame)
@@ -52,12 +57,15 @@ def run(self):
5257
initialRobustness = 100
5358

5459
def on_trackbar(val):
55-
cfg = dai.FeatureTrackerConfigRvc4()
60+
cfg = dai.FeatureTrackerConfig()
61+
cornerDetector = dai.FeatureTrackerConfig.CornerDetector()
62+
cornerDetector.numMaxFeatures = cv2.getTrackbarPos('numMaxFeatures', 'Features')
5663

57-
cfg.setHarrisCornerDetectorThreshold(cv2.getTrackbarPos('harris_score','Features'))
58-
cfg.setHarrisCornerDetectorRobustness(cv2.getTrackbarPos('robustness','Features'))
59-
cfg.setNumMaxFeatures(cv2.getTrackbarPos('numMaxFeatures', 'Features'))
64+
thresholds = dai.FeatureTrackerConfig.CornerDetector.Thresholds()
65+
thresholds.initialValue = cv2.getTrackbarPos('harris_score','Features')
66+
cornerDetector.thresholds = thresholds
6067

68+
cfg.setCornerDetector(cornerDetector)
6169
cfg.setMotionEstimator(motionEstimator)
6270

6371
inputConfigQueue.send(cfg)
@@ -67,7 +75,7 @@ def on_trackbar(val):
6775

6876
# create trackbars threshold and robustness change
6977
cv2.createTrackbar('harris_score','Features',2000,25000, on_trackbar)
70-
cv2.createTrackbar('robustness','Features',100,127, on_trackbar)
78+
# cv2.createTrackbar('robustness','Features',100,127, on_trackbar)
7179
cv2.createTrackbar('numMaxFeatures','Features',256,1024, on_trackbar)
7280

7381

@@ -77,12 +85,24 @@ def on_trackbar(val):
7785
hostCamera = pipeline.create(HostCamera)
7886
camQueue = hostCamera.output.createOutputQueue()
7987

80-
featureTrackerLeft = pipeline.create(dai.node.FeatureTrackerRvc4)
88+
featureTrackerLeft = pipeline.create(dai.node.FeatureTracker)
8189

82-
featureTrackerLeft.initialConfig.setHarrisCornerDetectorThreshold(initialThreshold)
83-
featureTrackerLeft.initialConfig.setHarrisCornerDetectorRobustness(initialRobustness)
84-
featureTrackerLeft.initialConfig.setCornerDetector(dai.FeatureTrackerConfigRvc4.CornerDetector.Type.HARRIS)
85-
featureTrackerLeft.initialConfig.setNumMaxFeatures(256)
90+
# featureTrackerLeft.initialConfig.setHarrisCornerDetectorThreshold(initialThreshold)
91+
# featureTrackerLeft.initialConfig.setHarrisCornerDetectorRobustness(initialRobustness)
92+
featureTrackerLeft.initialConfig.setCornerDetector(dai.FeatureTrackerConfig.CornerDetector.Type.HARRIS)
93+
featureTrackerLeft.initialConfig.setMotionEstimator(False)
94+
95+
motionEstimator = dai.FeatureTrackerConfig.MotionEstimator()
96+
motionEstimator.enable = True
97+
featureTrackerLeft.initialConfig.setMotionEstimator(motionEstimator)
98+
99+
cornerDetector = dai.FeatureTrackerConfig.CornerDetector()
100+
cornerDetector.numMaxFeatures = 256
101+
102+
103+
104+
105+
# featureTrackerLeft.initialConfig.setNumMaxFeatures(256)
86106

87107

88108
outputFeaturePassthroughQueue = featureTrackerLeft.passthroughInputImage.createOutputQueue()
@@ -91,10 +111,12 @@ def on_trackbar(val):
91111
hostCamera.output.link(featureTrackerLeft.inputImage)
92112

93113
inputConfigQueue = featureTrackerLeft.inputConfig.createInputQueue()
94-
motionEstimator = dai.FeatureTrackerConfigRvc4.MotionEstimator()
95-
motionEstimator.enable = False
96114

97-
thresholds = dai.FeatureTrackerConfigRvc4.CornerDetector.Thresholds()
115+
thresholds = dai.FeatureTrackerConfig.CornerDetector.Thresholds()
116+
thresholds.initialValue = cv2.getTrackbarPos('harris_score','Features')
117+
118+
cornerDetector.thresholds = thresholds
119+
featureTrackerLeft.initialConfig.setCornerDetector(cornerDetector)
98120

99121
pipeline.start()
100122
while pipeline.isRunning():
@@ -118,10 +140,17 @@ def on_trackbar(val):
118140
if key == ord('q'):
119141
break
120142
elif key == ord('m'):
121-
cfg = dai.FeatureTrackerConfigRvc4()
122-
cfg.setHarrisCornerDetectorThreshold(cv2.getTrackbarPos('harris_score','Features'))
123-
cfg.setHarrisCornerDetectorRobustness(cv2.getTrackbarPos('robustness','Features'))
124-
cfg.setNumMaxFeatures(cv2.getTrackbarPos('numMaxFeatures', 'Features'))
143+
cfg = dai.FeatureTrackerConfig()
144+
cornerDetector = dai.FeatureTrackerConfig.CornerDetector()
145+
cornerDetector.numMaxFeatures = cv2.getTrackbarPos('numMaxFeatures', 'Features')
146+
147+
thresholds = dai.FeatureTrackerConfig.CornerDetector.Thresholds()
148+
thresholds.initialValue = cv2.getTrackbarPos('harris_score','Features')
149+
cornerDetector.thresholds = thresholds
150+
151+
cfg.setCornerDetector(cornerDetector)
152+
cfg.setMotionEstimator(motionEstimator)
153+
125154
if motionEstimator.enable == False:
126155
motionEstimator.enable = True
127156
cfg.setMotionEstimator(motionEstimator)

examples/python/RVC4/FeatureTracker/feature_tracker_from_host_camera.py

+44-23
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ def __init__(self):
1111
self.output = dai.Node.Output(self)
1212
def run(self):
1313
# Create a VideoCapture object
14-
cap = cv2.VideoCapture(0)
14+
cap = cv2.VideoCapture("/home/nebojsa/Documents/depthai-device-kb/external/depthai-core/examples/python/models/construction_vest.mp4")
15+
# cap = cv2.VideoCapture(0)
1516
if not cap.isOpened():
1617
pipeline.stop()
1718
raise RuntimeError("Error: Couldn't open host camera")
1819
while self.isRunning():
1920
# frame = depthImg
2021
ret, frame = cap.read()
22+
# frame = cv2.imread("/home/nebojsa/Downloads/image.jpg")
23+
frame = cv2.resize(frame, (640, 640), interpolation = cv2.INTER_LINEAR)
2124
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
2225
imgFrame = dai.ImgFrame()
2326
imgFrame.setData(frame)
@@ -109,21 +112,23 @@ def __init__(self, trackbarName, windowName):
109112
initialRobustness = 100
110113

111114
def on_trackbar(val):
112-
cfg = dai.FeatureTrackerConfigRvc4()
115+
cfg = dai.FeatureTrackerConfig()
116+
cornerDetector = dai.FeatureTrackerConfig.CornerDetector()
117+
cornerDetector.numMaxFeatures = cv2.getTrackbarPos('numMaxFeatures', 'Features')
113118

114-
cfg.setHarrisCornerDetectorThreshold(cv2.getTrackbarPos('harris_score','Features'))
115-
cfg.setHarrisCornerDetectorRobustness(cv2.getTrackbarPos('robustness','Features'))
116-
cfg.setNumMaxFeatures(cv2.getTrackbarPos('numMaxFeatures', 'Features'))
119+
thresholds = dai.FeatureTrackerConfig.CornerDetector.Thresholds()
120+
thresholds.initialValue = cv2.getTrackbarPos('harris_score','Features')
121+
cornerDetector.thresholds = thresholds
117122

123+
cfg.setCornerDetector(cornerDetector)
118124
cfg.setMotionEstimator(motionEstimator)
119125

120126
inputConfigQueue.send(cfg)
121127

122128
cv2.namedWindow('Features')
123129

124130
# create trackbars threshold and robustness change
125-
cv2.createTrackbar('harris_score','Features',2000,25000, on_trackbar)
126-
cv2.createTrackbar('robustness','Features',100,127, on_trackbar)
131+
cv2.createTrackbar('harris_score','Features',20000,25000, on_trackbar)
127132
cv2.createTrackbar('numMaxFeatures','Features',256,1024, on_trackbar)
128133

129134
# Connect to device and start pipeline
@@ -132,19 +137,22 @@ def on_trackbar(val):
132137
hostCamera = pipeline.create(HostCamera)
133138
camQueue = hostCamera.output.createOutputQueue()
134139

135-
featureTrackerLeft = pipeline.create(dai.node.FeatureTrackerRvc4)
140+
featureTrackerLeft = pipeline.create(dai.node.FeatureTracker)
136141

137-
featureTrackerLeft.initialConfig.setHarrisCornerDetectorThreshold(initialThreshold)
138-
featureTrackerLeft.initialConfig.setHarrisCornerDetectorRobustness(initialRobustness)
139-
featureTrackerLeft.initialConfig.setCornerDetector(dai.FeatureTrackerConfigRvc4.CornerDetector.Type.HARRIS)
140-
featureTrackerLeft.initialConfig.setNumMaxFeatures(256)
141-
142-
# Disable optical flow
143-
featureTrackerLeft.initialConfig.setMotionEstimator(True)
144-
145-
# variable to keep track of state
146-
motionEstimator = dai.FeatureTrackerConfigRvc4.MotionEstimator()
147-
motionEstimator.enable = False
142+
143+
# featureTrackerLeft.initialConfig.setHarrisCornerDetectorThreshold(initialThreshold)
144+
# featureTrackerLeft.initialConfig.setHarrisCornerDetectorRobustness(initialRobustness)
145+
featureTrackerLeft.initialConfig.setCornerDetector(dai.FeatureTrackerConfig.CornerDetector.Type.HARRIS)
146+
featureTrackerLeft.initialConfig.setMotionEstimator(False)
147+
148+
motionEstimator = dai.FeatureTrackerConfig.MotionEstimator()
149+
motionEstimator.enable = True
150+
featureTrackerLeft.initialConfig.setMotionEstimator(motionEstimator)
151+
152+
cornerDetector = dai.FeatureTrackerConfig.CornerDetector()
153+
cornerDetector.numMaxFeatures = 256
154+
155+
# featureTrackerLeft.initialConfig.setNumMaxFeatures(256)
148156

149157
outputFeaturePassthroughQueue = featureTrackerLeft.passthroughInputImage.createOutputQueue()
150158
outputFeatureQueue = featureTrackerLeft.outputFeatures.createOutputQueue()
@@ -154,6 +162,12 @@ def on_trackbar(val):
154162
# config = featureTrackerLeft.initialConfig
155163
inputConfigQueue = featureTrackerLeft.inputConfig.createInputQueue()
156164

165+
thresholds = dai.FeatureTrackerConfig.CornerDetector.Thresholds()
166+
thresholds.initialValue = cv2.getTrackbarPos('harris_score','Features')
167+
168+
cornerDetector.thresholds = thresholds
169+
featureTrackerLeft.initialConfig.setCornerDetector(cornerDetector)
170+
157171
leftWindowName = "Features"
158172
leftFeatureDrawer = FeatureTrackerDrawer("Feature tracking duration (frames)", leftWindowName)
159173

@@ -181,10 +195,17 @@ def on_trackbar(val):
181195
if key == ord('q'):
182196
break
183197
elif key == ord('m'):
184-
cfg = dai.FeatureTrackerConfigRvc4()
185-
cfg.setHarrisCornerDetectorThreshold(cv2.getTrackbarPos('harris_score','Features'))
186-
cfg.setHarrisCornerDetectorRobustness(cv2.getTrackbarPos('robustness','Features'))
187-
cfg.setNumMaxFeatures(cv2.getTrackbarPos('numMaxFeatures', 'Features'))
198+
cfg = dai.FeatureTrackerConfig()
199+
cornerDetector = dai.FeatureTrackerConfig.CornerDetector()
200+
cornerDetector.numMaxFeatures = cv2.getTrackbarPos('numMaxFeatures', 'Features')
201+
202+
thresholds = dai.FeatureTrackerConfig.CornerDetector.Thresholds()
203+
thresholds.initialValue = cv2.getTrackbarPos('harris_score','Features')
204+
cornerDetector.thresholds = thresholds
205+
206+
cfg.setCornerDetector(cornerDetector)
207+
cfg.setMotionEstimator(motionEstimator)
208+
188209
if motionEstimator.enable == False:
189210
motionEstimator.enable = True
190211
cfg.setMotionEstimator(motionEstimator)

examples/python/RVC4/FeatureTracker/feature_tracker_motion_from_host_camera.py

+61-16
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ def __init__(self):
1111
self.output = dai.Node.Output(self)
1212
def run(self):
1313
# Create a VideoCapture object
14-
cap = cv2.VideoCapture(0)
14+
cap = cv2.VideoCapture("/home/nebojsa/Documents/depthai-device-kb/external/depthai-core/examples/python/models/construction_vest.mp4")
15+
# cap = cv2.VideoCapture(0)
1516
if not cap.isOpened():
1617
pipeline.stop()
1718
raise RuntimeError("Error: Couldn't open host camera")
1819
while self.isRunning():
1920
# frame = depthImg
2021
ret, frame = cap.read()
22+
# frame = cv2.imread("/home/nebojsa/Downloads/image.jpg")
23+
frame = cv2.resize(frame, (640, 640), interpolation = cv2.INTER_LINEAR)
2124
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
2225
imgFrame = dai.ImgFrame()
2326
imgFrame.setData(frame)
@@ -194,22 +197,25 @@ def __init__(self, windowName):
194197
initialRobustness = 100
195198

196199
def on_trackbar(val):
197-
cfg = dai.FeatureTrackerConfigRvc4()
200+
cfg = dai.FeatureTrackerConfig()
201+
cornerDetector = dai.FeatureTrackerConfig.CornerDetector()
202+
cornerDetector.numMaxFeatures = cv2.getTrackbarPos('numMaxFeatures', 'Features')
198203

199-
cfg.setHarrisCornerDetectorThreshold(cv2.getTrackbarPos('harris_score','Features'))
200-
cfg.setHarrisCornerDetectorRobustness(cv2.getTrackbarPos('robustness','Features'))
201-
cfg.setNumMaxFeatures(cv2.getTrackbarPos('numMaxFeatures', 'Features'))
202-
cfg.setMotionEstimator(True)
204+
thresholds = dai.FeatureTrackerConfig.CornerDetector.Thresholds()
205+
thresholds.initialValue = cv2.getTrackbarPos('harris_score','Features')
206+
cornerDetector.thresholds = thresholds
207+
208+
cfg.setCornerDetector(cornerDetector)
209+
cfg.setMotionEstimator(motionEstimator)
203210

204211
inputConfigQueue.send(cfg)
205212

206213

207214
cv2.namedWindow('HostCamera')
208215
cv2.namedWindow('Features')
209216

210-
# create trackbars threshold and robustness change
211-
cv2.createTrackbar('harris_score','Features',2000,25000, on_trackbar)
212-
cv2.createTrackbar('robustness','Features',100,127, on_trackbar)
217+
# create trackbars threshold and numMaxFeatures change
218+
cv2.createTrackbar('harris_score','Features',20000,25000, on_trackbar)
213219
cv2.createTrackbar('numMaxFeatures','Features',256,1024, on_trackbar)
214220

215221
# Connect to device and start pipeline
@@ -218,12 +224,21 @@ def on_trackbar(val):
218224
hostCamera = pipeline.create(HostCamera)
219225
camQueue = hostCamera.output.createOutputQueue()
220226

221-
featureTrackerLeft = pipeline.create(dai.node.FeatureTrackerRvc4)
227+
featureTrackerLeft = pipeline.create(dai.node.FeatureTracker)
228+
229+
# featureTrackerLeft.initialConfig.setHarrisCornerDetectorThreshold(initialThreshold)
230+
# featureTrackerLeft.initialConfig.setHarrisCornerDetectorRobustness(initialRobustness)
231+
featureTrackerLeft.initialConfig.setCornerDetector(dai.FeatureTrackerConfig.CornerDetector.Type.HARRIS)
232+
featureTrackerLeft.initialConfig.setMotionEstimator(False)
233+
234+
motionEstimator = dai.FeatureTrackerConfig.MotionEstimator()
235+
motionEstimator.enable = True
236+
featureTrackerLeft.initialConfig.setMotionEstimator(motionEstimator)
222237

223-
featureTrackerLeft.initialConfig.setHarrisCornerDetectorThreshold(initialThreshold)
224-
featureTrackerLeft.initialConfig.setHarrisCornerDetectorRobustness(initialRobustness)
225-
featureTrackerLeft.initialConfig.setCornerDetector(dai.FeatureTrackerConfigRvc4.CornerDetector.Type.HARRIS)
226-
featureTrackerLeft.initialConfig.setNumMaxFeatures(256)
238+
cornerDetector = dai.FeatureTrackerConfig.CornerDetector()
239+
cornerDetector.numMaxFeatures = 256
240+
241+
# featureTrackerLeft.initialConfig.setNumMaxFeatures(256)
227242

228243
# Enable optical flow
229244
featureTrackerLeft.initialConfig.setMotionEstimator(True)
@@ -234,6 +249,13 @@ def on_trackbar(val):
234249
hostCamera.output.link(featureTrackerLeft.inputImage)
235250

236251
inputConfigQueue = featureTrackerLeft.inputConfig.createInputQueue()
252+
253+
thresholds = dai.FeatureTrackerConfig.CornerDetector.Thresholds()
254+
thresholds.initialValue = cv2.getTrackbarPos('harris_score','Features')
255+
256+
cornerDetector.thresholds = thresholds
257+
featureTrackerLeft.initialConfig.setCornerDetector(cornerDetector)
258+
237259
cfg = dai.FeatureTrackerConfig()
238260

239261
left_window_name = "Features"
@@ -262,5 +284,28 @@ def on_trackbar(val):
262284
print("Motions:", motions_left)
263285
cv2.imshow(left_window_name, leftFrame)
264286

265-
if cv2.waitKey(1) == ord('q'):
266-
break
287+
key = cv2.waitKey(1)
288+
if key == ord('q'):
289+
break
290+
elif key == ord('m'):
291+
cfg = dai.FeatureTrackerConfig()
292+
cornerDetector = dai.FeatureTrackerConfig.CornerDetector()
293+
cornerDetector.numMaxFeatures = cv2.getTrackbarPos('numMaxFeatures', 'Features')
294+
295+
thresholds = dai.FeatureTrackerConfig.CornerDetector.Thresholds()
296+
thresholds.initialValue = cv2.getTrackbarPos('harris_score','Features')
297+
cornerDetector.thresholds = thresholds
298+
299+
cfg.setCornerDetector(cornerDetector)
300+
cfg.setMotionEstimator(motionEstimator)
301+
302+
if motionEstimator.enable == False:
303+
motionEstimator.enable = True
304+
cfg.setMotionEstimator(motionEstimator)
305+
print("Enabling motionEstimator")
306+
else:
307+
motionEstimator.enable = False
308+
cfg.setMotionEstimator(motionEstimator)
309+
print("Disabling motionEstimator")
310+
311+
inputConfigQueue.send(cfg)

0 commit comments

Comments
 (0)