-
Notifications
You must be signed in to change notification settings - Fork 0
/
beta_main.c
98 lines (76 loc) · 2.13 KB
/
beta_main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "netcreat.h"
#include "net_structs.h"
#include "netpass.h"
#include "netfile.h"
#include "net_errno.h"
#include "ipldefs.h"
#include "iplimage.h"
#include "edge_detect.h"
#define NEURO_PATH "/home/user/NeuroNet/neuro.data"
#define PICSPATH "/home/user/NeuroNet/pics"
#define CNTPICS 10
#define SAMPLE_SIZE 100
struct sample {
double *data;
};
static double *getdata(struct IplImage *img)
{
int x, y;
double *data;
data = (double *)malloc(sizeof(double) * img->width * img->height);
for (y = 0; y < img->height; y++)
for (x = 0; x < img->width; x++) {
unsigned char r, g, b, max;
r = img->data[img->nchans * (y * img->width + x) + 0];
g = img->data[img->nchans * (y * img->width + x) + 1];
b = img->data[img->nchans * (y * img->width + x) + 2];
max = (r > g)? r : g;
max = (b > max)? b : max;
data[y * img->width + x] = (double)max / 255.0 * 2.0 - 1.0;
}
return data;
}
int main(int argc, char** argv)
{
int stat;
int nl = 2;
int nn[] = {100, 2};
double *out;
struct neuronet *net = malloc(sizeof(struct neuronet));
struct sample *examples;
int i;
char name[256];
struct IplImage *img;
examples = (struct sample *)malloc(sizeof(struct sample) * CNTPICS);
for (i = 0; i < CNTPICS; i++) {
bzero(name, 256);
sprintf(name, "%s/%d.png", PICSPATH, i);
if ((img = ipl_readimg(name, IPL_RGB_MODE)) == NULL) {
fprintf(stderr, "error reading image: %s\n", name);
goto exit_failure;
}
(examples + i)->data = getdata(img);
ipl_freeimg(&img);
}
if ((stat = netfromfile(net, NEURO_PATH)) == -1) { // read net from file or create new
fprintf(stderr, "Can not open file %s: %s\n", strerror(errno), NEURO_PATH);
goto exit_failure;
}
for (i = 0; i < CNTPICS; i++) {
double isgun_val, isnotgun_val;
out = netfpass(net, (examples + i)->data);
isgun_val = *(out + net->total_nn - 2);
isnotgun_val = *(out + net->total_nn - 1);
if (isgun_val >= isnotgun_val)
printf("%d is gun\n", i);
else if (isgun_val < isnotgun_val)
printf("%d isn't gun\n", i);
}
return 0;
exit_failure:
return -1;
}