forked from shimat/opencvsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestBase.cs
154 lines (136 loc) · 5.11 KB
/
TestBase.cs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
[assembly: CollectionBehavior(/*MaxParallelThreads = 2, */DisableTestParallelization = true)]
#pragma warning disable CA1810 // Initialize reference type static fields inline
#pragma warning disable CA5359
namespace OpenCvSharp.Tests
{
public abstract class TestBase
{
private static readonly HttpClient httpClient;
static TestBase()
{
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
#pragma warning disable CA5364
#pragma warning disable CA5386
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
#pragma warning restore CA5364
#pragma warning restore CA5386
#pragma warning disable CA2000
var handler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = delegate { return true; }
};
#pragma warning restore CA2000
httpClient = new HttpClient(handler)
{
Timeout = TimeSpan.FromMinutes(5)
};
}
protected static Mat Image(string fileName, ImreadModes modes = ImreadModes.Color)
{
return new Mat(Path.Combine("_data", "image", fileName), modes);
}
protected static void ImageEquals(Mat img1, Mat img2)
{
if (img1 == null && img2 == null)
return;
Assert.NotNull(img1);
Assert.NotNull(img2);
#pragma warning disable CS8602
#pragma warning disable CA1062
Assert.Equal(img1.Type(), img2.Type());
#pragma warning restore CS8602
#pragma warning restore CA1062
using var comparison = new Mat();
Cv2.Compare(img1, img2, comparison, CmpType.NE);
if (img1.Channels() == 1)
{
Assert.Equal(0, Cv2.CountNonZero(comparison));
}
else
{
var channels = Cv2.Split(comparison);
try
{
foreach (var channel in channels)
{
Assert.Equal(0, Cv2.CountNonZero(channel));
}
}
finally
{
foreach (var channel in channels)
{
channel.Dispose();
}
}
}
}
protected static void ShowImagesWhenDebugMode(params Mat[] mats)
{
if (Debugger.IsAttached)
{
Window.ShowImages(mats);
}
}
protected static void ShowImagesWhenDebugMode(IEnumerable<Mat> mats, IEnumerable<string> names)
{
if (Debugger.IsAttached)
{
Window.ShowImages(mats, names);
}
}
protected static void Pause(string message = "Press any key to exit")
{
if (Debugger.IsAttached)
{
Console.WriteLine();
Console.WriteLine(message);
Console.Read();
}
}
protected static byte[] DownloadBytes(
Uri uri,
Action<(long BytesReceived, long TotalBytesToReceive, int ProgressPercentage)>? downloadProgressChangedEvent = null)
{
using var client = new MyWebClient();
if (downloadProgressChangedEvent == null)
{
return client.DownloadData(uri);
}
var task = client.DownloadDataTaskAsync(
uri,
new Progress<(long BytesReceived, long TotalBytesToReceive, int ProgressPercentage)>(downloadProgressChangedEvent));
return task.Result;
//var response = (httpClient.GetAsync(uri).Result).EnsureSuccessStatusCode();
//return response.Content.ReadAsByteArrayAsync().Result;
}
protected static async Task<byte[]> DownloadBytesAsync(Uri uri, CancellationToken token = default)
{
var response = await httpClient.GetAsync(uri, token).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
}
protected static async Task<Stream> DownloadStreamAsync(Uri uri, CancellationToken token = default)
{
var response = await httpClient.GetAsync(uri, token).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
}
protected static string DownloadString(Uri uri)
{
using var client = new MyWebClient();
return client.DownloadString(uri);
//var response = (await httpClient.GetAsync(url)).EnsureSuccessStatusCode();
//return await response.Content.ReadAsStringAsync();
}
}
}