5from result_model
import ResultModel
6from classification
import Classification
8if sys.version_info >= (3, 11):
9 from PySide6.QtCore
import QObject, QUrl, QAbstractListModel, Qt, QModelIndex
10 from PySide6.QtQml
import QQmlApplicationEngine, qmlRegisterType
11 from PySide6.QtGui
import QGuiApplication, QIcon
13 from PySide2.QtCore
import QObject, QUrl, QAbstractListModel, Qt, QModelIndex
14 from PySide2.QtQml
import QQmlApplicationEngine, qmlRegisterType
15 from PySide2.QtGui
import QGuiApplication, QIcon
18if __name__ ==
"__main__":
20 app = QGuiApplication([])
21 app.setOrganizationName(
'STEMMER IMAGING')
22 app.setOrganizationDomain(
'https://www.stemmer-imaging.com/')
23 app.setApplicationName(
'Polimago Python tutorial')
26 if sys.platform ==
'win32':
28 myappid =
u'stemmerimaging.commonvisionblox.pycookieclassification.0'
29 ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
31 app.setWindowIcon(QIcon(
'Tutorial-Python_32x32.png'))
36 cvb.AcquisitionStack.Vin)
40 result_model = ResultModel(image_controller)
43 classification = Classification(device, result_model)
49 engine = QQmlApplicationEngine()
50 context = engine.rootContext()
53 context.setContextProperty(
"mainImage", image_controller)
55 context.setContextProperty(
"resultModel", result_model)
57 context.setContextProperty(
"classification", classification)
60 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:1570
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")
Basically just calls qmlRegisterType(...).
Definition: __init__.py:124
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:196
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:8257
6if sys.version_info >= (3, 11):
7 from PySide6
import QtCore
8 from PySide6.QtCore
import QObject, Qt, QUrl, Property, Signal, Slot
10 from PySide2
import QtCore
11 from PySide2.QtCore
import QObject, Qt, QUrl, Property, Signal, Slot
13from result_model
import ResultModel, PolimagoResult
22class Classification(QtCore.QObject):
24 def __init__(self, device, result_model):
27 self._stream = device.stream()
28 self._model = result_model
33 self._search_predictor =
""
34 self._classification_predictor =
""
36 self.notify_source.connect(self.source_changed)
37 self.notify_search_predictor.connect(self.search_predictor_changed)
38 self.notify_classification_predictor.connect(self.classification_predictor_changed)
43 self._model.update([
None] * 0)
44 self._image, wait_status = self._stream.get_timed_snapshot(1000)
45 if (wait_status != cvb.WaitStatus.Ok):
48 self._model.refresh(self._image)
53 if self._search == 0
or self._classification == 0:
54 sys.stderr.write(
'No search predictor or classification predictor was loaded.\n')
58 search_res, calls = self._search.grid_search(self._image, self._image.bounds, GRID_STEP, THRESHOLD, LOCALITY)
60 polimago_results = [
None] * 0
62 for res
in search_res:
64 if res.quality >= 0.3:
67 clf_res, confidence_distribution = self._classification.classify(self._image, pos)
68 polimago_results.append(PolimagoResult(pos, res.quality, clf_res))
71 self._model.update(polimago_results)
76 def set_source(self, imageSource):
77 self._source = imageSource
78 self.notify_source.emit()
80 def search_predictor(self):
81 return self._search_predictor
83 def set_search_predictor(self, searchPredictor):
84 self._search_predictor = searchPredictor
85 self.notify_search_predictor.emit()
87 def classification_predictor(self):
88 return self._classification_predictor
90 def set_classification_predictor(self, classificationPredictor):
91 self._classification_predictor = classificationPredictor
92 self.notify_classification_predictor.emit()
95 def notify_source(self):
99 def notify_search_predictor(self):
103 def notify_classification_predictor(self):
107 def source_changed(self):
109 self._model.update([])
112 self._classification = 0
116 path = self._source.toLocalFile()
117 if path.endswith(
".vin"):
119 path, cvb.AcquisitionStack.Vin)
120 self._stream = self._device.stream()
122 elif path.endswith(
".bmp"):
124 self._model.refresh(self._image)
127 def search_predictor_changed(self):
128 if not self.search_predictor().isEmpty():
132 def classification_predictor_changed(self):
133 if not self.classification_predictor().isEmpty():
136 imageSource = Property(QUrl, source, set_source)
137 searchPredictor = Property(QUrl, search_predictor, set_search_predictor)
138 classificationPredictor = Property(QUrl, classification_predictor, set_classification_predictor)
The Common Vision Blox image.
Definition: __init__.py:2038
Multi-purpose 2D vector class.
Definition: __init__.py:4230
Predictor to classify patterns with.
Definition: __init__.py:20
Predictor that may be used for searching objects.
Definition: __init__.py:566
Common Vision Blox Polimago module for Python.
Definition: __init__.py:1
6if sys.version_info >= (3, 11):
7 from PySide6.QtCore
import QObject, QAbstractListModel, Qt, QModelIndex, Property, Signal, Slot
9 from PySide2.QtCore
import QObject, QAbstractListModel, Qt, QModelIndex, Property, Signal, Slot
11class PolimagoResult(object):
13 def __init__(self, pos, pos_quality, clf_res):
15 self._pos_quality = pos_quality
16 self._name = clf_res.name
17 self._color_quality = clf_res.quality
25 def position_quality(self):
26 return self._pos_quality
28 def color_quality(self):
29 return self._color_quality
31class ResultModel(QAbstractListModel):
33 LineText = Qt.UserRole
34 StartPosition = Qt.UserRole + 1
35 Quality = Qt.UserRole + 2
37 def __init__(self, image_controller, parent=None):
38 super(ResultModel, self).__init__(parent)
39 self._image_controller = image_controller
40 self._results = [
None] * 0
42 def update(self, polimago_results):
43 self._results = polimago_results
44 self.layoutChanged.emit()
48 roles[ResultModel.LineText] = b
"lineText"
49 roles[ResultModel.StartPosition] = b
"startPosition"
50 roles[ResultModel.Quality] = b
"quality"
53 def rowCount(self, parent = QModelIndex()):
54 return len(self._results)
56 def data(self, index, role = Qt.DisplayRole):
57 if not index.isValid():
60 result = self._results[index.row()]
62 if role == ResultModel.LineText:
64 elif role == ResultModel.StartPosition:
66 elif role == ResultModel.Quality:
67 return result.position_quality()
72 def refresh(self, image):
73 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;