From a9e0fa4faa03c5a5e71fde592cfdccf266caf7e2 Mon Sep 17 00:00:00 2001 From: Josh Klontz Date: Sun, 13 Sep 2015 13:14:57 -0400 Subject: [PATCH] initial interface for writing model files to likely --- app/br/br.cpp | 6 +++++- openbr/core/likely.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ openbr/core/likely.h | 27 +++++++++++++++++++++++++++ openbr/openbr.cpp | 6 ++++++ openbr/openbr.h | 2 ++ openbr/openbr_plugin.h | 1 + openbr/plugins/core/independent.cpp | 7 +++++++ openbr/plugins/core/pipe.cpp | 17 +++++++++++++++++ openbr/plugins/imgproc/quantize.cpp | 15 +++++++++++++++ 9 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 openbr/core/likely.cpp create mode 100644 openbr/core/likely.h diff --git a/app/br/br.cpp b/app/br/br.cpp index a1d9aa4..fccc042 100644 --- a/app/br/br.cpp +++ b/app/br/br.cpp @@ -189,6 +189,9 @@ public: } else if (!strcmp(fun, "deduplicate")) { check(parc == 3, "Incorrect parameter count for 'deduplicate'."); br_deduplicate(parv[0], parv[1], parv[2]); + } else if (!strcmp(fun, "likely")) { + check(parc == 3, "Incorrect parameter count for 'likely'."); + br_likely(parv[0], parv[1], parv[2]); } // Miscellaneous @@ -280,7 +283,8 @@ private: "-plotLandmarking ... {destination}\n" "-plotMetadata ... \n" "-project {output_gallery}\n" - "-deduplicate " + "-deduplicate \n" + "-likely \n" "-getHeader \n" "-setHeader {} \n" "- \n" diff --git a/openbr/core/likely.cpp b/openbr/core/likely.cpp new file mode 100644 index 0000000..7a13370 --- /dev/null +++ b/openbr/core/likely.cpp @@ -0,0 +1,53 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * Copyright 2015 Rank One Computing Corporation * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "likely.h" + +using namespace br; + +void br::Likely(const QString &inputType, const QString &outputType, const File &outputSourceFile) +{ + const QSharedPointer t(Transform::fromAlgorithm(Globals->algorithm)); + + QFile file(outputSourceFile.name); + if (!file.open(QFile::WriteOnly)) + qFatal("Failed to open Likely output source file for writing!"); + + file.write("; Automatically generated source code from:\n"); + file.write("; $ br -algorithm "); + file.write(Globals->algorithm.toLatin1()); + file.write(" -likely "); + file.write(inputType.toLatin1()); + file.write(" "); + file.write(outputType.toLatin1()); + file.write(" "); + file.write(outputSourceFile.flat().toLatin1()); + file.write("\n\n"); + + file.write("f :=\n"); + file.write(" src :->\n"); + + file.write(t->likely("")); + file.write("\n"); + + file.write("\n(extern "); + file.write(outputType.toLatin1()); + file.write(" "); + file.write(Globals->algorithm.toLower().toLatin1()); + file.write(" "); + file.write(inputType.toLatin1()); + file.write(" f)\n"); +} diff --git a/openbr/core/likely.h b/openbr/core/likely.h new file mode 100644 index 0000000..682d527 --- /dev/null +++ b/openbr/core/likely.h @@ -0,0 +1,27 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * Copyright 2015 Rank One Computing Corporation * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef BR_LIKELY_H +#define BR_LIKELY_H + +#include "openbr/openbr_plugin.h" + +namespace br +{ + void Likely(const QString &inputType, const QString &outputType, const File &outputSourceFile); +} + +#endif // BR_LIKELY_H diff --git a/openbr/openbr.cpp b/openbr/openbr.cpp index 0da35c8..7f15aae 100644 --- a/openbr/openbr.cpp +++ b/openbr/openbr.cpp @@ -20,6 +20,7 @@ #include "core/cluster.h" #include "core/eval.h" #include "core/fuse.h" +#include "core/likely.h" #include "core/plot.h" #include "core/qtutils.h" #include "plugins/openbr_internal.h" @@ -481,3 +482,8 @@ void br_deduplicate(const char *input_gallery, const char *output_gallery, const { br::Deduplicate(input_gallery, output_gallery, threshold); } + +void br_likely(const char *input_type, const char *output_type, const char *output_source_file) +{ + br::Likely(input_type, output_type, output_source_file); +} diff --git a/openbr/openbr.h b/openbr/openbr.h index 776d385..b7a2fc9 100644 --- a/openbr/openbr.h +++ b/openbr/openbr.h @@ -115,6 +115,8 @@ BR_EXPORT const char *br_version(); BR_EXPORT void br_slave_process(const char * baseKey); +BR_EXPORT void br_likely(const char *input_type, const char *output_type, const char *output_source_file); + // to avoid having to include unwanted headers // this will be this header's conception of a Template // any functions that need a Template pointer diff --git a/openbr/openbr_plugin.h b/openbr/openbr_plugin.h index 7cc742b..5ae5069 100644 --- a/openbr/openbr_plugin.h +++ b/openbr/openbr_plugin.h @@ -813,6 +813,7 @@ public: } virtual Transform * simplify(bool &newTransform) { newTransform = false; return this; } + virtual QByteArray likely(const QByteArray &indentation) const { (void) indentation; return "src"; } protected: Transform(bool independent = true, bool trainable = true); diff --git a/openbr/plugins/core/independent.cpp b/openbr/plugins/core/independent.cpp index 8c048ea..1096227 100644 --- a/openbr/plugins/core/independent.cpp +++ b/openbr/plugins/core/independent.cpp @@ -222,6 +222,13 @@ class IndependentTransform : public MetaTransform for (int i=0; iload(stream); } + + QByteArray likely(const QByteArray &indentation) const + { + if (transforms.size() != 1) + return "src"; // TODO: implement + return transforms.first()->likely(indentation); + } }; BR_REGISTER(Transform, IndependentTransform) diff --git a/openbr/plugins/core/pipe.cpp b/openbr/plugins/core/pipe.cpp index a19c306..a29a6e0 100644 --- a/openbr/plugins/core/pipe.cpp +++ b/openbr/plugins/core/pipe.cpp @@ -179,6 +179,23 @@ class PipeTransform : public CompositeTransform CompositeTransform::init(); } + QByteArray likely(const QByteArray &indentation) const + { + QByteArray result; + result.append("{\n"); + foreach (Transform *t, transforms) { + const QByteArray dst = t->likely(indentation + " "); + if (dst == "src") + continue; // Not implemented + result.append(indentation + " src := "); + result.append(dst); + result.append("\n"); + } + + result.append(indentation + " src\n}"); + return result; + } + protected: // Template list project -- process templates in parallel through Transform::project // or if parallelism is disabled, handle them sequentially diff --git a/openbr/plugins/imgproc/quantize.cpp b/openbr/plugins/imgproc/quantize.cpp index fa24cef..3bd911c 100644 --- a/openbr/plugins/imgproc/quantize.cpp +++ b/openbr/plugins/imgproc/quantize.cpp @@ -48,6 +48,21 @@ class QuantizeTransform : public Transform { src.m().convertTo(dst, CV_8U, a, b); } + + QByteArray likely(const QByteArray &indentation) const + { + QByteArray result; + result.append("\n" + indentation + "{ ; Quantize\n"); + result.append(indentation + " dst := (imitate-size src (imitate-dimensions u8 src.type))\n"); + result.append(indentation + " (dst src) :=>\n"); + result.append(indentation + " dst :<- src.(* "); + result.append(QByteArray::number(a, 'g', 9)); + result.append(").(+ "); + result.append(QByteArray::number(b, 'g', 9)); + result.append(")\n"); + result.append(indentation + "}"); + return result; + } }; BR_REGISTER(Transform, QuantizeTransform) -- libgit2 0.21.4