Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ncnn出问题 #21

Open
tututu11 opened this issue Aug 28, 2022 · 2 comments
Open

ncnn出问题 #21

tututu11 opened this issue Aug 28, 2022 · 2 comments

Comments

@tututu11
Copy link

运行ncnn例子,无任何修改,出现这种情况,哪位大佬知道是什么问题
result

@chatop2020
Copy link

chatop2020 commented Sep 2, 2022

ncnn的c++ demo上,我感觉有些问题,我用.net 重写一下,效果还行,你也可以自己改改,主要代码如下:

 outImg = extractor.Extract("758");
                if (outImg != null)
                {
                    for (int h = 0; h < outImg.H; h++)
                    {
                        for (int w = 0; w < outImg.H; w++)
/*
####这里顺便问一下大佬,outImg.H 真的对吗?是不是 你c++代码里笔误了,应该是outImg.W?#####
####我试过outImg.W好像和outImg.H没什么区别######################################
####其实我后来想想,也无所谓,因为进来的图片都是长宽一样的,可能就是因为这个,所以才无所谓吧######
*/
                        {
                            //前景概率
                            int obj_score_index = (0 * outImg.H * outImg.W) + (h * outImg.W) + w;
                            float obj_score = outImg[obj_score_index];
                            //解析类别
                            int category = 0;
                            float max_score = 0.0f;
                            for (long i = 0; i < _namesDic.Count; i++)
                            {
                                obj_score_index = (int) ((5 + i) * outImg.H * outImg.W) + (h * outImg.W) + w;
                                float cls_score = outImg[obj_score_index];
                                if (cls_score > max_score)
                                {
                                    max_score = cls_score;
                                    category = (int) i;
                                }
                            }

                            float score = (float) (Math.Pow(max_score, 0.4) * Math.Pow(obj_score, 0.6));
                            // 解析坐标
                            int x_offset_index = (1 * outImg.H * outImg.W) + (h * outImg.W) + w;
                            int y_offset_index = (2 * outImg.H * outImg.W) + (h * outImg.W) + w;
                            int box_width_index = (3 * outImg.H * outImg.W) + (h * outImg.W) + w;
                            int box_height_index = (4 * outImg.H * outImg.W) + (h * outImg.W) + w;


                            float x_offset = Tanh(outImg[x_offset_index]);
                            float y_offset = Tanh(outImg[y_offset_index]);
                            float box_width = Sigmoid(outImg[box_width_index]);
                            float box_height = Sigmoid(outImg[box_height_index]);

                            float cx = (w + x_offset) / outImg.W;
                            float cy = (h + y_offset) / outImg.H;

                            int x1 = (int) ((cx - box_width * 0.5) * inputImg.Cols);
                            int y1 = (int) ((cy - box_height * 0.5) * inputImg.Rows);
                            int x2 = (int) ((cx + box_width * 0.5) * inputImg.Cols);
                            int y2 = (int) ((cy + box_height * 0.5) * inputImg.Rows);
                            var box = new BoundingBox();
                            box.prob = score;
                            box.h = (uint) (y2 - y1);
                            box.w = (uint) (x2 - x1);
                            box.x = (uint) x1;
                            box.y = (uint) y1;
                            box.obj_id = (uint) category;
                            if (score > threshold && x1 >= 0 && y1 >= 0 && x2 <= inputImg.Cols && y2 <= inputImg.Rows)
                            {
                                outBBoxEx.FilterBoundingBoxes.Add(box);
                            }
                            else
                            {
                                outBBoxEx.OriginalBoundingBoxes.Add(box);
                            }
                        }
                    }

                    if (outBBoxEx.FilterBoundingBoxes.Count > 0)
                    {
                        outBBoxEx.FilterBoundingBoxes.Sort((x, y) => -x.prob.CompareTo(y.prob));
                        List<picks> picked = new List<picks>();
                        for (int i = 0; i < outBBoxEx.FilterBoundingBoxes.Count; i++)
                        {
                            bool keep = true;
                            for (int j = 0; j < picked.Count; j++)
                            {
                                if (picked[j].Remove) //如果是被过滤掉的,就不执行后续操作
                                {
                                    continue;
                                }

                                var obj1 = outBBoxEx.FilterBoundingBoxes[i];
                                var obj2 = outBBoxEx.FilterBoundingBoxes[picked[j].Index];
                                var rect1 = new Rectangle((int) obj1.x, (int) obj1.y, (int) obj1.w, (int) obj1.h);
                                var rect2 = new Rectangle((int) obj2.x, (int) obj2.y, (int) obj2.w, (int) obj2.h);
                                var a = bbOverlap(rect1, rect2); //计算两个矩形的交集,如果大于65%的就过滤掉
                                if (a > 0.65f && obj1.obj_id == obj2.obj_id)
                                {
                                    keep = false;
                                    break;
                                }

                                if (rect2.IntersectsWith(rect1)) //计算两个矩形的并集,如果是包含关系,同时obj1的分数大于obj2,还是同一个类型的,就保留当前的obj1
                                {
                                    if (obj1.prob > obj2.prob && obj1.obj_id == obj2.obj_id)
                                    {
                                        picked[j].Remove = true;
                                        break;
                                    }

                                    keep = false;
                                    break;
                                }
                            }

                            if (keep)
                            {
                                var p = new picks();
                                p.Index = i;
                                p.Remove = false;
                                picked.Add(p);
                            }
                        }

                        for (int i = 0; i < picked.Count; i++)
                        {
                            if (picked[i].Remove)
                            {
                                continue;
                            }

                            var obj = outBBoxEx.FilterBoundingBoxes[picked[i].Index];
                            if (obj.x >= 0 && obj.y >= 0 && obj.w <= inputImg.Cols && obj.h <= inputImg.Rows)
                                outBBoxEx.FinishBoundingBoxes.Add(obj);
                        }
                        outBBoxEx.FilterBoundingBoxes.Clear();
                        outBBoxEx.FilterBoundingBoxes = outBBoxEx.FinishBoundingBoxes;

                        Bitmap img = new Bitmap(filePath);
                        outBBoxEx.FilePath = filePath;
                        outBBoxEx.IsImgType = img.RawFormat;
                        outBBoxEx.Height = inputImg.Rows;
                        outBBoxEx.Width = inputImg.Cols;
                        outBBoxEx.PixelFormat = img.PixelFormat;
                        outBBoxEx.Threshold = threshold;
                        outBBoxEx.DetectTime = DateTime.Now;
                        FileInfo fi = new FileInfo(filePath);
                        outBBoxEx.FileSize = (ulong) fi.Length;
                        if (createResultImage)
                        {
                            darwBox(outBBoxEx, img, filePath + ".jpg");
                            outBBoxEx.ResultPath = filePath + ".jpg";
                        }

                        outBBoxEx.ElapsedMsc = (long) (DateTime.Now - dt1).TotalMilliseconds;
                        return outBBoxEx;
                    }
                }

@clue3clue32
Copy link

你是在vs上部署的吗?问题在于带“阈值”两个字的注释,注释下一行的代码出问题了。我是删了注释或者后面加个空格解决可以解决。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants