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 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)