-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
165 lines (134 loc) · 6.11 KB
/
main.py
File metadata and controls
165 lines (134 loc) · 6.11 KB
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
import os
import sys
import json
import glob
import cv2
import numpy as np
sys.path.append(os.path.join(os.path.dirname(__file__), 'Integration'))
from depthEstimator import DepthEstimator
from coneSegmentor import ConeSegmentor
from distanceEstimator import DistanceEstimator
from visualizationUtils import createFourTileVisualization
from coneLocalizer import perceptionToDetections, coneFilterToPathPlannerInput
sys.path.append(os.path.join(os.path.dirname(__file__), 'DepthEstimation'))
from ground_plane_ransac import estimateGroundPlane, getCameraIntrinsics
sys.path.append(os.path.join(os.path.dirname(__file__), 'Localization'))
from filter import ConeFilter
sys.path.append(os.path.join(os.path.dirname(__file__), 'PathPlanning'))
from path_planner import plan_path
def perception(image, depthEstimator, coneSegmentor, distEstimator, cameraIntrinsics):
depthMap = depthEstimator.estimateDepth(image)
segmentationResults = coneSegmentor.segment(image)
calibratedScale = distEstimator.calibrateDepthScale(
segmentationResults['boxes'],
segmentationResults['classes'],
depthMap
)
planeParams, inlierRatio, inlierMask = estimateGroundPlane(
depthMap,
cameraIntrinsics,
exclusionAreas=None,
subsampleStep=5,
inlierThreshold=0.1,
maxTrials=100,
maxTiltAngle=45,
depthScale=calibratedScale
)
coneDistances = distEstimator.estimateAllCones(
segmentationResults['boxes'],
segmentationResults['classes'],
depthMap,
planeParams,
depthScale=calibratedScale
)
return depthMap, segmentationResults, planeParams, inlierMask, coneDistances, calibratedScale
def main():
print("Initializing models...")
depthEstimator = DepthEstimator()
coneSegmentor = ConeSegmentor()
distEstimator = DistanceEstimator()
coneFilter = ConeFilter()
framePattern = os.path.join('SampleData', 'driverless-10fps', 'frame_*.jpg')
frameFiles = sorted(glob.glob(framePattern))
outputDir = 'output'
os.makedirs(outputDir, exist_ok=True)
print(f"Processing {len(frameFiles)} frames...")
for frameFile in frameFiles:
frameName = os.path.splitext(os.path.basename(frameFile))[0]
print(f"Processing {frameName}...")
image = cv2.imread(frameFile)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
H, W = image.shape[:2]
cameraIntrinsics = getCameraIntrinsics(W, H, fov=90)
depthMap, segmentationResults, planeParams, inlierMask, coneDistances, calibratedScale = perception(
image, depthEstimator, coneSegmentor, distEstimator, cameraIntrinsics
)
print(f" Calibrated depth scale: {calibratedScale:.4f}")
depthPath = os.path.join(outputDir, f'depth_{frameName}.npy')
np.save(depthPath, depthMap)
segResults = {
'numDetections': segmentationResults['numDetections'],
'boxes': segmentationResults['boxes'].tolist() if segmentationResults['boxes'] is not None else None,
'classes': segmentationResults['classes'],
'confidences': segmentationResults['confidences'].tolist() if segmentationResults['confidences'] is not None else None
}
segPath = os.path.join(outputDir, f'seg_{frameName}.json')
with open(segPath, 'w') as f:
json.dump(segResults, f, indent=2)
distancesOutput = []
for dist in coneDistances:
distancesOutput.append({
'distance': dist['distance'],
'bboxDistance': dist['bboxDistance'],
'depthMapDistance': dist['depthMapDistance'],
'groundPlaneDistance': dist['groundPlaneDistance'],
'class': dist['class']
})
distPath = os.path.join(outputDir, f'distances_{frameName}.json')
with open(distPath, 'w') as f:
json.dump(distancesOutput, f, indent=2)
visualization = createFourTileVisualization(
image, depthMap, segmentationResults, planeParams, inlierMask,
coneDistances, cameraIntrinsics, depthScale=calibratedScale
)
visPath = os.path.join(outputDir, f'vis_{frameName}.png')
visBgr = cv2.cvtColor(visualization, cv2.COLOR_RGB2BGR)
cv2.imwrite(visPath, visBgr)
# Convert perception output to vehicle-frame detections and update cone map
localizationDetections = perceptionToDetections(
segmentationResults['boxes'], segmentationResults['classes'],
segmentationResults['confidences'], coneDistances, cameraIntrinsics
)
coneFilter.update(localizationDetections, dx=0, dy=0, dyaw=0)
# Path planning
smoothPath = None
pathPlannerInput = coneFilterToPathPlannerInput(coneFilter.cones)
if pathPlannerInput is not None:
positions, coordinateConfidence, colors = pathPlannerInput
smoothPath, curvature = plan_path(
positions, coordinateConfidence, colors,
np.array([0.0, 0.0]), 0.0
)
if smoothPath is not None:
pathData = {
'smoothPath': smoothPath.tolist(),
'curvature': curvature.tolist()
}
pathJsonPath = os.path.join(outputDir, f'path_{frameName}.json')
with open(pathJsonPath, 'w') as f:
json.dump(pathData, f, indent=2)
else:
print(f" No valid path found for {frameName}")
else:
print(f" Insufficient cones for path planning in {frameName}")
mapPath = os.path.join(outputDir, f'map_{frameName}.png')
coneFilter.visualize(savePath=mapPath, showPlot=False, plannedPath=smoothPath)
coneMap = coneFilter.getConeMap()
mapData = [{'x': c[0], 'y': c[1], 'blue': c[2], 'yellow': c[3],
'sOrange': c[4], 'lOrange': c[5]} for c in coneMap]
mapJsonPath = os.path.join(outputDir, f'map_{frameName}.json')
with open(mapJsonPath, 'w') as f:
json.dump(mapData, f, indent=2)
print("Processing complete!")
if __name__ == "__main__":
main()