Skip to content

Commit

Permalink
Merge pull request #1397 from shimat/wechatqrcode_refactor
Browse files Browse the repository at this point in the history
wechat_qrcode refactoring
  • Loading branch information
shimat committed Mar 11, 2022
2 parents fec8e9f + 5dfa8c8 commit 044c535
Show file tree
Hide file tree
Showing 20 changed files with 144 additions and 105 deletions.
4 changes: 2 additions & 2 deletions src/OpenCvSharp/Internal/Vectors/VectorOfString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ public int Size
/// Converts std::vector to managed array
/// </summary>
/// <returns></returns>
public string?[] ToArray()
public string[] ToArray()
{
var size = Size;
if (size == 0)
return Array.Empty<string>();

var ret = new string?[size];
var ret = new string[size];
var cStringPointers = new IntPtr[size];
var stringLengths = new int[size];

Expand Down
1 change: 1 addition & 0 deletions src/OpenCvSharp/Modules/aruco/DetectorParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ public bool DetectInvertedMarker
set => Native.detectInvertedMarker = Convert.ToInt32(value);
}

#pragma warning disable CA1034
#pragma warning disable CA1051
#pragma warning disable 1591
[StructLayout(LayoutKind.Sequential)]
Expand Down
2 changes: 2 additions & 0 deletions src/OpenCvSharp/Modules/core/Mat/Mat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2994,6 +2994,7 @@ public Mat EmptyClone()
return new UnsafeIndexer<T>(this);
}

#pragma warning disable CA1034
/// <summary>
/// Mat Indexer
/// </summary>
Expand Down Expand Up @@ -3100,6 +3101,7 @@ internal Indexer(Mat parent)
}
}

#pragma warning disable CA1034
/// <summary>
/// Mat Indexer
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/OpenCvSharp/Modules/core/SparseMat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ public T Value<T>(int[] idx, long? hashVal = null)

#region Element Indexer

#pragma warning disable CA1034
/// <summary>
/// Mat Indexer
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/OpenCvSharp/Modules/face/Facemark/FacemarkAAM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static FacemarkAAM Create(Params? parameters = null)
return detector;
}

#pragma warning disable CA1034
/// <inheritdoc />
/// <summary>
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/OpenCvSharp/Modules/face/Facemark/FacemarkLBF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static FacemarkLBF Create(Params? parameters = null)
return detector;
}

#pragma warning disable CA1034
/// <inheritdoc />
/// <summary>
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/OpenCvSharp/Modules/features2d/SimpleBlobDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class SimpleBlobDetector : Feature2D
{
private Ptr? ptrObj;

#pragma warning disable CA1034
/// <summary>
/// SimpleBlobDetector parameters
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/OpenCvSharp/Modules/imgproc/ConnectedComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ private Mat GetLabelMask(int label)
}
}

#pragma warning disable CA1034
/// <summary>
/// One blob
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/OpenCvSharp/Modules/imgproc/LineIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ public int PlusStep

#endregion

#pragma warning disable CA1034
/// <summary>
/// LineIterator pixel data
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/OpenCvSharp/Modules/imgproc/Moments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ public double[] HuMoments()

#endregion

#pragma warning disable CA1034
#pragma warning disable 1591
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
[SuppressMessage("Microsoft.Design", "CA1815: Override equals and operator equals on value types")]
Expand Down
2 changes: 2 additions & 0 deletions src/OpenCvSharp/Modules/ml/DTrees.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ public int[] GetSubsets()

#region Types

#pragma warning disable CA1034
#pragma warning disable CA1051
/// <summary>
/// The class represents a decision tree node.
Expand Down Expand Up @@ -401,6 +402,7 @@ public struct Node
public int Split;
}

#pragma warning disable CA1034
/// <summary>
/// The class represents split in a decision tree.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/OpenCvSharp/Modules/tracking/TrackerCSRT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ protected override void DisposeUnmanaged()
}
}

#pragma warning disable CA1034
/// <summary>
/// CSRT Params
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/OpenCvSharp/Modules/tracking/TrackerKCF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ protected override void DisposeUnmanaged()
}
}

#pragma warning disable CA1034
#pragma warning disable CA1051
/// <summary>
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/OpenCvSharp/Modules/video/TrackerGOTURN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ protected override void DisposeUnmanaged()
}
}

#pragma warning disable CA1034
/// <summary>
///
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/OpenCvSharp/Modules/video/TrackerMIL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ protected override void DisposeUnmanaged()
}
}

#pragma warning disable CA1034
#pragma warning disable CA1051
/// <summary>
/// </summary>
Expand Down
112 changes: 112 additions & 0 deletions src/OpenCvSharp/Modules/wechat_qrcode/WeChatQRCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using OpenCvSharp.Internal;
using OpenCvSharp.Internal.Vectors;
using System;

namespace OpenCvSharp
{
/// <summary>
/// WeChat QRCode includes two CNN-based models:
/// A object detection model and a super resolution model.
/// Object detection model is applied to detect QRCode with the bounding box.
/// super resolution model is applied to zoom in QRCode when it is small.
/// </summary>
public class WeChatQRCode : DisposableCvObject
{
private Ptr? objectPtr;

internal WeChatQRCode(IntPtr ptr)
{
objectPtr = new Ptr(ptr);
this.ptr = objectPtr.Get();
}

/// <summary>
/// Initialize the WeChatQRCode.
/// It includes two models, which are packaged with caffe format.
/// Therefore, there are prototxt and caffe models (In total, four paramenters).
/// </summary>
/// <param name="detectorPrototxtPath">prototxt file path for the detector</param>
/// <param name="detectorCaffeModelPath">caffe model file path for the detector</param>
/// <param name="superResolutionPrototxtPath">prototxt file path for the super resolution model</param>
/// <param name="superResolutionCaffeModelPath">caffe file path for the super resolution model</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static WeChatQRCode Create(
string detectorPrototxtPath,
string detectorCaffeModelPath,
string superResolutionPrototxtPath,
string superResolutionCaffeModelPath)
{
if (string.IsNullOrWhiteSpace(detectorPrototxtPath))
throw new ArgumentException("empty string", nameof(detectorPrototxtPath));
if (string.IsNullOrWhiteSpace(detectorCaffeModelPath))
throw new ArgumentException("empty string", nameof(detectorCaffeModelPath));
if (string.IsNullOrWhiteSpace(superResolutionPrototxtPath))
throw new ArgumentException("empty string", nameof(superResolutionPrototxtPath));
if (string.IsNullOrWhiteSpace(superResolutionCaffeModelPath))
throw new ArgumentException("empty string", nameof(superResolutionCaffeModelPath));

NativeMethods.HandleException(
NativeMethods.wechat_qrcode_create1(
detectorPrototxtPath, detectorCaffeModelPath, superResolutionPrototxtPath, superResolutionCaffeModelPath,
out var ptr));

return new WeChatQRCode(ptr);
}

/// <summary>
/// Both detects and decodes QR code.
/// To simplify the usage, there is a only API: detectAndDecode
/// </summary>
/// <param name="inputImage">supports grayscale or color(BGR) image.</param>
/// <param name="bbox">optional output array of vertices of the found QR code quadrangle.Will be empty if not found.</param>
/// <param name="results">list of decoded string.</param>
public void DetectAndDecode(InputArray inputImage, out Mat[] bbox, out string[] results)
{
if (inputImage == null)
throw new ArgumentNullException(nameof(inputImage));
inputImage.ThrowIfDisposed();

using var bboxVec = new VectorOfMat();
using var texts = new VectorOfString();
NativeMethods.HandleException(
NativeMethods.wechat_qrcode_WeChatQRCode_detectAndDecode(
ptr, inputImage.CvPtr, bboxVec.CvPtr, texts.CvPtr));

bbox = bboxVec.ToArray();
results = texts.ToArray();
GC.KeepAlive(this);
GC.KeepAlive(inputImage);
}

/// <inheritdoc />
protected override void DisposeManaged()
{
objectPtr?.Dispose();
objectPtr = null;
base.DisposeManaged();
}

internal class Ptr : OpenCvSharp.Ptr
{
public Ptr(IntPtr ptr) : base(ptr)
{
}

public override IntPtr Get()
{
NativeMethods.HandleException(
NativeMethods.wechat_qrcode_Ptr_WeChatQRCode_get(ptr, out var ret));
GC.KeepAlive(this);
return ret;
}

protected override void DisposeUnmanaged()
{
NativeMethods.HandleException(
NativeMethods.wechat_qrcode_Ptr_delete(ptr));
base.DisposeUnmanaged();
}
}
}
}
90 changes: 0 additions & 90 deletions src/OpenCvSharp/Modules/wechat_qrcode/Wechat_Qrcode.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="System.Memory" Version="4.5.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Xunit.StaFact" Version="1.0.37" />
<PackageReference Include="Xunit.StaFact" Version="1.1.11" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions test/OpenCvSharp.Tests/OpenCvSharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="SharpZipLib" Version="1.3.3" />
<PackageReference Include="System.Memory" Version="4.5.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Xunit.StaFact" Version="1.0.37" />
<PackageReference Include="Xunit.StaFact" Version="1.1.11" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 044c535

Please sign in to comment.