-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatasetZip.cs
81 lines (68 loc) · 2.04 KB
/
DatasetZip.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
using System;
using System.Drawing;
using System.IO;
using System.IO.Compression;
namespace NeuralNetworkExample2
{
public class DatasetZip
{
struct DataElement
{
public string[] pathes;
public int num;
public DataElement(string[] pathes, int num)
{
this.pathes = pathes ?? throw new ArgumentNullException(nameof(pathes));
this.num = num;
}
}
string pathToZip;
string folderName;
bool extracted = false;
DataElement[] dataElements = new DataElement[10];
public DatasetZip(string pathToZip, string folderName = "dataset")
{
this.pathToZip = pathToZip;
this.folderName = folderName;
if (Directory.Exists(folderName))
{
Directory.Delete(folderName, true);
}
}
~DatasetZip()
{
Directory.Delete(folderName, true);
}
public void Extract()
{
using (ZipArchive archive = ZipFile.OpenRead(pathToZip))
{
archive.ExtractToDirectory(folderName);
}
for (int i = 0; i <= 9; i++)
{
int num = i;
string[] files = Directory.GetFiles($"dataset\\{num}");
dataElements[i] = new DataElement(files, num);
}
extracted = true;
}
public Bitmap GetRandomImageByNum(int num)
{
if (!extracted)
{
throw new Exception("you must first extract .zip");
}
return (Bitmap)Bitmap.FromFile(RandomElement(dataElements[num].pathes));
}
Random rnd = new Random();
string RandomElement(string[] array)
{
if (array == null || array.Length == 0)
{
throw new ArgumentException($"{array} is null or empty");
}
return array[rnd.Next(0, array.Length - 1)];
}
}
}