当前位置:   article > 正文

C# PaddleOCR标注工具_sdcb.paddleocr

sdcb.paddleocr

目录

介绍

效果

后台服务参考

下载

代码


介绍

基于以下开源项目改造的

https://gitee.com/BaoJianQiang/FastOCRLabel

一款基于PaddleOCR设计的半自动标注平台,可以运行在window系统上 

效果

可结合后台服务实现自动标注

后台服务参考

Onnx版
https://lw112190.blog.csdn.net/article/details/132082357

Sdcb.PaddleInference版
https://lw112190.blog.csdn.net/article/details/131847899

PaddleOCRSharp版
https://lw112190.blog.csdn.net/article/details/129127930

Sdcb.OpenVINO版
https://lw112190.blog.csdn.net/article/details/133784164

下载

可执行程序exe下载

源码下载

代码

标签绘制代码

private void picPreview_MouseDoubleClick(object sender, MouseEventArgs e)
{
    if (shapeOnMouseDown != null)
    {

        TreeNode node = null;
        foreach (TreeNode item in tvResults.Nodes)
        {
            if (item.FullPath == shapeOnMouseDown.ToString())
            {
                node = item;
                break;
            }
        }

        if (node == null)
        {
            return;
        }

        tvResults.SelectedNode = node;

        String originalText = shapeOnMouseDown.text;
        String originalKey = shapeOnMouseDown.key;

        if (originalText==null)
        {
            originalText = node.FirstNode.Text.Split('|')[1];
            originalKey = node.FirstNode.Text.Split('|')[0];
        }

        InputForm inputForm = new InputForm();
        inputForm.form5 = this;
        inputForm.points = shapeOnMouseDown.points;
        inputForm.StartPosition = FormStartPosition.CenterParent;
        inputForm.originalText = originalText;
        inputForm.originalKey = originalKey;

        if (inputForm.ShowDialog() == DialogResult.OK)
        {
            if (originalText == inputForm.outputText && originalKey == inputForm.outputKey)
                return;

            if (node.Parent != null)
            {
                node.Text = inputForm.outputKey + "|" + inputForm.outputText;
            }
            else
            {
                if (node.Nodes.Count == 0)
                {
                    node.Nodes.Add(new TreeNode(inputForm.outputKey + "|" + inputForm.outputText));
                }
                else
                {
                    node.Nodes[0].Text = inputForm.outputKey + "|" + inputForm.outputText;
                }
            }

            PaddleOCRForPoly poly = node.Tag as PaddleOCRForPoly;

            foreach (var shape in canvas.Shapes)
            {
                if (poly.points[0].X == shape.points[0].X && poly.points[0].Y == shape.points[0].Y
                 && poly.points[1].X == shape.points[1].X && poly.points[1].Y == shape.points[1].Y
                 && poly.points[2].X == shape.points[2].X && poly.points[2].Y == shape.points[2].Y
                 && poly.points[3].X == shape.points[3].X && poly.points[3].Y == shape.points[3].Y)
                {
                    shape.text = inputForm.outputText;
                    shape.key = inputForm.outputKey;
                    break;
                }
            }

            commandList.Add(new Command()
            {
                type = CommandEnum.ModifyText,
                oldPoly = new PaddleOCRForPoly()
                {
                    text = inputForm.originalText,
                    key = inputForm.originalKey,
                    points = new List<PointF>()
                    {
                        new PointF(poly.points[0].X, poly.points[0].Y),
                        new PointF(poly.points[1].X, poly.points[1].Y),
                        new PointF(poly.points[2].X, poly.points[2].Y),
                        new PointF(poly.points[3].X, poly.points[3].Y),
                    }
                }
            });

            //tvResults 更新

        }
    }
}

private void picPreview_MouseDown(object sender, MouseEventArgs e)
{
    if (currentImage != null)
    {
        float safeWidth = currentImage.Width * fScale;
        float safeHeight = currentImage.Height * fScale;

        if (e.X > safeWidth || e.Y > safeHeight)
            return;

        //四点标注
        if (btnFourPoints.Tag.ToString() == "0")
        {
            blnDrawFourPoints = true;

            allPointList4.Add(new CircleEntity()
            {
                x = Convert.ToInt32(e.X / fScale),
                y = Convert.ToInt32(e.Y / fScale),
                r = 5
            });

            if (allPointList4.Count == 4)
            {
                var polyEntity = new PaddleOCRForPoly()
                {
                    isNew = true,
                    points = new List<PointF>()
                };

                foreach (var item in allPointList4)
                {
                    var entity = new PointF()
                    {
                        X = item.x,
                        Y = item.y,
                    };

                    polyEntity.points.Add(entity);
                }

                canvas.Shapes.Add(new Shape()
                {
                    isNew = true,
                    points = polyEntity.points
                });

                TreeNode node = new TreeNode(polyEntity.ToString());
                node.Tag = polyEntity;
                tvResults.Nodes.Add(node);
                tvResults.SelectedNode = node;

                allPointList4.Clear();

                //关闭标注状态
                btnFourPoints.Tag = "1";
                btnRectLabel.Tag = "1";
                btnRectLabel.Text = "启动矩形标注(W)";
                btnFourPoints.Text = "启动四点标注(Q)";

                InputForm inputForm = new InputForm();
                inputForm.StartPosition = FormStartPosition.CenterParent;
                inputForm.form5 = this;
                inputForm.points = polyEntity.points;
                inputForm.originalText = "待标注";
                if (inputForm.ShowDialog() == DialogResult.OK)
                {
                    node.Nodes.Add(new TreeNode(inputForm.outputKey + "|" + inputForm.outputText));
                    tvResults.SelectedNode.Expand();
                }


                commandList.Add(new Command()
                {
                    type = CommandEnum.Add,
                    newPoly = new PaddleOCRForPoly()
                    {
                        text = inputForm.outputText,
                        key = inputForm.outputKey,
                        points = new List<PointF>()
                        {
                            new PointF(polyEntity.points[0].X, polyEntity.points[0].Y),
                            new PointF(polyEntity.points[1].X, polyEntity.points[1].Y),
                            new PointF(polyEntity.points[2].X, polyEntity.points[2].Y),
                            new PointF(polyEntity.points[3].X, polyEntity.points[3].Y),
                        }
                    }
                });
            }
        }//矩形标注
        else if (btnRectLabel.Tag.ToString() == "0")
        {
            blnDraw = true;
        }
        else
        {
            int x = Convert.ToInt32(e.X / fScale);
            int y = Convert.ToInt32(e.Y / fScale);

            if (e.Button == System.Windows.Forms.MouseButtons.Left)
                markOnMouseDown = canvas.GetMark(x, y, fScale);
            if (markOnMouseDown != null)
            {
                int duijiao1Pos = (markOnMouseDown.Pos + 2) % 4;
                PointF point = markOnMouseDown.Shape.points[duijiao1Pos];
                duijiaoPoint = new PointF(point.X, point.Y);


                originalPointList = new List<PointF>()
                {
                    new PointF(markOnMouseDown.Shape.points[0].X, markOnMouseDown.Shape.points[0].Y),
                    new PointF(markOnMouseDown.Shape.points[1].X, markOnMouseDown.Shape.points[1].Y),
                    new PointF(markOnMouseDown.Shape.points[2].X, markOnMouseDown.Shape.points[2].Y),
                    new PointF(markOnMouseDown.Shape.points[3].X, markOnMouseDown.Shape.points[3].Y)
                };

                return;
            }

            shapeOnMouseDown = canvas.GetShape(x, y);

            canvas.ClearSelection();

            if (shapeOnMouseDown != null)
            {
                originalPointList = new List<PointF>()
                {
                    new PointF(shapeOnMouseDown.points[0].X, shapeOnMouseDown.points[0].Y),
                    new PointF(shapeOnMouseDown.points[1].X, shapeOnMouseDown.points[1].Y),
                    new PointF(shapeOnMouseDown.points[2].X, shapeOnMouseDown.points[2].Y),
                    new PointF(shapeOnMouseDown.points[3].X, shapeOnMouseDown.points[3].Y)
                };

                pointInShapeOnMouseDown = new Point(
                    Convert.ToInt32(e.Location.X - shapeOnMouseDown.points[0].X * fScale),
                    Convert.ToInt32(e.Location.Y - shapeOnMouseDown.points[0].Y * fScale));
                shapeOnMouseDown.Selected = true;

                foreach (TreeNode node in tvResults.Nodes)
                {
                    var poly = node.Tag as PaddleOCRForPoly;

                    if (shapeOnMouseDown.points[0].X == poly.points[0].X && shapeOnMouseDown.points[0].Y == poly.points[0].Y
            && shapeOnMouseDown.points[1].X == poly.points[1].X && shapeOnMouseDown.points[1].Y == poly.points[1].Y
            && shapeOnMouseDown.points[2].X == poly.points[2].X && shapeOnMouseDown.points[2].Y == poly.points[2].Y
            && shapeOnMouseDown.points[3].X == poly.points[3].X && shapeOnMouseDown.points[3].Y == poly.points[3].Y
            )
                    {
                        tvResults.SelectedNode = node;
                        break;
                    }
                }
            }

            picPreview.Invalidate();
        }

        start = e.Location;
    }
}

private void picPreview_MouseMove(object sender, MouseEventArgs e)
{
    if (currentImage != null)
    {
        float safeWidth = currentImage.Width * fScale;
        float safeHeight = currentImage.Height * fScale;

        Point tempEndPoint = e.Location; //记录框的位置和大小
        if (e.X > safeWidth)
        {
            tempEndPoint.X = Convert.ToInt32(safeWidth);
        }

        if (e.Y > safeHeight)
        {
            tempEndPoint.Y = Convert.ToInt32(safeHeight);
        }

        if (btnRectLabel.Tag.ToString() == "0" && blnDraw)
        {
            if (e.Button != MouseButtons.Left)
                return;

            newRect = new Rectangle();
            newRect.Location = new Point(
                Math.Min(start.X, tempEndPoint.X),
                Math.Min(start.Y, tempEndPoint.Y));
            newRect.Size = new Size(
                Math.Abs(start.X - tempEndPoint.X),
                Math.Abs(start.Y - tempEndPoint.Y));
        }
        else if (btnFourPoints.Tag.ToString() == "0" && blnDrawFourPoints)
        {
            newRect3 = new Rectangle();
            newRect3.Location = new Point(Convert.ToInt32(e.X / fScale), Convert.ToInt32(e.Y / fScale));
            newRect3.Size = new Size(1, 1);
        }
        else
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (markOnMouseDown != null)
                {
                    int x = Convert.ToInt32(e.X / fScale);
                    int y = Convert.ToInt32(e.Y / fScale);

                    markOnMouseDown.MoveTo(x, y, duijiaoPoint);

                    picPreview.Invalidate();
                    return;
                }
                else if (shapeOnMouseDown != null)
                {
                    int x = Convert.ToInt32((e.X - pointInShapeOnMouseDown.X) / fScale);
                    int y = Convert.ToInt32((e.Y - pointInShapeOnMouseDown.Y) / fScale);
                    shapeOnMouseDown.MoveTo(new Point(x, y));
                    this.Cursor = Cursors.SizeAll;

                }

            }
        }

        picPreview.Invalidate();

        int x1 = Convert.ToInt32(e.X / fScale);
        int y1 = Convert.ToInt32(e.Y / fScale);

        Mark m = canvas.GetMark(x1, y1, fScale);
        if (m != null)
            this.Cursor = m.Cursor;
        else
            this.Cursor = Cursors.Default;
    }
}

private void picPreview_MouseUp(object sender, MouseEventArgs e)
{
    blnDraw = false;


    if (picPreview.Image == null)
        return;

    if (picPreview.Image != null && btnRectLabel.Tag.ToString() == "0")
    {
        if (newRect != null && newRect.Width > 0 && newRect.Height > 0)
        {
            int left = Convert.ToInt32(newRect.X / fScale);
            int top = Convert.ToInt32(newRect.Y / fScale);
            int width = Convert.ToInt32(newRect.Width / fScale);
            int height = Convert.ToInt32(newRect.Height / fScale);

            var poly = new PaddleOCRForPoly()
            {
                isNew = true,
                points = new List<PointF>()
                        {
                            new PointF(){X = left, Y = top},
                            new PointF(){X = left + width, Y = top},
                            new PointF(){X = left + width, Y = top + height},
                            new PointF(){X = left, Y = top + height},
                }
            };


            TreeNode node = new TreeNode(poly.ToString());
            node.Tag = poly;
            tvResults.Nodes.Add(node);
            tvResults.SelectedNode = node;

            canvas.Shapes.Add(new Shape()
            {
                isNew = true,
                points = poly.points
            });

            newRect = new Rectangle();

            //关闭标注状态
            btnFourPoints.Tag = "1";
            btnRectLabel.Tag = "1";
            btnRectLabel.Text = "启动矩形标注(W)";
            btnFourPoints.Text = "启动四点标注(Q)";

            InputForm inputForm = new InputForm();
            inputForm.StartPosition = FormStartPosition.CenterParent;
            inputForm.form5 = this;
            inputForm.points = poly.points;
            inputForm.originalText = "待标注";
            if (inputForm.ShowDialog() == DialogResult.OK)
            {
                //node.Nodes.Add(new TreeNode(inputForm.outputText));
                node.Nodes.Add(new TreeNode(inputForm.outputKey + "|" + inputForm.outputText));
                tvResults.SelectedNode.Expand();
            }

            canvas.Shapes[canvas.Shapes.Count - 1].text = inputForm.outputText;
            //add by lxw 20230628
            canvas.Shapes[canvas.Shapes.Count - 1].key = inputForm.outputKey;

            commandList.Add(new Command()
            {
                type = CommandEnum.Add,
                newPoly = new PaddleOCRForPoly()
                {
                    text = inputForm.outputText,
                    key = inputForm.outputKey,
                    points = new List<PointF>()
                    {
                        new PointF(poly.points[0].X, poly.points[0].Y),
                        new PointF(poly.points[1].X, poly.points[1].Y),
                        new PointF(poly.points[2].X, poly.points[2].Y),
                        new PointF(poly.points[3].X, poly.points[3].Y),
                    }
                }
            });
        }
    }
    else if (shapeOnMouseDown != null)
    {
        if (originalPointList[0].X == shapeOnMouseDown.points[0].X && originalPointList[0].Y == shapeOnMouseDown.points[0].Y
            && originalPointList[1].X == shapeOnMouseDown.points[1].X && originalPointList[1].Y == shapeOnMouseDown.points[1].Y
            && originalPointList[2].X == shapeOnMouseDown.points[2].X && originalPointList[2].Y == shapeOnMouseDown.points[2].Y
            && originalPointList[3].X == shapeOnMouseDown.points[3].X && originalPointList[3].Y == shapeOnMouseDown.points[3].Y
            )
            return;

        this.Cursor = Cursors.Default;

        //更新坐标
        tvResults.SelectedNode.Text = shapeOnMouseDown.ToString();

        var poly = tvResults.SelectedNode.Tag as PaddleOCRForPoly;
        poly.points = new List<PointF>() {
                        new PointF(shapeOnMouseDown.points[0].X, shapeOnMouseDown.points[0].Y),
                        new PointF(shapeOnMouseDown.points[1].X, shapeOnMouseDown.points[1].Y),
                        new PointF(shapeOnMouseDown.points[2].X, shapeOnMouseDown.points[2].Y),
                        new PointF(shapeOnMouseDown.points[3].X, shapeOnMouseDown.points[3].Y),
                    };
        tvResults.SelectedNode.Tag = poly;

        commandList.Add(new Command()
        {
            type = CommandEnum.Move,
            oldPoly = new PaddleOCRForPoly()
            {
                points = new List<PointF>()
                    {
                        new PointF(originalPointList[0].X, originalPointList[0].Y),
                        new PointF(originalPointList[1].X, originalPointList[1].Y),
                        new PointF(originalPointList[2].X, originalPointList[2].Y),
                        new PointF(originalPointList[3].X, originalPointList[3].Y),
                    }
            },
            newPoly = new PaddleOCRForPoly()
            {
                points = new List<PointF>()
                    {
                        new PointF(poly.points[0].X, poly.points[0].Y),
                        new PointF(poly.points[1].X, poly.points[1].Y),
                        new PointF(poly.points[2].X, poly.points[2].Y),
                        new PointF(poly.points[3].X, poly.points[3].Y),
                    }
            }
        });

        //shapeOnMouseDown = null;
        //originalPointList = null;
    }


}

private void picPreview_Paint(object sender, PaintEventArgs e)
{
    if (currentImage != null)
    {
        int safeWidth = Convert.ToInt32(currentImage.Width * fScale);
        int safeHeight = Convert.ToInt32(currentImage.Height * fScale);

        Pen pen = new Pen(Color.Gray, 1);
        pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
        pen.DashPattern = new float[] { 5, 5 };
        e.Graphics.DrawRectangle(pen, 0, 0, safeWidth, safeHeight);
    }

    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
    e.Graphics.SmoothingMode = SmoothingMode.HighQuality;

    Color color = Color.Blue;

    foreach (Shape shape in canvas.Shapes)
    {
        if (shape.key != null && Singleton.Instance().ColorDic.Keys.Contains(shape.key))
        {
            color = Singleton.Instance().ColorDic[shape.key];
        }
        else
        {
            color = Color.Blue;
        }

        //选中的Shape 红色标注
        if (shape.Selected)
        {
            color = Color.Red;
            foreach (Mark m in shape.Marks)
            {
                Rectangle rect = new Rectangle(
                   Convert.ToInt32(shape.points[m.Pos].X * fScale - m.MarkSize / 2),
                   Convert.ToInt32(shape.points[m.Pos].Y * fScale - m.MarkSize / 2),
                   m.MarkSize, m.MarkSize);

                e.Graphics.FillEllipse(Brushes.Aqua, rect);
                e.Graphics.DrawEllipse(Pens.Blue, rect);
            }
        }
        else
        {//新的Shape 橘色标注
            if (shape.isNew)
            {
                //color = Color.Orange;
            }
        }

        PointF p1 = new PointF(shape.points[0].X * fScale, shape.points[0].Y * fScale);
        PointF p2 = new PointF(shape.points[1].X * fScale, shape.points[1].Y * fScale);
        PointF p3 = new PointF(shape.points[2].X * fScale, shape.points[2].Y * fScale);
        PointF p4 = new PointF(shape.points[3].X * fScale, shape.points[3].Y * fScale);

        e.Graphics.DrawPolygon(new Pen(color, 2), new PointF[4] { p1, p2, p3, p4 });
    }

    if (blnDraw)
    {
        if (newRect != null && newRect.Width > 0 && newRect.Height > 0)
        {
            e.Graphics.DrawRectangle(new Pen(Color.Orange, 2), newRect);
        }
    }

    if (blnDrawFourPoints && newRect3 != null)
    {
        int new_x = Convert.ToInt32(newRect3.X * fScale);
        int new_y = Convert.ToInt32(newRect3.Y * fScale);

        if (allPointList4.Count == 1)
        {
            Pen pen = new Pen(Color.Orange, 2);
            pen.DashStyle = DashStyle.Dash;

            int p0_x = Convert.ToInt32(allPointList4[0].x * fScale);
            int p0_y = Convert.ToInt32(allPointList4[0].y * fScale);
            e.Graphics.DrawLine(pen, new_x, new_y, p0_x, p0_y);
        }
        else if (allPointList4.Count == 2)
        {
            Pen pen = new Pen(Color.Orange, 2);
            pen.DashStyle = DashStyle.Dash;

            int p0_x = Convert.ToInt32(allPointList4[0].x * fScale);
            int p0_y = Convert.ToInt32(allPointList4[0].y * fScale);
            int p1_x = Convert.ToInt32(allPointList4[1].x * fScale);
            int p1_y = Convert.ToInt32(allPointList4[1].y * fScale);
            e.Graphics.DrawLine(pen, p0_x, p0_y, p1_x, p1_y);
            e.Graphics.DrawLine(pen, new_x, new_y, p1_x, p1_y);
        }
        else if (allPointList4.Count == 3)
        {
            int p0_x = Convert.ToInt32(allPointList4[0].x * fScale);
            int p0_y = Convert.ToInt32(allPointList4[0].y * fScale);
            int p1_x = Convert.ToInt32(allPointList4[1].x * fScale);
            int p1_y = Convert.ToInt32(allPointList4[1].y * fScale);
            int p2_x = Convert.ToInt32(allPointList4[2].x * fScale);
            int p2_y = Convert.ToInt32(allPointList4[2].y * fScale);

            Pen pen = new Pen(Color.Orange, 2);
            pen.DashStyle = DashStyle.Dash;
            e.Graphics.DrawLine(pen, p0_x, p0_y, p1_x, p1_y);
            e.Graphics.DrawLine(pen, p1_x, p1_y, p2_x, p2_y);
            e.Graphics.DrawLine(pen, new_x, new_y, p0_x, p0_y);
            e.Graphics.DrawLine(pen, new_x, new_y, p2_x, p2_y);
        }
    }
}

  1. private void picPreview_MouseDoubleClick(object sender, MouseEventArgs e)
  2. {
  3. if (shapeOnMouseDown != null)
  4. {
  5. TreeNode node = null;
  6. foreach (TreeNode item in tvResults.Nodes)
  7. {
  8. if (item.FullPath == shapeOnMouseDown.ToString())
  9. {
  10. node = item;
  11. break;
  12. }
  13. }
  14. if (node == null)
  15. {
  16. return;
  17. }
  18. tvResults.SelectedNode = node;
  19. String originalText = shapeOnMouseDown.text;
  20. String originalKey = shapeOnMouseDown.key;
  21. if (originalText==null)
  22. {
  23. originalText = node.FirstNode.Text.Split('|')[1];
  24. originalKey = node.FirstNode.Text.Split('|')[0];
  25. }
  26. InputForm inputForm = new InputForm();
  27. inputForm.form5 = this;
  28. inputForm.points = shapeOnMouseDown.points;
  29. inputForm.StartPosition = FormStartPosition.CenterParent;
  30. inputForm.originalText = originalText;
  31. inputForm.originalKey = originalKey;
  32. if (inputForm.ShowDialog() == DialogResult.OK)
  33. {
  34. if (originalText == inputForm.outputText && originalKey == inputForm.outputKey)
  35. return;
  36. if (node.Parent != null)
  37. {
  38. node.Text = inputForm.outputKey + "|" + inputForm.outputText;
  39. }
  40. else
  41. {
  42. if (node.Nodes.Count == 0)
  43. {
  44. node.Nodes.Add(new TreeNode(inputForm.outputKey + "|" + inputForm.outputText));
  45. }
  46. else
  47. {
  48. node.Nodes[0].Text = inputForm.outputKey + "|" + inputForm.outputText;
  49. }
  50. }
  51. PaddleOCRForPoly poly = node.Tag as PaddleOCRForPoly;
  52. foreach (var shape in canvas.Shapes)
  53. {
  54. if (poly.points[0].X == shape.points[0].X && poly.points[0].Y == shape.points[0].Y
  55. && poly.points[1].X == shape.points[1].X && poly.points[1].Y == shape.points[1].Y
  56. && poly.points[2].X == shape.points[2].X && poly.points[2].Y == shape.points[2].Y
  57. && poly.points[3].X == shape.points[3].X && poly.points[3].Y == shape.points[3].Y)
  58. {
  59. shape.text = inputForm.outputText;
  60. shape.key = inputForm.outputKey;
  61. break;
  62. }
  63. }
  64. commandList.Add(new Command()
  65. {
  66. type = CommandEnum.ModifyText,
  67. oldPoly = new PaddleOCRForPoly()
  68. {
  69. text = inputForm.originalText,
  70. key = inputForm.originalKey,
  71. points = new List<PointF>()
  72. {
  73. new PointF(poly.points[0].X, poly.points[0].Y),
  74. new PointF(poly.points[1].X, poly.points[1].Y),
  75. new PointF(poly.points[2].X, poly.points[2].Y),
  76. new PointF(poly.points[3].X, poly.points[3].Y),
  77. }
  78. }
  79. });
  80. //tvResults 更新
  81. }
  82. }
  83. }
  84. private void picPreview_MouseDown(object sender, MouseEventArgs e)
  85. {
  86. if (currentImage != null)
  87. {
  88. float safeWidth = currentImage.Width * fScale;
  89. float safeHeight = currentImage.Height * fScale;
  90. if (e.X > safeWidth || e.Y > safeHeight)
  91. return;
  92. //四点标注
  93. if (btnFourPoints.Tag.ToString() == "0")
  94. {
  95. blnDrawFourPoints = true;
  96. allPointList4.Add(new CircleEntity()
  97. {
  98. x = Convert.ToInt32(e.X / fScale),
  99. y = Convert.ToInt32(e.Y / fScale),
  100. r = 5
  101. });
  102. if (allPointList4.Count == 4)
  103. {
  104. var polyEntity = new PaddleOCRForPoly()
  105. {
  106. isNew = true,
  107. points = new List<PointF>()
  108. };
  109. foreach (var item in allPointList4)
  110. {
  111. var entity = new PointF()
  112. {
  113. X = item.x,
  114. Y = item.y,
  115. };
  116. polyEntity.points.Add(entity);
  117. }
  118. canvas.Shapes.Add(new Shape()
  119. {
  120. isNew = true,
  121. points = polyEntity.points
  122. });
  123. TreeNode node = new TreeNode(polyEntity.ToString());
  124. node.Tag = polyEntity;
  125. tvResults.Nodes.Add(node);
  126. tvResults.SelectedNode = node;
  127. allPointList4.Clear();
  128. //关闭标注状态
  129. btnFourPoints.Tag = "1";
  130. btnRectLabel.Tag = "1";
  131. btnRectLabel.Text = "启动矩形标注(W)";
  132. btnFourPoints.Text = "启动四点标注(Q)";
  133. InputForm inputForm = new InputForm();
  134. inputForm.StartPosition = FormStartPosition.CenterParent;
  135. inputForm.form5 = this;
  136. inputForm.points = polyEntity.points;
  137. inputForm.originalText = "待标注";
  138. if (inputForm.ShowDialog() == DialogResult.OK)
  139. {
  140. node.Nodes.Add(new TreeNode(inputForm.outputKey + "|" + inputForm.outputText));
  141. tvResults.SelectedNode.Expand();
  142. }
  143. commandList.Add(new Command()
  144. {
  145. type = CommandEnum.Add,
  146. newPoly = new PaddleOCRForPoly()
  147. {
  148. text = inputForm.outputText,
  149. key = inputForm.outputKey,
  150. points = new List<PointF>()
  151. {
  152. new PointF(polyEntity.points[0].X, polyEntity.points[0].Y),
  153. new PointF(polyEntity.points[1].X, polyEntity.points[1].Y),
  154. new PointF(polyEntity.points[2].X, polyEntity.points[2].Y),
  155. new PointF(polyEntity.points[3].X, polyEntity.points[3].Y),
  156. }
  157. }
  158. });
  159. }
  160. }//矩形标注
  161. else if (btnRectLabel.Tag.ToString() == "0")
  162. {
  163. blnDraw = true;
  164. }
  165. else
  166. {
  167. int x = Convert.ToInt32(e.X / fScale);
  168. int y = Convert.ToInt32(e.Y / fScale);
  169. if (e.Button == System.Windows.Forms.MouseButtons.Left)
  170. markOnMouseDown = canvas.GetMark(x, y, fScale);
  171. if (markOnMouseDown != null)
  172. {
  173. int duijiao1Pos = (markOnMouseDown.Pos + 2) % 4;
  174. PointF point = markOnMouseDown.Shape.points[duijiao1Pos];
  175. duijiaoPoint = new PointF(point.X, point.Y);
  176. originalPointList = new List<PointF>()
  177. {
  178. new PointF(markOnMouseDown.Shape.points[0].X, markOnMouseDown.Shape.points[0].Y),
  179. new PointF(markOnMouseDown.Shape.points[1].X, markOnMouseDown.Shape.points[1].Y),
  180. new PointF(markOnMouseDown.Shape.points[2].X, markOnMouseDown.Shape.points[2].Y),
  181. new PointF(markOnMouseDown.Shape.points[3].X, markOnMouseDown.Shape.points[3].Y)
  182. };
  183. return;
  184. }
  185. shapeOnMouseDown = canvas.GetShape(x, y);
  186. canvas.ClearSelection();
  187. if (shapeOnMouseDown != null)
  188. {
  189. originalPointList = new List<PointF>()
  190. {
  191. new PointF(shapeOnMouseDown.points[0].X, shapeOnMouseDown.points[0].Y),
  192. new PointF(shapeOnMouseDown.points[1].X, shapeOnMouseDown.points[1].Y),
  193. new PointF(shapeOnMouseDown.points[2].X, shapeOnMouseDown.points[2].Y),
  194. new PointF(shapeOnMouseDown.points[3].X, shapeOnMouseDown.points[3].Y)
  195. };
  196. pointInShapeOnMouseDown = new Point(
  197. Convert.ToInt32(e.Location.X - shapeOnMouseDown.points[0].X * fScale),
  198. Convert.ToInt32(e.Location.Y - shapeOnMouseDown.points[0].Y * fScale));
  199. shapeOnMouseDown.Selected = true;
  200. foreach (TreeNode node in tvResults.Nodes)
  201. {
  202. var poly = node.Tag as PaddleOCRForPoly;
  203. if (shapeOnMouseDown.points[0].X == poly.points[0].X && shapeOnMouseDown.points[0].Y == poly.points[0].Y
  204. && shapeOnMouseDown.points[1].X == poly.points[1].X && shapeOnMouseDown.points[1].Y == poly.points[1].Y
  205. && shapeOnMouseDown.points[2].X == poly.points[2].X && shapeOnMouseDown.points[2].Y == poly.points[2].Y
  206. && shapeOnMouseDown.points[3].X == poly.points[3].X && shapeOnMouseDown.points[3].Y == poly.points[3].Y
  207. )
  208. {
  209. tvResults.SelectedNode = node;
  210. break;
  211. }
  212. }
  213. }
  214. picPreview.Invalidate();
  215. }
  216. start = e.Location;
  217. }
  218. }
  219. private void picPreview_MouseMove(object sender, MouseEventArgs e)
  220. {
  221. if (currentImage != null)
  222. {
  223. float safeWidth = currentImage.Width * fScale;
  224. float safeHeight = currentImage.Height * fScale;
  225. Point tempEndPoint = e.Location; //记录框的位置和大小
  226. if (e.X > safeWidth)
  227. {
  228. tempEndPoint.X = Convert.ToInt32(safeWidth);
  229. }
  230. if (e.Y > safeHeight)
  231. {
  232. tempEndPoint.Y = Convert.ToInt32(safeHeight);
  233. }
  234. if (btnRectLabel.Tag.ToString() == "0" && blnDraw)
  235. {
  236. if (e.Button != MouseButtons.Left)
  237. return;
  238. newRect = new Rectangle();
  239. newRect.Location = new Point(
  240. Math.Min(start.X, tempEndPoint.X),
  241. Math.Min(start.Y, tempEndPoint.Y));
  242. newRect.Size = new Size(
  243. Math.Abs(start.X - tempEndPoint.X),
  244. Math.Abs(start.Y - tempEndPoint.Y));
  245. }
  246. else if (btnFourPoints.Tag.ToString() == "0" && blnDrawFourPoints)
  247. {
  248. newRect3 = new Rectangle();
  249. newRect3.Location = new Point(Convert.ToInt32(e.X / fScale), Convert.ToInt32(e.Y / fScale));
  250. newRect3.Size = new Size(1, 1);
  251. }
  252. else
  253. {
  254. if (e.Button == System.Windows.Forms.MouseButtons.Left)
  255. {
  256. if (markOnMouseDown != null)
  257. {
  258. int x = Convert.ToInt32(e.X / fScale);
  259. int y = Convert.ToInt32(e.Y / fScale);
  260. markOnMouseDown.MoveTo(x, y, duijiaoPoint);
  261. picPreview.Invalidate();
  262. return;
  263. }
  264. else if (shapeOnMouseDown != null)
  265. {
  266. int x = Convert.ToInt32((e.X - pointInShapeOnMouseDown.X) / fScale);
  267. int y = Convert.ToInt32((e.Y - pointInShapeOnMouseDown.Y) / fScale);
  268. shapeOnMouseDown.MoveTo(new Point(x, y));
  269. this.Cursor = Cursors.SizeAll;
  270. }
  271. }
  272. }
  273. picPreview.Invalidate();
  274. int x1 = Convert.ToInt32(e.X / fScale);
  275. int y1 = Convert.ToInt32(e.Y / fScale);
  276. Mark m = canvas.GetMark(x1, y1, fScale);
  277. if (m != null)
  278. this.Cursor = m.Cursor;
  279. else
  280. this.Cursor = Cursors.Default;
  281. }
  282. }
  283. private void picPreview_MouseUp(object sender, MouseEventArgs e)
  284. {
  285. blnDraw = false;
  286. if (picPreview.Image == null)
  287. return;
  288. if (picPreview.Image != null && btnRectLabel.Tag.ToString() == "0")
  289. {
  290. if (newRect != null && newRect.Width > 0 && newRect.Height > 0)
  291. {
  292. int left = Convert.ToInt32(newRect.X / fScale);
  293. int top = Convert.ToInt32(newRect.Y / fScale);
  294. int width = Convert.ToInt32(newRect.Width / fScale);
  295. int height = Convert.ToInt32(newRect.Height / fScale);
  296. var poly = new PaddleOCRForPoly()
  297. {
  298. isNew = true,
  299. points = new List<PointF>()
  300. {
  301. new PointF(){X = left, Y = top},
  302. new PointF(){X = left + width, Y = top},
  303. new PointF(){X = left + width, Y = top + height},
  304. new PointF(){X = left, Y = top + height},
  305. }
  306. };
  307. TreeNode node = new TreeNode(poly.ToString());
  308. node.Tag = poly;
  309. tvResults.Nodes.Add(node);
  310. tvResults.SelectedNode = node;
  311. canvas.Shapes.Add(new Shape()
  312. {
  313. isNew = true,
  314. points = poly.points
  315. });
  316. newRect = new Rectangle();
  317. //关闭标注状态
  318. btnFourPoints.Tag = "1";
  319. btnRectLabel.Tag = "1";
  320. btnRectLabel.Text = "启动矩形标注(W)";
  321. btnFourPoints.Text = "启动四点标注(Q)";
  322. InputForm inputForm = new InputForm();
  323. inputForm.StartPosition = FormStartPosition.CenterParent;
  324. inputForm.form5 = this;
  325. inputForm.points = poly.points;
  326. inputForm.originalText = "待标注";
  327. if (inputForm.ShowDialog() == DialogResult.OK)
  328. {
  329. //node.Nodes.Add(new TreeNode(inputForm.outputText));
  330. node.Nodes.Add(new TreeNode(inputForm.outputKey + "|" + inputForm.outputText));
  331. tvResults.SelectedNode.Expand();
  332. }
  333. canvas.Shapes[canvas.Shapes.Count - 1].text = inputForm.outputText;
  334. //add by lxw 20230628
  335. canvas.Shapes[canvas.Shapes.Count - 1].key = inputForm.outputKey;
  336. commandList.Add(new Command()
  337. {
  338. type = CommandEnum.Add,
  339. newPoly = new PaddleOCRForPoly()
  340. {
  341. text = inputForm.outputText,
  342. key = inputForm.outputKey,
  343. points = new List<PointF>()
  344. {
  345. new PointF(poly.points[0].X, poly.points[0].Y),
  346. new PointF(poly.points[1].X, poly.points[1].Y),
  347. new PointF(poly.points[2].X, poly.points[2].Y),
  348. new PointF(poly.points[3].X, poly.points[3].Y),
  349. }
  350. }
  351. });
  352. }
  353. }
  354. else if (shapeOnMouseDown != null)
  355. {
  356. if (originalPointList[0].X == shapeOnMouseDown.points[0].X && originalPointList[0].Y == shapeOnMouseDown.points[0].Y
  357. && originalPointList[1].X == shapeOnMouseDown.points[1].X && originalPointList[1].Y == shapeOnMouseDown.points[1].Y
  358. && originalPointList[2].X == shapeOnMouseDown.points[2].X && originalPointList[2].Y == shapeOnMouseDown.points[2].Y
  359. && originalPointList[3].X == shapeOnMouseDown.points[3].X && originalPointList[3].Y == shapeOnMouseDown.points[3].Y
  360. )
  361. return;
  362. this.Cursor = Cursors.Default;
  363. //更新坐标
  364. tvResults.SelectedNode.Text = shapeOnMouseDown.ToString();
  365. var poly = tvResults.SelectedNode.Tag as PaddleOCRForPoly;
  366. poly.points = new List<PointF>() {
  367. new PointF(shapeOnMouseDown.points[0].X, shapeOnMouseDown.points[0].Y),
  368. new PointF(shapeOnMouseDown.points[1].X, shapeOnMouseDown.points[1].Y),
  369. new PointF(shapeOnMouseDown.points[2].X, shapeOnMouseDown.points[2].Y),
  370. new PointF(shapeOnMouseDown.points[3].X, shapeOnMouseDown.points[3].Y),
  371. };
  372. tvResults.SelectedNode.Tag = poly;
  373. commandList.Add(new Command()
  374. {
  375. type = CommandEnum.Move,
  376. oldPoly = new PaddleOCRForPoly()
  377. {
  378. points = new List<PointF>()
  379. {
  380. new PointF(originalPointList[0].X, originalPointList[0].Y),
  381. new PointF(originalPointList[1].X, originalPointList[1].Y),
  382. new PointF(originalPointList[2].X, originalPointList[2].Y),
  383. new PointF(originalPointList[3].X, originalPointList[3].Y),
  384. }
  385. },
  386. newPoly = new PaddleOCRForPoly()
  387. {
  388. points = new List<PointF>()
  389. {
  390. new PointF(poly.points[0].X, poly.points[0].Y),
  391. new PointF(poly.points[1].X, poly.points[1].Y),
  392. new PointF(poly.points[2].X, poly.points[2].Y),
  393. new PointF(poly.points[3].X, poly.points[3].Y),
  394. }
  395. }
  396. });
  397. //shapeOnMouseDown = null;
  398. //originalPointList = null;
  399. }
  400. }
  401. private void picPreview_Paint(object sender, PaintEventArgs e)
  402. {
  403. if (currentImage != null)
  404. {
  405. int safeWidth = Convert.ToInt32(currentImage.Width * fScale);
  406. int safeHeight = Convert.ToInt32(currentImage.Height * fScale);
  407. Pen pen = new Pen(Color.Gray, 1);
  408. pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
  409. pen.DashPattern = new float[] { 5, 5 };
  410. e.Graphics.DrawRectangle(pen, 0, 0, safeWidth, safeHeight);
  411. }
  412. e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
  413. e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
  414. Color color = Color.Blue;
  415. foreach (Shape shape in canvas.Shapes)
  416. {
  417. if (shape.key != null && Singleton.Instance().ColorDic.Keys.Contains(shape.key))
  418. {
  419. color = Singleton.Instance().ColorDic[shape.key];
  420. }
  421. else
  422. {
  423. color = Color.Blue;
  424. }
  425. //选中的Shape 红色标注
  426. if (shape.Selected)
  427. {
  428. color = Color.Red;
  429. foreach (Mark m in shape.Marks)
  430. {
  431. Rectangle rect = new Rectangle(
  432. Convert.ToInt32(shape.points[m.Pos].X * fScale - m.MarkSize / 2),
  433. Convert.ToInt32(shape.points[m.Pos].Y * fScale - m.MarkSize / 2),
  434. m.MarkSize, m.MarkSize);
  435. e.Graphics.FillEllipse(Brushes.Aqua, rect);
  436. e.Graphics.DrawEllipse(Pens.Blue, rect);
  437. }
  438. }
  439. else
  440. {//新的Shape 橘色标注
  441. if (shape.isNew)
  442. {
  443. //color = Color.Orange;
  444. }
  445. }
  446. PointF p1 = new PointF(shape.points[0].X * fScale, shape.points[0].Y * fScale);
  447. PointF p2 = new PointF(shape.points[1].X * fScale, shape.points[1].Y * fScale);
  448. PointF p3 = new PointF(shape.points[2].X * fScale, shape.points[2].Y * fScale);
  449. PointF p4 = new PointF(shape.points[3].X * fScale, shape.points[3].Y * fScale);
  450. e.Graphics.DrawPolygon(new Pen(color, 2), new PointF[4] { p1, p2, p3, p4 });
  451. }
  452. if (blnDraw)
  453. {
  454. if (newRect != null && newRect.Width > 0 && newRect.Height > 0)
  455. {
  456. e.Graphics.DrawRectangle(new Pen(Color.Orange, 2), newRect);
  457. }
  458. }
  459. if (blnDrawFourPoints && newRect3 != null)
  460. {
  461. int new_x = Convert.ToInt32(newRect3.X * fScale);
  462. int new_y = Convert.ToInt32(newRect3.Y * fScale);
  463. if (allPointList4.Count == 1)
  464. {
  465. Pen pen = new Pen(Color.Orange, 2);
  466. pen.DashStyle = DashStyle.Dash;
  467. int p0_x = Convert.ToInt32(allPointList4[0].x * fScale);
  468. int p0_y = Convert.ToInt32(allPointList4[0].y * fScale);
  469. e.Graphics.DrawLine(pen, new_x, new_y, p0_x, p0_y);
  470. }
  471. else if (allPointList4.Count == 2)
  472. {
  473. Pen pen = new Pen(Color.Orange, 2);
  474. pen.DashStyle = DashStyle.Dash;
  475. int p0_x = Convert.ToInt32(allPointList4[0].x * fScale);
  476. int p0_y = Convert.ToInt32(allPointList4[0].y * fScale);
  477. int p1_x = Convert.ToInt32(allPointList4[1].x * fScale);
  478. int p1_y = Convert.ToInt32(allPointList4[1].y * fScale);
  479. e.Graphics.DrawLine(pen, p0_x, p0_y, p1_x, p1_y);
  480. e.Graphics.DrawLine(pen, new_x, new_y, p1_x, p1_y);
  481. }
  482. else if (allPointList4.Count == 3)
  483. {
  484. int p0_x = Convert.ToInt32(allPointList4[0].x * fScale);
  485. int p0_y = Convert.ToInt32(allPointList4[0].y * fScale);
  486. int p1_x = Convert.ToInt32(allPointList4[1].x * fScale);
  487. int p1_y = Convert.ToInt32(allPointList4[1].y * fScale);
  488. int p2_x = Convert.ToInt32(allPointList4[2].x * fScale);
  489. int p2_y = Convert.ToInt32(allPointList4[2].y * fScale);
  490. Pen pen = new Pen(Color.Orange, 2);
  491. pen.DashStyle = DashStyle.Dash;
  492. e.Graphics.DrawLine(pen, p0_x, p0_y, p1_x, p1_y);
  493. e.Graphics.DrawLine(pen, p1_x, p1_y, p2_x, p2_y);
  494. e.Graphics.DrawLine(pen, new_x, new_y, p0_x, p0_y);
  495. e.Graphics.DrawLine(pen, new_x, new_y, p2_x, p2_y);
  496. }
  497. }
  498. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/679074
推荐阅读
相关标签
  

闽ICP备14008679号