嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 5 元微信扫码支付:5 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
using OpenCvSharp.Dnn;
using OpenCvSharp;
using OpenVinoSharp;
using OpenVinoSharp.Extensions.result;
using OpenVinoSharp.Extensions.process;
using System.Diagnostics;
using OpenVinoSharp.preprocess;
namespace openvino_asyn_csharp
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
//yolov8_det();
yolov8_async_det();
}
static void yolov8_async_det()
{
Core core = new Core();
Model model = core.read_model(model_path);
CompiledModel compiled_model = core.compile_model(model, "CPU");
VideoCapture capture = new VideoCapture(video_path);
if (!capture.IsOpened())
{
Console.WriteLine("ERROR: 视频无法打开");
return;
}
List<InferRequest> requests = new List<InferRequest> { compiled_model.create_infer_request(), compiled_model.create_infer_request() };
Mat frame = new Mat();
capture.Read(frame);
float factor = 0f;
float[] input_data = yolov8_preprocess(frame, out factor);
requests[0].get_input_tensor().set_data(input_data);
requests[0].start_async();
Stopwatch sw = new Stopwatch();
float[] total_infs = new float[3];
while (true)
{
Mat next_frame = new Mat();
if (!capture.Read(next_frame))
{
break;
}
sw.Restart();
input_data = yolov8_preprocess(frame, out factor);
requests[1].get_input_tensor().set_data(input_data);
sw.Stop();
total_infs[0] = sw.ElapsedMilliseconds;
sw.Restart();
requests[1].start_async();
requests[0].wait();
sw.Stop();
total_infs[1] = sw.ElapsedMilliseconds;
sw.Restart();
float[] output_data = requests[0].get_output_tensor().get_data<float>(8400 * 84);
DetResult result = yolov8_postprocess(output_data, factor);
sw.Stop();
total_infs[2] = sw.ElapsedMilliseconds;
Cv2.PutText(frame, "PreProcess: " (1000.0 / total_infs[0]).ToString("0.00") "FPS " (total_infs[0]).ToString("0.00") "ms",
new Point(20, 40), HersheyFonts.HersheyPlain, 2, new Scalar(255, 0, 255), 2);
Cv2.PutText(frame, "Inference: " (1000.0 / total_infs[1]).ToString("0.00") "FPS " (total_infs[1]).ToString("0.00") "ms",
new Point(20, 70), HersheyFonts.HersheyPlain, 2, new Scalar(255, 0, 255), 2);
Cv2.PutText(frame, "PostProcess: " (1000.0 / total_infs[2]).ToString("0.00") "FPS " (total_infs[2]).ToString("0.00") "ms",
new Point(20, 100), HersheyFonts.HersheyPlain, 2, new Scalar(255, 0, 255), 2);
Cv2.PutText(frame, "Total: " (1000.0 / (total_infs[0] total_infs[1] total_infs[2])).ToString("0.00")
"FPS " ((total_infs[0] total_infs[1] total_infs[2])).ToString("0.00") "ms",
new Point(20, 130), HersheyFonts.HersheyPlain, 2, new Scalar(255, 0, 255), 2);
Mat res_mat = Visualize.draw_det_result(result, frame);
Cv2.ImShow("Result", res_mat);
Cv2.WaitKey(10);
swap(requests);
frame = next_frame;
}
}