00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "thirdparty/CVML.h"
00024
00025 namespace cmlabs {
00026
00027
00028
00029
00030
00031
00032 CVMLHypothesis::CVMLHypothesis() {
00033 }
00034
00035 CVMLHypothesis::CVMLHypothesis(JString xml) {
00036 evaluation = 0;
00037 if (!Object::fromXML(xml)) {
00038 }
00039 }
00040
00041 CVMLHypothesis::CVMLHypothesis(XMLNode* node) {
00042 evaluation = 0;
00043 if (!fromXML(node)) {
00044 }
00045 }
00046
00047 CVMLHypothesis::~CVMLHypothesis() {}
00048
00049 Object* CVMLHypothesis::clone() const {
00050 CVMLHypothesis* fv = new CVMLHypothesis();
00051 fv->id = id;
00052 fv->previous = previous;
00053 fv->evaluation = evaluation;
00054 fv->parameters.copyAll(parameters);
00055 fv->evaluations.copyAll(evaluations);
00056 return fv;
00057 }
00058
00059 bool CVMLHypothesis::setLabel(JString label, JString value, double eval) {
00060 if (label.length() == 0)
00061 return false;
00062 parameters.put(label, value);
00063 evaluations.put(label, JString(eval));
00064 return true;
00065 }
00066
00067 bool CVMLHypothesis::setPrevious(JString prev) {
00068 previous = prev;
00069 return true;
00070 }
00071
00072 bool CVMLHypothesis::setEvaluation(double eval) {
00073 evaluation = eval;
00074 return true;
00075 }
00076
00077 Collection CVMLHypothesis::getLabels() {
00078 return parameters.getAllKeys();
00079 }
00080
00081
00082 JString CVMLHypothesis::getLabelValue(JString label) {
00083 return parameters.get(label);
00084 }
00085
00086 double CVMLHypothesis::getLabelEval(JString label) {
00087 return evaluations.get(label).toDouble();
00088 }
00089
00090 bool CVMLHypothesis::setID(JString newID) {
00091 id = newID;
00092 return true;
00093 }
00094
00095 JString CVMLHypothesis::getID() {
00096 return id;
00097 }
00098
00099 JString CVMLHypothesis::getPrevious() {
00100 return previous;
00101 }
00102
00103 double CVMLHypothesis::getEvaluation() {
00104 return evaluation;
00105 }
00106
00107 JString CVMLHypothesis::print() {
00108 return toXML();
00109 }
00110
00111 JString CVMLHypothesis::toXML() {
00112 JString xml, key, value;
00113 double eval;
00114 for (int n=0; n<parameters.getCount(); n++) {
00115 key = parameters.getKey(n);
00116 if (key.length() > 0) {
00117 value = parameters.get(n);
00118 eval = evaluations.get(key).toDouble();
00119 xml += JString::format("<%s evaluation=\"%f\">%s</%s>\n",
00120 (char*) key.xmlStringEncode(), eval, (char*) value.xmlStringEncode(), (char*) key.xmlStringEncode());
00121 }
00122 }
00123 if (xml.length() > 0)
00124 return JString::format("<hypothesis id=\"%s\" prev=\"%s\" evaluation=\"%f\">\n%s</hypothesis>\n", (char*) id, (char*) previous, evaluation, (char*) xml.indentXML());
00125 else
00126 return JString::format("<hypothesis id=\"%s\" prev=\"%s\" evaluation=\"%f\">\n</hypothesis>\n", (char*) id, (char*) previous, evaluation);
00127 }
00128
00129 bool CVMLHypothesis::fromXML(XMLNode* node) {
00130 if (node == NULL) {
00131 return false;
00132 }
00133 if (!node->getTag().equalsIgnoreCase("hypothesis"))
00134 return false;
00135
00136 id = node->getAttribute("id");
00137 previous = node->getAttribute("prev");
00138 evaluation = node->getAttribute("evaluation").toDouble();
00139
00140 ObjectCollection* nodes = node->getChildTags();
00141 JString key, value;
00142 double eval;
00143 XMLNode* xmlNode;
00144 if (nodes != NULL) {
00145 for (int n=0; n<nodes->getCount(); n++) {
00146 if ( (xmlNode = (XMLNode*) nodes->get(n)) != NULL) {
00147 key = xmlNode->getTag().toLowerCase();
00148 value = xmlNode->getText().toLowerCase();
00149 eval = xmlNode->getAttribute("evaluation").toDouble();
00150 parameters.put(key, value);
00151 evaluations.put(key, JString(eval));
00152 }
00153 }
00154 }
00155
00156 return true;
00157 }
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180 CVMLObjectBase::CVMLObjectBase() {
00181 type = "objectbase";
00182 }
00183
00184 CVMLObjectBase::CVMLObjectBase(JString xml) {
00185 if (!Object::fromXML(xml)) {
00186 }
00187 type = "objectbase";
00188 }
00189
00190 CVMLObjectBase::CVMLObjectBase(XMLNode* node) {
00191 if (!fromXML(node)) {
00192 }
00193 type = "objectbase";
00194 }
00195
00196 CVMLObjectBase::~CVMLObjectBase() {}
00197
00198 Object* CVMLObjectBase::clone() const {
00199 CVMLObjectBase* fv = new CVMLObjectBase();
00200 fv->id = id;
00201 fv->type = type;
00202 fv->appearance = appearance;
00203 fv->box = box;
00204 fv->hypotheses.copyAll(hypotheses);
00205 fv->features.copyAll(features);
00206 fv->members.copyAll(members);
00207 return fv;
00208 }
00209
00210 bool CVMLObjectBase::setBox(double x, double y, double w, double h, double orientation) {
00211 box.set(x, y, w, h);
00212 box.orientation = orientation;
00213 return true;
00214 }
00215
00216 bool CVMLObjectBase::setID(JString newID) {
00217 id = newID;
00218 return true;
00219 }
00220
00221 JString CVMLObjectBase::getID() {
00222 return id;
00223 }
00224
00225 bool CVMLObjectBase::setBox(Box b) {
00226 box = b;
00227 return true;
00228 }
00229
00230 Box CVMLObjectBase::getBox() {
00231 return box;
00232 }
00233
00234 bool CVMLObjectBase::setAppearance(JString appear) {
00235 appearance = appear;
00236 return true;
00237 }
00238
00239 bool CVMLObjectBase::addHypothesis(CVMLHypothesis* hyp) {
00240 if (hyp == NULL)
00241 return false;
00242 return hypotheses.put(hyp->id, hyp);
00243 }
00244
00245 bool CVMLObjectBase::addFeature(JString featurename, JString content) {
00246 return features.put(featurename, content);
00247 }
00248
00249 Dictionary CVMLObjectBase::getFeatureList() {
00250 return features;
00251 }
00252
00253 JString CVMLObjectBase::getAppearance() {
00254 return appearance;
00255 }
00256
00257 CVMLHypothesis* CVMLObjectBase::getHypothesis(JString hypID) {
00258 return (CVMLHypothesis*) hypotheses.get(hypID);
00259 }
00260
00261 Collection CVMLObjectBase::getHypothesesIDs() {
00262 return hypotheses.getAllKeys();
00263 }
00264
00265
00266
00267 JString CVMLObjectBase::print() {
00268 return toXML();
00269 }
00270
00271 JString CVMLObjectBase::toXML() {
00272 JString xml = JString::format("<orientation>%f</orientation>\n", box.orientation);
00273 xml += JString::format("<box x=\"%f\" y=\"%f\" w=\"%f\" h=\"%f\" />\n", box.getLeftX(), box.getUpperY(), box.getWidth(), box.getHeight());
00274
00275 xml += JString::format("<appearance>%s</appearance>\n", (char*)appearance.xmlStringEncode());
00276
00277 if (members.getCount() > 0) {
00278 xml += JString::format("<members>%s</members>\n", (char*) members.printListLine(",").xmlStringEncode());
00279 }
00280
00281 JString hypXML;
00282 for (CVMLHypothesis* hyp = (CVMLHypothesis*) hypotheses.getFirst(); hyp != NULL; hyp = (CVMLHypothesis*) hypotheses.getNext()) {
00283 hypXML += hyp->toXML();
00284 }
00285 if (hypXML.length() > 0)
00286 xml += JString::format("<hypothesislist>\n%s</hypothesislist>\n", (char*) hypXML.indentXML());
00287
00288 JString featureXML;
00289 for (JString feat = features.getFirstKey(); feat.length() > 0; feat = features.getNextKey()) {
00290 featureXML += JString::format("<%s>%s</%s>\n", (char*) feat, (char*) features.get(feat), (char*) feat);
00291 }
00292 if (featureXML.length() > 0)
00293 xml += JString::format("<featurelist>\n%s</featurelist>\n", (char*) featureXML.indentXML());
00294
00295 return JString::format("<%s id=\"%s\">\n%s</%s>\n", (char*) type.xmlStringEncode(), (char*) id.xmlStringEncode(), (char*) xml.indentXML(), (char*) type.xmlStringEncode());
00296 }
00297
00298 bool CVMLObjectBase::fromXML(XMLNode* node) {
00299 if (node == NULL) {
00300 return false;
00301 }
00302
00303
00304
00305 id = node->getAttribute("id");
00306
00307 ObjectCollection* nodes = node->getChildTags();
00308
00309 CVMLHypothesis* hyp;
00310 XMLNode* xmlNode;
00311 XMLNode* subNode;
00312 ObjectCollection* subNodes;
00313
00314 if (nodes != NULL) {
00315 for (int n=0; n<nodes->getCount(); n++) {
00316 if ( (xmlNode = (XMLNode*) nodes->get(n)) != NULL) {
00317 if (xmlNode->getTag().equalsIgnoreCase("orientation"))
00318 box.orientation = xmlNode->getText().toDouble();
00319 else if (xmlNode->getTag().equalsIgnoreCase("box"))
00320 box.fromXML(xmlNode);
00321
00322
00323 else if (xmlNode->getTag().equalsIgnoreCase("appearance"))
00324 appearance = xmlNode->getText();
00325 else if (xmlNode->getTag().equalsIgnoreCase("members"))
00326 members = xmlNode->getText().split(",");
00327 else if (xmlNode->getTag().equalsIgnoreCase("hypothesislist")) {
00328 if ( (subNodes = xmlNode->getChildTags()) != NULL) {
00329 for (subNode = (XMLNode*) subNodes->getFirst(); subNode != NULL; subNode = (XMLNode*) subNodes->getNext()) {
00330 if (subNode->getTag().equalsIgnoreCase("hypothesis")) {
00331 hyp = new CVMLHypothesis(subNode);
00332 hypotheses.put(hyp->id, hyp);
00333 }
00334 }
00335 }
00336 }
00337 else if (xmlNode->getTag().equalsIgnoreCase("featurelist")) {
00338 if ( (subNodes = xmlNode->getChildTags()) != NULL) {
00339 for (subNode = (XMLNode*) subNodes->getFirst(); subNode != NULL; subNode = (XMLNode*) subNodes->getNext()) {
00340 features.put(subNode->getTag(), subNode->getText());
00341 }
00342 }
00343 }
00344 }
00345 }
00346 }
00347
00348 return true;
00349 }
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373 CVMLObject::CVMLObject() : CVMLObjectBase() {
00374 type = "object";
00375 }
00376
00377 CVMLObject::CVMLObject(JString xml) : CVMLObjectBase(xml) {
00378 type = "object";
00379 }
00380
00381 CVMLObject::CVMLObject(XMLNode* node) : CVMLObjectBase(node) {
00382 type = "object";
00383 }
00384
00385 CVMLObject::~CVMLObject() {}
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400 CVMLGroup::CVMLGroup() : CVMLObjectBase() {
00401 type = "group";
00402 }
00403
00404 CVMLGroup::CVMLGroup(JString xml) : CVMLObjectBase(xml) {
00405 type = "group";
00406 }
00407
00408 CVMLGroup::CVMLGroup(XMLNode* node) : CVMLObjectBase(node) {
00409 type = "group";
00410 }
00411
00412 CVMLGroup::~CVMLGroup() {}
00413
00414
00415 bool CVMLGroup::addMember(JString id) {
00416 return members.add(id);
00417 }
00418
00419 Collection CVMLGroup::getMembers() {
00420 return members;
00421 }
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450 CVMLFrame::CVMLFrame() {
00451 }
00452
00453 CVMLFrame::CVMLFrame(JString xml) {
00454 if (!Object::fromXML(xml)) {
00455 }
00456 }
00457
00458 CVMLFrame::CVMLFrame(XMLNode* node) {
00459 if (!fromXML(node)) {
00460 }
00461 }
00462
00463 CVMLFrame::~CVMLFrame() {}
00464
00465 Object* CVMLFrame::clone() const {
00466 CVMLFrame* fv = new CVMLFrame();
00467 fv->id = id;
00468 fv->objects.copyAll(this->objects);
00469 fv->groups.copyAll(this->groups);
00470 return fv;
00471 }
00472
00473 bool CVMLFrame::addObject(CVMLObject* object) {
00474 return objects.put(object->id, object);
00475 }
00476
00477 bool CVMLFrame::addGroup(CVMLGroup* group) {
00478 return groups.put(group->id, group);
00479 }
00480
00481 bool CVMLFrame::removeObject(JString id) {
00482 return objects.remove(id);
00483 }
00484
00485 bool CVMLFrame::removeGroup(JString id) {
00486 return groups.remove(id);
00487 }
00488
00489
00490 JString CVMLFrame::print() {
00491 return toXML();
00492 }
00493
00494 JString CVMLFrame::toXML() {
00495 JString xml;
00496 int n;
00497 CVMLObject* object;
00498 CVMLGroup* group;
00499
00500 xml += "<objectlist>\n";
00501 for (n=0; n<objects.getCount(); n++) {
00502 if ( (object = ((CVMLObject*) objects.get(n))) != NULL ) {
00503 xml += object->toXML().indentXML();
00504 }
00505 }
00506 xml += "</objectlist>\n";
00507
00508 xml += "<grouplist>\n";
00509 for (n=0; n<groups.getCount(); n++) {
00510 if ( (group = ((CVMLGroup*) groups.get(n))) != NULL ) {
00511 xml += group->toXML().indentXML();
00512 }
00513 }
00514 xml += "</grouplist>\n";
00515
00516 return JString::format("<frame number=\"%s\">\n%s</frame>\n", (char*) id.xmlStringEncode(), (char*) xml.indentXML());
00517 }
00518
00519 bool CVMLFrame::fromXML(XMLNode* node) {
00520 if (node == NULL) {
00521 return false;
00522 }
00523 if (!node->getTag().equalsIgnoreCase("frame"))
00524 return false;
00525
00526 id = node->getAttribute("number");
00527
00528 CVMLObject* object;
00529 CVMLGroup* group;
00530 XMLNode* subNode;
00531
00532 XMLNode* objectList = node->getChildNode("objectlist");
00533 if (objectList != NULL) {
00534 ObjectCollection* objectNodes = objectList->getChildTags();
00535 if (objectNodes != NULL) {
00536 for (subNode = (XMLNode*) objectNodes->getFirst(); subNode != NULL; subNode = (XMLNode*) objectNodes->getNext()) {
00537 object = new CVMLObject(subNode);
00538 objects.put(object->id, object);
00539 }
00540 }
00541 }
00542
00543 XMLNode* groupList = node->getChildNode("grouplist");
00544 if (groupList != NULL) {
00545 ObjectCollection* groupNodes = groupList->getChildTags();
00546 if (groupNodes != NULL) {
00547 for (subNode = (XMLNode*) groupNodes->getFirst(); subNode != NULL; subNode = (XMLNode*) groupNodes->getNext()) {
00548 group = new CVMLGroup(subNode);
00549 groups.put(group->id, group);
00550 }
00551 }
00552 }
00553
00554 return true;
00555 }
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571 CVMLDataSet::CVMLDataSet() {
00572 }
00573
00574 CVMLDataSet::CVMLDataSet(JString xml) {
00575 if (!Object::fromXML(xml)) {
00576 }
00577 }
00578
00579 CVMLDataSet::CVMLDataSet(XMLNode* node) {
00580 if (!fromXML(node)) {
00581 }
00582 }
00583
00584 CVMLDataSet::~CVMLDataSet() {}
00585
00586 Object* CVMLDataSet::clone() const {
00587 CVMLDataSet* fv = new CVMLDataSet();
00588 fv->name = name;
00589 fv->frames.copyAll(this->frames);
00590 return fv;
00591 }
00592
00593 bool CVMLDataSet::addFrame(CVMLFrame* frame) {
00594 return frames.put(frame->id, frame);
00595 }
00596
00597 bool CVMLDataSet::removeFrame(JString id) {
00598 return frames.remove(id);
00599 }
00600
00601
00602 JString CVMLDataSet::print() {
00603 return toXML();
00604 }
00605
00606 JString CVMLDataSet::toXML() {
00607 JString xml;
00608 CVMLFrame* frame;
00609
00610 for (frame = (CVMLFrame*) frames.getFirst(); frame != NULL; frame = (CVMLFrame*) frames.getNext()) {
00611
00612 xml += frame->toXML().indentXML();
00613 }
00614
00615
00616 return JString::format("<dataset name=\"%s\">\n%s</dataset>\n", (char*) name.xmlStringEncode(), (char*) xml.indentXML());
00617 }
00618
00619 bool CVMLDataSet::fromXML(XMLNode* node) {
00620 if (node == NULL) {
00621 return false;
00622 }
00623 if (!node->getTag().equalsIgnoreCase("dataset"))
00624 return false;
00625
00626 name = node->getAttribute("name");
00627
00628 CVMLFrame* frame;
00629 XMLNode* subNode;
00630
00631 ObjectCollection* frameNodes = node->getChildTags();
00632 if (frameNodes != NULL) {
00633 for (subNode = (XMLNode*) frameNodes->getFirst(); subNode != NULL; subNode = (XMLNode*) frameNodes->getNext()) {
00634 frame = new CVMLFrame(subNode);
00635 frames.put(frame->id, frame);
00636 }
00637 }
00638
00639 return true;
00640 }
00641
00642 int CVMLDataSet::getCount() {
00643 return frames.getCount();
00644 }
00645
00646
00647
00648
00649 bool CVMLDataSet::unitTest(int max) {
00650
00651 StopWatch watch;
00652 watch.start();
00653 CVMLDataSet dataset;
00654
00655 int n, m, k;
00656
00657 CVMLFrame* frame;
00658 CVMLObject* object;
00659 CVMLGroup* group;
00660 CVMLHypothesis* hyp;
00661
00662 int count = 0;
00663 for (n=0; n<max; n++) {
00664 frame = new CVMLFrame();
00665 frame->id = JString(count++);
00666
00667 for (m=0; m<3; m++) {
00668 object = new CVMLObject();
00669 object->setAppearance("Visible");
00670 object->setBox(10, 20, 30, 40, 0);
00671 object->setID(JString(count++));
00672 for (k=0; k<3; k++) {
00673 hyp = new CVMLHypothesis();
00674 hyp->setID(JString(count++));
00675 hyp->setLabel("Moving", "Walking", 1.0);
00676 hyp->setEvaluation(1.0);
00677 object->addHypothesis(hyp);
00678 }
00679 frame->addObject(object);
00680
00681 group = new CVMLGroup();
00682 group->setAppearance("Visible");
00683 group->setBox(10, 20, 30, 40, 0);
00684 group->setID(JString(count++));
00685 group->addMember(JString(1));
00686 group->addMember(JString(2));
00687 group->addMember(JString(3));
00688 for (k=0; k<3; k++) {
00689 hyp = new CVMLHypothesis();
00690 hyp->setID(JString(count++));
00691 hyp->setLabel("Moving", "Walking", 1.0);
00692 hyp->setEvaluation(1.0);
00693 group->addHypothesis(hyp);
00694 }
00695 frame->addGroup(group);
00696 }
00697 dataset.addFrame(frame);
00698 }
00699
00700 watch.setMark(JString::format("Creating set with %d frames", max));
00701
00702 if (dataset.getCount() != max) {
00703 printf("Dataset only contains %d frames and not %d\n", dataset.getCount(), max);
00704 return false;
00705 }
00706
00707 JString xml = dataset.toXML();
00708 watch.setMark("DataSet toXML()");
00709
00710 XMLParser parser;
00711 parser.parseXML(xml);
00712 watch.setMark("Parsing raw XML");
00713
00714 XMLNode* node = parser.getRootNode();
00715
00716 CVMLDataSet dataset2;
00717 dataset2.fromXML(node);
00718 watch.setMark("DataSet fromXML(rootnode)");
00719
00720 JString xml2 = dataset.toXML();
00721 watch.setMark("DataSet toXML() again");
00722
00723 if (!xml.equalsIgnoreCase(xml2)) {
00724 watch.setMark("DataSet second toXML() does not match first toXML()");
00725 return false;
00726 }
00727
00728 watch.stop();
00729 printf("%s", (char*) watch.print());
00730
00731 return true;
00732 }
00733
00734
00735 }
00736