101 lines
3.6 KiB
C#
101 lines
3.6 KiB
C#
///
|
|
/// Copyright (c) 2022 Sensus Slovensko a.s.
|
|
///
|
|
using System.Collections.Generic;
|
|
#if OCR
|
|
using ViDi2;
|
|
#endif
|
|
|
|
namespace TBF.Rig.DataEntry
|
|
{
|
|
public class OcrVidi
|
|
{
|
|
#if OCR
|
|
/// Static variable: OCR ViDi control can only have one instance
|
|
static ViDi2.Runtime.IControl control;
|
|
readonly IWorkspace workspace;
|
|
#endif
|
|
readonly string ocrWorkspaceFile;
|
|
|
|
/// <summary>
|
|
/// Create OcrVidi wrapper class
|
|
/// </summary>
|
|
/// <param name="ocrWorkspaceFile">Cognex runtime workspace file (vrws)</param>
|
|
public OcrVidi(string ocrWorkspaceFile)
|
|
{
|
|
this.ocrWorkspaceFile = ocrWorkspaceFile;
|
|
#if OCR
|
|
if (control == null)
|
|
{
|
|
/// Create and initialize the main control
|
|
control = new ViDi2.Runtime.Local.Control(GpuMode.NoSupport);
|
|
control.InitializeComputeDevices(GpuMode.NoSupport, new List<int>() { }); /// Initializes all CUDA devices
|
|
}
|
|
|
|
/// Open a runtime workspace from file
|
|
workspace = control.Workspaces.Add(string.Format("ws{0}", control.Workspaces.Count + 1), ocrWorkspaceFile);
|
|
#endif
|
|
}
|
|
|
|
public string Recognize(string streamName, string imagePath, double denominator = 1000, string format = "F3")
|
|
{
|
|
#if OCR
|
|
IStream stream;
|
|
try { stream = workspace.Streams[streamName]; }
|
|
catch (Exception) { return string.Empty; }
|
|
if (stream == null) return string.Empty;
|
|
|
|
string findedText = string.Empty; /// Hladane cislo - stav vodomera - string
|
|
double findedValue = 0; /// Hladane cislo - stav vodomera
|
|
bool finded = false; /// Ci sme nasli hodnotu
|
|
|
|
// load an image from file
|
|
using (IImage image = new ViDi2.Local.LibraryImage(imagePath)) //disposing the image when we do not need it anymore
|
|
{
|
|
// allocates a sample with the image
|
|
using (ISample sample = stream.CreateSample(image))
|
|
{
|
|
ITool tool = stream.Tools["Read"];
|
|
|
|
// process image with the blue read tool
|
|
sample.Process(tool);
|
|
|
|
IBlueMarking blueReadMarking = sample.Markings[tool.Name] as IBlueMarking;
|
|
|
|
foreach (IBlueView view in blueReadMarking.Views)
|
|
{
|
|
foreach (IMatch match in view.Matches)
|
|
{
|
|
//System.Console.WriteLine($"This model says \" {(match as IReadModelMatch).FeatureString} \" with a score of {match.Score}");
|
|
string word = (match as IReadModelMatch).FeatureString;
|
|
if (word.Length >= 7 && word.Length <= 8 // hladane cislo ma od 7 do 8 znakov
|
|
&& double.TryParse(word,out findedValue)) // a ak najdeny text je cislo
|
|
{
|
|
findedValue /= denominator; // Convert to m3, red digits are fractions of m3
|
|
finded = true;
|
|
break;
|
|
}
|
|
}
|
|
if (finded) break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (finded)
|
|
{
|
|
findedText = findedValue.ToString(format);
|
|
}
|
|
|
|
return findedText;
|
|
#else
|
|
return null;
|
|
#endif
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return ocrWorkspaceFile;
|
|
}
|
|
}
|
|
}
|