aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/TCC/Tango.TCC.BL/CardDetector.cs
blob: 50bcceebabde88cd50c0c032cd49f69d9f76a691 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using Google.Protobuf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Tango.Core.Helpers;
using Tango.PMR.TCC;

namespace Tango.TCC.BL
{
    public class CardDetector
    {
        private ColorDetector _colorDetector;
        private static byte[] _defaultTemplate;
        private static List<DetectionBenchmark> _defaultBenchmarks;
        private byte[] _sourceBitmapBytes;

        private bool _canDetect;
        public bool CanDetect
        {
            get { return _canDetect; }
            private set { _canDetect = value; }
        }

        public CardDetector()
        {
            if (_defaultTemplate == null)
            {
                _defaultTemplate = File.ReadAllBytes(AssemblyHelper.GetCurrentAssemblyFolder() + "\\TCC\\template.bmp");
                _defaultBenchmarks = ColorDetector.LoadBenchmarks(AssemblyHelper.GetCurrentAssemblyFolder() + "\\TCC\\benchmarks_rgb_lab.csv").ToList();
            }

            CanDetect = true;
            _colorDetector = new ColorDetector();
        }

        public Task<CardDetectionResult> Detect(BitmapSource source, CardDetectionConfig config)
        {
            if (!CanDetect)
            {
                throw new InvalidOperationException("Cannot detect the card at this moment. Please wait for CanDetect to be true.");
            }

            CanDetect = false;

            var cloned = source.Clone();
            cloned.Freeze();

            return Task.Factory.StartNew<CardDetectionResult>(() =>
            {
                CardDetectionResult detectionResult = new CardDetectionResult();
                detectionResult.Source = cloned;

                _sourceBitmapBytes = cloned.ToBytes(PixelFormats.Rgb24);

                Tango.TCC.CardDetector.CardDetection detector = new TCC.CardDetector.CardDetection();
                var result = detector.Detect(_sourceBitmapBytes, new TCC.CardDetector.CardDetectionConfig()
                {
                    DesiredBitmapWidth = config.DesiredBitmapWidth,
                    DesiredBitmapHeight = config.DesiredBitmapHeight,
                    TemplateBitmap = config.TemplateBitmapBytes != null ? config.TemplateBitmapBytes : _defaultTemplate,
                    SimilarityTolerance = config.SimilarityTolerance,
                    HistogramMethod = (int)config.HistogramMethod,
                    EnableDoubleChecking = config.EnableDoubleChecking,
                });

                detectionResult.Similarity = result.Similarity;
                detectionResult.Barcode = result.Barcode;

                if (result.IsDetected)
                {
                    detectionResult.IsDetected = true;
                    detectionResult.DetectedBitmap = result.DetectedBitmap.ToBitmapSource();

                    var input = new DetectionInput()
                    {
                        Bitmap = ByteString.CopyFrom(detectionResult.DetectedBitmap.ToBmpBytes()),
                        Columns = config.Columns,
                        Rows = config.Rows,
                        TargetIndex = config.TargetIndex,
                        RequestColorMatrix = true,
                    };

                    if (config.Benchmarks.Count > 0)
                    {
                        input.Benchmarks.AddRange(config.Benchmarks);
                    }
                    else
                    {
                        input.Benchmarks.AddRange(_defaultBenchmarks);
                    }

                    detectionResult.ColorDetectionOutput = _colorDetector.Detect(input);
                }

                CanDetect = true;

                return detectionResult;
            });
        }
    }
}