def saveFile(self): annotationFilePath = "models/research/object_detection/images/img_"+str(self.selectedPointList[self.currentIndexOfSelectedPoint-1])+".xml" filePath = "models/research/object_detection/images/img_"+str(self.selectedPointList[self.currentIndexOfSelectedPoint-1])+".jpg" if self.labelFile is None: self.labelFile = LabelFile() self.labelFile.verified = self.canvas.verified def format_shape(s): return dict(label=s.label, line_color=s.line_color.getRgb(), fill_color=s.fill_color.getRgb(), points=[(p.x(), p.y()) for p in s.points], difficult = s.difficult) self.currentTextLable = self.canvas.shapes[0].label # data = "item {\n\tid: 1\n\tname: '"+self.currentTextLable+"'\n}" data = "item {\n\tid: 1\n\tname: 'cell'\n}" file = open("models/research/object_detection/training/labelmap.pbtxt", "w") file.write(data) file.close() shapes = [format_shape(shape) for shape in self.canvas.shapes] try: imageFilePath = os.popen('cd').read()[:-1] self.labelFile.savePascalVocFormat(annotationFilePath, shapes, imageFilePath+"/"+filePath) return True except: self.errorMessage(u'Error', u'Error in saving label data') return False
def saveLabels(self, annotationFilePath): annotationFilePath = annotationFilePath imgFileDir = os.path.dirname(annotationFilePath) imgFileName = os.path.basename(annotationFilePath) dataFileName = os.path.splitext(imgFileName)[0] + ".compressed" dataPath = os.path.join(imgFileDir, dataFileName) imgPath = os.path.join(imgFileDir, os.path.splitext(imgFileName)[0] + ".jpg") if self.labelFile is None: self.labelFile = LabelFile() self.labelFile.verified = self.canvas.verified def format_shape(s): self.calcShapeParent(s) return dict(label=s.label, guid=s.guid, parentGuid=s.parentGuid, lineWidth=s.lineWidth, rotate=s.rotate, r1=s.r1, r2=s.r2, center=(s.center.x(), s.center.y()), points=[(p.x(), p.y()) for p in s.points], ellipse_points=[(p.x(), p.y()) for p in s.ellipse_points]) shapes = [format_shape(shape) for shape in self.canvas.shapes] # Can add differrent annotation formats here try: if self.usingPascalVocFormat is True: print('Img: ' + imgPath + ' -> Its xml: ' + annotationFilePath + ' -> label data file: ' + dataPath) labelNames = [] for label in self.labelInfoDict.keys(): labelNames.append(label[0]) self.labelFile.setLabels(labelNames) self.labelFile.savePascalVocFormat(annotationFilePath, shapes, imgPath, self.imageData) #for deeplearing, no need to save compress label data if self.ui.actionSave_Label_Compress_Data.isChecked(): self.labelFile.saveData( dataPath, self.canvas.shapes, imgPath, (self.ui.cannyKSize.value(), self.ui.cannyKSize.value()), (self.ui.boxKSize.value(), self.ui.boxKSize.value()), (self.ui.labelKSize.value(), self.ui.labelKSize.value()), self.ui.actionSave_Rf_Data.isChecked( )) #check if save random forest data file else: self.labelFile.save(annotationFilePath, shapes, imgPath, self.imageData) return True except LabelFileError as e: self.errorMessage(u'Error saving label data', u'<b>%s</b>' % e) return False
def main(): for fname in os.listdir(args.dir): root, ext = os.path.splitext(fname) if ext != ".txt" or root == "classes": continue image_path = os.path.join(args.dir, f"{root}.jpg") image_data = read(image_path) image = QImage.fromData(image_data) yolo_path = os.path.join(args.dir, fname) tYoloParseReader = YoloReader(yolo_path, image) shapes = tYoloParseReader.getShapes() shapes = loadLabels(shapes) shapes = [format_shape(shape) for shape in shapes] label_file = LabelFile() voc_path = os.path.join(args.dir, f"{root}.xml") label_file.savePascalVocFormat( voc_path, shapes, image_path, image_data)
def saveFile(self): annotationFilePath = "images\\img_"+str(self.selectedPointList[self.currentIndexOfSelectedPoint-1])+".xml" filePath = "images\\img_"+str(self.selectedPointList[self.currentIndexOfSelectedPoint-1])+".jpg" if self.labelFile is None: self.labelFile = LabelFile() self.labelFile.verified = self.canvas.verified def format_shape(s): return dict(label=s.label, line_color=s.line_color.getRgb(), fill_color=s.fill_color.getRgb(), points=[(p.x(), p.y()) for p in s.points], difficult = s.difficult) shapes = [format_shape(shape) for shape in self.canvas.shapes] try: imageFilePath = os.popen('cd').read()[:-1] self.labelFile.savePascalVocFormat(annotationFilePath, shapes, imageFilePath+"\\"+filePath) return True except: self.errorMessage(u'Error', u'Error in saving label data') return False
def saveLabels(self, annotationFilePath): annotationFilePath = ustr(annotationFilePath) if self.labelFile is None: self.labelFile = LabelFile() self.labelFile.verified = self.canvas.verified def format_shape(s): return dict(label=s.label, line_color=s.line_color.getRgb() if s.line_color != self.lineColor else None, fill_color=s.fill_color.getRgb() if s.fill_color != self.fillColor else None, points=[(p.x(), p.y()) for p in s.points], difficult=s.difficult) shapes = [format_shape(shape) for shape in self.canvas.shapes] try: if self.usingPascalVocFormat is True: print('Img: ' + self.filePath + ' -> Its xml: ' + annotationFilePath) self.labelFile.savePascalVocFormat(annotationFilePath, shapes, self.filePath, self.imageData, self.lineColor.getRgb(), self.fillColor.getRgb()) else: self.labelFile.save(annotationFilePath, shapes, self.filePath, self.imageData, self.lineColor.getRgb(), self.fillColor.getRgb()) return True except LabelFileError as e: self.errorMessage(u'Error saving label data', u'<b>%s</b>' % e) return False
import numpy as np import json from IPython import embed import os, sys import cv2 import tqdm from libs.labelFile import LabelFile import glob labelFile = LabelFile() def generate_xml(images): #take out these: for image_left in tqdm.tqdm(images): image_left = image_left.split('/')[-1] this = image_path + image_left if to_gray: data = cv2.imread(this, cv2.IMREAD_GRAYSCALE) else: data = cv2.imread(this) h_scaler = np.ceil(540 / data.shape[0]) w_scaler = np.ceil(960 / data.shape[1]) blurer = int(max(3, max(data.shape) / 65)) data = np.tile(data, (int(h_scaler), int(w_scaler))) data = data[:540, :960] data = cv2.blur(data, (blurer, blurer)) image_save_as = "./tmp_empty_xmls/%s" % (image_left.replace( alter_image_format[0], alter_image_format[1])) #WARNING:MUST SAVE to get none 0x0 image!! cv2.imwrite(image_save_as, data) labelFile.savePascalVocFormat(
def loadFile(self, filePath=None): self.resetState() self.canvas.setEnabled(False) if filePath is None: filePath = self.settings.get(SETTING_FILENAME) filePath = ustr(filePath) unicodeFilePath = ustr(filePath) unicodeFilePath = os.path.abspath(unicodeFilePath) if unicodeFilePath and self.fileListWidget.count() > 0: if unicodeFilePath in self.mImgList: index = self.mImgList.index(unicodeFilePath) fileWidgetItem = self.fileListWidget.item(index) fileWidgetItem.setSelected(True) else: self.fileListWidget.clear() self.mImgList.clear() 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) self.canvas.verified = self.labelFile.verified else: # Load image: # read data first and store for saving into label file. self.imageData = read(unicodeFilePath, None) self.labelFile = None self.canvas.verified = False if isinstance(self.imageData, QImage): image = self.imageData else: 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) self.showBoundingBoxFromAnnotationFile(filePath) self.setWindowTitle(__appname__ + ' ' + filePath) # Default : select last item if there is at least one item if self.labelList.count(): self.labelList.setCurrentItem(self.labelList.item(self.labelList.count()-1)) self.labelList.item(self.labelList.count()-1).setSelected(True) self.canvas.setFocus(True) return True return False
def loadFile(self, filePath=None): self.resetState() self.canvas.setEnabled(False) if filePath is None: filePath = self.settings.get(SETTING_FILENAME) unicodeFilePath = ustr(filePath) 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: 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) if self.usingPascalVocFormat is True: if 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) else: xmlPath = os.path.splitext(filePath)[0] + XML_EXT if os.path.isfile(xmlPath): self.loadPascalXMLByFilename(xmlPath) self.setWindowTitle(__appname__ + ' ' + filePath) if self.labelList.count(): self.labelList.setCurrentItem( self.labelList.item(self.labelList.count() - 1)) self.labelList.item(self.labelList.count() - 1).setSelected(True) self.canvas.setFocus(True) return True return False
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(SETTING_FILENAME) # Make sure that filePath is a regular python string, rather than QString filePath = ustr(filePath) # Fix bug: An index error after select a directory when open a new file. unicodeFilePath = ustr(filePath) unicodeFilePath = os.path.abspath(unicodeFilePath) # Tzutalin 20160906 : Add file list and dock to move faster # Highlight the file item if unicodeFilePath and self.listView.count() > 0: if unicodeFilePath in self.mImgList: index = self.mImgList.index(unicodeFilePath) fileWidgetItem = self.listView.addItem(index) # item 没找到 fileWidgetItem.setSelected(True) else: self.listView.clear() self.mImgList.clear() 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) self.canvas.verified = self.labelFile.verified else: # Load image: # read data first and store for saving into label file. self.imageData = read(unicodeFilePath, None) self.labelFile = None self.canvas.verified = False 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) self.showBoundingBoxFromAnnotationFile(filePath) self.setWindowTitle(__appname__ + ' ' + filePath) # Default : select last item if there is at least one item if self.labelList.count(): self.labelList.setCurrentItem( self.labelList.item(self.labelList.count() - 1)) self.labelList.item(self.labelList.count() - 1).setSelected(True) self.canvas.setFocus(True) return True return False
def loadFile(self, filePath=None): """Load the specified file, or the last opened file if None.""" self.canvas.setEnabled(False) if filePath is None: filePath = self.settings.get('filename') unicodeFilePath = filePath # Tzutalin 20160906 : Add file list and dock to move faster # Highlight the file item if unicodeFilePath and self.ui.fileList.count() > 0: index = self.mImgList.index(unicodeFilePath) fileWidgetItem = self.ui.fileList.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 else: # Load image: # read data first and store for saving into label file self.canvas.image_np = cv2.imread( unicodeFilePath) # read as 3-channel with open(unicodeFilePath, 'rb') as f: self.imageData = f.read() 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.paintCanvas() self.addRecentFile(self.filePath) # Label xml file and show bound box according to its filename if self.usingPascalVocFormat is True: if 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) else: xmlPath = os.path.splitext(self.filePath)[0] + XML_EXT if os.path.isfile(xmlPath): self.loadPascalXMLByFilename(xmlPath) self.setWindowTitle(__appname__ + ' ' + filePath) # Default : select last item if there is at least one item #if self.ui.labelList.count(): #self.ui.labelList.setCurrentItem(self.ui.labelList.item(self.ui.labelList.count()-1)) #self.ui.labelList.setItemSelected(self.ui.labelList.item(self.ui.labelList.count()-1), True) self.canvas.setFocus(True) return True return False