프로젝트

일반

사용자정보

개정판 7b2a4455

ID7b2a4455b3c40f5b749d62481b9ea6a2a4a5780e
상위 2894ed79
하위 7c9ef943

김정우 이(가) 6년 이상 전에 추가함

검출된 텍스트를 더블 클릭하여 텍스트를 수정 가능하도록 변경

차이점 보기:

DTI_PID/DTI_PID/AppDocData.py
9 9
import SymbolBase
10 10
import symbol
11 11
import datetime
12
from PIL import PngImagePlugin, JpegImagePlugin
13
from PIL import Image
14
from PIL.ImageQt import ImageQt
15
#import PIL.ImageQt as ImageQt
16

  
17
try:
18
    from PyQt5.QtCore import QBuffer
19
    from PyQt5.QtGui import QImage, QPixmap
20
except ImportError:
21
    from PyQt4.QtCore import QBuffer
22
    from PyQt4.QtGui import QImage, QPixmap
23
import numpy as np
24

  
25
class Source:
26
    def __init__(self, source):
27
        if type(source) is np.ndarray:
28
            self.source = Image.fromarray(source)
29
        elif type(source) is PngImagePlugin.PngImageFile or type(source) is JpegImagePlugin.JpegImageFile:
30
            self.source = source
31
        elif type(source) is QImage:
32
            self.source = Image.fromqimage(source)
33
        elif type(source) is QPixmap:
34
            self.source = Image.fromqpixmap(source)
35
            
36
    def getPyImageOnRect(self, rect):
37
        return self.source.copy().crop((rect.left(), rect.top(), rect.right(), rect.bottom()))
38

  
39
    def getQImageOnRect(self, rect):
40
        image = ImageQt(self.getPyImageOnRect(rect))
41
        return image.convertToFormat(QImage.Format_RGBA8888)
12 42

  
13 43
class Area:
14 44
    def __init__(self):
......
46 76
        self._areas = []
47 77
        self.lineNos = []
48 78

  
79
    def setCurrentPidSource(self, image):
80
        self.currentPidSource = Source(image)
81

  
82
    def getCurrentPidSource(self):
83
        return self.currentPidSource
84

  
49 85
    '''
50 86
        @brief  build application database
51 87
        @author humkyung
DTI_PID/DTI_PID/Commands/AreaOcrCommand.py
1 1
import os.path
2 2
import AbstractCommand
3 3
try:
4
    from PyQt5.QtCore import Qt, QPoint, QPointF, QRectF, pyqtSignal, QT_VERSION_STR, QBuffer
4
    from PyQt5.QtCore import Qt, QPoint, QPointF, QRectF, pyqtSignal, QT_VERSION_STR, QBuffer, QRect
5 5
    from PyQt5.QtGui import QImage, QPixmap, QPainterPath, QPainter, QColor, QPen, QBrush, QCursor, QTransform, QFont
6 6
    from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QFileDialog, QMessageBox
7 7
except ImportError:
8 8
    try:
9
        from PyQt4.QtCore import Qt, QPoint, QPointF, QRectF, pyqtSignal, QT_VERSION_STR, QBuffer
9
        from PyQt4.QtCore import Qt, QPoint, QPointF, QRectF, pyqtSignal, QT_VERSION_STR, QBuffer, QRect
10 10
        from PyQt4.QtGui import QGraphicsView, QGraphicsScene, QImage, QPixmap, QPainterPath, QFileDialog, QPainter, QColor, QPen, QBrush, QCursor, QTransform, QFont
11 11
    except ImportError:
12 12
        raise ImportError("ImageViewerQt: Requires PyQt5 or PyQt4.")
......
53 53
                            if textInfoList is not None and len(textInfoList) > 0:
54 54
                                for textInfo in textInfoList:
55 55
                                    print("Accepted")
56
                                    x = textInfo.getX()
57
                                    y = textInfo.getY()
56
                                    x = int(selectionBBox.x()) + textInfo.getX()
57
                                    y = int(selectionBBox.y()) + textInfo.getY()
58 58
                                    angle = textInfo.getAngle()
59 59
                                    text = textInfo.getText()
60 60
                                    width = textInfo.getW()
61 61
                                    height = textInfo.getH()
62 62
                                    item = QEngineeringTextItem()
63
                                    item.loc = (round(selectionBBox.x()), int(selectionBBox.y()))
63
                                    item.loc = (x, y)#(int(selectionBBox.x() + x), int(selectionBBox.y() + y))
64 64
                                    item.size = (width, height)
65
                                    item.angle = angle
65 66
                                    item.setPlainText(text)
66 67
                                    item.setDefaultTextColor(Qt.blue)
67
                                    sx = 1
68
                                    sy = 1
69
                                    rect = None
70
                                    transform = QTransform()
71
                                    transform.translate(int(selectionBBox.x()), int(selectionBBox.y()))
72
                                    if (1.57 == angle) or (4.71 == angle):
73
                                        font = QFont('Arial', width*1.2)
74
                                        item.setFont(font)
75
                                        rect = item.boundingRect()
76
                                        sx = width/rect.height()
77
                                        sy = height/rect.width()
68
                                    item.addTextItemToScene(self.imageViewer.scene)
69
                                    #sx = 1
70
                                    #sy = 1
71
                                    #rect = None
72
                                    #transform = QTransform()
73
                                    #transform.translate(int(selectionBBox.x()), int(selectionBBox.y()))
74
                                    #if (1.57 == angle) or (4.71 == angle):
75
                                    #    font = QFont('Arial', width*1.2)
76
                                    #    item.setFont(font)
77
                                    #    rect = item.boundingRect()
78
                                    #    sx = width/rect.height()
79
                                    #    sy = height/rect.width()
78 80
                                        
79
                                        transform.translate(x, y)
80
                                        transform.translate(width*0.5, height*0.5)
81
                                        transform.scale(1, sy)
82
                                        transform.rotateRadians(-angle)
83
                                        transform.translate(-rect.width()*0.5, -rect.height()*0.5)
84
                                    elif 3.14 == angle:
85
                                        font = QFont('Arial', height*1.2)
86
                                        item.setFont(font)
87
                                        rect = item.boundingRect()
88
                                        sx = width/rect.width()
89
                                        sy = height/rect.height()
81
                                    #    transform.translate(x, y)
82
                                    #    transform.translate(width*0.5, height*0.5)
83
                                    #    transform.scale(1, sy)
84
                                    #    transform.rotateRadians(-angle)
85
                                    #    transform.translate(-rect.width()*0.5, -rect.height()*0.5)
86
                                    #elif 3.14 == angle:
87
                                    #    font = QFont('Arial', height*1.2)
88
                                    #    item.setFont(font)
89
                                    #    rect = item.boundingRect()
90
                                    #    sx = width/rect.width()
91
                                    #    sy = height/rect.height()
90 92

  
91
                                        transform.translate(x, y - round((rect.height()-height)*0.5))
92
                                        transform.scale(sx, 1)
93
                                        transform.rotateRadians(-angle)
94
                                        transform.translate(-width*0.5, -height*0.5)
95
                                    else:
96
                                        font = QFont('Arial', height*1.2)
97
                                        item.setFont(font)
98
                                        rect = item.boundingRect()
99
                                        sx = width/rect.width()
100
                                        sy = height/rect.height()
93
                                    #    transform.translate(x, y - round((rect.height()-height)*0.5))
94
                                    #    transform.scale(sx, 1)
95
                                    #    transform.rotateRadians(-angle)
96
                                    #    transform.translate(-width*0.5, -height*0.5)
97
                                    #else:
98
                                    #    font = QFont('Arial', height*1.2)
99
                                    #    item.setFont(font)
100
                                    #    rect = item.boundingRect()
101
                                    #    sx = width/rect.width()
102
                                    #    sy = height/rect.height()
101 103

  
102
                                        #if '\n' not in text:
103
                                        transform.translate(x, y - round((rect.height()-height)*0.5))
104
                                        transform.scale(sx, 1)
105
                                        #else:
106
                                        #    transform.translate(x, y)
107
                                        #    transform.scale(sx, sy)
108
                                    item.setTransform(transform)
109
                                    #item.setTextWidth(width)
104
                                    #    #if '\n' not in text:
105
                                    #    transform.translate(x, y - round((rect.height()-height)*0.5))
106
                                    #    transform.scale(sx, 1)
107
                                    #    #else:
108
                                    #    #    transform.translate(x, y)
109
                                    #    #    transform.scale(sx, sy)
110
                                    #item.setTransform(transform)
111
                                    ##item.setTextWidth(width)
110 112
                
111
                                    self.imageViewer.scene.addItem(item)
113
                                    #self.imageViewer.scene.addItem(item)
112 114
                            else:
113 115
                                QMessageBox.about(self.imageViewer, "알림", "텍스트 검출 실패")
114 116
            finally:
DTI_PID/DTI_PID/MainWindow.py
9 9
from PyQt5.QtWidgets import *
10 10
from PyQt5.QtSvg import *
11 11

  
12
from PIL import Image
13

  
12 14
import DTI_PID_UI
13 15
import QtImageViewer
14 16
from SingletonInstance import SingletonInstane
......
154 156
        @brief  Open image drawing file and then display it
155 157
        @author humkyung
156 158
        @date   2018.??.??
159
        @history    18.04.23    Jeongwoo    Add AppDocData.instance().setCurrentPidSource
160
                    18.04.23    Jeongwoo    Add Store Set Current Pid Image on AppDocData
157 161
    '''
158 162
    def openImageDrawing(self, MainWindow):
159 163
        import random
......
164 168
            self.path = self.graphicsView.loadImageFromFile()
165 169
            if os.path.isfile(self.path):
166 170
                baseName = os.path.basename(self.path)
171
                AppDocData.instance().setCurrentPidSource(Image.open(self.path))
167 172
                self.resultTreeWidget.setCurrentPID(baseName)
168 173

  
169 174
                # DEBUG
......
407 412
        @date       2018.04.??
408 413
        @history    humkyung 2018.01.12 parse originalpoint and connectionpoint
409 414
                    Jeongwoo 2018.04.17 add QGraphicItem with Rotated text
415
                    Jeongwoo 2018.04.23 Change to Draw texts on QEngineeringTextItem
410 416
    '''
411 417
    def loadRecognitionResult(self, xmlPath):
412 418
        from xml.etree.ElementTree import Element, SubElement, dump, ElementTree, parse
......
473 479
                item = QEngineeringTextItem()
474 480
                item.loc = (x, y)
475 481
                item.size = (width, height)
482
                item.angle = angle
476 483
                item.setPlainText(text)
477 484
                item.setDefaultTextColor(Qt.blue)
478
                sx = 1
479
                sy = 1
480
                rect = None
481
                transform = QTransform()
482
                if (1.57 == angle) or (4.71 == angle):
483
                    font = QFont('Arial', width*1.2)
484
                    item.setFont(font)
485
                    rect = item.boundingRect()
486
                    sx = width/rect.height()
487
                    sy = height/rect.width()
488

  
489
                    transform.translate(x + width*0.5,y + height*0.5)
490
                    transform.scale(1, sy)
491
                    transform.rotateRadians(-angle)
492
                    transform.translate(-rect.width()*0.5, -rect.height()*0.5)
493
                elif 3.14 == angle:
494
                    font = QFont('Arial', height*1.2)
495
                    item.setFont(font)
496
                    rect = item.boundingRect()
497
                    sx = width/rect.width()
498
                    sy = height/rect.height()
499

  
500
                    transform.translate(x, y - round((rect.height()-height)*0.5))
501
                    transform.scale(sx, 1)
502
                    transform.rotateRadians(-angle)
503
                    transform.translate(-width*0.5, -height*0.5)
504
                else:
505
                    font = QFont('Arial', height*1.2)
506
                    item.setFont(font)
507
                    rect = item.boundingRect()
508
                    sx = width/rect.width()
509
                    sy = height/rect.height()
510

  
511
                    transform.translate(x, y - round((rect.height()-height)*0.5))
512
                    transform.scale(sx, 1)
513
                item.setTransform(transform)
514
                #item.setTextWidth(width)
485
                item.addTextItemToScene(self.graphicsView.scene)
486
                #sx = 1
487
                #sy = 1
488
                #rect = None
489
                #transform = QTransform()
490
                #if (1.57 == angle) or (4.71 == angle):
491
                #    font = QFont('Arial', width*1.2)
492
                #    item.setFont(font)
493
                #    rect = item.boundingRect()
494
                #    sx = width/rect.height()
495
                #    sy = height/rect.width()
496

  
497
                #    transform.translate(x + width*0.5,y + height*0.5)
498
                #    transform.scale(1, sy)
499
                #    transform.rotateRadians(-angle)
500
                #    transform.translate(-rect.width()*0.5, -rect.height()*0.5)
501
                #elif 3.14 == angle:
502
                #    font = QFont('Arial', height*1.2)
503
                #    item.setFont(font)
504
                #    rect = item.boundingRect()
505
                #    sx = width/rect.width()
506
                #    sy = height/rect.height()
507

  
508
                #    transform.translate(x, y - round((rect.height()-height)*0.5))
509
                #    transform.scale(sx, 1)
510
                #    transform.rotateRadians(-angle)
511
                #    transform.translate(-width*0.5, -height*0.5)
512
                #else:
513
                #    font = QFont('Arial', height*1.2)
514
                #    item.setFont(font)
515
                #    rect = item.boundingRect()
516
                #    sx = width/rect.width()
517
                #    sy = height/rect.height()
518

  
519
                #    transform.translate(x, y - round((rect.height()-height)*0.5))
520
                #    transform.scale(sx, 1)
521
                #item.setTransform(transform)
522
                ##item.setTextWidth(width)
515 523
                
516
                self.graphicsView.scene.addItem(item)
524
                #self.graphicsView.scene.addItem(item)
517 525
            # up to here
518 526
        except Exception as ex:
519 527
            print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno))
DTI_PID/DTI_PID/QOcrResultDialog.py
12 12
import math
13 13

  
14 14
class QOcrResultDialog(QDialog):
15
    def __init__(self, parent, qimage, boundingBox):
15
    def __init__(self, parent, qimage, boundingBox, isModify = False, text = None):
16 16
        QDialog.__init__(self, parent)
17
        self.textInfoList = None
18

  
19
        self.isModify = isModify
17 20
        self.image = qimage
18 21
        self.originImageWidth = qimage.width()
19 22
        self.originImageHeight = qimage.height()
......
28 31
        self.imgH = qimage.height()
29 32
        self.image = self.image.scaled(self.imgW, self.imgH)
30 33
        self.graphicsView = QtImageViewer.QtImageViewer()
31
        #self.graphicsView.setParent(self.ui.gridLayoutWidget_2)
32 34
        self.graphicsView.useDefaultCommand() ##### USE DEFAULT COMMAND
33 35
        self.graphicsView.setImage(self.image)
34 36
        self.ui.tVerticalLayout_2.addWidget(self.graphicsView)
......
37 39
        self.ui.clockPushButton_2.clicked.connect(lambda : self.rotateImage(False))
38 40
        self.ui.redetectPushButton_2.clicked.connect(self.detectText)
39 41

  
40
        self.detectText()
42
        if self.isModify == False:
43
            self.detectText()
44
        else:
45
            self.ui.detectResultTextEdit_2.setPlainText(text)
41 46

  
42 47
        self.isAccepted = False
43 48
        
......
116 121
                        height = textInfo.getH()
117 122
                        textInfo.setW(height) ## SWAP
118 123
                        textInfo.setH(width) ## SWAP
119
        QDialog.accept(self)
124
            QDialog.accept(self)
125
        else:
126
            QMessageBox.about(self.ui.ocrDialogButtonBox, "알림", "텍스트 검출을 하신 후 다시 시도해주세요.")
120 127

  
121 128
    def reject(self):
122 129
        self.isAccepted = False
DTI_PID/DTI_PID/Shapes/QEngineeringTextItem.py
2 2
import os.path
3 3
import copy
4 4
try:
5
    from PyQt5.QtCore import Qt, QPointF, QRectF, pyqtSignal, QT_VERSION_STR
6
    from PyQt5.QtGui import QImage, QPixmap, QPainterPath, QBrush, QPen, QTransform
5
    from PyQt5.QtCore import Qt, QPointF, QRectF, pyqtSignal, QT_VERSION_STR, QRect
6
    from PyQt5.QtGui import QImage, QPixmap, QPainterPath, QBrush, QPen, QTransform, QFont
7 7
    from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QFileDialog, QGraphicsItem, QAbstractGraphicsShapeItem, QGraphicsTextItem
8 8
except ImportError:
9 9
    try:
10
        from PyQt4.QtCore import Qt, QRectF, pyqtSignal, QT_VERSION_STR
11
        from PyQt4.QtGui import QGraphicsView, QGraphicsScene, QImage, QPixmap, QPainterPath, QFileDialog
10
        from PyQt4.QtCore import Qt, QRectF, pyqtSignal, QT_VERSION_STR, QRect
11
        from PyQt4.QtGui import QGraphicsView, QGraphicsScene, QImage, QPixmap, QPainterPath, QFileDialog, QFont
12 12
    except ImportError:
13 13
        raise ImportError("ImageViewerQt: Requires PyQt5 or PyQt4.")
14 14

  
15 15
from QGraphicsPolylineItem import QGraphicsPolylineItem
16 16
from QGraphicsBoundingBoxItem import QGraphicsBoundingBoxItem
17
import QOcrResultDialog
18
from AppDocData import AppDocData
17 19

  
18 20
class QEngineeringTextItem(QGraphicsTextItem):
19 21
    def __init__(self, parent=None):
......
21 23

  
22 24
        self.loc = None
23 25
        self.size = None
26
        self.angle = 0
24 27
        self.conns = []
25 28
        self.setFlags(QGraphicsItem.ItemIsSelectable)
26 29
        self.setAcceptHoverEvents(True)
......
48 51
        pass
49 52

  
50 53
    def hoverMoveEvent(self, event):
51
        pass
54
        pass
55

  
56
    '''
57
        @brief      Double click event, Show QOcrResultDialog
58
        @author     Jeongwoo
59
        @date       18.04.23
60
        @history    .
61
    '''
62
    def mouseDoubleClickEvent(self, event):
63
        dialog = QOcrResultDialog.QOcrResultDialog(None, AppDocData.instance().getCurrentPidSource().getQImageOnRect(QRect(self.loc[0], self.loc[1], self.size[0], self.size[1]))
64
                                                   , QRect(self.loc[0], self.loc[1], self.size[0], self.size[1]), True, self.text())
65
        (isAccept, textInfoList) = dialog.showDialog()
66

  
67
        if isAccept:
68
            scene = self.scene()
69
            scene.removeItem(self)
70
            #self.loc = (x, y)#(int(selectionBBox.x() + x), int(selectionBBox.y() + y))
71
            #self.size = (width, height)
72
            #self.angle = angle
73
            text = textInfoList[0].getText()
74
            self.setPlainText(text)
75
            self.setDefaultTextColor(Qt.blue)
76
            self.addTextItemToScene(scene)
77

  
78
    '''
79
        @brief      Put text on scene
80
        @author     Jeongwoo
81
        @date       18.04.23
82
        @history    .
83
    '''
84
    def addTextItemToScene(self, scene):
85
        print("Put text")
86
        sx = 1
87
        sy = 1
88
        width = self.size[0]
89
        height = self.size[1]
90
        x = self.loc[0]
91
        y = self.loc[1]
92
        rect = None
93
        transform = QTransform()
94
        #transform.translate(int(selectionBBox.x()), int(selectionBBox.y()))
95
        if (1.57 == self.angle) or (4.71 == self.angle):
96
            font = QFont('Arial', width*1.2)
97
            self.setFont(font)
98
            rect = self.boundingRect()
99
            sx = width/rect.height()
100
            sy = height/rect.width()
101
                                        
102
            transform.translate(x, y)
103
            transform.translate(width*0.5, height*0.5)
104
            transform.scale(1, sy)
105
            transform.rotateRadians(-self.angle)
106
            transform.translate(-rect.width()*0.5, -rect.height()*0.5)
107
        elif 3.14 == self.angle:
108
            font = QFont('Arial', height*1.2)
109
            self.setFont(font)
110
            rect = self.boundingRect()
111
            sx = width/rect.width()
112
            sy = height/rect.height()
113

  
114
            transform.translate(x, y - round((rect.height()-height)*0.5))
115
            transform.scale(sx, 1)
116
            transform.rotateRadians(-self.angle)
117
            transform.translate(-width*0.5, -height*0.5)
118
        else:
119
            font = QFont('Arial', height*1.2)
120
            self.setFont(font)
121
            rect = self.boundingRect()
122
            sx = width/rect.width()
123
            sy = height/rect.height()
124

  
125
            #if '\n' not in text:
126
            transform.translate(x, y - round((rect.height()-height)*0.5))
127
            transform.scale(sx, 1)
128
            #else:
129
            #    transform.translate(x, y)
130
            #    transform.scale(sx, sy)
131
        self.setTransform(transform)
132
        #item.setTextWidth(width)
133
                
134
        scene.addItem(self)

내보내기 Unified diff

클립보드 이미지 추가 (최대 크기: 500 MB)