Hello,
I have an issue with the custom detector, the ZED Retrieve objects stucks, and never return or complete, it takes like 10 minutes to return back.
Here is my sample code
private void Predict(Texture2D frame)
{
using Tensor inputImage = TextureConverter.ToTensor(frame, width: 640, height: 640, channels: 3);
inputsCache["images"] = inputImage;
worker.Execute(inputsCache);
var boxCoords = worker.PeekOutput("boxCoords") as TensorFloat;
var nmsOutput = worker.PeekOutput("nmsOutput") as TensorInt;
var classIDs = worker.PeekOutput("classIDs") as TensorInt;
if (nmsOutput.shape[0] == 0) return;
var boxCoordIDs = ops.Slice(nmsOutput, new int[] { 2 }, new int[] { 3 }, new int[] { 1 }, new int[] { 1 });
var boxCoordIDsFlat = boxCoordIDs.ShallowReshape(new TensorShape(boxCoordIDs.shape.length)) as TensorInt;
var output = ops.Gather(boxCoords, boxCoordIDsFlat, 1) as TensorFloat;
var labelIDs = ops.Gather(classIDs, boxCoordIDsFlat, 2) as TensorInt;
output.MakeReadable();
labelIDs.MakeReadable();
List<Vector4> boxesList = new List<Vector4>();
List<float> confidencesList = new List<float>();
List<int> classIdsList = new List<int>();
for (int i = 0; i < output.shape[1]; i++)
{
float x1 = output[0, i, 0];
float y1 = output[0, i, 1] ;
float x2 = output[0, i, 2];
float y2 = output[0, i, 3];
int classId = labelIDs[0, i, 0];
float x_min = x1 - 0.5f * x2;
float x_max = x1 + 0.5f * x2;
float y_min = y1 - 0.5f * y2;
float y_max = y1 + 0.5f * y2;
boxesList.Add(new Vector4(x1, y1, x2, y2));
confidencesList.Add(1.0f); // Assuming confidence is 1.0 for simplicity
classIdsList.Add(classId);
}
IngestCustomData(boxesList, confidencesList, classIdsList);
}
private Vector2[] XYXY2ABCD(Vector4 box)
{
Vector2[] abcd = new Vector2[4];
// Normalize coordinates to [0, 1] range
float x1 = box.x ;
float y1 = box.y ;
float x2 = box.z ;
float y2 = box.w ;
// A ------ B
// | Object |
// D ------ C
abcd[0] = new Vector2(x1, y1); // A
abcd[1] = new Vector2(x2, y1); // B
abcd[2] = new Vector2(x2, y2); // C
abcd[3] = new Vector2(x1, y2); // D
// Save results to a text file
string path = Application.persistentDataPath + "/BoundingBoxLog.txt";
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine("Original Bounding Box:");
writer.WriteLine($"X1: {x1}, Y1: {y1}, X2: {x2}, Y2: {y2}");
writer.WriteLine("Normalized Bounding Box:");
writer.WriteLine($"A: {abcd[0]}, B: {abcd[1]}, C: {abcd[2]}, D: {abcd[3]}");
writer.WriteLine("=====================================");
}
// Debug log for validation
Debug.Log("Bounding box saved to: " + path);
return abcd;
}
private void IngestCustomData(List<Vector4> boxesList, List<float> confidencesList, List<int> classIdsList)
{
Debug.Log("Box list count: " + boxesList.Count);
List<CustomBoxObjectData> objects_in = new List<CustomBoxObjectData>();
for (int idx = 0; idx < boxesList.Count; ++idx)
{
CustomBoxObjectData tmp = new CustomBoxObjectData();
tmp.uniqueObjectID = sl.ZEDCamera.GenerateUniqueID();
tmp.label = 0;
tmp.probability = 0.45f;
tmp.isGrounded = false;
// Convert xyxy to abcd format
Vector2[] bbox = XYXY2ABCD(boxesList[idx]);
tmp.boundingBox2D = bbox;
objects_in.Add(tmp);
}
zedManager.customObjects.Clear();
zedManager.customObjects.AddRange(objects_in);
customObjectDetectionRuntime = new CustomObjectDetectionRuntimeParameters();
customObjectDetectionRuntime.objectDetectionProperties = new CustomObjectDetectionProperties();
customObjectDetectionRuntime.objectDetectionProperties.enabled = true;
customObjectDetectionRuntime.objectDetectionProperties.isGrounded = false;
customObjectDetectionRuntime.objectDetectionProperties.detectionConfidenceThreshold = 0.5f;
customObjectDetectionRuntime.objectDetectionProperties.classID = 0;
customObjectDetectionRuntime.objectDetectionProperties.maxBoxHeightNormalized = 1.0f;
customObjectDetectionRuntime.objectDetectionProperties.maxBoxWidthNormalized = 1.0f;
customObjectDetectionRuntime.objectDetectionProperties.isStatic = false;
customObjectDetectionRuntime.objectDetectionProperties.trackingMaxDist = 40.0f;
customObjectDetectionRuntime.objectDetectionProperties.trackingTimeout = 55;
zedManager.customObjectDetectionRuntimeParameters = customObjectDetectionRuntime;
objectClassDetectionProperties = new List<CustomObjectDetectionProperties>();
objectClassDetectionProperties.Add(customObjectDetectionRuntime.objectDetectionProperties);
customObjectDetectionRuntime.objectClassDetectionProperties = objectClassDetectionProperties;
sl.Objects objframe = new Objects();
zedManager.zedCamera.RetrieveObjects(ref customObjectDetectionRuntime, ref objframe);
}