I have tried to make an AI detection box with using Onnx where i want to make it so if i am in vs2022 and make the application i want to make it so if i check a checkbox it will activate the AI and will capture my screen in real time and it works but the problem i am having is that the AI boxes always show up on the left side of my screen and i do not really know why it can be because i am using 640 x 640 but as i see it it should be in the middle and not up to the left, idk it is hard to explain but if someone needs more explaining i can do that, here is some of the code that i have now for these stuff:
private void DetectItems(Bitmap screen)
{
var inputTensor = PreprocessImage(screen, 640, 640);
var input = new List<NamedOnnxValue> { NamedOnnxValue.CreateFromTensor("images", inputTensor) };
List<DetectedItem> detectedItems;
using (var results = session.Run(input))
{
detectedItems = ParseYoloOutput(results);
}
// Draw bounding boxes directly on the screen (overlay)
using (Graphics g = Graphics.FromHwnd(IntPtr.Zero)) // Drawing on the screen
{
foreach (var item in detectedItems)
{
g.DrawRectangle(Pens.Red, item.Box);
string detectedClassName = GetClassNameFromId(item.ClassId);
g.DrawString($"{detectedClassName} ({item.Confidence:P0})", new Font("Arial", 16), Brushes.Red, item.Box.X, item.Box.Y);
}
}
}
private List<DetectedItem> ParseYoloOutput(IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results)
{
var output = results.First().AsEnumerable<float>().ToArray();
List<DetectedItem> detectedItems = new List<DetectedItem>();
for (int i = 0; i < output.Length; i += 6)
{
float x = output[i] * Screen.PrimaryScreen.Bounds.Width;
float y = output[i + 1] * Screen.PrimaryScreen.Bounds.Height;
float width = output[i + 2] * Screen.PrimaryScreen.Bounds.Width;
float height = output[i + 3] * Screen.PrimaryScreen.Bounds.Height;
float confidence = output[i + 4];
int classId = (int)output[i + 5];
if (confidence > 0.1f)
{
DetectedItem item = new DetectedItem
{
Box = new Rectangle((int)x, (int)y, (int)width, (int)height),
ClassId = classId,
Confidence = confidence
};
detectedItems.Add(item);
}
}
return detectedItems;
}
private Tensor<float> PreprocessImage(Bitmap bitmap, int width, int height)
{
Bitmap resized = new Bitmap(bitmap, new Size(width, height));
var input = new DenseTensor<float>(new[] { 1, 2, height, width });
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Color pixel = resized.GetPixel(x, y);
input[0, 0, y, x] = pixel.R / 255f;
input[0, 1, y, x] = pixel.G / 255f;
input[0, 2, y, x] = pixel.B / 255f;
}
}
return input;
}
private void checkBoxEnableAI_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxEnableAI.Checked)
{
if (detectionTimer == null)
{
detectionTimer = new Timer();
detectionTimer.Interval = 1000; // Detect every second
detectionTimer.Tick += (s, ev) =>
{
Bitmap screenshot = CaptureScreen();
DetectItems(screenshot); // Run detection and draw bounding boxes
};
}
detectionTimer.Start();
}
else
{
detectionTimer?.Stop();
}
}
i tried to chance it so i had like an FOV in the middle of my screen that was like an 640 x 640 and it was always visible from the start of starting the menu and in the fov is where the AI will be active everything outside of the fov will make the AI not detect the object like a banana or something but i could not make the fov be in the middle of my screen while making the AI only target everything inside of it because i am not the best in C# got some help from chatgpt but that it not work so i am here now needing some tips
2