Skip to content

Instantly share code, notes, and snippets.

@alumican
Created January 14, 2012 15:32
Show Gist options
  • Save alumican/1611795 to your computer and use it in GitHub Desktop.
Save alumican/1611795 to your computer and use it in GitHub Desktop.
openFrameworksとZinniaで手書き文字認識
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup()
{
ofSetDataPathRoot("data/");
ofBackground(0);
//setup recognizer
recognizer = zinnia::Recognizer::create();
if (!recognizer->open(ofToDataPath("handwriting-ja.model").c_str())) {
cerr << recognizer->what() << endl;
return;
}
//setup character
character = zinnia::Character::create();
//
isWriting = false;
result = NULL;
}
//--------------------------------------------------------------
void testApp::exit()
{
clearCharacter();
delete character;
delete recognizer;
}
//--------------------------------------------------------------
void testApp::update()
{
}
//--------------------------------------------------------------
void testApp::draw()
{
//draw character
if (lines.size() > 0) {
ofSetColor(128);
ofSetLineWidth(3);
vector<ofPolyline>::iterator it;
for (it = lines.begin(); it != lines.end(); ++it) {
it->draw();
}
}
//usage
stringstream ss;
ss << "1. drag mouse and draw single character on window.\n"
<< "2. recognition result is shown on console.\n"
<< "3. press any key to clear character.";
ofSetColor(255);
ofDrawBitmapString(ss.str(), 10, 20);
}
//--------------------------------------------------------------
void testApp::keyPressed(int key)
{
clearCharacter();
}
//--------------------------------------------------------------
void testApp::keyReleased(int key)
{
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y)
{
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button)
{
addPoint();
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button)
{
if (isWriting) {
startStroke();
} else {
startCharacter();
}
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button)
{
endStroke();
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h)
{
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg)
{
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo)
{
}
//--------------------------------------------------------------
void testApp::startCharacter()
{
clearCharacter();
oldX = mouseX;
oldY = mouseY;
strokeIndex = -1;
isWriting = true;
character->set_width(ofGetWidth());
character->set_height(ofGetHeight());
startStroke();
}
//--------------------------------------------------------------
void testApp::startStroke()
{
++strokeIndex;
character->add(strokeIndex, mouseX, mouseY);
ofPolyline line;
lines.push_back(line);
lines[strokeIndex].addVertex(mouseX, mouseY);
}
//--------------------------------------------------------------
void testApp::addPoint()
{
if (ofDist(oldX, oldY, mouseX, mouseY) > 10)
{
character->add(strokeIndex, mouseX, mouseY);
lines[strokeIndex].addVertex(mouseX, mouseY);
oldX = mouseX;
oldY = mouseY;
}
}
//--------------------------------------------------------------
void testApp::endStroke()
{
//add last point
character->add(strokeIndex, mouseX, mouseY);
lines[strokeIndex].addVertex(mouseX, mouseY);
//recognize
if (result != NULL) delete result;
result = recognizer->classify(*character, 10);
if (result == NULL) {
cerr << recognizer->what() << endl;
} else {
//draw result
ofSetColor(255, 0, 0);
string s = "(word)\t(score)\n";
for (size_t i = 0; i < result->size(); ++i) {
s += (string)result->value(i) + "\t" + ofToString(result->score(i)) + "\n";
}
cout<<"----------------------------------------"<<endl<<s;
}
}
//--------------------------------------------------------------
void testApp::clearCharacter()
{
isWriting = false;
character->clear();
lines.clear();
if (result != NULL) {
delete result;
result = NULL;
}
}
#pragma once
#include "ofMain.h"
#include "zinnia.h"
class testApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
void exit();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y);
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
void startCharacter();
void startStroke();
void addPoint();
void endStroke();
void clearCharacter();
zinnia::Recognizer *recognizer;
zinnia::Character *character;
zinnia::Result *result;
int oldX;
int oldY;
int strokeIndex;
bool isWriting;
vector<ofPolyline> lines;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment