Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example updates #7

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 33 additions & 52 deletions examples/fns_candy_style_transfer/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,54 @@

//--------------------------------------------------------------
void ofApp::setup() {
const ORTCHAR_T* modelName = L"candy.onnx";

fbo.allocate(720, 720, GL_RGB);
fbo.begin();
ofClear(0, 255);
fbo.end();


ort.setup(720, 720);

}

//--------------------------------------------------------------
void ofApp::update() {

fbo.begin();
ofSetColor(ofRandom(0, 255), ofRandom(0, 255), ofRandom(0, 255));
ofDrawCircle(mouseX, mouseY, ofRandom(10, 100));
fbo.end();


//I'm not sure if this is the right implementation for exclusions.
if (fbo.checkStatus()) {


ort.lock();
if (!_isImageSet && !ort.isThreadRunning()) {
setImageToModel();

}
if (ort._isImageProcessed && _isImageSet) {
readImageFromModel();
}

ort.unlock();



if (!ort.isThreadRunning()) {
ort.startThread(false);
}



if (isMousePressed) {
fbo.begin();
ofSetColor(ofRandom(0, 255), ofRandom(0, 255), ofRandom(0, 255));
ofDrawCircle(ofGetMouseX(), ofGetMouseY(), ofRandom(10, 100));
fbo.end();
}
ort.lock();
if (!_isImageSet && !ort.isThreadRunning()) {
setImageToModel();

}
if (ort._isImageProcessed && _isImageSet) {
readImageFromModel();
}
ort.unlock();
if (!ort.isThreadRunning()) {
ort.startThread(false);
}
}

//--------------------------------------------------------------
void ofApp::draw() {

fbo.draw(0.0, 0.0);
resultImage.draw(720.0, 0.0);
if (resultImage.isAllocated()) {
resultImage.draw(720.0, 0.0);
}
ofDrawBitmapString(ofToString(ofGetFrameRate()), 0, 10);
}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button) {
fbo.begin();
ofSetColor(ofRandom(0, 255), ofRandom(0, 255), ofRandom(0, 255));
ofDrawCircle(ofGetMouseX(), ofGetMouseY(), ofRandom(10, 100));
fbo.end();
isMousePressed = true;
}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button) {
isMousePressed = false;
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key) {
if (key == ' ') {
Expand All @@ -69,32 +60,22 @@ void ofApp::keyPressed(int key) {
}

void ofApp::updateResultImage() {
resultImage.setFromPixels(ort._dstPixels);
resultImage.update();
resultImage.loadData(ort._dstPixels);
}

void ofApp::exit() {


ort.stopThread();



}

void ofApp::setImageToModel() {

fbo.getTexture().readToPixels(ort._srcHWCPixels);


_isImageSet = true;
ort._isImageProcessed = false;

}

void ofApp::readImageFromModel() {

updateResultImage();
_isImageSet = false;
ort._isImageProcessed = false;
}
}
16 changes: 7 additions & 9 deletions examples/fns_candy_style_transfer/src/ofApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class ThreadedInference : public ofThread {

void setup(const int width, const int height)
{
const ORTCHAR_T* modelName = L"candy.onnx";
ort = new ofxOrt(modelName, true);
ort = new ofxOrt(ORT_TSTR("data/model/candy.onnx"), true);
ort->printModelInfo();
_srcHWCPixels.allocate(width, height, OF_IMAGE_COLOR);
}
Expand Down Expand Up @@ -40,8 +39,8 @@ class ThreadedInference : public ofThread {
ofxOrtImageTensor<float> output_tensor(memory_info, pixCHW, _srcHWCPixels.getWidth(), _srcHWCPixels.getHeight());

ort->forward(Ort::RunOptions{ nullptr }, input_names,
&(input_tensor.getTensor()), 1, output_names,
&(output_tensor.getTensor()), 1);
& (input_tensor.getTensor()), 1, output_names,
& (output_tensor.getTensor()), 1);

_dstPixels.setFromAlignedPixels(output_tensor.getTexData().data(),
pixCHW.getWidth(), pixCHW.getHeight(),
Expand Down Expand Up @@ -73,21 +72,20 @@ class ofApp : public ofBaseApp {
void draw();
void exit() override;
void keyPressed(int key);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void updateResultImage();

void setImageToModel();
void readImageFromModel();

//ofFloatPixels inference(ofFloatPixels& content, int width, int height);


ofFloatPixels pixCHW;

ofFbo fbo;
ofVideoGrabber grabber;
ThreadedInference ort;
ofImage resultImage;
ofTexture resultImage;
bool isFboReady = false;
bool _isImageSet = false;

bool isMousePressed;
};
File renamed without changes.
5 changes: 2 additions & 3 deletions examples/mnist/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ void ofApp::setup() {
}

void ofApp::buildModel() {
const ORTCHAR_T *modelName = L"mnist.onnx";
ort = new ofxOrt(modelName, true);
ort = new ofxOrt(ORT_TSTR("data/model/mnist.onnx"), true);
}

void ofApp::inference() {
Expand Down Expand Up @@ -104,4 +103,4 @@ void ofApp::clearFbos() {
void ofApp::allocateFbos() {
screenFbo.allocate(ofGetWidth(), ofGetHeight(), GL_RGB);
sampleFbo.allocate(28, 28, GL_RGB);
}
}
1 change: 1 addition & 0 deletions examples/objectDetection/bin/data/model/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

9 changes: 4 additions & 5 deletions examples/objectDetection/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

//--------------------------------------------------------------
void ofApp::setup() {
ort = new ofxOrt(ORT_TSTR("tinyyolov2-8.onnx"), true);
ort = new ofxOrt(ORT_TSTR("data/model/tinyyolov2-8.onnx"), true);
ort->printModelInfo();

original.load("cat.jpg");
ofLoadImage(original, "image/cat.jpg");

fbo.allocate(416, 416, GL_RGB);
}
Expand All @@ -20,8 +20,7 @@ void ofApp::draw() {
original.draw(0.0, 0.0, fbo.getWidth(), fbo.getHeight());
fbo.end();

original.draw(0.0, 0.0, original.getWidth() * scale,
original.getHeight() * scale);
original.draw(0.0, 0.0, original.getWidth() * scale, original.getHeight() * scale);

ofFloatPixels pix;
ofFloatPixels chw;
Expand Down Expand Up @@ -154,4 +153,4 @@ void ofApp::drawBoundingBox(std::array<float, 4> &bbox, std::string &className,
ofDrawRectangle(0, 0.0, abs(bbox[0] - bbox[2]), abs(bbox[1] - bbox[3]));
ofFill();
ofPopMatrix();
}
}
2 changes: 1 addition & 1 deletion examples/objectDetection/src/ofApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ofApp : public ofBaseApp {
ofxOrt *ort;
ofFbo fbo;

ofFloatImage original;
ofTexture original;

const int IMG_WIDTH = 416;
const int IMG_HEIGHT = 416;
Expand Down
1 change: 1 addition & 0 deletions examples/poseEstimation/bin/data/model/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

4 changes: 1 addition & 3 deletions examples/poseEstimation/src/ofApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

//--------------------------------------------------------------
void ofApp::setup() {
const ORTCHAR_T *modelName = L"pose.onnx";
ort = new ofxOrt(modelName, true);

ort = new ofxOrt(ORT_TSTR("data/model/pose.onnx"), true);
ort->printModelInfo();

vid.load("danceInOcean.mp4");
Expand Down
10 changes: 8 additions & 2 deletions src/ofxOrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,23 @@ std::vector<std::string> ofxOrt::getSessionInputNames(size_t inputCount) const {
assert(session_);
Ort::AllocatorWithDefaultOptions defaultAllocator;
std::vector<std::string> inputNames;
std::vector<Ort::AllocatedStringPtr> inputNodeNameAllocatedStrings;
for (int i = 0; i < inputCount; i++) {
inputNames.push_back(session_->GetInputName(inputCount - i - 1, defaultAllocator));
auto input_name = session_->GetInputNameAllocated(inputCount - i - 1, defaultAllocator);
inputNodeNameAllocatedStrings.push_back(std::move(input_name));
inputNames.push_back(inputNodeNameAllocatedStrings.back().get());
}
return inputNames;
}
std::vector<std::string> ofxOrt::getSessionOutputNames(size_t outputCount) const {
assert(session_);
Ort::AllocatorWithDefaultOptions defaultAllocator;
std::vector<std::string> outputNames;
std::vector<Ort::AllocatedStringPtr> outputNodeNameAllocatedStrings;
for (int i = 0; i < outputCount; i++) {
outputNames.push_back(session_->GetOutputName(outputCount - i - 1, defaultAllocator));
auto output_name = session_->GetOutputNameAllocated(outputCount - i - 1, defaultAllocator);
outputNodeNameAllocatedStrings.push_back(std::move(output_name));
outputNames.push_back(outputNodeNameAllocatedStrings.back().get());
}
return outputNames;
}
Expand Down