赞
踩
目录
基于以下开源项目改造的
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
标签绘制代码
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);
}
}
}
- 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);
- }
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。