Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions examples/SystemDrawing.WinForms/MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Sdcb.SimdPaddleOCR;
using System;
using System.Diagnostics;
using System.Drawing;
Expand All @@ -7,7 +8,7 @@
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sdcb.SimdPaddleOCR;
using System.Xml.Linq;

namespace SystemDrawing.WinForms;

Expand Down Expand Up @@ -71,6 +72,10 @@ private void BuildLayout()
AddPathRow(3, "REC 模型", _recPath, "模型|*.onnx|所有文件|*.*", false);
AddPathRow(4, "字典", _dictPath, "字典|*.txt|所有文件|*.*", false);

CheckBox bppCheckBox = new() { Text = "颜色通道数(默认选中为3通道BGR, 不选为4通道BGRA)", Checked = true, Size = new Size(400, 30), Anchor = AnchorStyles.Left, Margin = new Padding(0, 7, 6, 4) };
bppCheckBox.CheckedChanged += (_, _) => PaddleOcrAll.BytesPerPixel = bppCheckBox.Checked ? 3 : 4;
_paths.Controls.Add(bppCheckBox, 1, 5);

TableLayoutPanel root = new() { Dock = DockStyle.Fill, Padding = new Padding(12), RowCount = 3, ColumnCount = 1 };
root.RowStyles.Add(new RowStyle(SizeType.AutoSize)); root.RowStyles.Add(new RowStyle(SizeType.Percent, 100)); root.RowStyles.Add(new RowStyle(SizeType.Absolute, 34));
root.Controls.Add(_paths, 0, 0);
Expand Down Expand Up @@ -145,15 +150,33 @@ private async Task RunOcrAsync()
finally { _run.Enabled = _ocr is not null; _paths.Enabled = true; }
}

private OcrResult RunPipeline(string imagePath)
unsafe private OcrResult RunPipeline(string imagePath)
{
//改用直接访问内存,提升性能
using Bitmap bitmap = new(imagePath);
byte[] bgr = ToBgr(bitmap, out int stride); PaddleOcrResult result = _ocr!.Run(bgr, bitmap.Width, bitmap.Height, stride); Bitmap annotated = new(bitmap);
using Graphics graphics = Graphics.FromImage(annotated); using Pen pen = new(Color.LimeGreen, 3); using Brush brush = new SolidBrush(Color.Red);
var w = bitmap.Width;
var h = bitmap.Height;
var stride = w * PaddleOcrAll.BytesPerPixel;
var data = bitmap.LockBits(
new Rectangle(0, 0, w, h),
ImageLockMode.ReadOnly,
PaddleOcrAll.BytesPerPixel == 4 ? PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb);
PaddleOcrResult result = _ocr!.Run(new ReadOnlySpan<byte>((void*)data.Scan0, stride * h), w, h, stride);
bitmap.UnlockBits(data);

Bitmap annotated = new(bitmap);
using Graphics graphics = Graphics.FromImage(annotated);
using Pen pen = new(Color.LimeGreen, 3);
using Brush brush = new SolidBrush(Color.Red);
foreach (PaddleOcrLine line in result.Lines)
{
Point[] points = [new((int)line.Box.X1, (int)line.Box.Y1), new((int)line.Box.X2, (int)line.Box.Y2), new((int)line.Box.X3, (int)line.Box.Y3), new((int)line.Box.X4, (int)line.Box.Y4)];
graphics.DrawPolygon(pen, points); graphics.DrawString(line.Text, Font, brush, points[0]);
Point[] points = [
new((int)line.Box.X1,(int)line.Box.Y1),
new((int)line.Box.X2, (int)line.Box.Y2),
new((int)line.Box.X3, (int)line.Box.Y3),
new((int)line.Box.X4, (int)line.Box.Y4)];
graphics.DrawPolygon(pen, points);
//graphics.DrawString(line.Text, Font, brush, points[0]);
}
return new OcrResult(string.Join(Environment.NewLine, result.Lines.Select(line => line.Text)), result.DetectedCount, annotated);
}
Expand All @@ -162,10 +185,19 @@ private OcrResult RunPipeline(string imagePath)

private static byte[] ToBgr(Bitmap bitmap, out int stride)
{
stride = checked(bitmap.Width * 3); byte[] bgr = new byte[checked(stride * bitmap.Height)]; Rectangle rectangle = new(0, 0, bitmap.Width, bitmap.Height);
stride = checked(bitmap.Width * 3);
byte[] bgr = new byte[checked(stride * bitmap.Height)];
Rectangle rectangle = new(0, 0, bitmap.Width, bitmap.Height);
BitmapData data = bitmap.LockBits(rectangle, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
try { for (int y = 0; y < bitmap.Height; y++) Marshal.Copy(new IntPtr(data.Scan0.ToInt64() + y * (long)data.Stride), bgr, y * stride, stride); }
finally { bitmap.UnlockBits(data); }
try
{
for (int y = 0; y < bitmap.Height; y++)
Marshal.Copy(new IntPtr(data.Scan0.ToInt64() + y * (long)data.Stride), bgr, y * stride, stride);
}
finally
{
bitmap.UnlockBits(data);
}
return bgr;
}

Expand Down Expand Up @@ -201,6 +233,8 @@ private static string FindRepositoryRoot()
private sealed class OcrResult
{
public OcrResult(string text, int count, Bitmap annotated) { Text = text; Count = count; Annotated = annotated; }
public string Text { get; } public int Count { get; } public Bitmap Annotated { get; }
public string Text { get; }
public int Count { get; }
public Bitmap Annotated { get; }
}
}
120 changes: 120 additions & 0 deletions examples/SystemDrawing.WinForms/MainForm.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<AutoGenerateBindingRedirects Condition="'$(TargetFramework)' == 'net48'">true</AutoGenerateBindingRedirects>
<ApplicationManifest Condition="'$(TargetFramework)' == 'net48'">app.manifest</ApplicationManifest>
<PlatformTarget>x64</PlatformTarget>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Sdcb.SimdPaddleOCR\Sdcb.SimdPaddleOCR.csproj" />
Expand Down
9 changes: 5 additions & 4 deletions src/Sdcb.SimdPaddleOCR/Kernels/Warp.Avx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static unsafe void SampleCubicAvx(byte* source, int stride,
Vector256<double> wy = CubicWeightVectorAvx(ty);
Vector256<double> wx0 = Avx2.Permute4x64(wx, 0x00), wx1 = Avx2.Permute4x64(wx, 0x55);
Vector256<double> wx2 = Avx2.Permute4x64(wx, 0xAA), wx3 = Avx2.Permute4x64(wx, 0xFF);
byte* row = source + (yBase - 1) * stride + (xBase - 1) * 3;
byte* row = source + (yBase - 1) * stride + (xBase - 1) * PaddleOcrAll.BytesPerPixel;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kernel层不应该引用业务逻辑层的PaddleOcrAll.BytesPerPixel

Vector256<double> acc = AccumulateRowAvx(row, wx0, wx1, wx2, wx3,
Avx2.Permute4x64(wy, 0x00), Vector256<double>.Zero);
acc = AccumulateRowAvx(row + stride, wx0, wx1, wx2, wx3, Avx2.Permute4x64(wy, 0x55), acc);
Expand All @@ -84,10 +84,11 @@ private static unsafe Vector256<double> AccumulateRowAvx(byte* row,
Vector256<double> wx0, Vector256<double> wx1, Vector256<double> wx2, Vector256<double> wx3,
Vector256<double> wy, Vector256<double> acc)
{
int bpp = PaddleOcrAll.BytesPerPixel;
acc = Avx.Add(acc, Avx.Multiply(Avx.Multiply(LoadPixelAvx(row), wx0), wy));
acc = Avx.Add(acc, Avx.Multiply(Avx.Multiply(LoadPixelAvx(row + 3), wx1), wy));
acc = Avx.Add(acc, Avx.Multiply(Avx.Multiply(LoadPixelAvx(row + 6), wx2), wy));
acc = Avx.Add(acc, Avx.Multiply(Avx.Multiply(LoadPixelAvx(row + 9), wx3), wy));
acc = Avx.Add(acc, Avx.Multiply(Avx.Multiply(LoadPixelAvx(row + bpp), wx1), wy));
acc = Avx.Add(acc, Avx.Multiply(Avx.Multiply(LoadPixelAvx(row + 2 * bpp), wx2), wy));
acc = Avx.Add(acc, Avx.Multiply(Avx.Multiply(LoadPixelAvx(row + 3 * bpp), wx3), wy));
return acc;
}

Expand Down
9 changes: 5 additions & 4 deletions src/Sdcb.SimdPaddleOCR/Kernels/Warp.Avx512.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private static unsafe void SampleCubicAvx512(byte* source, int stride,
Vector512<double> wx1 = Vector512.Create(wx.GetElement(1));
Vector512<double> wx2 = Vector512.Create(wx.GetElement(2));
Vector512<double> wx3 = Vector512.Create(wx.GetElement(3));
byte* row = source + (yBase - 1) * stride + (xBase - 1) * 3;
byte* row = source + (yBase - 1) * stride + (xBase - 1) * PaddleOcrAll.BytesPerPixel;
Vector512<double> acc = AccumulateRowAvx512(row, wx0, wx1, wx2, wx3,
Vector512.Create(wy.GetElement(0)), Vector512<double>.Zero);
acc = AccumulateRowAvx512(row + stride, wx0, wx1, wx2, wx3, Vector512.Create(wy.GetElement(1)), acc);
Expand All @@ -89,10 +89,11 @@ private static unsafe Vector512<double> AccumulateRowAvx512(byte* row,
Vector512<double> wx0, Vector512<double> wx1, Vector512<double> wx2, Vector512<double> wx3,
Vector512<double> wy, Vector512<double> acc)
{
int bpp = PaddleOcrAll.BytesPerPixel;
acc = Avx512F.Add(acc, Avx512F.Multiply(Avx512F.Multiply(LoadPixelAvx512(row), wx0), wy));
acc = Avx512F.Add(acc, Avx512F.Multiply(Avx512F.Multiply(LoadPixelAvx512(row + 3), wx1), wy));
acc = Avx512F.Add(acc, Avx512F.Multiply(Avx512F.Multiply(LoadPixelAvx512(row + 6), wx2), wy));
acc = Avx512F.Add(acc, Avx512F.Multiply(Avx512F.Multiply(LoadPixelAvx512(row + 9), wx3), wy));
acc = Avx512F.Add(acc, Avx512F.Multiply(Avx512F.Multiply(LoadPixelAvx512(row + bpp), wx1), wy));
acc = Avx512F.Add(acc, Avx512F.Multiply(Avx512F.Multiply(LoadPixelAvx512(row + 2 * bpp), wx2), wy));
acc = Avx512F.Add(acc, Avx512F.Multiply(Avx512F.Multiply(LoadPixelAvx512(row + 3 * bpp), wx3), wy));
return acc;
}

Expand Down
9 changes: 5 additions & 4 deletions src/Sdcb.SimdPaddleOCR/Kernels/Warp.Scalar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal static unsafe void ProcessPixel(byte* sourcePtr, int sourceWidth, int s
throw new InvalidDataException("Invalid perspective transform.");
int destinationX = rotateVertical ? unrotatedHeight - 1 - y : x;
int destinationY = rotateVertical ? x : y;
int destination = checked((destinationY * outputWidth + destinationX) * 3);
int destination = checked((destinationY * outputWidth + destinationX) * PaddleOcrAll.BytesPerPixel);
SampleCubicScalar(sourcePtr, sourceWidth, sourceHeight, sourceStride,
sx, sy, cropPtr, destination);
}
Expand All @@ -58,14 +58,15 @@ internal static unsafe void SampleCubicScalar(byte* source, int width, int heigh
CubicWeight(y - (yBase + 1)),
CubicWeight(y - (yBase + 2)),
];
int bpp = PaddleOcrAll.BytesPerPixel;
double value0 = 0, value1 = 0, value2 = 0;
if (xBase >= 1 && xBase < width - 2 && yBase >= 1 && yBase < height - 2)
{
for (int ky = 0; ky < 4; ky++)
{
double wy = yWeights[ky];
int sourceOffset = (yBase + ky - 1) * stride + (xBase - 1) * 3;
for (int kx = 0; kx < 4; kx++, sourceOffset += 3)
int sourceOffset = (yBase + ky - 1) * stride + (xBase - 1) * bpp;
for (int kx = 0; kx < 4; kx++, sourceOffset += bpp)
{
double wx = xWeights[kx];
value0 += source[sourceOffset] * wx * wy;
Expand All @@ -84,7 +85,7 @@ internal static unsafe void SampleCubicScalar(byte* source, int width, int heigh
{
double wx = xWeights[kx];
int sx = Clamp(xBase + kx - 1, width);
int sourceOffset = sy * stride + sx * 3;
int sourceOffset = sy * stride + sx * bpp;
value0 += source[sourceOffset] * wx * wy;
value1 += source[sourceOffset + 1] * wx * wy;
value2 += source[sourceOffset + 2] * wx * wy;
Expand Down
9 changes: 5 additions & 4 deletions src/Sdcb.SimdPaddleOCR/Kernels/Warp.Vector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static unsafe void SampleCubicVector(byte* source, int stride,
Vector<double> wy = CubicWeightVector(new Vector<double>(y) - tap);
Vector<double> wx0 = new(wx.GetElement(0)), wx1 = new(wx.GetElement(1));
Vector<double> wx2 = new(wx.GetElement(2)), wx3 = new(wx.GetElement(3));
byte* row = source + (yBase - 1) * stride + (xBase - 1) * 3;
byte* row = source + (yBase - 1) * stride + (xBase - 1) * PaddleOcrAll.BytesPerPixel;
Vector<double> acc = AccumulateRowVector(row, wx0, wx1, wx2, wx3,
new Vector<double>(wy.GetElement(0)), Vector<double>.Zero);
acc = AccumulateRowVector(row + stride, wx0, wx1, wx2, wx3, new Vector<double>(wy.GetElement(1)), acc);
Expand All @@ -84,10 +84,11 @@ private static unsafe Vector<double> AccumulateRowVector(byte* row,
Vector<double> wx0, Vector<double> wx1, Vector<double> wx2, Vector<double> wx3,
Vector<double> wy, Vector<double> acc)
{
int bpp = PaddleOcrAll.BytesPerPixel;
acc += LoadPixelVector(row) * wx0 * wy;
acc += LoadPixelVector(row + 3) * wx1 * wy;
acc += LoadPixelVector(row + 6) * wx2 * wy;
acc += LoadPixelVector(row + 9) * wx3 * wy;
acc += LoadPixelVector(row + bpp) * wx1 * wy;
acc += LoadPixelVector(row + 2 * bpp) * wx2 * wy;
acc += LoadPixelVector(row + 3 * bpp) * wx3 * wy;
return acc;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sdcb.SimdPaddleOCR/Kernels/Warp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private static unsafe void SampleMappedPixel(byte* sourcePtr, int sourceWidth, i
{
int destinationX = rotateVertical ? unrotatedHeight - 1 - y : x;
int destinationY = rotateVertical ? x : y;
int destination = checked((destinationY * outputWidth + destinationX) * 3);
int destination = checked((destinationY * outputWidth + destinationX) * PaddleOcrAll.BytesPerPixel);
SampleCubic(sourcePtr, sourceWidth, sourceHeight, sourceStride,
pixelX, pixelY, cropPtr, destination);
}
Expand Down
Loading
Loading