Ejemplo n.º 1
0
def main():

  db_name=sys.argv[1] # database name 
  gt_output_fn=sys.argv[2]
  annotations_dir=os.environ['DATABASES']+os.sep+db_name+os.sep+"pics"
  pics_dir=os.environ['DATABASES']+os.sep+db_name+os.sep+"pics/"
  gt_output_path=os.environ['DATABASES']+os.sep+db_name+os.sep+gt_output_fn

  print "Annotations path: " , annotations_dir 
  annotated_im_nbr=0
  with open(gt_output_path, 'w') as f:
    for subdir, dirs, files in os.walk(annotations_dir):
        for file_ in files:
          #print  "file:", file_     
          #if fnmatch.fnmatch(file_, '*.'+LabelFile.getSuffix()):
          if LabelFile.isLabelFile(file_):
                 try:
                        filename=os.path.join(subdir, file_)
                        labelFile = LabelFile(filename)
                        if  LabelFile.isLabelFile(labelFile.imagePath):
                               print "there is a problem with image path : ",labelFile.imagePath
			
			for bbox in  getBBox(labelFile.shapes):
				f.write ("%s %s %d %d %d %d %s\n" % (bbox[0],labelFile.imagePath.replace(pics_dir,''),float(bbox[1]),float(bbox[2]), float(bbox[3]),float(bbox[4]),bbox[0]))

                 except LabelFileError, e:
                        print "%s, Make sure %s is a valid label file."%(z)
                        return False
		 annotated_im_nbr+=1
Ejemplo n.º 2
0
def main():
  db_name=sys.argv[1] # database name 
  annotations_dir=os.environ['DATABASES']+os.sep+db_name+os.sep+"pics"
  print "Annotations path: " , annotations_dir 
  for subdir, dirs, files in os.walk(annotations_dir):
        for file_ in files:
          #print  "file:", file_     
          #if fnmatch.fnmatch(file_, '*.'+LabelFile.getSuffix()):
          if LabelFile.isLabelFile(file_):
                 try:
                        filename=os.path.join(subdir, file_)
                        labelFile = LabelFile(filename)
                        if  LabelFile.isLabelFile(labelFile.imagePath):
                               print "image",labelFile.imagePath
			       # fix image path and save 
			       #labelFile.imagePath = labelFile.imagePath.replace(LabelFile.getSuffix(),".jpg")
			       labelFile.imagePath = re.sub(LabelFile.getSuffix()+'$',".jpg",labelFile.imagePath)
			       #print labelFile.imagePath
			       #pprint(labelFile.__dict__)
			       labelFile.ssave(filename)
			       
                        getBBox(labelFile.shapes)

                 except LabelFileError, e:
                        print "%s, Make sure %s is a valid label file."%(e,file_)
                        return False
Ejemplo n.º 3
0
 def loadFile(self, filename=None):
     """Load the specified file, or the last opened file if None."""
     self.resetState()
     self.canvas.setEnabled(False)
     if filename is None:
         filename = self.settings.get('filename', '')
     filename = str(filename)
     if QFile.exists(filename):
         if LabelFile.isLabelFile(filename):
             try:
                 self.labelFile = LabelFile(filename)
                 # FIXME: PyQt4 installed via Anaconda fails to load JPEG
                 # and JSON encoded images.
                 # https://github.com/ContinuumIO/anaconda-issues/issues/131
                 if QImage.fromData(self.labelFile.imageData).isNull():
                     raise LabelFileError(
                         'Failed loading image data from label file.\n'
                         'Maybe this is a known issue of PyQt4 built on'
                         ' Anaconda, and may be fixed by installing PyQt5.')
             except LabelFileError as e:
                 self.errorMessage('Error opening file',
                         ("<p><b>%s</b></p>"
                          "<p>Make sure <i>%s</i> is a valid label file.")\
                         % (e, filename))
                 self.status("Error reading %s" % filename)
                 return False
             self.imageData = self.labelFile.imageData
             self.lineColor = QColor(*self.labelFile.lineColor)
             self.fillColor = QColor(*self.labelFile.fillColor)
         else:
             # Load image:
             # read data first and store for saving into label file.
             self.imageData = read(filename, None)
             self.labelFile = None
         image = QImage.fromData(self.imageData)
         if image.isNull():
             formats = ['*.{}'.format(fmt.data().decode())
                        for fmt in QImageReader.supportedImageFormats()]
             self.errorMessage(
                 'Error opening file',
                 '<p>Make sure <i>{0}</i> is a valid image file.<br/>'
                 'Supported image formats: {1}</p>'
                 .format(filename, ','.join(formats)))
             self.status("Error reading %s" % filename)
             return False
         self.status("Loaded %s" % os.path.basename(str(filename)))
         self.image = image
         self.filename = filename
         self.canvas.loadPixmap(QPixmap.fromImage(image))
         if self.labelFile:
             self.loadLabels(self.labelFile.shapes)
         self.setClean()
         self.canvas.setEnabled(True)
         self.adjustScale(initial=True)
         self.paintCanvas()
         self.addRecentFile(self.filename)
         self.toggleActions(True)
         return True
     return False
Ejemplo n.º 4
0
    def loadFile(self, filePath=None):
        """Load the specified file, or the last opened file if None."""
        self.resetState()
        self.canvas.setEnabled(False)
        if filePath is None:
            filePath = self.settings.get('filename')

        unicodeFilePath = u(filePath)
        # Tzutalin 20160906 : Add file list and dock to move faster
        # Highlight the file item
        if unicodeFilePath and self.fileListWidget.count() > 0:
            index = self.mImgList.index(unicodeFilePath)
            fileWidgetItem = self.fileListWidget.item(index)
            fileWidgetItem.setSelected(True)

        if unicodeFilePath and os.path.exists(unicodeFilePath):
            if LabelFile.isLabelFile(unicodeFilePath):
                try:
                    self.labelFile = LabelFile(unicodeFilePath)
                except LabelFileError as e:
                    self.errorMessage(u'Error opening file',
                                      (u"<p><b>%s</b></p>"
                                       u"<p>Make sure <i>%s</i> is a valid label file.") \
                                      % (e, unicodeFilePath))
                    self.status("Error reading %s" % unicodeFilePath)
                    return False
                self.imageData = self.labelFile.imageData
                self.lineColor = QColor(*self.labelFile.lineColor)
                self.fillColor = QColor(*self.labelFile.fillColor)
            else:
                # Load image:
                # read data first and store for saving into label file.
                self.imageData = read(unicodeFilePath, None)
                self.labelFile = None
            image = QImage.fromData(self.imageData)
            if image.isNull():
                self.errorMessage(
                    u'Error opening file',
                    u"<p>Make sure <i>%s</i> is a valid image file." %
                    unicodeFilePath)
                self.status("Error reading %s" % unicodeFilePath)
                return False
            self.status("Loaded %s" % os.path.basename(unicodeFilePath))
            self.image = image
            self.filePath = unicodeFilePath
            self.canvas.loadPixmap(QPixmap.fromImage(image))
            if self.labelFile:
                self.loadLabels(self.labelFile.shapes)
            self.setClean()
            self.canvas.setEnabled(True)
            self.adjustScale(initial=True)
            self.paintCanvas()
            self.addRecentFile(self.filePath)
            self.toggleActions(True)

            ## Label xml file and show bound box according to its filename
            if self.usingPascalVocFormat is True and \
                            self.defaultSaveDir is not None:
                basename = os.path.basename(
                    os.path.splitext(self.filePath)[0]) + XML_EXT
                xmlPath = os.path.join(self.defaultSaveDir, basename)
                self.loadPascalXMLByFilename(xmlPath)

            return True
        return False
Ejemplo n.º 5
0
    def loadFile(self, filename=None):
        """Load the specified file, or the last opened file if None."""
        self.resetState()
        self.canvas.setEnabled(False)
        if filename is None:
            filename = self.settings.get('filename')

        # Tzutalin 20160906 : Add file list and dock to move faster
        # Highlight the file item
        if filename and self.fileListWidget.count() > 0:
            index = self.mImgList.index(filename)
            fileWidgetItem = self.fileListWidget.item(index)
            fileWidgetItem.setSelected(True)

        if filename and QFile.exists(filename):
            if LabelFile.isLabelFile(filename):
                try:
                    self.labelFile = LabelFile(filename)
                except LabelFileError as e:
                    self.errorMessage(u'Error opening file',
                            (u"<p><b>%s</b></p>"
                             u"<p>Make sure <i>%s</i> is a valid label file.")\
                            % (e, filename))
                    self.status("Error reading %s" % filename)
                    return False
                self.imageData = self.labelFile.imageData
                self.lineColor = QColor(*self.labelFile.lineColor)
                self.fillColor = QColor(*self.labelFile.fillColor)
            else:
                # Load image:
                # read data first and store for saving into label file.
                self.imageData = read(filename, None)
                self.labelFile = None
            image = QImage.fromData(self.imageData)
            if image.isNull():
                self.errorMessage(u'Error opening file',
                        u"<p>Make sure <i>%s</i> is a valid image file." % filename)
                self.status("Error reading %s" % filename)
                return False
            self.status("Loaded %s" % os.path.basename(str(filename)))
            self.image = image
            self.filename = filename
            self.canvas.loadPixmap(QPixmap.fromImage(image))
            if self.labelFile:
                self.loadLabels(self.labelFile.shapes)
            self.setClean()
            self.canvas.setEnabled(True)
            self.adjustScale(initial=True)
            self.paintCanvas()
            self.addRecentFile(self.filename)
            self.toggleActions(True)

            ## Label xml file and show bound box according to its filename
            if self.usingPascalVocFormat is True and \
                    self.defaultSaveDir is not None:
                    basename = os.path.basename(os.path.splitext(self.filename)[0]) + '.xml'
                    xmlPath = os.path.join(self.defaultSaveDir, basename)
                    self.loadPascalXMLByFilename(xmlPath)

            return True
        return False