Created
June 1, 2019 12:40
-
-
Save vvzen/f5f4fe4c294dd4f13f203ed03cf1a3d5 to your computer and use it in GitHub Desktop.
Test osc communication between openframeworks and Runway
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 characters
#include "ofApp.h" | |
//-------------------------------------------------------------- | |
void ofApp::setup() { | |
ofSetWindowTitle("runway-im2txt test"); | |
ofSetFrameRate(60); // run at 60 fps | |
ofSetVerticalSync(true); | |
// open an outgoing connection to HOST:PORT | |
sender.setup(HOST, PORT); | |
// preload image to send into a buffer | |
imgAsBuffer = ofBufferFromFile("of-logo.png", true); | |
// Create an empty ofxIO::ByteBuffer. | |
ofxIO::ByteBuffer buffer; | |
// Load the file contents into the buffer. | |
ofxIO::ByteBufferUtils::loadFromFile("of-logo.png", buffer); | |
// Base64 encode the images's bytes. | |
// Additional encoding option include URL-safety, chunking and padding. | |
base_64_buffer = ofxIO::Base64Encoding::encode(buffer); | |
// ofLog() << "base 64 image: " << base_64_buffer; | |
} | |
//-------------------------------------------------------------- | |
void ofApp::update() { | |
while(receiver.hasWaitingMessages()){ | |
// get the next OSC message | |
ofxOscMessage m; | |
receiver.getNextMessage(m); | |
// grab the data | |
string data = m.getArgAsString(0); | |
// parse it to JSON | |
ofxJSONElement results; | |
results.parse(data); | |
// From the JSONObject we want the key with the results | |
// And from the array of captions (3) we want the first one | |
// So we assign that caption to our global caption variable | |
std::string caption = results["results"][0]["caption"].asString(); | |
ofLog() << "recevied caption: " << caption; | |
} | |
} | |
//-------------------------------------------------------------- | |
void ofApp::draw() { | |
ofBackgroundGradient(255, 100); | |
// draw image if it's loaded | |
if (img.isAllocated()) { | |
ofSetColor(255); | |
img.draw(ofGetWidth() / 2 - img.getWidth() / 2, | |
ofGetHeight() / 2 - img.getHeight() / 2); | |
} | |
// display instructions | |
string buf = "sending osc messages to: " + string(HOST); | |
buf += " " + ofToString(PORT); | |
buf += "\npress I to send base64 image to /image"; | |
ofDrawBitmapStringHighlight(buf, 10, 20); | |
} | |
//-------------------------------------------------------------- | |
void ofApp::keyPressed(int key) { | |
// send an image | |
// note: the size of the image depends greatly on your network buffer sizes, | |
// if an image is too big the message won't come through and you may need | |
// to break it up into multiple blobs | |
if (key == 'i' || key == 'I') { | |
// load image from buffer | |
img.load(imgAsBuffer); | |
// send as a base 64 image as required by RunwayML | |
ofxOscMessage m; | |
m.setAddress("/image"); | |
json_message["image"] = base_64_buffer; | |
m.addStringArg(json_message["image"].asString()); | |
sender.sendMessage(m); | |
ofLog() << "sending image"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment