ViewFaceCoreDemo icon indicating copy to clipboard operation
ViewFaceCoreDemo copied to clipboard

webapi问题

Open Lxiao911 opened this issue 3 years ago • 11 comments

麻烦问下,用webapi做成服务然后调用一切正常,但是连续多访问几次服务就会蹦掉,是我哪里出问题了么?

Lxiao911 avatar May 25 '21 14:05 Lxiao911

检查用到的模型是否都添加了, 检查代码是否有内存泄漏

View12138 avatar May 26 '21 00:05 View12138

都添加了,和ViewFaceTest中的代码一模一样,都引用ViewFaceCore项目, 发现不行,然后又通过nuget下载包,和ViewFaceTestPackage中的代码一样也是不行。代码都是复制粘贴的。但是新建一个控制台程序就可以,会不会是webapi调用有什么机制不一样呢?

Lxiao911 avatar May 26 '21 02:05 Lxiao911

可以参考这个项目:百度网盘 提取码:fvld

View12138 avatar May 26 '21 02:05 View12138

非常感谢您的回复!我先参考下

Lxiao911 avatar May 26 '21 02:05 Lxiao911

我刚刚试了下,我选择一张照片以后多点几次对比,服务一样会蹦掉呢

Lxiao911 avatar May 26 '21 03:05 Lxiao911

我刚刚试了下,我选择一张照片以后多点几次对比,服务一样会蹦掉呢

你好,我又测试了以下,刚刚的例子在频繁点击上传的时候确实会出现问题。 然后以下是改进(刚刚测试没有问题了)。可以参考一下:

[Route("api/[controller]/[action]")]
[ApiController]
public class ViewFaceController : ControllerBase
{
    private readonly static object locker = new object();

    private static ViewFace viewFace;

    static ViewFaceController()
    {
        string modelPath = Path.Combine(AppContext.BaseDirectory, @"model/");
        viewFace = new ViewFace(modelPath);
        GC.SuppressFinalize(viewFace);
    }

    [HttpPost]
    public IActionResult UploadSmallFile(IFormFile file)
    {
        if (file == null)
        {
            return new Result("未接收到上传的文件", -1001);
        }
        
        lock (locker)
        {
            using MemoryStream ms = new MemoryStream();
            file.CopyTo(ms);
            using Bitmap bitmap = new Bitmap(ms);
            var res = viewFace.FaceDetector(bitmap);
            var message = res.Length > 0 ? "人脸识别成功" : "未检测到人脸";
            return new Result<FaceInfo[]>(res, message);
        }
    }
}

View12138 avatar May 26 '21 03:05 View12138

刚刚测试了下没问题了。我再研究研究,感谢大佬,向大佬致敬!

Lxiao911 avatar May 26 '21 03:05 Lxiao911

在请教一个问题,就是除了检查人脸信息以外,我还加了活体检测,认证对比,多点几次还是一样会蹦掉,是不是还需要在哪里释放内存?

Lxiao911 avatar May 26 '21 04:05 Lxiao911

这个我也不太清楚了, 估计是 ViewFace.dll 里还有一些错误

View12138 avatar May 26 '21 06:05 View12138

viewface.dll可能没什么问题,因为用.net core 的控制台程序都不会报错,只是webapi会出现这个问题。还有就是我又重新再之前给的代码里加了个循环,一样会蹦服务 for (int a = 0; a < 160; a++) { using MemoryStream ms = new MemoryStream(); file.CopyTo(ms); using Bitmap bitmap = new Bitmap(ms); var res = viewFace.FaceDetector(bitmap); var message = res.Length > 0 ? "人脸识别成功" : "未检测到人脸"; Console.WriteLine($"第{a} 次"); }

Lxiao911 avatar May 26 '21 06:05 Lxiao911

viewface.dll可能没什么问题,因为用.net core 的控制台程序都不会报错,只是webapi会出现这个问题。还有就是我又重新再之前给的代码里加了个循环,一样会蹦服务 for (int a = 0; a < 160; a++) { using MemoryStream ms = new MemoryStream(); file.CopyTo(ms); using Bitmap bitmap = new Bitmap(ms); var res = viewFace.FaceDetector(bitmap); var message = res.Length > 0 ? "人脸识别成功" : "未检测到人脸"; Console.WriteLine($"第{a} 次"); }

目测是viewFace.FaceDetector调用的非托管资源没有回收,GC应该没用, 请问最后你解决了吗?

robotlulu avatar Jun 09 '21 08:06 robotlulu