123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Globalization;
- using System.IO;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Windows;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Media.TextFormatting;
- using Path = System.IO.Path;
- namespace Test
- {
- [StructLayout(LayoutKind.Sequential)]
- internal struct Box
- {
- public float X;
- public float Y;
- public float Width;
- public float Height;
- public int Label;
- public float Prob;
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct StructStringInfo
- {
- public IntPtr result;
- public int bufferSize;
- }
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- [DllImport("libYoloNcnn.dll")]
- private static extern void CreateNet(out IntPtr hDect, out IntPtr hRec, IntPtr dectBinName, IntPtr dectParaName, IntPtr recBinName, IntPtr recParaName, IntPtr keyTxtName);
- [DllImport("libYoloNcnn.dll")]
- private static extern unsafe void Test(IntPtr net, IntPtr net2, IntPtr input, int iw, int ih, ref StructStringInfo* ob, ref int count);
- [DllImport("libYoloNcnn.dll")]
- private static extern void FreeNet(IntPtr dectNet, IntPtr recNet);
- private IntPtr _detect;
- private IntPtr _rec;
- private byte[] _dectBinMem;
- private byte[] _dectParamMem;
- private byte[] _recBinMem;
- private byte[] _recParamMem;
- private readonly BitmapImage _image;
- private static readonly string[] ClassNames = {
- "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
- "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
- "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
- "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
- "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
- "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
- "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone",
- "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear",
- "hair drier", "toothbrush"
- };
- public MainWindow()
- {
- InitializeComponent();
- var imageFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test4.png");
- _image = new BitmapImage();
- _image.BeginInit();
- _image.StreamSource = new MemoryStream(File.ReadAllBytes(imageFile));
- _image.EndInit();
- TestImage.Source = _image;
- }
- private void DrawText(string textLine, Point loc, DrawingContext context)
- {
- var text = new FormattedText(textLine,
- CultureInfo.CurrentCulture,
- FlowDirection.LeftToRight,
- new Typeface(FontFamily, FontStyles.Normal, FontWeights.Normal, new FontStretch()),
- 36,
- Brushes.Black,
- null,
- 96);
- var textLocation = new Point(loc.X, loc.Y - text.Height);
- textLocation.Offset(1,0);
- context.DrawText(text, textLocation);
- }
- private unsafe void OnTestClick(object sender, RoutedEventArgs e)
- {
- var imageData = new byte[_image.PixelWidth * _image.PixelHeight * 4];
- _image.CopyPixels(imageData, _image.PixelWidth * 4, 0);
- var imgPtr = Marshal.UnsafeAddrOfPinnedArrayElement(imageData, 0);
- StructStringInfo* ob = null;
- int count = 0;
- Test(_detect, _rec, imgPtr, _image.PixelWidth, _image.PixelHeight, ref ob, ref count);
- // 访问每个字符串结果
- string[] resultString = new string[count];
- for (int i = 0; i < count; i++)
- {
- resultString[i] = Marshal.PtrToStringUTF8(ob[i].result/*, ob[i].bufferSize*/);
- }
- var rect = new Rect(new Size(_image.PixelWidth, _image.PixelHeight));
- var drawingVisual = new DrawingVisual();
- var drawingContext = drawingVisual.RenderOpen();
- //drawingContext.DrawImage(_image,rect);
- for (var i = 0; i < count; i++)
- {
- DrawText(resultString[i], new Point(100, 100 + 50 * i), drawingContext);
- }
- drawingContext.Close();
- var bmp = new RenderTargetBitmap(_image.PixelWidth, _image.PixelHeight, 96, 96, PixelFormats.Pbgra32);
- bmp.Render(drawingVisual);
- OutImage.Source = bmp;
- }
- private void OnLoadClick(object sender, RoutedEventArgs e)
- {
- var typeInfo = GetType().GetTypeInfo();
- var assemblyName = typeInfo.Assembly.GetName().Name;
- var dectbin = $"{assemblyName}.ValidModels.ch_PP-OCRv4_det_infer.bin";
- var stream = typeInfo.Assembly.GetManifestResourceStream(dectbin);
- if (stream == null)
- {
- throw new InvalidOperationException($"Resource not found: {dectbin}");
- }
- using (var ms = new MemoryStream())
- {
- stream.CopyTo(ms);
- _dectBinMem = ms.ToArray();
- }
- var dectparam = $"{assemblyName}.ValidModels.ch_PP-OCRv4_det_infer.param";
- stream = typeInfo.Assembly.GetManifestResourceStream(dectparam);
- if (stream == null)
- {
- throw new InvalidOperationException($"Resource not found: {dectparam}");
- }
- using (var ms = new MemoryStream())
- {
- stream.CopyTo(ms);
- _dectParamMem = ms.ToArray();
- }
- var recbin = $"{assemblyName}.ValidModels.ch_PP-OCRv3_rec.bin";
- stream = typeInfo.Assembly.GetManifestResourceStream(recbin);
- if (stream == null)
- {
- throw new InvalidOperationException($"Resource not found: {recbin}");
- }
- using (var ms = new MemoryStream())
- {
- stream.CopyTo(ms);
- _recBinMem = ms.ToArray();
- }
- var recparam = $"{assemblyName}.ValidModels.ch_PP-OCRv3_rec.param";
- stream = typeInfo.Assembly.GetManifestResourceStream(recparam);
- if (stream == null)
- {
- throw new InvalidOperationException($"Resource not found: {recparam}");
- }
- using (var ms = new MemoryStream())
- {
- stream.CopyTo(ms);
- _recParamMem = ms.ToArray();
- }
- var dectbinPtr = Marshal.UnsafeAddrOfPinnedArrayElement(_dectBinMem, 0);
- var dectparamPtr = Marshal.UnsafeAddrOfPinnedArrayElement(_dectParamMem, 0);
- var recbinPtr = Marshal.UnsafeAddrOfPinnedArrayElement(_recBinMem, 0);
- var recparamPtr = Marshal.UnsafeAddrOfPinnedArrayElement(_recParamMem, 0);
- IntPtr keyName = Marshal.StringToHGlobalAnsi("ppocr_keys_v1.txt");
- CreateNet(out _detect, out _rec, dectbinPtr, dectparamPtr, recbinPtr, recparamPtr, keyName);
- if (_detect != IntPtr.Zero)
- {
- LoadButton.IsEnabled = false;
- TestButton.IsEnabled = true;
- BenchmarkButton.IsEnabled = true;
- }
- }
- protected override void OnClosing(CancelEventArgs e)
- {
- if (_detect != IntPtr.Zero)
- {
- FreeNet(_detect, _rec);
- }
- base.OnClosing(e);
- }
- private unsafe void OnBenchmarkClick(object sender, RoutedEventArgs e)
- {
- var imageData = new byte[_image.PixelWidth * _image.PixelHeight * 4];
- _image.CopyPixels(imageData, _image.PixelWidth * 4, 0);
- var imgPtr = Marshal.UnsafeAddrOfPinnedArrayElement(imageData, 0);
- var boxes = new Box[100];
- var boxPtr = Marshal.UnsafeAddrOfPinnedArrayElement(boxes, 0);
- var start = Environment.TickCount;
- for (var i = 0; i < 100; i++)
- {
- StructStringInfo* ob = null;
- int count = 0;
- Test(_detect, _rec, imgPtr, _image.PixelWidth, _image.PixelHeight, ref ob, ref count);
- // 访问每个字符串结果
- string[] resultString = new string[count];
- for (int j = 0; j < count; j++)
- {
- resultString[i] = Marshal.PtrToStringUTF8(ob[i].result/*, ob[i].bufferSize*/);
- }
- }
- var total = Environment.TickCount - start;
- var seconds = total / 1000d;
- var fps = (int)(100 / seconds);
- MessageBox.Show($"FPS: {fps}");
- }
- }
- }
|