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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
#pragma once
#define IMAGE_STORE_SIZE 256
#define MIN_TILE_SIZE 8
#define MAX_TILE_SIZE 32
#define FLANN_MATRIX_SIZE 5
#define INSTAGRAM_DISPLAY_TIME 5
#define IMAGE_FREEZE_TIME 5
#include "ofMain.h"
#include "ofxJSONElement.h"
#include "ofxOpenCv.h"
//#include <opencv/include/opencv2/flann/flann.hpp>
//~9fps@33% 32*3
using namespace cvflann;
long ofToLong(const string& intString);
class imageStore : public ofThread{
public:
float interval; //time between refreshes in seconds
std::string instagram_url;
ofxJSONElement response;
std::map<std::string,ofImage> images;
vector<ofImage*> imageptrs;
deque<std::string> to_update;
bool newinstagram;
ofImage instagram_image;
float newinstagramtime;
//Matrix<float> dataset; doesn't need to be retained?
//need to be able to add arbitrary data and keep it?
//see pucilar behaviour if we delete the data after indexing it
//we see some puzzling performance anomalies if we add data
//maybe rebuild the index rather than use index.addPoints()
//this way the data can be kept contiguously
//or maybe just get a pointer to vector data with an offset
vector<float> data;
//to begin, get a single point per image
//retain a pointer to the flann indexer
Index<L2<float> >* index;
//Index<L2<float> > index(dataset, cv::flann::KDTreeIndexParams(4));
imageStore(){
instagram_url = "https://api.instagram.com/v1/tags/tycleeson/media/recent?client_id=c1d74d701fdf4ddd9f8d30ee9e8f944b";
interval=5.00f;
ofImage img;
img.allocate(MAX_TILE_SIZE,MAX_TILE_SIZE,OF_IMAGE_COLOR);
img.clear();
img.setUseTexture(true);
img.update();
images["000000"]=img;
//colours["000000"]=ofColor(0,0,0);
newinstagram=false;
}
~imageStore(){
delete index;
}
void set_interval(float _interval){
interval=_interval;
}
void start(){
startThread(true, false); // blocking, verbose
}
void stop(){
stopThread();
}
//naive implementation
//too slow!
//looks shit!
//how to make the search algorithm faster and better
//http://www.semanticmetadata.net/lire/
std::map<std::string,ofColor> colours;
ofImage& get_image(const ofColor& col){
//float shortest_dist=999999.0f;
int sd=1000;
ofImage& im=images.begin()->second;
if( lock() ){
std::string s=images.begin()->first;
for (map<string,ofImage>::iterator it=images.begin();it!=images.end();++it){
ofColor& c=colours[it->first];
int rd=c.v[0]-col.v[0];
int gd=c.v[1]-col.v[1];
int bd=c.v[2]-col.v[2];
//float dist=pow((float)((rd*rd)+(gd*gd)+(bd*bd)),0.5);
int dist=abs(rd)+abs(gd)+abs(bd);
if (dist<sd){
sd=dist;
im=it->second;
s=it->first;
}
}
unlock();
}
//cerr<<"got image "<<s<<endl;
return im;
}
ofImage& get_image(float r,float g,float b){
ofImage& im=images.begin()->second;
if( lock() ){
float* test=new float[3];
test[0]=r;
test[1]=g;
test[2]=b;
Matrix<float> query(test,1,3);
Matrix<int> indices(new int[1], query.rows, 1);
Matrix<float> dists(new float[1], query.rows, 1);
index->knnSearch(query, indices, dists, 1,SearchParams(20));
im=*imageptrs[*indices[0]];
//int i=rand()%imageptrs.size();
//im=images[imageptrs[i]];
//cerr<<"returning image "<<i<<endl;
unlock();
}
return im;
}
ofImage& get_image(float* data){
ofImage& im=images.begin()->second;
if( lock() ){
Matrix<float> query(data,1,FLANN_MATRIX_SIZE*FLANN_MATRIX_SIZE*3);
Matrix<int> indices(new int[1], query.rows, 1);
Matrix<float> dists(new float[1], query.rows, 1);
index->knnSearch(query, indices, dists, 1,SearchParams(4));
im=*imageptrs[*indices[0]];
//int i=rand()%imageptrs.size();
//im=images[imageptrs[i]];
//cerr<<"returning image "<<i<<endl;
unlock();
}
return im;
}
ofColor get_colour(const ofImage& _img){
ofImage img=_img;
img.resize(1,1);
return ofColor(img.getPixels()[0],img.getPixels()[1],img.getPixels()[2]);
}
void add_data(const ofImage& _img){
ofImage img=_img;
img.resize(FLANN_MATRIX_SIZE,FLANN_MATRIX_SIZE);
for (int i=0;i<FLANN_MATRIX_SIZE;i++){
for (int j=0;j<FLANN_MATRIX_SIZE;j++){
data.push_back(img.getPixels()[(j*FLANN_MATRIX_SIZE+i)*3]);
data.push_back(img.getPixels()[(j*FLANN_MATRIX_SIZE+i)*3+1]);
data.push_back(img.getPixels()[(j*FLANN_MATRIX_SIZE+i)*3+2]);
}
}
}
bool new_instagram(){
if (newinstagram){
if( lock() ){
loadtexture(instagram_image);
newinstagram=false;
newinstagramtime=ofGetElapsedTimef();
unlock();
return true;
}
}
return false;
}
bool drawinstagram(int x,int y,int w,int h){
if( lock() ){
instagram_image.draw(x,y,w,h);
unlock();
}
if (ofGetElapsedTimef()-newinstagramtime>INSTAGRAM_DISPLAY_TIME) return false;
return true;
}
void instagram_floats(float* floatImage){
if( lock() ){
int size=ceil(ofGetHeight()/MAX_TILE_SIZE)*FLANN_MATRIX_SIZE;
ofImage imm=instagram_image;
imm.resize(size,size);
uint8_t* cptr=imm.getPixels();
for (int i=0;i<size;i++){
for (int j=0;j<size;j++){
floatImage[(i*size+j)*3]=cptr[(i*size+j)*3];
floatImage[(i*size+j)*3+1]=cptr[(i*size+j)*3+1];
floatImage[(i*size+j)*3+2]=cptr[(i*size+j)*3+2];
}
}
unlock();
}
}
//void flann(const ofImage& _img){
//--------------------------
void threadedFunction(){
//1st get the pre-existing images
ofDirectory image_path(ofToString(IMAGE_STORE_SIZE)+"/");
cerr<<"image path: "<<image_path.getAbsolutePath()<<endl;
if( lock() ){ //lock the thread while preparing the initial images
if (image_path.exists()){
image_path.listDir();
cerr<<"image path found, "<<image_path.size()<<" images"<<endl;
for (int i=0;i<image_path.size();i++){
ofImage img;
img.setUseTexture(false);
img.loadImage(ofToString(IMAGE_STORE_SIZE)+"/"+image_path.getFiles()[i].getFileName());
if (i==image_path.size()-1) instagram_image=img;
img.resize(MAX_TILE_SIZE,MAX_TILE_SIZE);
//colours[image_path.getFiles()[i].getBaseName()]=get_colour(img);
add_data(img);
images[image_path.getFiles()[i].getBaseName()]=img;
imageptrs.push_back(&images[image_path.getFiles()[i].getBaseName()]);
//cerr<<image_path.getFiles()[i].getBaseName()<<": "<<colours[image_path.getFiles()[i].getBaseName()]<<endl;
to_update.push_back(image_path.getFiles()[i].getBaseName());
}
}
else {
cerr<<"creating image path"<<endl;
image_path.create();
}
//build the flann index
Matrix<float> dataset(&data[0],data.size()/(3*FLANN_MATRIX_SIZE*FLANN_MATRIX_SIZE),3*FLANN_MATRIX_SIZE*FLANN_MATRIX_SIZE);
index= new Index<L2<float> >(dataset, KDTreeIndexParams(4));
index->buildIndex();
unlock();
}
cout << "Api: " << instagram_url<<endl;
while( isThreadRunning() != 0 ){
cout<<"."<<std::flush;
if (!response.open(instagram_url)) {
cout << "Failed to parse JSON\n" << endl;
}
else { //int numImages = MIN(5,response["data"].size());
for(int i=0; i< response["data"].size(); i++) {
//cout << "response " <<response["data"][i]["caption"]["id"].asString()<< endl;
if (images.find(response["data"][i]["caption"]["id"].asString())==images.end()){
std::string url = response["data"][i]["images"]["standard_resolution"]["url"].asString();
std::string id = response["data"][i]["caption"]["id"].asString();
cout<<"fetching "<<id<<":"<<instagram_url<<endl;
ofImage img;
img.setUseTexture(false);
img.loadImage(url);
if( lock() ){
instagram_image=img;
unlock();
}
img.resize(IMAGE_STORE_SIZE,IMAGE_STORE_SIZE);
img.saveImage(ofToString(IMAGE_STORE_SIZE)+"/"+id+".png");
img.resize(MAX_TILE_SIZE,MAX_TILE_SIZE);
if( lock() ){
newinstagram=true;
colours[id]=get_colour(img);
add_data(img);
images[id]=img;
imageptrs.push_back(&images[id]);
to_update.push_back(id);
//rebuild the flann index
float t=ofGetElapsedTimef();
delete index;
Matrix<float> dataset(&data[0],data.size()/(3*FLANN_MATRIX_SIZE*FLANN_MATRIX_SIZE),3*FLANN_MATRIX_SIZE*FLANN_MATRIX_SIZE);
index= new Index<L2<float> >(dataset, KDTreeIndexParams(4));
index->buildIndex();
cout<<"rebuilt flann index of "<<data.size()/(3*FLANN_MATRIX_SIZE*FLANN_MATRIX_SIZE)<<" images in "<<(ofGetElapsedTimef()-t)<<"seconds"<<endl;
unlock();
}
}
}
}
ofSleepMillis(interval * 1000);
}
}
void loadtexture(ofImage& im){
const ofPixels& pix = im.getPixelsRef();
im.getTextureReference().allocate(
pix.getWidth()
,pix.getHeight()
,ofGetGlInternalFormat(pix)
);
im.setUseTexture(true);
im.update();
}
void update(){
//loads one texture
if( lock() ){
if (to_update.size()){
std::string im = to_update.front();
loadtexture(images[im]);
/*
const ofPixels& pix = images[im].getPixelsRef();
images[im].getTextureReference().allocate(
pix.getWidth()
,pix.getHeight()
,ofGetGlInternalFormat(pix)
);
images[im].setUseTexture(true);
images[im].update();
*/
to_update.pop_front();
/*
int drawcount=0;
for (map<string,ofImage>::iterator i=images.begin();i!=images.end();++i){
if(i->second.isUsingTexture()){
drawcount++;
}
}
cout<<"loaded "<<im<<" "<<ofToLong(im)%(long)(ofGetWidth()-images[im].getWidth()+1)<<","<<ofToLong(im)%(long)(ofGetHeight()-images[im].getHeight()+1)<<endl;
*/
}
unlock();
}
}
//--------------------------
void draw(){
if( lock() ){
/*
for (map<string,ofImage>::iterator i=images.begin();i!=images.end();++i){
if(i->second.isUsingTexture()){
i->second.draw(ofToLong(i->first)%(long)(ofGetWidth()-i->second.getWidth()+1),ofToLong(i->first)%(long)(ofGetHeight()-i->second.getHeight()+1));
}
}
*/
map<string,ofImage>::iterator it=images.begin();
if (it!=images.end()){
for (int i=0;i<ofGetWidth()/MAX_TILE_SIZE;i++){
for (int j=0;j<ofGetHeight()/MAX_TILE_SIZE;j++){
if (it->second.isUsingTexture()){
it->second.draw(i*MAX_TILE_SIZE,j*MAX_TILE_SIZE);
}
it++;
if (it==images.end()) it=images.begin();
}
}
}
unlock();
}
}
};
|