微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin IOS UI 线程由于 CPU 密集型方法而被阻塞,尝试了 Await/Async/Threading/Tasks 没有解决它 见下面的代码

如何解决Xamarin IOS UI 线程由于 CPU 密集型方法而被阻塞,尝试了 Await/Async/Threading/Tasks 没有解决它 见下面的代码

负责从预测中检索关键点的 GetJoints 方法会阻止/影响 UI。这可能在应用程序运行后 3.4 分钟发生,因此在启动时不会引起注意。如果我删除 GetJoints 方法,则 UI 很好并且不存在块/滞后。我只在 UI 上使用简单的动画,所以没有什么密集的,但我们可以看到它们开始被阻止。

https://github.com/tyronelinton/samplygame/blob/main/AppDelegate.cs

//方法

public static List<Joint> GetJoints(LightBuzz5Output prediction,int sourceWidth,int       sourceHeight)
    {
        var heatmaps = prediction.Final_heatmaps_0;
        var keypointCount = (int)heatmaps.Shape[2] - 1;
        var heatmapWidth = (int)heatmaps.Shape[3];
        var heatmapHeight = (int)heatmaps.Shape[4];

        List<Joint> points = new List<Joint>();

        for (int i = 0; i < keypointCount; i++)
        {
            int positionX = 0;
            int positionY = 0;

            float confidence = 0.0f;

            for (int x = 0; x < heatmapWidth; x++)
            {
                for (int y = 0; y < heatmapHeight; y++)
                {
                    int index = y + heatmapHeight * (x + heatmapWidth * i);

                    if (heatmaps[index].FloatValue > confidence)
                    {
                        confidence = heatmaps[index].FloatValue;

                        positionX = x;
                        positionY = y;
                    }
                }
            }

            points.Add(new Joint
            {
                X = (float)sourceWidth * (float)positionY / (float)heatmapHeight,Y = (float)sourceHeight * (float)positionX / (float)heatmapWidth,Confidence = confidence
            });
        }

        return points;
    }

//用法: 私有无效进程(UIImage 图像) { UIImage resizedImage = image.Scale(new CGSize(256,256));

            CVPixelBuffer buffer = Utilities.GetBuffer(resizedImage.CGImage);

            NSError error;
            var prediction = model.GetPrediction(buffer,out error);

            List<Joint> joints = null;

            if (error != null)
            {
                 System.Diagnostics.Debug.WriteLine(error);
            }
            else
            {
                joints = Utilities.GetJoints(prediction,width,height);
            }            
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。