Avalonia跨平台入门第二十二篇之人脸检测

在前面分享的几篇中咱已经玩耍了PopupListBox多选Grid动态分RadioButton模板控件的拖放效果控件的置顶和置底控件的锁定自定义Window样式动画效果Expander控件ListBox折叠列表聊天窗口ListBox图片消息窗口抖动语音发送语音播放语音播放问题玩耍CEF;今晚接着研究在linux下怎么玩耍人脸检测;最终效果:

1、在虚拟机下把摄像头配置好:

38c9242eb2a163e98fe1405157496eba.png

2、直接用开源的项目中的VideoCapture:

https://github.com/emgucv/emgucv

3、直接参考前人的代码:

private void VideoCapture_ImageGrabbed(object ? sender, EventArgs e)
{
    Image < Bgr, Byte > imgeOrigenal = videoCapture.QueryFrame().ToImage < Bgr, byte > ();
    imgeOrigenal.ToBitmap();
    _cascadeClassifier = new CascadeClassifier("haarcascade_frontalface_alt2.xml");
    var grayframe = imgeOrigenal.Convert < Gray,
        byte > ();
    //the actual face detection happens here
    var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.1, 3, maxSize: new System.Drawing.Size(200, 200));
    foreach(var face in faces)
    {
        //the detected face(s) is highlighted here using a box that is drawn around it/them
        imgeOrigenal.Draw(face, new Bgr(System.Drawing.Color.Green), 3);
    }
    System.Drawing.Bitmap bitmap = imgeOrigenal.ToBitmap();
    Dispatcher.UIThread.InvokeAsync(new Action(() =>
    {
        Img.Source = ImageExtensions.ConvertToAvaloniaBitmap(bitmap);
    }));
}

4、依赖配置:

需要把libcvextern.so复制到输出目录

5、在Ubuntu出现问题:

7502c74a60e4bf33fbeb9ab8ea188bf4.png

6、使用ldd libcvextern.so查看系统缺少的依赖

a2c6f1475cca92f8767c7a776ed2edc8.png

7、使用sudo apt-get install去下载依赖环境,参考出处:

https://github.com/emgucv/emgucv/issues/524

8、使用到的指令

libOpenGL.so.0 => not found
1、sudo apt-get install libopengl0


libavcodec.so.58 => not found
2、sudo apt-get install libavcodec58


libavformat.so.58 => not found
3、sudo apt-get install libavformat58


libavutil.so.56 => not found
4、sudo apt-get install libavutil56


libswscale.so.5 => not found
5、sudo apt-get install libswscale5


libgeotiff.so.5 => not found
6、sudo apt-get install libgeotiff5


libdc1394.so.22 => not found
7、sudo apt-get install libdc1394-22-dev libdc1394-22 libdc1394-utils

最终简单的效果先这样吧3438fab3b586adb787bc771785561fc6.png;以后有时间的话,可以再去摸索一下更复杂的效果46a2564e3f2188324db34e230c7a1c84.png;编程不息、Bug不止、无Bug、无生活88a6413e3ab339793f5602166bbf84b4.png;改bug的冷静、编码的激情、完成后的喜悦、挖坑的激动 、填坑的兴奋;这也许就是屌丝程序员的乐趣吧;今天就到这里吧;希望自己有动力一步一步坚持下去;生命不息,代码不止;大家抽空可以看看今天分享的效果,有好的意见和想法,可以在留言板随意留言;我看到后会第一时间回复大家,多谢大家的一直默默的关注和支持!如果觉得不错,那就伸出您的小手点个赞并关注一下,多谢您的支持!

911e8277e9b1cdcf1d9e62e113ae8aea.png

这个问题通常是由于Emgu.CV库在加载C++库时发生的错误导致的。为了解决这个问题,你可以尝试以下步骤: 1.确保你已经按照Emgu.CV的安装指南进行了安装,并且已经添加了所需的引用。 2.在你的项目中添加以下NativeLibrary.SetDllImportResolver代码,该代码会解决安卓项目下无法找到C++库的问题。 ```csharp using System.Runtime.InteropServices; using Emgu.CV; using NativeLibrary = System.Runtime.InteropServices.NativeLibrary; public static class Bootstrapper { public static void Init() { NativeLibrary.SetDllImportResolver(typeof(CvInvoke).Assembly, DllImportResolver); } private static IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) { if (libraryName == "libgdiplus") { return LoadLibgdiplus(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "libgdiplus.so")); } return IntPtr.Zero; } [DllImport("libdl.so")] private static extern IntPtr dlopen(string filename, int flags); [DllImport("libdl.so")] private static extern IntPtr dlerror(); private static IntPtr LoadLibgdiplus(string path) { IntPtr lib = dlopen(path, RTLD_NOW); if (lib == IntPtr.Zero) { var error = Marshal.PtrToStringAnsi(dlerror()); throw new Exception($"Failed to load libgdiplus from '{path}'. {error}"); } return lib; } private const int RTLD_NOW = 2; } ``` 3.在你的Android项目的MainActivity.cs文件中添加以下代码,用于在应用程序启动时初始化Emgu.CV。 ```csharp protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Bootstrapper.Init(); // Initialize Emgu.CV // Other initialization code } ``` 通过这些步骤,你应该可以解决System.TypeInitializationException: The type initializer for 'Emgu.CV.CvInvoke' threw an exception问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值