Created
January 13, 2016 11:54
-
-
Save rettuce/a0718913c1b0790abc9f to your computer and use it in GitHub Desktop.
Revisions
-
rettuce created this gist
Jan 13, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,170 @@ #include "ofMain.h" #include "ofxJSON.h" #include "ofxHttpUtils.h" struct GData{ public: string description; float score; }; class ofApp : public ofBaseApp { ofVideoGrabber videocam; ofxHttpUtils httpUtils; int counter = 0; string responseStr; string requestStr; string action_url; ofxJSONElement result; vector<GData> datas; ofImage uploadImage; bool isVideo = false; public: void setup() { videocam.setDeviceID(1); videocam.setup(1920/2, 1080/2); ofSetFrameRate(60); action_url = "http://test.com/GoogleGloudVision/upload.php"; // test.com = virtual_host ofAddListener(httpUtils.newResponseEvent,this,&ofApp::newResponse); httpUtils.start(); } void update() { videocam.update(); } void draw() { glPushAttrib(GL_ALL_ATTRIB_BITS); ofPushMatrix(); ofPushStyle(); { ofEnableAlphaBlending(); ofDisableDepthTest(); ofBackground(0); ofSetColor(255); if(isVideo) videocam.draw(0,0); else uploadImage.draw(0,0, uploadImage.getWidth()*2, uploadImage.getHeight()*2); float blockH = 45.; float blockW = 10.; if(!datas.empty()){ ofSetColor(255,180); ofRect(10, 10, 380, (blockH+5)*datas.size()); for (int i=0; i<datas.size(); i++) { if(i==0) ofSetColor(255,0,0); else ofSetColor(0); ofDrawBitmapString( "Lank : "+ofToString(i+1), blockW+10, 30+blockH*i ); ofDrawBitmapString( " Descreption : "+ofToString(datas[i].description), blockW+10, 30+blockH*i+14 ); ofDrawBitmapString( " Score : "+ofToString(datas[i].score), blockW+10, 30+blockH*i+28 ); } } } ofPopStyle(); ofPopMatrix(); glPopAttrib(); } void keyPressed(int key) { switch (key) { case 'v': // toggle video mode datas.clear(); isVideo = !isVideo; break; case 'b': // image change > API Call loadimg(); break; case 'm': // video capture > API Call capture(); break; } }; void capture() { // $G(Data)->top.readToPixels(pixels1); ofImage temp; ofPixels pixels = videocam.getPixels(); string fname = "cap/"+ofGetTimestampString()+".jpg"; ofSaveImage(pixels, fname ); postdata( fname ); } void loadimg() { // setImage string fname = "nuko"+ofToString(counter%4)+".jpg"; uploadImage.load(fname); postdata( fname ); } void postdata( string fname ) { datas.clear(); ofxHttpForm form; form.action = action_url; form.method = OFX_HTTP_POST; form.addFile("file",fname); httpUtils.addForm(form); requestStr = "message sent: " + ofToString(counter); counter++; } void newResponse(ofxHttpResponse & response) { responseStr = (string)response.responseBody; if(response.status==200) { result = ofxJSONElement( responseStr ); // ofLogNotice("json ==> ") << result.getRawString(); bool isObject = result.isObject(); if(isObject) { if(result["responses"].isArray()){ if(result["responses"][0]["labelAnnotations"].isArray()) { datas.clear(); for (int i=0; i<result["responses"][0]["labelAnnotations"].size(); i++) { GData gd; gd.description = result["responses"][0]["labelAnnotations"][i]["description"].asString(); gd.score = result["responses"][0]["labelAnnotations"][i]["score"].asFloat(); datas.push_back( gd ); } } } } }else{ ofLog(OF_LOG_ERROR, ofToString(response.status)); } }; }; int main(int argc, const char** argv) { ofSetupOpenGL(1920, 1080, OF_WINDOW); ofRunApp(new ofApp); return 0; }