def savePascalVocFormat(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) #imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format if isinstance(imageData, QImage): image = imageData else: image = QImage() image.load(imagePath) imageShape = [image.height(), image.width(), 1 if image.isGrayscale() else 3] writer = PascalVocWriter(imgFolderName, imgFileName, imageShape, localImgPath=imagePath) writer.verified = self.verified for shape in shapes: points = shape['points'] label = shape['label'] # Add Chris difficult = int(shape['difficult']) bndbox = LabelFile.convertPoints2BndBox(points) writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult) writer.save(targetFile=filename) return
def savePascalVocFormat(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format image = QImage() image.load(imagePath) imageShape = [ image.height(), image.width(), 1 if image.isGrayscale() else 3 ] self.writer = PascalVocWriter(imgFolderName, imgFileNameWithoutExt, imageShape, localImgPath=imagePath) self.writer.verified = self.verified for shape in shapes: label = shape['label'] guid = shape['guid'] parentGuid = shape['parentGuid'] lineWidth = shape['lineWidth'] points = shape['points'] ellipse_points = shape['ellipse_points'] rotate = shape['rotate'] r1 = shape['r1'] r2 = shape['r2'] center = shape['center'] #print("shape:", guid, " parent: ", parentGuid) self.writer.addVertexs(guid, parentGuid, label, lineWidth, rotate, r1, r2, center, points, ellipse_points) self.writer.save(targetFile=filename) return
def savePascalVocFormat(self, filename, shapes, imagePath, imageData, saveTimes=0, opUserName="******", opTime="无", isCheck=False, lastCheckTime=None, lineColor=None, fillColor=None, databaseSrc=None): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) #imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format filePath = ustr(imagePath) unicodeFilePath = ustr(filePath) if filePath.split('.')[-1] == 'tif': Img = cv2.imdecode(np.fromfile(unicodeFilePath, dtype=np.uint8), -1) height, width, bytesPerComponent = Img.shape bytesPerLine = 3 * width cv2.cvtColor(Img, cv2.COLOR_BGR2RGB, Img) image = QImage(Img.data, width, height, bytesPerLine, QImage.Format_RGB888) else: image = QImage() image.load(imagePath) imageShape = [ image.height(), image.width(), 1 if image.isGrayscale() else 3 ] writer = PascalVocWriter(imgFolderName, imgFileName, imageShape, saveTimes, opUserName, opTime, isCheck, lastCheckTime, localImgPath=imagePath) writer.verified = self.verified for shape in shapes: points = shape['points'] label = shape['label'] # Add Chris difficult = int(shape['difficult']) bndbox = LabelFile.convertPoints2BndBox(points) writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult) writer.save(targetFile=filename) return
def save_xml(imgFolderName, imgFileName, imagePath, imageShape, bndbox_list, save_xml_path, difficult=0): # imgFolderName = 'test' # imgFileName = '2007_000032.jpg' # imagePath = '/media/workspaces/test/2007_000032.jpg' # imageShape = [375, 500, 3] writer = PascalVocWriter(foldername=imgFolderName, filename=imgFileName, imgSize=imageShape, localImgPath=imagePath) # difficult = 0 for bndbox_label in bndbox_list: bndbox = bndbox_label[:4] label = bndbox_label[-1] # bndbox = (94, 178, 215, 231) # label = 'dog' writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult) # 添加框及坐标及困难度 filename = os.path.join(save_xml_path, imgFileName[:-4] + '.xml') writer.save(targetFile=filename)
def savePascalVocFormat(filename, shapes, imagePath, image): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) # imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format imageShape = [image.shape[0], image.shape[1], image.shape[2]] writer = PascalVocWriter(imgFolderName, imgFileName, imageShape, localImgPath=imagePath) def format_shape(s): return dict( label=s.label, line_color=None, fill_color=None, points=[(p.x, p.y) for p in s.points], # add chris difficult=s.difficult) for shape in shapes: shape = format_shape(shape) points = shape['points'] label = shape['label'] # Add Chris difficult = int(shape['difficult']) bndbox = LabelFile.convertPoints2BndBox(points) writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult) writer.save(targetFile=filename) return
def savePascalVocFormat(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None): """ Save a XML in pascal Voc format. :param filename: The name to save the file with. :param shapes: The shapes to save to the XML file. :param imagePath: The path to the image that the annotations correspond to. :param imageData: The data about the image, including the width, height, and depth. :param lineColor: The line color that is saved with the boxes. :param fillColor: The fill color that is saved with the boxes. :param databaseSrc: The source of the database that the annotations are located. """ imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format image = QImage() image.load(imagePath) imageShape = [image.height(), image.width(), 1 if image.isGrayscale() else 3] writer = PascalVocWriter(imgFolderName, imgFileNameWithoutExt,\ imageShape, localImgPath=imagePath) bSave = False for shape in shapes: points = shape['points'] label = shape['label'] bndbox = LabelFile.convertPoints2BndBox(points) writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label) bSave = True if bSave: writer.save(targetFile = filename) return
def savePascalVocFormat(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) #imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format image = QImage() image.load(imagePath) imageShape = [image.height(), image.width(), 1 if image.isGrayscale() else 3] writer = PascalVocWriter(imgFolderName, imgFileName, imageShape,localImgPath=imagePath) writer.verified = self.verified for shape in shapes: points = shape['points'] label = shape['label'] # Add Chris difficult = int(shape['difficult']) illumination = str(shape['illumination']) distince = str(shape['distince']) yaw = str(shape['yaw']) pitch = str(shape['pitch']) roll = str(shape['roll']) glasses = str(shape['glasses']) # yyk add bndbox = LabelFile.convertPoints2BndBox(points) writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult, illumination, distince, yaw, pitch, roll, glasses) writer.save(targetFile=filename) return
def savePascalVocFormat(self, filename, shapes, imagePath, imageData,type, lineColor=None, fillColor=None, databaseSrc=None): imgFolderPath = os.path.dirname(imagePath) # 图片路径 imgFolderName = os.path.split(imgFolderPath)[-1] # 文件夹名 imgFileName = os.path.basename(imagePath) # 文件名 # imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format image=cv2.imread(imagePath) imageShape = [image.shape[0], image.shape[1],image.shape[2]] writer = PascalVocWriter(imgFolderName, imgFileName,# 初始化写入类 imageShape,databaseSrc,imagePath) writer.verified = self.verified for shape in shapes: # 保存点集 points label type difficult points = shape['points'] label = shape['label'] # Add Chris difficult = int(shape['difficult']) type = int(shape['type']) order = int(shape['order']) current_id = int(shape['current_id']) track_id = int(shape['track_id']) occluded = int(shape['occluded']) exist_3D = int(shape['exist_3D']) # 当3D框存在对应的2D框,就无需保存,与2D框的属性保存在一起 if type == ShapeType['cuboid'].value and shape['exist_2D']: continue if type in [ShapeType['rect'].value,ShapeType['point'].value]: points_3D=[] if shape['exist_3D']: for tmp_shape in shapes: if tmp_shape['current_id']==shape['current_id'] and int(tmp_shape['type'])==ShapeType['cuboid'].value: points_3D = tmp_shape['points'] bndbox = LabelFile.convertPoints2BndBox(points) writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult, type,order,current_id,track_id,occluded,exist_3D,points_3D) else: writer.addLine(points, label, difficult, type, order,current_id,track_id,occluded,exist_3D) writer.save(targetFile=filename) return
def save_pascal_voc_format(self, filename, shapes, image_path, image_data, line_color=None, fill_color=None, database_src=None): img_folder_path = os.path.dirname(image_path) img_folder_name = os.path.split(img_folder_path)[-1] img_file_name = os.path.basename(image_path) # imgFileNameWithoutExt = os.path.splitext(img_file_name)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format if isinstance(image_data, QImage): image = image_data else: image = QImage() image.load(image_path) image_shape = [ image.height(), image.width(), 1 if image.isGrayscale() else 3 ] writer = PascalVocWriter(img_folder_name, img_file_name, image_shape, local_img_path=image_path) writer.verified = self.verified writer.class_ = self.class_ for shape in shapes: points = shape['points'] label = shape['label'] # Add Chris difficult = int(shape['difficult']) bnd_box = LabelFile.convert_points_to_bnd_box(points) writer.add_bnd_box(bnd_box[0], bnd_box[1], bnd_box[2], bnd_box[3], label, difficult) writer.save(target_file=filename) return
def savePascalVocFormat(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) #imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format image = QImage() image.load(imagePath) imageShape = [ image.height(), image.width(), 1 if image.isGrayscale() else 3 ] writer = PascalVocWriter(imgFolderName, imgFileName, imageShape, localImgPath=imagePath) writer.verified = self.verified for shape in shapes: label = shape['label'] pose = shape['pose'] unique = shape['unique'] # Add Chris difficult = int(shape['difficult']) x = shape['x'] y = shape['y'] polygontype = shape['polygonType'] if polygontype == 'rectangle': xbox, ybox = self.convertPoints2BndBox(x, y) else: xbox, ybox = x, y inpolygon = shape['inpolygon'] writer.addBndBox(label, pose, unique, difficult, polygontype, xbox, ybox, inpolygon) writer.save(targetFile=filename) return
def savePascalVocFormat(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) #imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format image = QImage() image.load(imagePath) imageShape = [ image.height(), image.width(), 1 if image.isGrayscale() else 3 ] writer = PascalVocWriter(imgFolderName, imgFileName, imageShape, localImgPath=imagePath) writer.verified = self.verified for shape in shapes: points = shape['points'] label = shape['label'] # Add Chris difficult = int(shape['difficult']) illumination = str(shape['illumination']) distince = str(shape['distince']) yaw = str(shape['yaw']) pitch = str(shape['pitch']) roll = str(shape['roll']) glasses = str(shape['glasses']) # yyk add bndbox = LabelFile.convertPoints2BndBox(points) writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult, illumination, distince, yaw, pitch, roll, glasses) writer.save(targetFile=filename) return
def run(self): '''主循环,逐帧进行处理,每帧逐bbox进行处理, 每个bbox:对self.have_tracked中的各个particle进行比较,求算质心距离,符合的则归为一类,不符合的则创建一类 对一帧处理结束要给所有的正在追踪Particle + 1,而之前每次放入新的Particle轨迹会初始化其数值 ''' flag = 0 for i in range(len(self.bboxs)): # 每一个图片 for j in range(len(self.bboxs[i])): # 判断 bbox = self.bboxs[i][j] if len(bbox) == 6: # 框的参数必须为5 self.search(i, j, bbox) if len(self.have_tracked) > 0: # TODO:这里后面需要修改,将两种方法合并为一种 win_no_nano = self.Frame_limit if self.Method == 0: # 减第一帧所用的track for k in range(len(self.have_tracked)): # 对已经跟踪的类进行比较 if self.have_tracked[k][0][2] != 'OVER': if self.have_tracked[k][0][2] < win_no_nano: self.have_tracked[k][0][2] += 1 # else: # self.have_tracked[k][0][2] = 'OVER' elif (self.have_tracked[k][0][2] >= win_no_nano) and (self.have_tracked[k][-1][0] % self.SubImg_T != 0): self.have_tracked[k][0][2] = 'OVER' elif self.Method == 1: # 减前一帧所用的track for k in range(len(self.have_tracked)): if self.have_tracked[k][0][2] != 'OVER': if self.have_tracked[k][0][2] < win_no_nano: self.have_tracked[k][0][2] += 1 if len(self.have_tracked[k]) >= 2 and self.have_tracked[k][0][2] >= 5: # 里面只有第一帧和最后一帧 if self.have_tracked[k][-1][2] == "debinding": self.have_tracked[k][0][2] = 'OVER' # 进度条的加载 flag += 1 if self.quick_flag == 1: break prograssbar_value = round(flag / len(self.bboxs), 2) * 100 self.progressBarValue[int].emit(int(prograssbar_value)) # 将跟踪后的写入 for i in range(len(self.bboxs)): newbbox = self.bboxs[i] name = int(self.dir[i].split(".")[0]) filename = "%d.tif" % name xmlwriter = PascalVocWriter("VOC2007", filename, [523, 525, 1]) for box in newbbox: xmlwriter.addBndBox(box[1], box[2], box[3], box[4], box[0], "0", box[5]) xmlwriter.save(self.track_xml_dir + "/" + str(name) + ".xml") self.after_track[int].emit(1)
def savePascalVocFormat(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) # imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format imageShape = [ imageData.height(), imageData.width(), 1 if imageData.isGrayscale() else 3 ] writer = PascalVocWriter(imgFolderName, imgFileName, imageShape, localImgPath=imagePath) writer.verified = self.verified for shape in shapes: points = shape['points'] label = shape['label'] bbox_source = shape['bbox_source'] id_number = shape['id_number'] score = shape['score'] mask = shape['mask'] # try: # score = shape['score'] # except KeyError: # score = -1 # Add Chris difficult = int(shape['difficult']) bndbox = LabelFile.convertPoints2BndBox(points, label) writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult, bbox_source, id_number, score, mask) writer.save(targetFile=filename) return
def savePascalVocFormat(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) image = QImage() image.load(imagePath) imageShape = [image.height(), image.width(), 1 if image.isGrayscale() else 3] writer = PascalVocWriter(imgFolderName, imgFileName, imageShape, localImgPath=imagePath) writer.verified = self.verified for shape in shapes: points = shape['points'] label = shape['label'] # difficult = int(shape['difficult']) contour_points = shape['contour_points'] confidence = shape['confidence'] contourEdited = shape['contourEdited'] bndbox = LabelFile.convertPoints2BndBox(points) writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, contour_points, confidence, contourEdited) writer.save(targetFile=filename) return
def savePascalVocFormat(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) # Read from file path because self.imageData might be empty if saving to # Pascal format image = QImage() image.load(imagePath) imageShape = [ image.height(), image.width(), 1 if image.isGrayscale() else 3 ] writer = PascalVocWriter(imgFolderName, imgFileName, imageShape, localImgPath=imagePath) writer.verified = self.verified for shape in shapes: points = shape['points'] label = shape['label'] # Add Chris difficult = int(shape['difficult']) # # Add Jerry shapeType = shape["shapeType"] writer.addShape(shapeType, label, points, difficult) writer.save(targetFile=filename) return
def run(self): # 测试一张图片 dirname = self.dirname imgs = os.listdir(dirname) if imgs is not None: img_paths = sorted( imgs, key=lambda x: int(x.split('/')[-1].split('.')[0])) img_size = cv2.imread(dirname + "/" + imgs[0]).shape flag = 0 for img_path in img_paths: # print(img_path) path = dirname + "/" + img_path id = img_path.split('.')[0] img = Image.open(path) # low_img = np.array(img).astype(np.uint8) low_img = cv2.imread(path) image = Image.fromarray(low_img) # print(np.array(image).dtype) bboxs = self.yolo4.detect_image(image) # print(bboxs) # 寻找最大值 if self.is_search_central == 1: newbboxs = self.search_central(img, bboxs) else: newbboxs = bboxs filename = id + ".tif" xmlwriter = PascalVocWriter("VOC2007", filename, img_size) for box in newbboxs: intensity = self.get_intensity(img, box) # 获取强度值 xmlwriter.addBndBox(box[0], box[1], box[2], box[3], self.classes[int(box[-1])], "0", intensity) xmlwriter.save(self.defaultSaveDir + "/" + id + ".xml") flag += 1 if self.quick_flag == 1: break prograssbar_value = round(flag / len(imgs), 2) * 100 self.progressBarValue[int].emit(int(prograssbar_value))
def run(self): # 测试一张图片 dirname = self.dirname imgs = os.listdir(dirname) if imgs is not None: imgs = sorted(imgs, key=lambda x: int(x.split('/')[-1].split('.')[0])) img_size = cv2.imread(dirname + "/" + imgs[0]).shape flag = 0 for img in imgs: print(img) path = dirname + "/" + img id = img.split('.')[0] result = inference_detector(self.model, path) if isinstance(result, tuple): bbox_result, segm_result = result else: bbox_result, segm_result = result, None bboxes = np.vstack(bbox_result) scores = bboxes[:, -1] inds = scores > self.score bboxes = bboxes[inds, :] temp = [] for bbox in bboxes: bbox_int = bbox.astype(np.int32) temp.append(bbox_int) newbbox = self.check_bbox(temp) filename = id + ".tif" xmlwriter = PascalVocWriter("VOC2007", filename, img_size) for box in newbbox: xmlwriter.addBndBox(box[0], box[1], box[2], box[3], "nano", "0") xmlwriter.save(self.defaultSaveDir + "/" + id + ".xml") flag += 1 if self.quick_flag == 1: break prograssbar_value = round(flag / len(imgs), 2) * 100 self.progressBarValue[int].emit(int(prograssbar_value))
def savePascalVocFormat(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None, objects=None, geoInfo=None): """ :param filename: :param shapes: :param imagePath: :param imageData: :param lineColor: :param fillColor: :param databaseSrc: :param objectItems: is the objectItems data for xml file to write :return: """ imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) #imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format image = QImage() image.load(imagePath) imageShape = [ image.height(), image.width(), 1 if image.isGrayscale() else 3 ] writer = PascalVocWriter(imgFolderName, imgFileName, imageShape, localImgPath=imagePath) writer.verified = self.verified try: writer.latitude = geoInfo[0] writer.longitude = geoInfo[1] writer.altitude = geoInfo[2] except: print("no geoinfo in labelfile savePascalFormat") count = 0 for shape in shapes: points = shape['points'] label = shape['label'] # Add Chris difficult = int(shape['difficult']) bndbox = LabelFile.convertPoints2BndBox(points) try: writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult, objectItems=objects[count]) except Exception as e: print('Exception in savePascalVocFormat labelFile.py:', str(e)) writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult) count += 1 writer.save(targetFile=filename) return
def savePascalVocFormat(self, filename, shapes, imagePath, model=None, canvas=None): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) #imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format image = QImage() image.load(imagePath) imageShape = [image.height(), image.width(), 1 if image.isGrayscale() else 3] writer = PascalVocWriter(imgFolderName, imgFileName, imageShape, model, localImgPath=imagePath, hsv=canvas.hsv) writer.verified = self.verified maskFilePath = os.path.dirname(filename) fName = os.path.basename(filename).split(".")[0] # create mask folder maskFilePath = os.path.dirname(maskFilePath) maskFilePath = os.path.join(maskFilePath, "masks") if not os.path.exists(maskFilePath): os.makedirs(maskFilePath) idx = 1 for shape in shapes: # generate mask filename label = shape['label'] maskType = shape['maskType'] points = shape['points'] if maskType == 0: # bounding box difficult = int(shape['difficult']) bndbox = LabelFile.convertPoints2BndBox(points) writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult, maskType) elif maskType == 1: # get mask for line maskFileName = fName + "_" + str(idx) + "_" + str(label) + ".png" maskFile = os.path.join(maskFilePath,maskFileName) # use tmask to get bounding box and maskPixmap for segment tmask = canvas.getMask(points, maskType) segment,x1,y1,x2,y2 = libs.np2qt.getSegment(tmask, maskFile) #TODO: make string (list of tuples) strPoints = str(points) writer.addBndBox(x1, y1, x2, y2, label, 0, str(maskType), strPoints,segment) idx +=1 elif maskType == 2: # BOX Mask maskFileName = fName + "_" + str(idx) + "_" + str(label) + ".png" maskFile = os.path.join(maskFilePath,maskFileName) # use tmask to get bounding box and maskPixmap for segment tmask = canvas.getMask(points, maskType) segment,x1,y1,x2,y2 = libs.np2qt.getBoxSegment(tmask, imagePath, canvas.hsv, maskFile) #TODO: make string (list of tuples) strPoints = str(points) writer.addBndBox(x1, y1, x2, y2, label, 0, str(maskType), strPoints,segment) idx +=1 writer.save(targetFile=filename) return
def scaleAndOffset(src, xmin, ymin, xmax, ymax, outPath, shapes): width = xmax - xmin height = ymax - ymin ####################### # scale ####################### scale = random.uniform(MIN_SCALE, 1.0) if ((width * scale < PX_MINSIZE) and (height * scale < PX_MINSIZE)): scale = PX_MINSIZE / width if ( PX_MINSIZE / width < PX_MINSIZE / height) else PX_MINSIZE / height if ((width * scale > PX_MAXSIZE) or (height * scale > PX_MAXSIZE)): scale = PX_MAXSIZE / width if ( PX_MAXSIZE / width < PX_MAXSIZE / height) else PX_MAXSIZE / height if (src.shape[1] * scale < PX_OUTSIZE or src.shape[0] * scale < PX_OUTSIZE): scale = PX_OUTSIZE / src.shape[1] if ( src.shape[1] < src.shape[0]) else PX_OUTSIZE / src.shape[0] if (scale > 1.0): scale = 1.0 dst = cv2.resize(src, dsize=None, fx=scale, fy=scale) ####################### # offset ####################### offsetX = (PX_OUTSIZE - width * scale) / 2 * random.uniform(-1.0, 1.0) offsetY = (PX_OUTSIZE - height * scale) / 2 * random.uniform(-1.0, 1.0) left = int((xmin + width / 2) * scale - PX_OUTSIZE / 2 + offsetX) top = int((ymin + height / 2) * scale - PX_OUTSIZE / 2 + offsetY) if (left < 0): offsetX -= left left = 0 if (left + PX_OUTSIZE > dst.shape[1]): offsetX -= left - (dst.shape[1] - PX_OUTSIZE) left = dst.shape[1] - PX_OUTSIZE if (top < 0): offsetY -= top top = 0 if (top + PX_OUTSIZE > dst.shape[0]): offsetY -= top - (dst.shape[0] - PX_OUTSIZE) top = dst.shape[0] - PX_OUTSIZE dst = dst[top:top + PX_OUTSIZE, left:left + PX_OUTSIZE] foldername = os.path.basename(os.path.dirname(outPath)) filename = os.path.basename(outPath) imgSize = (PX_OUTSIZE, PX_OUTSIZE) xmlWriter = PascalVocWriter(foldername, filename, imgSize, localImgPath=outPath) for i in range(len(shapes)): outXmin = int(shapes[i][1][0][0] * scale - left) outYmin = int(shapes[i][1][0][1] * scale - top) outXmax = int(shapes[i][1][2][0] * scale - left) outYmax = int(shapes[i][1][2][1] * scale - top) xmlWriter.addBndBox(outXmin, outYmin, outXmax, outYmax, shapes[i][0], 0) cv2.imwrite(outPath, dst) xmlWriter.save(outPath[:-4] + ".xml")
def savePascalVocFormat(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) #imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format image = QImage() image.load(imagePath) imageShape = [ image.height(), image.width(), 1 if image.isGrayscale() else 3 ] writer = PascalVocWriter(imgFolderName, imgFileName, imageShape, localImgPath=imagePath) writer.verified = self.verified for shape in shapes: points = shape['points'] label = shape['label'] difficult = int(shape['difficult']) tetragon = shape['tetragon'] angle = int(round(shape['deg'])) d3 = shape['shape3D'] if d3 == False: if tetragon == False: bndbox = LabelFile.convertPoints2BndBox(points) writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult, tetragon, angle) elif tetragon == True: writer.addBndBox2(points, label, difficult, tetragon, angle) elif d3: writer.addBndBox3(d3, points, label, angle, difficult) writer.save(targetFile=filename) return
def savePascalVocFormat(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) #imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format image = QImage() image.load(imagePath) imageShape = [ image.height(), image.width(), 1 if image.isGrayscale() else 3 ] writer = PascalVocWriter(imgFolderName, imgFileName, imageShape, localImgPath=imagePath) writer.verified = self.verified enable_del = False init_read_save_file = False new_shapes = [] for shape in shapes: points = shape['points'] label = shape['label'] if label == "del": if not init_read_save_file: init_read_save_file = True file_img = imgFolderPath + "/" + imgFileName img = cv2.imread(file_img) #draw black rectangle cv2.rectangle(img, (int(points[0][0]), int(points[0][1])), (int(points[2][0]), int(points[2][1])), (0, 0, 0), -1) continue # copy data new_shapes.append(shape) #save file if init_read_save_file: b = cv2.imwrite(file_img, img) print("save new file {} = {}".format(file_img, b)) #save xml for shape in new_shapes: points = shape['points'] label = shape['label'] # Add Chris difficult = int(shape['difficult']) bndbox = LabelFile.convertPoints2BndBox(points) writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult) writer.save(targetFile=filename) return
def run(self): path = self.msg['path'] frame_number = self.msg['frame_number'] width = self.msg['width'] height = self.msg['height'] channel = self.msg['channel'] bboxes = self.msg['bboxes'] scores = self.msg['scores'] labels = self.msg['labels'] bbox_source = self.msg['bbox_source'] id_numbers = self.msg['id_numbers'] last_frame_number = self.msg['last_frame_number'] trigger_tracking_request = self.msg['trigger_tracking_request'] num_frames = self.msg['num_frames'] new_box_from_pt = (bbox_source == 'single_object_tracker') num_new_bboxes = len(bboxes) if 'masks' in self.msg.keys(): masks = self.msg['masks'] else: masks = None trigger_batch_message = num_frames > 1 if not self.main_window.frames_reader or os.path.abspath( path) != self.main_window.frames_reader.get_path(): print("Incorrect path!") # send_self.msg_to_connection(dict(status="failure", error="Incorrect path!"), connection) else: if new_box_from_pt or self.save_boxes: xml_path = self.main_window.get_annotation_path(frame_number) if os.path.exists(xml_path): try: tVocParseReader = PascalVocReader(xml_path) shapes = tVocParseReader.getShapes() except: shapes = [] else: shapes = [] imageShape = [height, width, channel] if isinstance(self.main_window.frames_reader, DirectoryReader): img_full_path = self.main_window.mImgList[frame_number] folder_name = os.path.dirname(img_full_path) file_name = os.path.basename(img_full_path) else: folder_name = self.main_window.frames_reader.video_fn file_name = str(frame_number) tVocWriter = PascalVocWriter(folder_name, file_name, imageShape) # for label, points, _, _, difficult, _bbox_source, id_number, score, mask, mask_img in shapes: existing_box_from_gt = _bbox_source == "ground_truth" if new_box_from_pt and existing_box_from_gt and id_number == id_numbers[ 0]: print( 'Received duplicale target {:d} bbox for frame {:d} for which GT exists' .format(id_numbers[0], frame_number)) return bndbox = LabelFile.convertPoints2BndBox(points, label) if existing_box_from_gt or _bbox_source != bbox_source or label == 'gate' or \ (_bbox_source == "single_object_tracker" and id_number != id_numbers[0]): # Override previous bboxes from the same source tVocWriter.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult, _bbox_source, id_number, score, mask, mask_img) for j in range(num_new_bboxes): bbox = bboxes[j] xmin = bbox['xmin'] ymin = bbox['ymin'] xmax = bbox['xmax'] ymax = bbox['ymax'] label = labels[j] score = scores[j] difficulty = False id_number = id_numbers[j] if masks is not None: # box_h = ymax - ymin # box_w = xmax - xmin # mask_img = np.array(masks[j]).reshape((box_h, box_w)) # mask_img = np.array(masks[j]) print('Loading mask from {:s}'.format(masks[j])) # mask_img = cv2.imread(masks[j]) mask_img = np.load(masks[j]) if mask_img is None: print('mask image could not be read') else: mask_img = None tVocWriter.addBndBox(xmin, ymin, xmax, ymax, label, difficulty, bbox_source, id_number, score, None, mask_img) # print('bbox: ', bbox) tVocWriter.save(targetFile=xml_path) if self.currIndex == frame_number: self.reload_signal.emit() if new_box_from_pt: print('Received target {:d} bbox for frame {:d}'.format( id_numbers[0], frame_number)) else: self.update_status_signal.emit( "Received {:d} {:s} bboxes for frame {:d}.".format( num_new_bboxes, bbox_source, frame_number)) if last_frame_number == frame_number and not trigger_tracking_request: self.update_status_signal.emit( "Running finished for {:d} frames.".format(num_frames)) self.finish_signal.emit(trigger_batch_message)
class LabelFile(object): # It might be changed as window creates. By default, using XML ext # suffix = '.lif' suffix = XML_EXT def __init__(self, filename=None): self.shapes = () self.imagePath = None self.imageData = None self.verified = False self.labels = [] def setLabels(self, labels): self.labels = labels def savePascalVocFormat(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format image = QImage() image.load(imagePath) imageShape = [ image.height(), image.width(), 1 if image.isGrayscale() else 3 ] self.writer = PascalVocWriter(imgFolderName, imgFileNameWithoutExt, imageShape, localImgPath=imagePath) self.writer.verified = self.verified for shape in shapes: label = shape['label'] guid = shape['guid'] parentGuid = shape['parentGuid'] lineWidth = shape['lineWidth'] points = shape['points'] ellipse_points = shape['ellipse_points'] rotate = shape['rotate'] r1 = shape['r1'] r2 = shape['r2'] center = shape['center'] #print("shape:", guid, " parent: ", parentGuid) self.writer.addVertexs(guid, parentGuid, label, lineWidth, rotate, r1, r2, center, points, ellipse_points) self.writer.save(targetFile=filename) return def saveData(self, filename, shapes, imagePath, cannyKSize, boxKSize, labelKSize, save_rf_Data=False): labelMat = self.writer.createLabelMat(imagePath, shapes, cannyKSize, boxKSize, labelKSize) image = QImage() image.load(imagePath) label = 0 data = QByteArray() minMaxPoints = self.getMinMaxPoints(shapes) start = time.time() with open(filename, 'wb') as f: for y in range(image.height()): for x in range(image.width()): if x > minMaxPoints[0] and y > minMaxPoints[ 1] and x < minMaxPoints[2] and y < minMaxPoints[3]: label = labelMat[y, x] else: label = 0 data.append('{0}'.format(label)) #data.append('\n') #f.write(data) f.write(QtCore.qCompress(data, 9)) elapse = time.time() - start if save_rf_Data: vizCompressed(filename) print("finish write data to file: {0}s".format(elapse)) def getPixelLabel(self, x, y, shapes): labels = [] if shapes is None: return 0 for shape in shapes: result = shape.isContainsPixel(x, y) if result == 0: labels.append(self.labels.index((shape.label)) + 1) size = len(labels) if size < 1: return 0 else: labels.sort() return labels[size - 1] def getMinMaxPoints(self, shapes): x = [] y = [] for shape in shapes: minMaxPoints = shape.getMinMaxPoints() x.append(minMaxPoints[0]) x.append(minMaxPoints[2]) y.append(minMaxPoints[1]) y.append(minMaxPoints[3]) x.sort() y.sort() size = len(x) return (x[0], y[0], x[size - 1], y[size - 1]) def toggleVerify(self): self.verified = not self.verified @staticmethod def isLabelFile(filename): fileSuffix = os.path.splitext(filename)[1].lower() return fileSuffix == LabelFile.suffix
#TODO get the depth from the image filename = PurePosixPath(name).name filexml = Path(name).stem + '.xml' filepng = Path(name).stem + '.png' printname = Path(name).stem xmlfilepath = PurePath(rootpath, filexml) jpgfilepath = PurePath(rootpath, filename) debugfilepath = PurePath(rootpathdebug, Path(name).stem + '.png') depth = 3 # TODO Depth is 3 for color images, how to read this out writer = PascalVocWriter(xmlpathheader, filename, (cols, rows, depth)) difficult = 1 # print("File [%s] Object [%s] width [%d] height [%d] depth [%d]" % (filename, objectname, cols, rows, depth)) # Run the model out = sess.run([ sess.graph.get_tensor_by_name('num_detections:0'), sess.graph.get_tensor_by_name('detection_scores:0'), sess.graph.get_tensor_by_name('detection_boxes:0'), sess.graph.get_tensor_by_name('detection_classes:0') ], feed_dict={ 'image_tensor:0': inp.reshape(1, inp.shape[0], inp.shape[1], 3) })
def savePascalVocFormat(self, filename, shapes, imagePath, imageData, lineColor=None, fillColor=None, databaseSrc=None): imgFolderPath = os.path.dirname(imagePath) imgFolderName = os.path.split(imgFolderPath)[-1] imgFileName = os.path.basename(imagePath) #imgFileNameWithoutExt = os.path.splitext(imgFileName)[0] # Read from file path because self.imageData might be empty if saving to # Pascal format image = QImage() image.load(imagePath) imageShape = [image.height(), image.width(), 1 if image.isGrayscale() else 3] writer = PascalVocWriter(imgFolderName, imgFileName, imageShape, localImgPath=imagePath) writer.verified = self.verified for shape in shapes: points = shape['points'] label = shape['label'] # Add Chris gender = int(shape['ismale']) age = int(shape['age']) blurriness = int(shape['blur']) if shape['laugh']: emotion = 1 elif shape['shock']: emotion = 2 else: emotion = 0 if shape['dim']: illumination = 1 elif shape['bright']: illumination = 2 elif shape['backlight']: illumination = 3 elif shape['yinyang']: illumination = 4 else: illumination = 0 if shape['partial']: occlusion = 1 elif shape['heavy']: occlusion = 2 elif shape['sunglasses']: occlusion = 3 else: occlusion = 0 if shape['yaw_30']: yaw = 1 elif shape['yaw_60']: yaw = 2 else: yaw = 0 if shape['roll_20']: roll = 1 elif shape['roll_45']: roll = 2 else: roll = 0 if shape['pitch_20up']: pitch = 1 elif shape['pitch_45up']: pitch = 2 elif shape['pitch_20down']: pitch = 3 elif shape['pitch_45down']: pitch = 4 else: pitch = 0 bndbox = LabelFile.convertPoints2BndBox(points) writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, gender, age, emotion, illumination, blurriness, occlusion, yaw, roll, pitch) writer.save(targetFile=filename) return
def saveMasks(seq_path, xml_path, out_mask_size, out_border, fixed_ar, save_raw_mask, show_img, out_root_path='', save_test=1, save_train=1, frames_reader=None, masks_per_seq=0, enable_out_suffix=1, train_fnames=None, test_fnames=None, map_to_bbox=0, out_img_dir='', enable_xml_annotations=0, allow_skipping_images=0): global _pause, _exit if not xml_path or not os.path.isdir(xml_path): raise IOError( 'Folder containing the loaded boxes does not exist: {}'.format( xml_path)) files = glob.glob(os.path.join(xml_path, '*.xml')) n_files = len(files) if n_files == 0: raise IOError('No loaded boxes found') if frames_reader is None: frames_reader = get_frames_reader(seq_path, save_as_bin=False) min_dim = max_dim = 0 out_w, out_h = out_mask_size print('out_mask_size: {}'.format(out_mask_size)) if out_w == -1 and out_h == -1: out_w = out_h = 0 if out_w == -1: max_dim = out_h elif out_h == -1: min_dim = out_w if fixed_ar: print('Using fixed aspect ratio: {}'.format(fixed_ar)) print('out_border: {}'.format(out_border)) def getint(fn): basename = os.path.basename(fn) num = re.sub("\D", "", basename) try: return int(num) except: return 0 if len(files) > 0: files = sorted(files, key=getint) print('Loading annotations from {:d} files'.format(n_files)) file_id = 0 n_boxes = 0 seq_root_dir = os.path.dirname(seq_path) seq_name = os.path.basename(seq_path) if not out_root_path: out_root_path = os.path.join(seq_root_dir, 'masks') if not enable_out_suffix: out_seq_name = seq_name else: if map_to_bbox: out_seq_name = '{}_mapped'.format(seq_name) else: out_seq_name = '{}_{}x{}'.format(seq_name, out_w, out_h) if fixed_ar: out_seq_name = '{}_ar_{}'.format(out_seq_name, fixed_ar) else: out_seq_name = '{}_{}'.format(out_seq_name, out_border) out_seq_name = out_seq_name.replace('.', 'p') train_root_path = os.path.join(out_root_path, out_seq_name) if not save_test and not save_train: raise AssertionError('Either save_test or save_train must be on') # print('Saving output sequences to {}'.format(out_root_path)) if save_train: out_img_root_path = train_root_path if out_img_dir: out_img_root_path = os.path.join(out_img_root_path, out_img_dir) out_mask_root_path = os.path.join(train_root_path, 'labels') print('Saving training mask sequence to {}'.format(train_root_path)) if not os.path.isdir(out_img_root_path): os.makedirs(out_img_root_path) if not os.path.isdir(out_mask_root_path): os.makedirs(out_mask_root_path) if enable_xml_annotations: out_xml_path = os.path.join(out_img_root_path, 'annotations') print('Saving xml_annotations to {}'.format(out_xml_path)) if not os.path.isdir(out_xml_path): os.makedirs(out_xml_path) if save_test: out_test_seq_name = out_seq_name + '_test' test_img_root_path = os.path.join(out_root_path, out_test_seq_name) print('Saving unlabeled testing mask sequence to {}'.format( test_img_root_path)) if not os.path.isdir(test_img_root_path): os.makedirs(test_img_root_path) win_name = 'patch and mask' disable_resizing = 0 scale_x = scale_y = 1.0 if out_w == 0 and out_h == 0: print('Resizing disabled') disable_resizing = 1 csv_raw = [] test_csv_raw = [] n_files = len(files) if save_raw_mask: print('Saving raw labels') mask_pix_val = (1, 1, 1) else: mask_pix_val = (255, 255, 255) n_masks = 0 _train_fnames = [] _test_fnames = [] _exit_seq = 0 disp_img = None for file_id, file in enumerate(files): xml_reader = PascalVocReader(file) filename = os.path.basename(xml_reader.filename) filename_no_ext = os.path.splitext(filename)[0] # file_id = int(re.sub("\D", "", filename)) # print('filename: {}'.format(filename)) # print('file_id: {}'.format(file_id)) img = frames_reader.get_frame_by_name(filename, convert_to_rgb=0) if img is None: print('image {} could not be read'.format(filename)) continue img_h, img_w = img.shape[:2] mask_img = None shapes = xml_reader.getShapes() n_shapes = len(shapes) if n_shapes > 1: print('{} boxes found for {} in {}'.format(n_shapes, filename, file)) obj_id = 0 img_written = 0 for shape in shapes: label, points, _, _, difficult, bbox_source, id_number, score, mask, mask_img = shape if not mask: if not save_test: continue xmin, ymin = points[0] xmax, ymax = points[2] img_root_path = test_img_root_path else: if not save_train: continue mask_pts_list = Shape.getContourPts(mask, verbose=0) mask_pts = np.asarray(mask_pts_list) xmin, ymin = np.min(mask_pts, axis=0).astype(np.int32) xmax, ymax = np.max(mask_pts, axis=0).astype(np.int32) img_root_path = out_img_root_path if fixed_ar: w, h = xmax - xmin, ymax - ymin src_ar = float(w) / float(h) if fixed_ar > src_ar: border_x = int((h * fixed_ar - w) / 2.0) border_y = 0 else: border_y = int((w / fixed_ar - h) / 2.0) border_x = 0 else: border_x = border_y = out_border # start_row, start_col = max(0, ymin - border_y), max(0, xmin - border_x) # end_row, end_col = min(img_h - 1, ymax + border_y), min(img_w - 1, xmax + border_x) start_row, start_col = ymin - border_y, xmin - border_x end_row, end_col = ymax + border_y, xmax + border_x if start_row < 0 or start_col < 0 or end_row >= img_h or end_col >= img_w: msg = 'Invalid border {} for box {} in image {} of size {}'.format( [border_x, border_y], [xmin, ymin, xmax, ymax], filename, [img_w, img_h]) if allow_skipping_images: print('\n' + msg + '\n') continue else: raise AssertionError(msg) if mask: n_masks += 1 w, h = end_col - start_col, end_row - start_row patch_img = img[start_row:end_row, start_col:end_col, :] if not disable_resizing: if max_dim > 0: if w > h: out_w = max_dim out_h = 0 else: out_h = max_dim out_w = 0 elif min_dim > 0: if w < h: out_w = min_dim out_h = 0 else: out_h = min_dim out_w = 0 else: out_w, out_h = out_mask_size scale_x = float(out_w) / float(w) scale_y = float(out_h) / float(h) if scale_x == 0: scale_x = scale_y out_w = int(w * scale_x) elif scale_y == 0: scale_y = scale_x out_h = int(h * scale_y) try: patch_img = cv2.resize(patch_img, (out_w, out_h)) # print('patch_img: {}'.format(patch_img.shape)) except cv2.error as e: print('patch_img: {}'.format(patch_img.shape)) print('out_size: {}, {}'.format(start_row, start_col)) print('out_size: {}, {}'.format(end_row, end_col)) print('out_size: {}, {}'.format(out_w, out_h)) raise cv2.error(e) else: out_w, out_h = w, h _label = label if id_number is None: id_number = -1 if id_number > 0: _label = '{}_{}'.format(_label, id_number) if enable_out_suffix: out_fname = '{}_{}_{}'.format(filename_no_ext, obj_id, label) else: out_fname = filename_no_ext _xmin, _ymin = int((xmin - start_col) * scale_x), int( (ymin - start_row) * scale_y) _xmax, _ymax = int((xmax - start_col) * scale_x), int( (ymax - start_row) * scale_y) if map_to_bbox: if not img_written: img_written = 1 out_img_path = os.path.join(img_root_path, filename) cv2.imwrite(out_img_path, img) if enable_xml_annotations: imageShape = [xml_reader.height, xml_reader.width, 3] xml_writer = PascalVocWriter(out_xml_path, filename, imageShape) if mask: if enable_xml_annotations: bndbox = [xmin, ymin, xmax, ymax] xml_writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label, difficult, bbox_source, id_number, score, mask, mask_img) raw_data = { 'target_id': int(id_number), 'filename': filename, 'width': img_w, 'height': img_h, 'class': label, 'xmin': xmin, 'ymin': ymin, 'xmax': xmax, 'ymax': ymax } if show_img: cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2) disp_img = img else: img_out_fname = out_fname + '.jpg' if mask: out_img_path = os.path.join(img_root_path, img_out_fname) cv2.imwrite(out_img_path, patch_img) if enable_xml_annotations: n_mask = len(mask) _mask = [] for i in range(n_mask): _mask.append([(mask[i][0] - start_col) * scale_x, (mask[i][1] - start_row) * scale_y, mask[i][2]]) imageShape = [xml_reader.height, xml_reader.width, 3] xml_writer = PascalVocWriter(out_xml_path, xml_reader.filename, imageShape) bndbox = [_xmin, _ymin, _xmax, _ymax] xml_writer.addBndBox(_xmin, _ymin, _xmax, _ymax, label, difficult, bbox_source, id_number, score, _mask) raw_data = { 'target_id': int(id_number), 'filename': img_out_fname, 'width': out_w, 'height': out_h, 'class': label, 'xmin': _xmin, 'ymin': _ymin, 'xmax': _xmax, 'ymax': _ymax } if show_img: cv2.rectangle(patch_img, (_xmin, _ymin), (_xmax, _ymax), (0, 255, 0), 2) disp_img = patch_img if mask: if mask_img is None: mask_img = np.zeros_like(img) # print('border_x: {}'.format(border_x)) # print('border_y: {}'.format(border_y)) # print('scale_x: {}'.format(scale_x)) # print('scale_y: {}'.format(scale_y)) # # print('xmin: {}'.format(xmin)) # print('ymin: {}'.format(ymin)) mask_pts = [[(x - xmin + border_x) * scale_x, (y - ymin + border_y) * scale_y] for x, y in mask_pts] curr_mask = np.zeros_like(patch_img, dtype=np.uint8) # print('mask_img: {}'.format(mask_img.shape)) mask_out_fname = out_fname + '.png' # np.savetxt('mask_seq_mask_pts.txt', mask_pts, fmt='%.6f') curr_mask = cv2.fillPoly( curr_mask, np.array([ mask_pts, ], dtype=np.int32), mask_pix_val) # print('min: {} max: {}'.format( # np.min(mask_img.flatten()), # np.max(mask_img.flatten())) # ) if map_to_bbox: mask_img = map_mask_to_bbox( (xmin, ymin, xmax, ymax), curr_mask, fixed_ar, out_border, mask_img.shape, mask_img) else: mask_img = curr_mask out_mask_path = os.path.join(out_mask_root_path, mask_out_fname) cv2.imwrite(out_mask_path, mask_img) _train_fnames.append((out_img_path, out_mask_path)) if show_img: disp_mask_img = mask_img.copy() if save_raw_mask: disp_mask_img[disp_mask_img > 0] = 255 blended_img = np.asarray( Image.blend(Image.fromarray(patch_img), Image.fromarray(disp_mask_img), 0.5)) disp_img = np.concatenate( (disp_img, disp_mask_img, blended_img), axis=1) csv_raw.append(raw_data) else: test_csv_raw.append(raw_data) if not map_to_bbox: _test_fnames.append(out_img_path) if show_img and not map_to_bbox: # if _pause: # print('frame {} :: {}'.format(file_id, filename)) cv2.imshow(win_name, disp_img) k = cv2.waitKey(1 - _pause) if k == ord('q'): _exit = 1 break elif k == 27: _exit_seq = 1 break elif k == 32: _pause = 1 - _pause obj_id += 1 if map_to_bbox and img is not None: out_img_path = os.path.join(out_img_root_path, filename) if save_train and mask_img is not None: mask_out_fname = filename_no_ext + '.png' out_mask_path = os.path.join(out_mask_root_path, mask_out_fname) cv2.imwrite(out_mask_path, mask_img) if enable_xml_annotations: out_xml_file = os.path.join(out_xml_path, os.path.basename(file)) xml_writer.save(targetFile=out_xml_file) _train_fnames.append((out_img_path, out_mask_path)) if show_img: disp_mask_img = mask_img if save_raw_mask: disp_mask_img[disp_mask_img > 0] = 255 blended_img = np.asarray( Image.blend(Image.fromarray(img), Image.fromarray(disp_mask_img), 0.5)) disp_img = np.concatenate( (disp_img, disp_mask_img, blended_img), axis=1) elif save_test: out_img_path = os.path.join(test_img_root_path, filename) if out_img_path in _test_fnames: raise IOError( 'Duplicate out_img_path: {}'.format(out_img_path)) _test_fnames.append(out_img_path) if show_img and disp_img is not None: cv2.imshow(win_name, disp_img) k = cv2.waitKey(1 - _pause) if k == ord('q'): _exit = 1 break elif k == 27: break elif k == 32: _pause = 1 - _pause if _exit: break sys.stdout.write( '\rDone {:d}/{:d} files {:s} ({:d} masks found)'.format( file_id + 1, n_files, filename, n_masks)) sys.stdout.flush() if masks_per_seq > 0 and n_masks >= masks_per_seq: break sys.stdout.write('\n') sys.stdout.flush() if not _exit_seq and save_train and n_masks == 0: raise IOError('\nNo masks found for {}\n'.format(seq_path)) train_csv_path = test_csv_path = '' if csv_raw: print('Saved {} labeled files in training sequence'.format( len(csv_raw))) train_csv_path = os.path.join(out_img_root_path, 'annotations.csv') pd.DataFrame(csv_raw).to_csv(train_csv_path) if test_csv_raw: print('Saved {} unlabeled files in test sequence'.format( len(test_csv_raw))) test_csv_path = os.path.join(test_img_root_path, 'annotations.csv') pd.DataFrame(test_csv_raw).to_csv(test_csv_path) if show_img: cv2.destroyWindow(win_name) if save_train and train_fnames is not None: train_fnames[ out_seq_name] = _train_fnames, train_root_path, csv_raw, train_csv_path if save_test and test_fnames is not None: test_fnames[ out_test_seq_name] = _test_fnames, test_img_root_path, test_csv_raw, test_csv_path return n_masks