nv2-test-server.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef NEOVISIONII_NV2_TEST_SERVER_C_DEFINED
00039 #define NEOVISIONII_NV2_TEST_SERVER_C_DEFINED
00040
00041 #include "NeovisionII/nv2_common.h"
00042 #include "NeovisionII/nv2_label_server.h"
00043
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <unistd.h>
00047
00048 static char** load_words(const size_t wordlen, size_t* nwords_ret)
00049 {
00050 const size_t nwords = *nwords_ret;
00051
00052 char** const result =
00053 malloc(sizeof(char*) * nwords + nwords * (wordlen + 1));
00054
00055 char* const wordarea = (char*)(result + nwords);
00056
00057 for (size_t i = 0; i < nwords; ++i)
00058 {
00059 result[i] = wordarea + i * (wordlen + 1);
00060 result[i][0] = '\0';
00061 }
00062
00063 if (result == 0)
00064 {
00065 fprintf(stderr, "malloc() failed\n");
00066 exit(-1);
00067 }
00068
00069 FILE* f = fopen("/usr/share/dict/words", "r");
00070
00071 if (f == 0)
00072 {
00073 fprintf(stderr, "couldn't open /usr/share/dict/words\n");
00074 exit(-1);
00075 }
00076
00077 size_t i = 0;
00078
00079 size_t p = 0;
00080
00081 while (1)
00082 {
00083 int c = getc_unlocked(f);
00084 if (c == EOF)
00085 {
00086 result[i][p] = '\0';
00087 break;
00088 }
00089 else if (c == '\n')
00090 {
00091 if (p == wordlen)
00092 {
00093 result[i][p] = '\0';
00094 if (++i >= nwords) break;
00095 }
00096
00097 p = 0;
00098 }
00099 else
00100 {
00101 if (p < wordlen)
00102 {
00103 result[i][p] = c;
00104 }
00105
00106 ++p;
00107 }
00108 }
00109
00110 fclose(f);
00111
00112 *nwords_ret = i;
00113
00114 fprintf(stderr, "loaded %d words of length %d "
00115 "from /usr/share/dict/words\n",
00116 (int) *nwords_ret, (int) wordlen);
00117
00118 return result;
00119 }
00120
00121 int main (int argc, char* const argv[])
00122 {
00123 if (argc < 2 || argc > 6)
00124 {
00125 fprintf(stderr,
00126 "usage: %s ident patch-reader-port=%d "
00127 "label-reader-ip-addr=127.0.0.1 "
00128 "label-reader-port=%d "
00129 "send-interval=1\n",
00130 argv[0], NV2_PATCH_READER_PORT,
00131 NV2_LABEL_READER_PORT);
00132 return 0;
00133 }
00134
00135 const char* const ident = argv[1];
00136 const int patch_reader_port =
00137 argc >= 3 ? atoi(argv[2]) : NV2_PATCH_READER_PORT;
00138 const char* const label_reader_ip_addr =
00139 argc >= 4 ? argv[3] : "127.0.0.1";
00140 const int label_reader_port =
00141 argc >= 5 ? atoi(argv[4]) : NV2_LABEL_READER_PORT;
00142 const int send_interval =
00143 argc >= 6 ? atoi(argv[5]) : 1;
00144
00145 struct nv2_label_server* s =
00146 nv2_label_server_create(patch_reader_port,
00147 label_reader_ip_addr,
00148 label_reader_port);
00149
00150
00151 const size_t wordlen = 13;
00152 size_t nwords = 40000;
00153
00154 char** const words = load_words(wordlen, &nwords);
00155
00156 double confidence = 0.0;
00157
00158 while (1)
00159 {
00160 struct nv2_image_patch p;
00161 const enum nv2_image_patch_result res =
00162 nv2_label_server_get_current_patch(s, &p);
00163
00164 if (res == NV2_IMAGE_PATCH_END)
00165 {
00166 fprintf(stdout, "ok, quitting\n");
00167 break;
00168 }
00169 else if (res == NV2_IMAGE_PATCH_NONE)
00170 {
00171 usleep(10000);
00172 continue;
00173 }
00174
00175
00176
00177 confidence += 0.05 * drand48();
00178 if (confidence > 1.0) confidence = 0.0;
00179
00180 struct nv2_patch_label l;
00181 l.protocol_version = NV2_LABEL_PROTOCOL_VERSION;
00182 l.patch_id = p.id;
00183 l.confidence = (uint32_t)(0.5 + confidence
00184 * (double)(NV2_MAX_LABEL_CONFIDENCE));
00185 snprintf(l.source, sizeof(l.source), "%s",
00186 ident);
00187 snprintf(l.name, sizeof(l.name), "%s (%ux%u #%u)",
00188 words[rand() % nwords],
00189 (unsigned int) p.width,
00190 (unsigned int) p.height,
00191 (unsigned int) p.id);
00192 snprintf(l.extra_info, sizeof(l.extra_info),
00193 "auxiliary information");
00194
00195 if (l.patch_id % send_interval == 0)
00196 {
00197 nv2_label_server_send_label(s, &l);
00198
00199 fprintf(stdout, "sent label '%s (%s)'\n",
00200 l.name, l.extra_info);
00201 }
00202 else
00203 {
00204 fprintf(stdout, "DROPPED label '%s (%s)'\n",
00205 l.name, l.extra_info);
00206 }
00207
00208 nv2_image_patch_destroy(&p);
00209 }
00210
00211 nv2_label_server_destroy(s);
00212
00213 return 0;
00214 }
00215
00216
00217
00218
00219
00220
00221
00222
00223 #endif // NEOVISIONII_NV2_TEST_SERVER_C_DEFINED