using OpenCvSharp;
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
var session = new InferenceSession("yolov8n.onnx");
using var frame = Cv2.ImRead("frame_0421.jpg");
var blob = Cv2.BlobFromImage(frame, 1.0 / 255.0,
new Size(640, 640), swapRB: true, crop: false);
var tensor = new DenseTensor<float>(new[] { 1, 3, 640, 640 });
for (int c = 0; c < 3; c++)
for (int y = 0; y < 640; y++)
for (int x = 0; x < 640; x++)
tensor[0, c, y, x] = blob.At<float>(0, c, y, x);
using var results = session.Run(inputs);
foreach (var b in PostProcess(results)) {
Cv2.Rectangle(frame, b.Rect, Scalar.Red, 2);
}
import torch
import torch.nn as nn
import torchvision.transforms as T
from torch.cuda.amp import autocast
class VisionNet(nn.Module):
def __init__(self, num_classes=1000):
super().__init__()
self.backbone = nn.Sequential(
nn.Conv2d(3, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
nn.Conv2d(64, 128, 3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
)
def forward(self, x):
return self.backbone(x).flatten(1)
// Real-time pipeline
// FPS: 60+ on RTX 4090
public class RealtimeDetector : IDisposable
{
private readonly InferenceSession _session;
private readonly VideoCapture _cap;
public void Run(Action<Frame> onFrame)
{
var frame = new Mat();
while (_cap.Read(frame) && !frame.Empty())
{
var result = Infer(frame);
onFrame(result);
}
}
}
from ultralytics import YOLO
import cv2
import numpy as np
model = YOLO("weights/best.pt")
stream = cv2.VideoCapture(0)
while stream.isOpened():
ok, frame = stream.read()
if not ok:
break
result = model(frame, verbose=False)[0]
boxes = result.boxes.xyxy.cpu().numpy()
scores = result.boxes.conf.cpu().numpy()
public sealed class FramePipeline
{
private readonly Channel<Mat> _frames;
private readonly CancellationToken _token;
public async Task ProcessAsync()
{
await foreach (var frame in _frames.Reader.ReadAllAsync(_token))
{
using var input = Preprocess(frame);
var detections = await DetectAsync(input);
Publish(detections);
}
}
}
[19:04:12.104] capture frame=10421 fps=61.8
[19:04:12.120] preprocess 2.14 ms
[19:04:12.126] inference 5.87 ms cuda:0
[19:04:12.128] nms 0.61 ms
[19:04:12.129] objects=7 confidence>0.72
[19:04:12.136] publish latency=11.2 ms
[19:04:12.152] capture frame=10422 fps=61.7
float3 edge = abs(ddx(normal)) + abs(ddy(normal));
float confidence = saturate(score * visibility);
float3 overlay = lerp(frame.rgb, debugColor, confidence * 0.25);
if (confidence > threshold)
{
output.rgb = overlay;
output.a = 1.0;
}