Common Vision Blox 15.0
Loading...
Searching...
No Matches
Polimago - Search and Classify

 

In this example, we assume that classifiers have already been created and apply them to test images. The aim is to find all cookies in an image and then assign them to a cookie type. The two classifiers used are Cookies.psc (psc for polimago search classifier) and CookieType.pcc (pcc for polimago classification classifier). They can be found together with the image files and the TeachBench project files in the Polimago/Images/Cookies directory (%cvb%/Tutorial/Polimago/Images/Cookies) within the Common Vision Blox Tutorial folder.

#include <iostream>
#include <cvb/polimago/search_predictor.hpp>
#include <cvb/polimago/classification_predictor.hpp>
int main()
{
auto search_clf = Cvb::Polimago::SearchPredictor::Load(Cvb::InstallPath() + CVB_LIT("Tutorial/Polimago/Images/Cookies/Cookies.psc"));
auto classification_clf = Cvb::Polimago::ClassificationPredictor::Load(Cvb::InstallPath() + CVB_LIT("Tutorial/Polimago/Images/Cookies/CookieType.pcc"));
auto image = Cvb::Image::Load(Cvb::InstallPath() + CVB_LIT("Tutorial/Polimago/Images/Cookies/Test05.bmp"));
// Search for all cookies
auto searchres = search_clf->GridSearch(*image, image->Bounds(), 0.5, 0.0, 1.0);
for (int i = 0; i < searchres.size(); i++)
{
if (searchres[i].Quality() > 0.3)
{
// Classify a found cookie
auto pos = Cvb::Point2D<int>{ static_cast<int>(searchres[i].X()), static_cast<int>(searchres[i].Y()) };
auto clf_res = classification_clf->Classify(*image, pos);
std::wcout << searchres[i].X() << " " << searchres[i].Y() << " " << clf_res.Name() << "\n";
}
}
}
static std::unique_ptr< Image > Load(const String &fileName)
T X() const noexcept
static std::unique_ptr< ClassificationPredictor > Load(const String &fileName)
static std::unique_ptr< SearchPredictor > Load(const String &fileName)
String InstallPath()

var search_clf = new SearchPredictor(Environment.ExpandEnvironmentVariables(@"%CVB%\Tutorial\Polimago\Images\Cookies\Cookies.psc"));
var classification_clf = new ClassificationPredictor(Environment.ExpandEnvironmentVariables(@"%CVB%\Tutorial\Polimago\Images\Cookies\CookieType.pcc"));
var image = Image.FromFile(Environment.ExpandEnvironmentVariables(@"%CVB%\Tutorial\Polimago\Images\Cookies\Test05.bmp"));
// Search for all cookies
var searchres = search_clf.GridSearch(image, image.Bounds, 0.5, 0.0, 1.0);
for (int i = 0; i < searchres.Length; i++)
{
if (searchres[i].Quality > 0.3)
{
// Classify a found cookie
var pos = new Point2D((int)searchres[i].X, (int)searchres[i].Y);
var clf_res = classification_clf.Classify(image, pos);
Console.WriteLine("{0} {1} {2}", searchres[i].X, searchres[i].Y, clf_res.Name);
}
}
__int3264 Image

import os
import matplotlib.pyplot as plt
if __name__ == "__main__":
search_clf = cvb.polimago.SearchPredictor (os.path.join(cvb.install_path(), "Tutorial/Polimago/Images/Cookies/Cookies.psc"))
classify_clf = cvb.polimago.ClassificationPredictor (os.path.join(cvb.install_path(), "Tutorial/Polimago/Images/Cookies/CookieType.pcc"))
testimage = cvb.Image.load(os.path.join(cvb.install_path(), "Tutorial/Polimago/Images/Cookies/Test05.bmp"))
# Search for all cookies:
search_res, _ = search_clf.grid_search(testimage, testimage.bounds, grid_step=0.5, threshold=0.0, locality=1.0)
plt.imshow(cvb.as_array(testimage), 'grey')
for res in search_res:
if res.quality >= 0.3:
# Classify a found cookie
pos = cvb.Point2D(res.x, res.y)
clf_res, _ = classify_clf.classify (testimage, pos)
plt.scatter(res.x, res.y)
plt.text (res.x, res.y, clf_res.name)
plt.show()
cvb.Image load(str file_name)
str install_path()
numpy.array as_array(Any buffer, bool copy=False)

The above python code will produce the following image

The classified cookies.