Size mismatch (size [1 x :?] ~= size [0 x 1]) - GPU Coder Deployment
2 次查看(过去 30 天)
显示 更早的评论
Hello, I am trying to deploy a code from MATLAB to Jetson Nano,to summarize. This code is used to read live detections from a camera and Lidar VLP16, then It detects an object using the camera(eg.person), once the object is present on both camera and lidar, It computes the distance the object is from both LIDAR and Camera. Now, I am using helperComputeDistance function. I am going to provide step by step from my code.
Note that this code works perfectly when I run it on MATLAB, everything is fine. (Ofcourse I dont use obj, camObj, etc). I define LIDAR & camera normally.
function AliSamehTrial1(mdlName,calibFile,port,cameraName,resolution)
%#codegen
hwobj = jetson()
%%
% Create hwobj
hwobj = jetson();
camObj = camera(hwobj,cameraName,resolution);
dispObj = imageDisplay(hwobj);
obj = velodynelidar(hwobj,mdlName,calibFile,'Port',2368);
%%
R = [102.3 0 -178.5 ];
Translation = [-0.19 -0.32 0.08];
tform1 = rigidtform3d(R, Translation);
camToLidar = invert(tform1);
LidarToCam = tform1;
focalLength = [2.030730245970483e+03 2.028918613870282e+03];
principalPoint = [1.372086996058644e+03 7.403715833893430e+02];
imageSize = [720 1280];
intrinsics = cameraIntrinsics(focalLength,principalPoint,imageSize);
persistent yolov4Obj;
if isempty(yolov4Obj)
yolov4Obj = coder.loadDeepLearningNetwork('tinyyolov4coco.mat');
end
%%
continousStreaming = true;
%%
start(obj)
if strcmp(mdlName,'VLP16')
lenOut = 120;
outStructLen = 28000;
else
lenOut = 70;
outStructLen = 48000;
end
%%
while continousStreaming
% Capture the image from the Jetson camera hardware.
I = snapshot(camObj);
pc = read(obj);
M=16;
N=1808;
reshapedLocation = reshape(pc.Location, [M, N, 3]);
outStruct = pointCloud(reshapedLocation);
% Call to detect method
[bboxes, scores, labels] = detect(yolov4Obj, I, 'Threshold', 0.3);
personIndices = find(labels == 'person');
personBboxes = bboxes(personIndices, :);
personScores = scores(personIndices, :);
personLabels = labels(personIndices, :);
% Convert categorical labels to cell array of character vectors
labels = cellstr(labels);
% Annotate detections in the camera image.
img = insertObjectAnnotation(I, 'rectangle', bboxes, labels);
% Remove ground plane
groundPtsIndex = segmentGroundFromLidarData(outStruct, 'ElevationAngleDelta', 5, ...
'InitialElevationAngle', 0);
nonGroundPts = select(outStruct, ~groundPtsIndex);
% Process lidar data if there are detections
if bboxes == personBboxes
[lidarBbox, ~, boxUsed] = bboxCameraToLidar(bboxes, nonGroundPts, intrinsics, ...
camToLidar, 'ClusterThreshold', 0.3, 'MaxDetectionRange', [1, 50]);
[distance, nearestRect, idx] = helperComputeDistance(bboxes, nonGroundPts, lidarBbox, ...
intrinsics, LidarToCam);
% Print distance information
fprintf('DistOfPerson: %.2f meters\n', distance);
else
[lidarBbox, ~, boxUsed] = bboxCameraToLidar(bboxes, nonGroundPts, intrinsics, ...
camToLidar, 'ClusterThreshold', 0.3, 'MaxDetectionRange', [1, 50]);
[distance, nearestRect, idx] = helperComputeDistance(bboxes, nonGroundPts, lidarBbox, ...
intrinsics, LidarToCam);
% Print distance information
fprintf('DistOfObj: %.2f meters\n', distance);
% Update image with bounding boxes
% im = updateImage(display, im, nearestRect, distance);
% updateLidarBbox(display, lidarBbox)
end
end
This is the main function, which has all the commands thats required.
As seen in this line of code:
[distance, nearestRect, idx] = helperComputeDistance(bboxes, nonGroundPts, lidarBbox, ...
intrinsics, LidarToCam);
helperComputeDistance is used, I defined helperComputeDistance below here:
function [distance, nearestRect, index] = helperComputeDistance(bboxes, outStruct, lidarBbox, intrinsic, lidarToCam)
numLidarDetections = size(lidarBbox, 1);
nearestRect = zeros(0, 4);
distance = zeros(1, numLidarDetections);
index = zeros(0, 1);
for i = 1:numLidarDetections
bboxCuboid = lidarBbox(i, :);
% Create cuboidModel
model = cuboidModel(bboxCuboid);
% Find points inside cuboid
ind = findPointsInsideCuboid(model, outStruct);
pts = select(outStruct, ind);
% Project cuboid points to image
imPts = projectLidarPointsOnImage(pts, intrinsic, lidarToCam);
% Find 2-D rectangle corresponding to 3-D bounding box
[nearestRect(i, :), idx] = findNearestRectangle(imPts, bboxes);
index = [index, idx];
% Calculate the median or mean distance from lidar points
distances = sqrt(sum(pts.Location.^2, 2)); % Euclidean distance
distance(i) = mean(distances); % Use median or mean depending on your preference
end
end
function [nearestRect,idx] = findNearestRectangle(imPts,bboxes)
numBbox = size(bboxes,1);
ratio = zeros(numBbox,1);
% Iterate over all the rectangles
for i = 1:numBbox
bbox = bboxes(i,:);
corners = getCornersFromBbox(bbox);
% Find overlapping ratio of the projected points and the rectangle
idx = (imPts(:,1) > corners(1,1)) & (imPts(:,1) < corners(2,1)) & ...
(imPts(:,2) > corners(1,2)) & (imPts(:,2) < corners(3,1));
ratio(i) = sum(idx);
end
% Get nearest rectangle
[~,idx] = max(ratio);
nearestRect = bboxes(idx,:);
end
function cornersCamera = getCornersFromBbox(bbox)
cornersCamera = zeros(4,2);
cornersCamera(1,1:2) = bbox(1:2);
cornersCamera(2,1:2) = bbox(1:2) + [bbox(3),0];
cornersCamera(3,1:2) = bbox(1:2) + bbox(3:4);
cornersCamera(4,1:2) = bbox(1:2) + [0,bbox(4)];
end
end
However; I get this error
codegen('-config ',cfg,'-args',inputArgs,'AliSamehTrial1','-report');
Size mismatch (size [1 x :?] ~= size [0 x 1]).
The size to the left is the size of the left-hand side of the assignment.
More information
Error in ==> AliSamehTrial1 Line: 93 Column: 5
Line 93 is:
index = [index, idx];
I tried doing the following:
1- coder.extrinsic('helperComputeDistance');
%This one didnt work.
2- I tried coder.varsize('index')
%This didnt work as well.
%Or actually if someone can actually tell me how to properly use it
I do understand that this link here: https://www.mathworks.com/help/coder/ug/limitations-with-variable-size-support-for-code-generation.html
Most probably has solution to my issue, but I do not know how to implement it. So can somebody please help me?
回答(1 个)
Bruno Luong
2023-12-12
编辑:Bruno Luong
2023-12-12
Try to initialize
index = zeros(1, 0);
since you concatenate index horizontally in the for loop.
3 个评论
Bruno Luong
2023-12-12
This seems to be different problem.
The previous error (line 93) in your original question seems to be fixed.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Camera Calibration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!