Код: Выделить всё
private void DetectItems(Bitmap screen)
{
var inputTensor = PreprocessImage(screen, 640, 640);
var input = new List { NamedOnnxValue.CreateFromTensor("images", inputTensor) };
List 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 ParseYoloOutput(IDisposableReadOnlyCollection results)
{
var output = results.First().AsEnumerable().ToArray();
List detectedItems = new List();
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 PreprocessImage(Bitmap bitmap, int width, int height)
{
Bitmap resized = new Bitmap(bitmap, new Size(width, height));
var input = new DenseTensor(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();
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... that-works