C#在工业上软件开发使用很多,但是如果要做深度学习的话,C#明显力不从心,为了解决C#在目标追踪中短缺,封装了一个bytrack的.NET库,可以通过这个库和yolov3/yolov4/yolov5/yolox/yolov7等目标检测框架对接,实现了two stage最优方法,目前测试发现bytetrck性能要优于当前所有追踪框架,而且精度也很高,因此使用bytrack作为追踪不失为一个比较好的方法。实现的追踪主框架代码如下:
const string Cfg = @"mydata\\yolov7-tiny.cfg";
const string Weight = @"mydata\\yolov7-tiny.weights";
const string Names = @"mydata\\coco.names";
var detector = new DarknetManager(Weight, Cfg, Names);
VideoCapture capture = new VideoCapture(@"D:\car.mp4");
ByteTrackerWrapper track = new ByteTrackerWrapper(20);
if (!capture.IsOpened())
{
Console.WriteLine("video not open!");
return;
}
Mat frame = new Mat();
Mat resultImg = new Mat();
var sw = new Stopwatch();
int fps = 0;
while (true)
{
capture.Read(frame);
if (frame.Empty())
{
Console.WriteLine("data is empty!");
break;
}
sw.Start();
var result = detector.InferenceImage(frame);
string data = detector.MakeResultString(result);
//Console.WriteLine(data);
if(!string.IsNullOrEmpty(data))
{
var trackerResult = track.Update(data);
resultImg = detector.DrawImage(frame,trackerResult);
}
else
{
resultImg = detector.DrawImage(frame, result);
}
sw.Stop();
fps = Convert.ToInt32(1 / sw.Elapsed.TotalSeconds);
sw.Reset();
Cv2.PutText(resultImg, "FPS=" + fps, new OpenCvSharp.Point(30, 30), HersheyFonts.HersheyComplex, 1.0, new Scalar(255, 0, 0), 3);
//显示结果
Cv2.ImShow("Result", resultImg);
int key = Cv2.WaitKey(10);
if (key == 27)
break;
}
track.Dispose();
capture.Release();
detector.Dispose();
更多的请参考视频教程演示:基于C#实现yolov7+bytetrack目标追踪的算法结果演示_哔哩哔哩_bilibili