Эх сурвалжийг харах

Added new label button , so the user wont start drawing unless he clicks the button , in labelme
Added the code to do that in canvas
Added select shape , when the user click on any shape it's choosen and the outline becomes white
(done that in canavas and shape)

Hussein 13 жил өмнө
parent
commit
b1fb6c7487
3 өөрчлөгдсөн 68 нэмэгдсэн , 20 устгасан
  1. 43 15
      canvas.py
  2. 9 3
      labelme.py
  3. 16 2
      shape.py

+ 43 - 15
canvas.py

@@ -11,14 +11,19 @@ class Canvas(QLabel):
 
     def __init__(self, *args, **kwargs):
         super(Canvas, self).__init__(*args, **kwargs)
+        self.startLabeling=False # has to click new label buttoon to starting drawing new polygons
         self.shapes = []
         self.current = None
+        self.selectedShape=None # save the selected shape here
         self.line_color = QColor(0, 0, 255)
         self.line = Shape(line_color=self.line_color)
+        self.mouseButtonIsPressed=False #when it is true and shape is selected , move the shape with the mouse move event
+        
+        
 
     def mouseMoveEvent(self, ev):
         """Update line with last point and current coordinates."""
-        if self.current:
+        if self.current and self.startLabeling: 
             if len(self.current) > 1 and self.closeEnough(ev.pos(), self.current[0]):
                 self.line[1] = self.current[0]
                 self.line.line_color = self.current.line_color
@@ -26,28 +31,50 @@ class Canvas(QLabel):
                 self.line[1] = ev.pos()
                 self.line.line_color = self.line_color
             self.repaint()
-
+            return
+            
+        if self.mouseButtonIsPressed:
+            print ev.x()
+            
     def mousePressEvent(self, ev):
-        if ev.button() == 1:
-            if self.current:
-                self.current.addPoint(self.line[1])
-                self.line[0] = self.current[-1]
-                if self.current.isClosed():
-                    self.finalise()
-                self.repaint()
-            else:
-                self.current = Shape()
-                self.line.points = [ev.pos(), ev.pos()]
-                self.current.addPoint(ev.pos())
-                self.setMouseTracking(True)
+        if ev.button() == 1: 
+            if self.startLabeling:
+                
+                if self.current :
+                    self.current.addPoint(self.line[1])
+                    self.line[0] = self.current[-1]
+                    if self.current.isClosed():
+                        self.finalise()
+                    self.repaint()
+                else:
+                    self.current = Shape()
+                    self.line.points = [ev.pos(), ev.pos()]
+                    self.current.addPoint(ev.pos())
+                    self.setMouseTracking(True)
+            else: # not in adding new label mode
+                self.selectShape(ev.pos())
 
     def mouseDoubleClickEvent(self, ev):
-        if self.current:
+        if self.current and self.startLabeling:
             # Add first point in the list so that it is consistent
             # with shapes created the normal way.
             self.current.addPoint(self.current[0])
             self.finalise()
 
+    def selectShape(self,point):
+        self.deSelectShape()
+        for shape in self.shapes:
+            if shape.containPoint(point):
+                shape.selected=True
+                self.selectedShape=shape
+                self.repaint()
+                return
+        
+                #shape.select()
+    def deSelectShape(self):
+        if self.selectedShape:
+            self.selectedShape.selected=False
+            self.repaint()
     def paintEvent(self, event):
         super(Canvas, self).paintEvent(event)
         qp = QPainter()
@@ -66,6 +93,7 @@ class Canvas(QLabel):
         self.current.fill = True
         self.shapes.append(self.current)
         self.current = None
+        self.startLabeling=False
         # TODO: Mouse tracking is still useful for selecting shapes!
         self.setMouseTracking(False)
         self.repaint()

+ 9 - 3
labelme.py

@@ -94,6 +94,7 @@ class MainWindow(QMainWindow, WindowMixin):
         quit = action(self, '&Quit', self.close, 'Ctrl+Q', u'Exit application')
         open = action(self, '&Open', self.openFile, 'Ctrl+O', u'Open file')
         color = action(self, '&Color', self.chooseColor, 'Ctrl+C', u'Choose line color')
+        new_Label=action(self,'&New Label',self.newlabel,'Ctrl+N',u'Add new label')
         labl = self.dock.toggleViewAction()
         labl.setShortcut('Ctrl+L')
 
@@ -107,11 +108,13 @@ class MainWindow(QMainWindow, WindowMixin):
                 edit=self.menu('&Image'),
                 view=self.menu('&View'))
         add_actions(self.menus.file, (open, quit))
-        add_actions(self.menus.edit, (color, fit_window))
+        add_actions(self.menus.edit, (new_Label,color, fit_window))
+
         add_actions(self.menus.view, (labl,))
+        
 
         self.tools = self.toolbar('Tools')
-        add_actions(self.tools, (open, color, None, zoom, fit_window, None, quit))
+        add_actions(self.tools, (open, color, None,new_Label,None, zoom, fit_window, None, quit))
 
 
         self.statusBar().showMessage('%s started.' % __appname__)
@@ -240,7 +243,10 @@ class MainWindow(QMainWindow, WindowMixin):
         # Change the color for all shape lines:
         Shape.line_color = self.color
         self.canvas.repaint()
-
+        
+    def newlabel(self):
+        self.canvas.deSelectShape()
+        self.canvas.startLabeling=True
 
 class Settings(object):
     """Convenience dict-like wrapper around QSettings."""

+ 16 - 2
shape.py

@@ -11,6 +11,7 @@ class Shape(object):
     ## of _all_ shape objects.
     line_color = QColor(0, 255, 0, 128)
     fill_color = QColor(255, 0, 0, 128)
+    select_color=QColor(255,255,255,255)
     point_type = P_SQUARE
     point_size = 8
 
@@ -18,6 +19,7 @@ class Shape(object):
         self.label = label
         self.points = []
         self.fill = False
+        self.selected=False
         if line_color is not None:
             # Override the class line_color attribute
             # with an object attribute. Currently this
@@ -39,7 +41,12 @@ class Shape(object):
     # The paths could be stored and elements added directly to them.
     def paint(self, painter):
         if self.points:
-            pen = QPen(self.line_color)
+            if self.selected:
+                pen = QPen(self.select_color)
+            else:
+                pen = QPen(self.line_color)
+
+            
             painter.setPen(pen)
 
             line_path = QPainterPath()
@@ -64,7 +71,14 @@ class Shape(object):
             path.addRect(point.x() - d/2, point.y() - d/2, d, d)
         else:
             path.addEllipse(QPointF(point), d/2.0, d/2.0)
-
+    
+    
+    def containPoint(self,point):
+        path=QPainterPath(QPointF(self.points[0]))
+        for p in self.points[1:]:
+            path.lineTo(QPointF(p))
+        return path.contains(QPointF(point))
+        
     def __len__(self):
         return len(self.points)