forked from GabRayz/OCR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.c
214 lines (192 loc) · 5.88 KB
/
dataset.c
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "image.h"
#include "segmentation.h"
#include "dataset.h"
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <ImageMagick-7/MagickWand/MagickWand.h>
#include <sys/stat.h>
#include "ccl.h"
/**
* Concatenate two strings
*/
char *concat(char *a, char *b)
{
int len = strlen(a) + strlen(b) + 1;
char *res = malloc(sizeof(char) * len);
strcpy(res, a);
strcat(res, b);
return res;
}
/**
* Take a set of Img structs containing a path to an image and loads the pixels into the struct.
* @param images Set of Img structs. Each img has to contain a path.
* @param dataCount Size of the Img set.
*/
void dataset_to_pixels(Img **images, int dataCount)
{
fputs("\e[?25l", stdout); /* hide the cursor */
for (int i = 0; i < dataCount && images[i]; i++)
{
printf("\r%d / %d", i + 1, dataCount);
MagickWand *mw = NewMagickWand();
Img *image = images[i];
if (MagickReadImage(mw, image->filepath) == MagickTrue)
{
// printf("File opened successfuly\n");
// MagickAdaptiveResizeImage(mw, 28, 28);
MagickExportImagePixels(mw, 0, 0, MagickGetImageWidth(mw), MagickGetImageHeight(mw), "R", DoublePixel, image->pixels);
//print_image(image);
}
else
printf("FAILED: %s\n", image->filepath);
DestroyMagickWand(mw);
}
fputs("\e[?25h", stdout); /* show the cursor */
printf("\n");
}
const char *string = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'()-_.,?!:;";
/**
* Create an array of resized images from a list of blocks. The final size is 28*28.
* @param source: A pointer to the source image.
* @param chars: A pointer to the linked list of blocks to resize.
* @param count: The number of blocks.
*
*/
Img **images_from_list(Img *source, LinkedList *chars, int *count)
{
int length = list_length(chars);
Img **images = malloc(sizeof(Img) * length);
Node *n = chars->start;
int i = 0;
for (i = 0; i < length && string[i] != '\0'; i++)
{
Img *c = img_resize(source, n->data, 28, 28);
images[i] = c;
images[i]->label = string[i];
n = n->next;
}
*count = i;
return images;
}
/**
* Create an array of resized images from a list of images. The final size is 28*28.
* @param source: A pointer to the source image.
* @param chars: A pointer to the linked list of images to resize.
* @param count: The number of blocks.
*
*/
Img **images_from_list_of_img(LinkedList *chars, int *count)
{
int length = list_length(chars);
Img **images = malloc(sizeof(Img) * length);
Node *n = chars->start;
int i = 0;
for (i = 0; i < length && string[i] != '\0'; i++)
{
Block *block = img_make_block(n->data);
// If the node is a whitespace, skip it
if (block->label != '\0')
{
n = n->next;
i--;
continue;
}
Img *c = img_resize(n->data, block, 28, 28);
images[i] = c;
images[i]->label = string[i];
n = n->next;
}
*count = i;
return images;
}
/**
* Open the training images of lines and create training images of chars.
* @param source: The path to the image to split.
* @param destination: The path where to save the dataset.
*/
void create_dataset_from_img(char *source, char *destination)
{
printf("Creating dataset from images...\n");
// Check source path
DIR *src = opendir(source);
if (src == NULL)
{
printf("ERROR : source path invalid. Unable to create dataset from it.\n");
return;
}
// Check destination path
DIR *dest = opendir(destination);
if (dest == NULL)
mkdir(destination, 0755);
else
closedir(dest);
struct dirent *file;
int i = 0;
char filepath[521];
// Skip self and parent directories
readdir(src);
readdir(src);
// For each file in dir
while ((file = readdir(src)) != NULL)
{
// Skip hidden files
if (file->d_name[0] == '.')
continue;
// open image of the line
sprintf(filepath, "%s/%s", source, file->d_name);
Img *img = img_import(filepath);
// Split into characters
// LinkedList *chars = segmentation(img, false);
LinkedList *chars = ccl_segmentation(img, false);
// Create images
int count = 0;
Img **images = images_from_list_of_img(chars, &count);
// Img **images = images_from_list(img, chars, &count);
// Save images
for (int k = 0; k < count; k++)
{
sprintf(filepath, "%s/%u_%d.png", destination, (unsigned char)images[k]->label, i);
img_save(images[k], filepath);
}
i++;
}
closedir(src);
printf("Dataset created at : %s\n", destination);
}
/**
* Read a dataset. Open every images of the filepath and return a LinkedList of paths.
* You then need to call dataset_to_pixels to loads the pixels.
* @param filepath: path to the dataset folder to parse.
*/
LinkedList *read_dataset(char *filepath)
{
/* Read the dataset without knowing number of files */
printf("Read dataset : %s\n", filepath);
LinkedList *images = list_init();
DIR *dir = opendir(filepath);
if (dir == NULL)
{
printf("ERROR : Dataset filepath does not exist.\n");
exit(1);
}
readdir(dir);
readdir(dir);
struct dirent *file;
int i = 0;
while ((file = readdir(dir)) != NULL)
{
// Skip hidden files
if (file->d_name[0] == '.')
continue;
// Store the file in the img
Img *image = img_init(28, 28);
image->filepath = malloc(sizeof(char) * 512);
sprintf(image->filepath, "%s/%s", filepath, file->d_name);
int c = atoi(file->d_name); // atoi il fé de la merd
image->label = c;
list_insert(images, node_init(image));
i++;
}
return images;
}