5from result_model
import ResultModel
6from classification
import Classification
8from PySide2.QtCore
import QObject, QUrl, QAbstractListModel, Qt, QModelIndex
9from PySide2.QtQml
import QQmlApplicationEngine, qmlRegisterType
10from PySide2.QtGui
import QGuiApplication, QIcon
13if __name__ ==
"__main__":
15 app = QGuiApplication([])
16 app.setOrganizationName(
'STEMMER IMAGING')
17 app.setOrganizationDomain(
'https://www.stemmer-imaging.com/')
18 app.setApplicationName(
'Polimago Python tutorial')
21 if sys.platform ==
'win32':
23 myappid =
u'stemmerimaging.commonvisionblox.pycookieclassification.0'
24 ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
26 app.setWindowIcon(QIcon(
'Tutorial-Python_32x32.png'))
31 cvb.AcquisitionStack.Vin)
35 result_model = ResultModel(image_controller)
38 classification = Classification(device, result_model)
44 engine = QQmlApplicationEngine()
45 context = engine.rootContext()
48 context.setContextProperty(
"mainImage", image_controller)
50 context.setContextProperty(
"resultModel", result_model)
52 context.setContextProperty(
"classification", classification)
55 engine.load(os.path.join(os.path.dirname(os.path.abspath(__file__)),
"main.qml"))
Union[cvb.GenICamDevice, cvb.VinDevice, cvb.EmuDevice, cvb.VideoDevice, cvb.NonStreamingDevice] open(str provider, int acquisition_stack=cvb.AcquisitionStack.PreferVin)
Opens a device with the given provider and acquisition stack.
Definition: __init__.py:1327
Controller object for the QML image view item.
Definition: __init__.py:14
None register(cls, str uri="CvbQuick", int version_major=1, int version_minor=0, str qml_name="ImageLabel")
Convenience method to register this type or a derived type in QML.
Definition: __init__.py:122
None register(cls, str uri="CvbQuick", int version_major=1, int version_minor=0, str qml_name="ImageView")
Convenience method to register this type or a derived type in QML.
Definition: __init__.py:193
Common Vision Blox UI module for Python.
Definition: __init__.py:1
str install_path()
Directory Common Vision Blox has been installed to.
Definition: __init__.py:7146
6from PySide2
import QtCore
7from PySide2.QtCore
import QObject, Qt, QUrl, Property, Signal, Slot
9from result_model
import ResultModel, PolimagoResult
18class Classification(QtCore.QObject):
20 def __init__(self, device, result_model):
23 self._stream = device.stream()
24 self._model = result_model
29 self._search_predictor =
""
30 self._classification_predictor =
""
32 self.notify_source.connect(self.source_changed)
33 self.notify_search_predictor.connect(self.search_predictor_changed)
34 self.notify_classification_predictor.connect(self.classification_predictor_changed)
39 self._model.update([
None] * 0)
40 self._image, wait_status = self._stream.get_timed_snapshot(1000)
41 if (wait_status != cvb.WaitStatus.Ok):
44 self._model.refresh(self._image)
49 if self._search == 0
or self._classification == 0:
50 sys.stderr.write(
'No search predictor or classification predictor was loaded.\n')
54 search_res, calls = self._search.grid_search(self._image, self._image.bounds, GRID_STEP, THRESHOLD, LOCALITY)
56 polimago_results = [
None] * 0
58 for res
in search_res:
60 if res.quality >= 0.3:
63 clf_res, confidence_distribution = self._classification.classify(self._image, pos)
64 polimago_results.append(PolimagoResult(pos, res.quality, clf_res))
67 self._model.update(polimago_results)
72 def set_source(self, imageSource):
73 self._source = imageSource
74 self.notify_source.emit()
76 def search_predictor(self):
77 return self._search_predictor
79 def set_search_predictor(self, searchPredictor):
80 self._search_predictor = searchPredictor
81 self.notify_search_predictor.emit()
83 def classification_predictor(self):
84 return self._classification_predictor
86 def set_classification_predictor(self, classificationPredictor):
87 self._classification_predictor = classificationPredictor
88 self.notify_classification_predictor.emit()
91 def notify_source(self):
95 def notify_search_predictor(self):
99 def notify_classification_predictor(self):
103 def source_changed(self):
105 self._model.update([])
108 self._classification = 0
112 path = self._source.toLocalFile()
113 if path.endswith(
".vin"):
115 path, cvb.AcquisitionStack.Vin)
116 self._stream = self._device.stream()
118 elif path.endswith(
".bmp"):
120 self._model.refresh(self._image)
123 def search_predictor_changed(self):
124 if not self.search_predictor().isEmpty():
128 def classification_predictor_changed(self):
129 if not self.classification_predictor().isEmpty():
132 imageSource = Property(QUrl, source, set_source)
133 searchPredictor = Property(QUrl, search_predictor, set_search_predictor)
134 classificationPredictor = Property(QUrl, classification_predictor, set_classification_predictor)
The Common Vision Blox image.
Definition: __init__.py:1737
Multi-purpose 2D vector class.
Definition: __init__.py:3406
Predictor to classify patterns with.
Definition: __init__.py:20
Predictor that may be used for searching objects.
Definition: __init__.py:544
Common Vision Blox Polimago module for Python.
Definition: __init__.py:1
6from PySide2.QtCore
import QObject, QAbstractListModel, Qt, QModelIndex, Property, Signal, Slot
8class PolimagoResult(object):
10 def __init__(self, pos, pos_quality, clf_res):
12 self._pos_quality = pos_quality
13 self._name = clf_res.name
14 self._color_quality = clf_res.quality
22 def position_quality(self):
23 return self._pos_quality
25 def color_quality(self):
26 return self._color_quality
28class ResultModel(QAbstractListModel):
30 LineText = Qt.UserRole
31 StartPosition = Qt.UserRole + 1
32 Quality = Qt.UserRole + 2
34 def __init__(self, image_controller, parent=None):
35 super(ResultModel, self).__init__(parent)
36 self._image_controller = image_controller
37 self._results = [
None] * 0
39 def update(self, polimago_results):
40 self._results = polimago_results
41 self.layoutChanged.emit()
45 roles[ResultModel.LineText] = b
"lineText"
46 roles[ResultModel.StartPosition] = b
"startPosition"
47 roles[ResultModel.Quality] = b
"quality"
50 def rowCount(self, parent = QModelIndex()):
51 return len(self._results)
53 def data(self, index, role = Qt.DisplayRole):
54 if not index.isValid():
57 result = self._results[index.row()]
59 if role == ResultModel.LineText:
61 elif role == ResultModel.StartPosition:
63 elif role == ResultModel.Quality:
64 return result.position_quality()
69 def refresh(self, image):
70 self._image_controller.refresh(image)
PySide2.QtCore.QPointF cvb_to_qt_point(cvb.Point2D point)
Convenience converter for points.
Definition: __init__.py:381
2import CvbQuick 1.0 as CvbQuick
3import QtQuick.Controls 1.3
4import QtQuick.Layouts 1.2
5import QtQuick.Dialogs 1.2
11 property int margin: 11
19 anchors.margins: margin
25 Layout.fillWidth: true
26 Layout.fillHeight: true
34 imageX : startPosition.x
35 imageY : startPosition.y
48 onClicked: openImageDialog.open()
53 text: "Load Search Predictor"
54 onClicked: loadSearchPredictorDialog.open()
59 text: "Load Classification Predictor"
60 onClicked: loadClassificationPredictorDialog.open()
67 onClicked: classification.snap()
73 onClicked: classification.classify()
84 nameFilters: [ "Image source files (*.bmp)", "Common Vision Blox driver files (*.vin)" ]
87 classification.imageSource = openImageDialog.fileUrl;
88 var path = classification.imageSource.toString();
89 if (path.endsWith(".bmp"))
90 btnSnap.enabled = false
92 btnSnap.enabled = true
98 id: loadSearchPredictorDialog
99 title: "Load Search Predictor"
101 nameFilters: [ "Search classifier (*.psc)" ]
102 onAccepted: classification.searchPredictor = loadSearchPredictorDialog.fileUrl;
107 id: loadClassificationPredictorDialog
108 title: "Load Classification Predictor"
110 nameFilters: [ "Classification predictor (*.pcc)" ]
111 onAccepted: classification.classificationPredictor = loadClassificationPredictorDialog.fileUrl;