hytos / DTI_PID / DTI_PID / TrainingEditorDialog.py @ d45df999
이력 | 보기 | 이력해설 | 다운로드 (7.14 KB)
1 | c96518e7 | esham21 | import sys |
---|---|---|---|
2 | import os |
||
3 | from PyQt5.QtCore import * |
||
4 | from PyQt5.QtGui import * |
||
5 | from PyQt5.QtWidgets import * |
||
6 | from AppDocData import AppDocData, Source |
||
7 | import TrainingEditor_UI |
||
8 | import QtImageViewer |
||
9 | from GraphicsBoundingBoxItem import QGraphicsBoundingBoxItem |
||
10 | from TrainingBoxItem import QTrainingBoxItem |
||
11 | import cv2 |
||
12 | 6913dbc8 | esham21 | from PIL import Image |
13 | c96518e7 | esham21 | import AreaZoomCommand |
14 | import PlaceLineCommand |
||
15 | |||
16 | |||
17 | |||
18 | class QTrainingEditorDialog(QDialog): |
||
19 | def __init__(self, parent): |
||
20 | d45df999 | esham21 | self.spinBoxFlag = False |
21 | c96518e7 | esham21 | QDialog.__init__(self, parent)
|
22 | |||
23 | appDocData = AppDocData.instance() |
||
24 | project = appDocData.getCurrentProject() |
||
25 | |||
26 | self.ui = TrainingEditor_UI.Ui_TrainingEditorDialog()
|
||
27 | self.ui.setupUi(self) |
||
28 | |||
29 | self.graphicsViewTrainingDrawing = QtImageViewer.QtImageViewer(self) |
||
30 | self.graphicsViewTrainingDrawing.setParent(self.ui.centralWidget) |
||
31 | self.graphicsViewTrainingDrawing.useDefaultCommand()
|
||
32 | self.ui.verticalLayoutTrainingDrawing.addWidget(self.graphicsViewTrainingDrawing) |
||
33 | 6913dbc8 | esham21 | |
34 | self.graphicsViewZoomDrawing = QGraphicsView(self) |
||
35 | d45df999 | esham21 | self.graphicsViewZoomDrawing.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
|
36 | self.graphicsViewZoomDrawing.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
|
||
37 | 6913dbc8 | esham21 | self.graphicsViewZoomDrawing.setParent(self.ui.leftSideWidget) |
38 | self.ui.horizontalLayoutZoomDrawing.addWidget(self.graphicsViewZoomDrawing) |
||
39 | |||
40 | c96518e7 | esham21 | |
41 | d45df999 | esham21 | # 학습 이미지 읽어서 메인 뷰에 그림, 사이드 뷰에 추가
|
42 | c96518e7 | esham21 | try:
|
43 | trainingImgPath = os.path.join(project.getTrainingFilePath(), 'seed.seedF.exp0.tif')
|
||
44 | cvImg = cv2.cvtColor(cv2.imread(trainingImgPath), cv2.COLOR_BGR2GRAY) |
||
45 | blur = cv2.GaussianBlur(cvImg, (5,5),0) |
||
46 | cvImg = cv2.threshold(cvImg, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1] |
||
47 | bytesPerLine = cvImg.shape[1]
|
||
48 | image = QImage(cvImg.data, cvImg.shape[1], cvImg.shape[0], bytesPerLine, QImage.Format_Indexed8) |
||
49 | |||
50 | #img = cv2.imread(trainingImgPath)
|
||
51 | #image = QImage(img.data, img.shape[1], img.shape[0], img.shape[1], QImage.Format_Indexed8)
|
||
52 | |||
53 | self.graphicsViewTrainingDrawing.setImage(image)
|
||
54 | d45df999 | esham21 | |
55 | # 사이드 뷰
|
||
56 | if type(image) is QPixmap: |
||
57 | pixmap = image |
||
58 | elif type(image) is QImage: |
||
59 | pixmap = QPixmap.fromImage(image) |
||
60 | scene = QGraphicsScene() |
||
61 | self.graphicsViewZoomDrawing.setScene(scene)
|
||
62 | self._pixmapHandle = self.graphicsViewZoomDrawing.scene().addPixmap(pixmap) |
||
63 | rect = QGraphicsRectItem(0, 0, 0, 0) |
||
64 | pen = QPen(Qt.SolidLine) |
||
65 | pen.setColor(Qt.green) |
||
66 | pen.setWidthF(1)
|
||
67 | pen.setJoinStyle(Qt.MiterJoin) |
||
68 | rect.setPen(pen) |
||
69 | self.graphicsViewZoomDrawing.scene().addItem(rect)
|
||
70 | |||
71 | c96518e7 | esham21 | except Exception as ex: |
72 | print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
||
73 | |||
74 | d45df999 | esham21 | # 박스 읽어서 메인 뷰에 그림
|
75 | c96518e7 | esham21 | try:
|
76 | trainingBoxPath = os.path.join(project.getTrainingFilePath(), 'seed.seedF.exp0.box')
|
||
77 | fBox = open(trainingBoxPath, 'r', encoding='utf8') |
||
78 | SBox = fBox.read() |
||
79 | fBox.close() |
||
80 | boxList = [] |
||
81 | boxList = SBox.split('\n')
|
||
82 | |||
83 | for box in boxList: |
||
84 | boxComponent = box.split(' ')
|
||
85 | 265a1e83 | esham21 | singleBox = QTrainingBoxItem(int(boxComponent[1]), cvImg.shape[0] - int(boxComponent[4]), int(boxComponent[3]) - int(boxComponent[1]), int(boxComponent[4]) - int(boxComponent[2])) |
86 | c96518e7 | esham21 | singleBox.loc = [int(boxComponent[1]), cvImg.shape[0] - int(boxComponent[2])] |
87 | singleBox.size = [int(boxComponent[3]) - int(boxComponent[1]), int(boxComponent[2]) - int(boxComponent[4])] |
||
88 | singleBox.angle = 0
|
||
89 | #singleBox.setPlainText(boxComponent[0])
|
||
90 | singleBox.transfer.onRemoved.connect(self.itemRemoved)
|
||
91 | d45df999 | esham21 | singleBox.addTextItemToScene(self.ui, self.graphicsViewTrainingDrawing, self.graphicsViewZoomDrawing, self.spinBoxFlag) |
92 | c96518e7 | esham21 | |
93 | except Exception as ex: |
||
94 | print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
||
95 | |||
96 | #'''test'''
|
||
97 | #item = QGraphicsBoundingBoxItem(0, 0, 30, 30)
|
||
98 | #item.isSymbol = True
|
||
99 | #item.angle = 0
|
||
100 | #item.setPen(QPen(Qt.red, 1, Qt.SolidLine))
|
||
101 | #self.graphicsViewTrainingDrawing.scene.addItem(item)
|
||
102 | |||
103 | self.removedItems = []
|
||
104 | |||
105 | self.ui.pushButtonZoom.clicked.connect(self.onAreaZoom) |
||
106 | d45df999 | esham21 | self.ui.spinBoxLeft.valueChanged.connect(self.spinBoxChangedEvent) |
107 | self.ui.spinBoxTop.valueChanged.connect(self.spinBoxChangedEvent) |
||
108 | self.ui.spinBoxWidth.valueChanged.connect(self.spinBoxChangedEvent) |
||
109 | self.ui.spinBoxHeight.valueChanged.connect(self.spinBoxChangedEvent) |
||
110 | 6913dbc8 | esham21 | |
111 | d45df999 | esham21 | def spinBoxChangedEvent(self, event): |
112 | 6913dbc8 | esham21 | items = self.graphicsViewTrainingDrawing.scene.selectedItems()
|
113 | d45df999 | esham21 | if(len(items) is not 1) or self.spinBoxFlag: |
114 | 6913dbc8 | esham21 | return
|
115 | d45df999 | esham21 | |
116 | spinBoxName = self.sender().objectName()
|
||
117 | 6913dbc8 | esham21 | rect = items[0].rect()
|
118 | d45df999 | esham21 | bound = self.graphicsViewZoomDrawing.scene().items()[0] |
119 | |||
120 | if spinBoxName == 'spinBoxLeft': |
||
121 | spinBoxValue = self.ui.spinBoxLeft.value()
|
||
122 | items[0].setRect(QRectF(spinBoxValue, rect.y(), rect.width(), rect.height()))
|
||
123 | elif spinBoxName == 'spinBoxTop': |
||
124 | spinBoxValue = self.ui.spinBoxTop.value()
|
||
125 | items[0].setRect(QRectF(rect.x(), spinBoxValue, rect.width(), rect.height()))
|
||
126 | elif spinBoxName == 'spinBoxWidth': |
||
127 | spinBoxValue = self.ui.spinBoxWidth.value()
|
||
128 | items[0].setRect(QRectF(rect.x(), rect.y(), spinBoxValue, rect.height()))
|
||
129 | elif spinBoxName == 'spinBoxHeight': |
||
130 | spinBoxValue = self.ui.spinBoxHeight.value()
|
||
131 | items[0].setRect(QRectF(rect.x(), rect.y(), rect.width(), spinBoxValue))
|
||
132 | |||
133 | rect = items[0].rect()
|
||
134 | bound.setRect(rect) |
||
135 | rectSide = QRectF(rect.x() -3, rect.y() -3, rect.width() +6, rect.height() +6) |
||
136 | self.graphicsViewZoomDrawing.fitInView(rectSide)
|
||
137 | 6913dbc8 | esham21 | self.graphicsViewTrainingDrawing.scene.update()
|
138 | |||
139 | c96518e7 | esham21 | def onAreaZoom(self, action): |
140 | if self.ui.pushButtonZoom.isChecked(): |
||
141 | cmd = AreaZoomCommand.AreaZoomCommand(self.graphicsViewTrainingDrawing)
|
||
142 | cmd.onRejected.connect(self.onCommandRejected)
|
||
143 | self.graphicsViewTrainingDrawing.command = cmd
|
||
144 | |||
145 | def onCommandRejected(self, cmd): |
||
146 | try:
|
||
147 | if type(cmd) is AreaZoomCommand.AreaZoomCommand: |
||
148 | self.ui.pushButtonZoom.setChecked(False) |
||
149 | finally:
|
||
150 | self.graphicsViewTrainingDrawing.useDefaultCommand()
|
||
151 | |||
152 | def itemRemoved(self, item): |
||
153 | try:
|
||
154 | if type(item) is QTrainingBoxItem: |
||
155 | self.removedItems.append(str(item.uid)) |
||
156 | |||
157 | if item.scene() is not None: item.scene().removeItem(item) |
||
158 | except Exception as ex: |
||
159 | print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
||
160 | |||
161 | |||
162 | |||
163 | |||
164 |