def args_for_test(self): image_area = ImageArea(None, None, generate_block(1, 1)) image_area_h = ImageArea(generate_line(1), None, generate_block(1, 1)) image_area_p = ImageArea(None, generate_block(1, 1), generate_block(1, 1)) image_area_hp = ImageArea(generate_line(2), generate_block(1, 1), generate_block(1, 1)) tail = Tail(None, None) return [ VicarFile(gen_labels(RECSIZE=1, LBLSIZE=1), image_area, None, tail), VicarFile(gen_labels(RECSIZE=1, LBLSIZE=1, EOL=0), image_area, None, tail), VicarFile(gen_labels(RECSIZE=1, LBLSIZE=1, EOL=1), image_area, gen_eol_labels(1, LBLSIZE=1), tail), VicarFile(gen_labels(RECSIZE=2, LBLSIZE=2, NBB=1), image_area_p, None, tail), VicarFile(gen_labels(RECSIZE=2, LBLSIZE=2, EOL=0, NBB=1), image_area_p, None, tail), VicarFile(gen_labels(RECSIZE=2, LBLSIZE=2, EOL=1, NBB=1), image_area_p, gen_eol_labels(2, LBLSIZE=2), tail), VicarFile(gen_labels(RECSIZE=1, LBLSIZE=1, NLB=1), image_area_h, None, tail), VicarFile(gen_labels(RECSIZE=1, LBLSIZE=1, EOL=0, NLB=1), image_area_h, None, tail), VicarFile(gen_labels(RECSIZE=1, LBLSIZE=1, EOL=1, NLB=1), image_area_h, gen_eol_labels(1, LBLSIZE=1), tail), VicarFile(gen_labels(RECSIZE=2, LBLSIZE=2, NBB=1, NLB=1), image_area_hp, None, tail), VicarFile(gen_labels(RECSIZE=2, LBLSIZE=2, EOL=0, NBB=1, NLB=1), image_area_hp, None, tail), VicarFile(gen_labels(RECSIZE=2, LBLSIZE=2, EOL=1, NBB=1, NLB=1), image_area_hp, gen_eol_labels(2, LBLSIZE=2), tail), ]
class ImagePreview (): """ @summary: Class that manage flip dialog. """ def __init__(self, showFrame=True): """ @summary: Create image preview control. @param showFrame: True if it wants show frame """ self.__fPreview__ = None self.__im__ = ImageArea(enlarge=True) self.__im__.set_from_stock(gtk.STOCK_MISSING_IMAGE) self.__lPreview__ = None if (showFrame): self.__fPreview__ = gtk.Frame() self.__lPreview__ = gtk.Label() self.__lPreview__.set_use_markup(True) self.__lPreview__.set_text(_("Preview")) self.__fPreview__.set_label_widget(self.__lPreview__) aPreview = gtk.Alignment(xalign=0.5, yalign=0.5, xscale=1.0, yscale=1.0) aPreview.set_padding(0, 0, 0, 0) aPreview.add(self.__im__) self.__fPreview__.add(aPreview) def getTitleFrame(self): """ @summary: Gets title of frame if frame exists. @return: str within title. """ if (self.__lPreview__ != None): return self.__lPreview__.get_text() else: return "" def getControl(self): """ @summary: Gets control to show. @return: If it is gonna show frame, it gets gtk.Frame control. Else it gets gtk.Image control. """ if (self.__fPreview__ != None): return self.__fPreview__ else: return self.__im__ def getImageControl(self): """ @summary: Gets control that shows image. @return: ImageArea control. """ return self.__im__
def back_migrate_image_area(old_binary_header_len, pds4_tail, pds4_image_area): # type: (int, Tail, ImageArea) -> ImageArea """ Move any binary prefixes from the tail back into the image area. Remove any added padding from the binary header. """ if pds4_image_area.binary_header is None: pds3_binary_header = None else: pds3_binary_header = \ pds4_image_area.binary_header[:old_binary_header_len] return ImageArea(pds3_binary_header, pds4_tail.binary_prefixes_at_tail, pds4_image_area.binary_image_lines)
def migrate_image_area(new_recsize, pds3_image_area): # type: (int, ImageArea) -> ImageArea """ Drop the binary labels. """ if pds3_image_area.binary_header is None: pds4_binary_header = None else: pds3_len = len(pds3_image_area.binary_header) pds4_len = round_to_multiple_of(pds3_len, new_recsize) excess = pds4_len - pds3_len padding = excess * '\0' pds4_binary_header = pds3_image_area.binary_header + padding return ImageArea(pds4_binary_header, None, pds3_image_area.binary_image_lines)
def __init__(self, showFrame=True): """ @summary: Create image preview control. @param showFrame: True if it wants show frame """ self.__fPreview__ = None self.__im__ = ImageArea(enlarge=True) self.__im__.set_from_stock(gtk.STOCK_MISSING_IMAGE) self.__lPreview__ = None if (showFrame): self.__fPreview__ = gtk.Frame() self.__lPreview__ = gtk.Label() self.__lPreview__.set_use_markup(True) self.__lPreview__.set_text(_("Preview")) self.__fPreview__.set_label_widget(self.__lPreview__) aPreview = gtk.Alignment(xalign=0.5, yalign=0.5, xscale=1.0, yscale=1.0) aPreview.set_padding(0, 0, 0, 0) aPreview.add(self.__im__) self.__fPreview__.add(aPreview)
def __init__(self, parent=None): self.image_dir = '' self.image_map_list = [] self.image_map = [] self.image_index = 0 self.image_name = '' self.classes_list = CLASSES self.roi_list_one = [] # 保存一张图片的roi self.roi_list_many = [] # 保存多张图片的roi self.low = 0 self.high = 1024 * 1024 super(Pic_Menu, self).__init__(parent) self.mainLayout = QtWidgets.QVBoxLayout() self.containerLayout = QtWidgets.QVBoxLayout() # 垂直图片展示布局管理器 self.filtrate_area = QtWidgets.QHBoxLayout() # 上部类别布局管理器 self.image_area_layout = QtWidgets.QHBoxLayout() # 中间图片展示布局管理包括文件列表和展示 self.photo_change_area = QtWidgets.QHBoxLayout() # 底部图片切换布局管理 self.mainSpliter = QtWidgets.QSplitter(Qt.Horizontal) self.dirModel = QtWidgets.QDirModel(self) self.dirModel.setFilter(QDir.Dirs | QDir.NoDotAndDotDot) # 文件夹列表view self.dirTreeView = QtWidgets.QTreeView() # 绑定model self.dirTreeView.setModel(self.dirModel) # self.dirTreeView.setRootIndex(self.dirModel.index(dir)) # 设置文件夹路径 # self.dirTreeView.expand(self.dirModel.index(dir)) #展开当前文件夹 # self.dirTreeView.scrollTo(self.dirModel.index(dir)) #视图滚动 self.dirTreeView.hideColumn(1) # 控制显示列数据 self.dirTreeView.hideColumn(2) self.dirTreeView.hideColumn(3) # DirTree事件响应 self.dirTreeView.selectionModel().selectionChanged.connect( self.dirTreeClicked) self.mainSpliter.addWidget(self.dirTreeView) self.imageContainer = QtWidgets.QFrame(self.mainSpliter) self.imageContainer.setMinimumWidth(int(self.geometry().width() * 0.6)) # 类别筛选框 classLabel = QtWidgets.QLabel("类别选择") classLabel.setFixedWidth(60) self.filtrate_area.addWidget(classLabel) self.check_box = ComboCheckBox(CLASSES) self.check_box.setFixedWidth(350) self.filtrate_area.addWidget(self.check_box) # bounding-box筛选框 self.box_input = BoxInput() self.box_input.setFixedWidth(200) self.filtrate_area.addWidget(self.box_input) self.confirm_button = QtWidgets.QPushButton('确定') self.confirm_button.setFixedWidth(150) self.confirm_button.clicked.connect(self.confim_choice) self.filtrate_area.addWidget(self.confirm_button) self.containerLayout.addLayout(self.filtrate_area) # 图片显示区域 self.image_list = ImageList() self.image_list.image_name.connect(self.get_name) # self.image_list.connect(self, QtCore.SIGNAL("transfer_child"), self.w2.receive) self.image_area_layout.addWidget(self.image_list) # self.item_area = QtWidgets.QWidget() # self.item_area.setFixedSize(800, 800) # # self.image_area = QtWidgets.QLabel(self.item_area) # self.image_area.setFixedSize(800, 800) # self.image_area.setStyleSheet("background-color: rgb(255, 255, 255); border:1px solid black") # # self.image_area_layout.addWidget(self.item_area) self.image_area = ImageArea() self.image_area_layout.addWidget(self.image_area) self.containerLayout.addLayout(self.image_area_layout) # 图片切换按钮 self.pre_photo_button = QtWidgets.QPushButton('上一张') self.next_photo_button = QtWidgets.QPushButton('下一张') self.pre_photo_button.setFixedWidth(150) self.next_photo_button.setFixedWidth(150) self.pre_photo_button.clicked.connect(self.pre_photo) self.next_photo_button.clicked.connect(self.next_photo) self.photo_change_area.addWidget(self.pre_photo_button) self.photo_change_area.addWidget(self.next_photo_button) self.containerLayout.addLayout(self.photo_change_area) self.imageContainer.setLayout(self.containerLayout) self.mainSpliter.addWidget(self.imageContainer) self.mainLayout.addWidget(self.mainSpliter)
class Pic_Menu(QtWidgets.QWidget): def __init__(self, parent=None): self.image_dir = '' self.image_map_list = [] self.image_map = [] self.image_index = 0 self.image_name = '' self.classes_list = CLASSES self.roi_list_one = [] # 保存一张图片的roi self.roi_list_many = [] # 保存多张图片的roi self.low = 0 self.high = 1024 * 1024 super(Pic_Menu, self).__init__(parent) self.mainLayout = QtWidgets.QVBoxLayout() self.containerLayout = QtWidgets.QVBoxLayout() # 垂直图片展示布局管理器 self.filtrate_area = QtWidgets.QHBoxLayout() # 上部类别布局管理器 self.image_area_layout = QtWidgets.QHBoxLayout() # 中间图片展示布局管理包括文件列表和展示 self.photo_change_area = QtWidgets.QHBoxLayout() # 底部图片切换布局管理 self.mainSpliter = QtWidgets.QSplitter(Qt.Horizontal) self.dirModel = QtWidgets.QDirModel(self) self.dirModel.setFilter(QDir.Dirs | QDir.NoDotAndDotDot) # 文件夹列表view self.dirTreeView = QtWidgets.QTreeView() # 绑定model self.dirTreeView.setModel(self.dirModel) # self.dirTreeView.setRootIndex(self.dirModel.index(dir)) # 设置文件夹路径 # self.dirTreeView.expand(self.dirModel.index(dir)) #展开当前文件夹 # self.dirTreeView.scrollTo(self.dirModel.index(dir)) #视图滚动 self.dirTreeView.hideColumn(1) # 控制显示列数据 self.dirTreeView.hideColumn(2) self.dirTreeView.hideColumn(3) # DirTree事件响应 self.dirTreeView.selectionModel().selectionChanged.connect( self.dirTreeClicked) self.mainSpliter.addWidget(self.dirTreeView) self.imageContainer = QtWidgets.QFrame(self.mainSpliter) self.imageContainer.setMinimumWidth(int(self.geometry().width() * 0.6)) # 类别筛选框 classLabel = QtWidgets.QLabel("类别选择") classLabel.setFixedWidth(60) self.filtrate_area.addWidget(classLabel) self.check_box = ComboCheckBox(CLASSES) self.check_box.setFixedWidth(350) self.filtrate_area.addWidget(self.check_box) # bounding-box筛选框 self.box_input = BoxInput() self.box_input.setFixedWidth(200) self.filtrate_area.addWidget(self.box_input) self.confirm_button = QtWidgets.QPushButton('确定') self.confirm_button.setFixedWidth(150) self.confirm_button.clicked.connect(self.confim_choice) self.filtrate_area.addWidget(self.confirm_button) self.containerLayout.addLayout(self.filtrate_area) # 图片显示区域 self.image_list = ImageList() self.image_list.image_name.connect(self.get_name) # self.image_list.connect(self, QtCore.SIGNAL("transfer_child"), self.w2.receive) self.image_area_layout.addWidget(self.image_list) # self.item_area = QtWidgets.QWidget() # self.item_area.setFixedSize(800, 800) # # self.image_area = QtWidgets.QLabel(self.item_area) # self.image_area.setFixedSize(800, 800) # self.image_area.setStyleSheet("background-color: rgb(255, 255, 255); border:1px solid black") # # self.image_area_layout.addWidget(self.item_area) self.image_area = ImageArea() self.image_area_layout.addWidget(self.image_area) self.containerLayout.addLayout(self.image_area_layout) # 图片切换按钮 self.pre_photo_button = QtWidgets.QPushButton('上一张') self.next_photo_button = QtWidgets.QPushButton('下一张') self.pre_photo_button.setFixedWidth(150) self.next_photo_button.setFixedWidth(150) self.pre_photo_button.clicked.connect(self.pre_photo) self.next_photo_button.clicked.connect(self.next_photo) self.photo_change_area.addWidget(self.pre_photo_button) self.photo_change_area.addWidget(self.next_photo_button) self.containerLayout.addLayout(self.photo_change_area) self.imageContainer.setLayout(self.containerLayout) self.mainSpliter.addWidget(self.imageContainer) self.mainLayout.addWidget(self.mainSpliter) # self.setLayout(self.mainLayout) def openfile(self): dir = QtWidgets.QFileDialog.getExistingDirectory(self, "选取数据文件夹", "./") print(dir) if dir: self.show_files(dir) def show_files(self, dir): self.dirTreeView.setRootIndex(self.dirModel.index(dir)) #设置文件夹路径 # self.mainSpliter.replaceWidget(0,self.dirTreeView) # self.dirTreeView.selectAll() # self.dirTreeClicked() def get_name(self, name): self.image_name = name self.image_index = self.image_map.index(name) file_path = os.path.join(self.image_dir, name) show_image = self.get_image(file_path) self.show_image(show_image) def get_image(self, file_path): image = cv2.imdecode(fromfile(file_path, dtype=uint8), -1) image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) self.image_area.file_path = file_path image_path = file_path xml_path = image_path.replace('JPEGImages', 'Annotations') xml_path = xml_path.replace('jpg', 'xml') in_file = open(xml_path) tree = ET.parse(in_file) root = tree.getroot() # 记录展示数据 self.image_area.classes = [] self.image_area.width_list = [] self.image_area.height_list = [] for obj in root.iter('object'): cls = obj.find('name').text self.image_area.classes.append(cls) xmlbox = obj.find('bndbox') b = (int(xmlbox.find('xmin').text), int(xmlbox.find('xmax').text), int(xmlbox.find('ymin').text), int(xmlbox.find('ymax').text)) width = b[1] - b[0] height = b[3] - b[2] self.image_area.width_list.append(width) self.image_area.height_list.append(height) # if (cls in self.classes_list) and (self.high >= width * height >= self.low): if cls in self.classes_list: cv2.rectangle(image, (b[0], b[2]), (b[1], b[3]), COLORS[cls], 2) cv2.putText(image, cls, (b[0], b[2] - 3), cv2.FONT_HERSHEY_COMPLEX, 0.8, COLORS[cls], 2) roi = ROI(type=cls, width=width, height=height, xmin=b[0], ymin=b[2], location=file_path) self.roi_list_one.append(roi) # 产生html # class_pie = Classes_Pie() # class_pie.bin(self.image_area.classes).render("./html/class_pie.html")#类别饼图 # class_bar = Classes_Bar() # class_bar.bar(self.image_area.classes).render("./html/class_bar.html")#类别柱状图 class_bar_line = Classes_Bar_Line() class_bar_line.make_bar_line( self.image_area.classes).render("./html/class_bar_line.html") class_bar_pie = Classes_Bar_Pie() class_bar_pie.make_bar_pie( self.image_area.classes).render("./html/class_bar_pie.html") wh_area_compose = WH_AREA_Compose() wh_area_compose.make_compose( self.image_area.width_list, self.image_area.height_list).render("./html/wh_area_compose.html") size = (int(self.image_area.width()), int(self.image_area.height())) # shrink = cv2.resize(image, size, interpolation=cv2.INTER_AREA) shrink = image print(shrink.shape) show_image = QtGui.QImage(shrink.data, shrink.shape[1], shrink.shape[0], shrink.shape[1] * 3, QtGui.QImage.Format_RGB888) return show_image def show_image(self, image): self.image_area.load_image(image) #这个是最左边文文件夹改变时的变化 def search_file(self, dirpath, folder): for image_path in os.listdir(dirpath): if os.path.isdir(os.path.join(dirpath, image_path)): if folder == '': next_folder = image_path else: next_folder = folder + '/' + image_path self.search_file(os.path.join(dirpath, image_path), next_folder) else: item = image_path if item.split('.')[-1] in FILE_TYPE: if folder == '': item = image_path else: item = folder + '/' + image_path self.image_map.append(item) def get_rois(self): self.roi_list_many.clear() for image in self.image_map: image_path = os.path.join(self.image_dir, image) # 获取文件名,除掉前缀 file_name = os.path.splitext(image_path)[0] xml_path = file_name + ".xml" in_file = open(xml_path) tree = ET.parse(in_file) root = tree.getroot() # 记录展示数据 for obj in root.iter('object'): cls = obj.find('name').text xmlbox = obj.find('bndbox') b = (int(xmlbox.find('xmin').text), int(xmlbox.find('xmax').text), int(xmlbox.find('ymin').text), int(xmlbox.find('ymax').text)) width = b[1] - b[0] height = b[3] - b[2] roi = ROI(type=cls, width=width, height=height, xmin=b[0], ymin=b[2], location=image) self.roi_list_many.append(roi) def dirTreeClicked(self): print('dirTreeClicked') # 获取选择的路径 pathSelected = self.dirModel.filePath( self.dirTreeView.selectedIndexes()[0]) print('pathSelected ', pathSelected) self.image_dir = pathSelected # 遍历路径下的媒体文件 self.image_map.clear() self.image_index = 0 # 检索多层数据 self.search_file(pathSelected, '') # 显示文件夹第一张图片 if len(self.image_map) > 0: self.image_map_list = self.image_map # print(self.image_dir, self.image_map) self.image_list.set_items(self.image_map) self.image_name = self.image_map[0] self.image_list.set_highlight(self.image_index) try: image_name = self.image_map[0] file_path = os.path.join(self.image_dir, image_name) print("image_dir:", self.image_dir) print("image_name:", image_name) print("filepath:", file_path) show_image = self.get_image(file_path) self.show_image(show_image) except Exception as e: print(u'出错了', e) else: self.image_list.set_items(['空']) self.image_area.setStyleSheet( "background-color: rgb(255,255, 255); border:1px solid black") # 统计所有ROI self.get_rois() def pre_photo(self): self.image_index -= 1 if self.image_index < 0: self.image_index += 1 self.image_name = self.image_map[self.image_index] self.image_list.set_highlight(self.image_index) image_name = self.image_map[self.image_index] file_path = os.path.join(self.image_dir, image_name) show_image = self.get_image(file_path) self.show_image(show_image) def next_photo(self): self.image_index += 1 if self.image_index == len(self.image_map): self.image_index -= 1 self.image_name = self.image_map[self.image_index] self.image_list.set_highlight(self.image_index) image_name = self.image_map[self.image_index] file_path = os.path.join(self.image_dir, image_name) show_image = self.get_image(file_path) self.show_image(show_image) def confim_choice(self): classes_list = self.check_box.Selectlist() low, high = self.box_input.get_num() if classes_list: self.classes_list = classes_list if low and high: self.low = int(low) self.high = int(high) image_map_temp = [] for image_name in self.image_map_list: image_path = os.path.join(self.image_dir, image_name) xml_path = image_path.replace('JPEGImages', 'Annotations') xml_path = xml_path.replace('jpg', 'xml') in_file = open(xml_path) tree = ET.parse(in_file) root = tree.getroot() for obj in root.iter('object'): cls = obj.find('name').text xmlbox = obj.find('bndbox') b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text)) width = b[1] - b[0] height = b[3] - b[2] if (cls in self.classes_list) and ( self.high > width > self.low or self.high > height > self.low): image_map_temp.append(image_name) break self.image_map = image_map_temp if self.image_map: try: self.image_index = self.image_map.index(self.image_name) except Exception as e: if len(self.image_map) > 0: self.image_index = 0 else: self.image_index = -1 if not self.image_index == -1: self.image_list.set_items(self.image_map) self.image_list.set_highlight(self.image_index) image_name = self.image_map[self.image_index] file_path = os.path.join(self.image_dir, image_name) show_image = self.get_image(file_path) self.show_image(show_image) else: self.image_list.set_items(['空']) self.image_area.setStyleSheet( "background-color: rgb(255,255, 255); border:1px solid black")
def test__init__(self): image_area = ImageArea(None, None, generate_block(1, 1)) image_area_h = ImageArea(generate_line(1), None, generate_block(1, 1)) image_area_p = ImageArea(None, generate_block(1, 1), generate_block(1, 1)) image_area_hp = ImageArea(generate_line(2), generate_block(1, 1), generate_block(1, 1)) tail = Tail(None, None) # Verify that bad inputs raise an exception. # missing sections: with self.assertRaises(Exception): VicarFile(None, image_area, gen_labels({}), tail) with self.assertRaises(Exception): VicarFile(gen_labels(1, RECSIZE=1, LBLSIZE=1, EOL=1), None, gen_labels(1, ), tail) with self.assertRaises(Exception): VicarFile(gen_labels(1, RECSIZE=1, LBLSIZE=1, EOL=1), image_area, gen_labels(1), None) # inconsistent keywords: with self.assertRaises(Exception): # missing RECSIZE VicarFile(gen_labels(1), image_area, gen_labels(1), tail) with self.assertRaises(Exception): # missing EOL VicarFile(gen_labels(1, RECSIZE=1, LBLSIZE=1), image_area, gen_labels(1), tail) with self.assertRaises(Exception): # zero EOL VicarFile(gen_labels(RECSIZE=1, LBLSIZE=1, EOL=0), image_area, gen_labels(1), tail) with self.assertRaises(Exception): # nonzero NBB but no binary prefixes VicarFile(gen_labels(1, RECSIZE=1, LBLSIZE=1, NBB=1), image_area, None, tail) with self.assertRaises(Exception): # zero NBB but with binary prefixes VicarFile(gen_labels(2, RECSIZE=2, LBLSIZE=2, NBB=0), image_area_p, None, tail) with self.assertRaises(Exception): # nonzero NLB but no binary header VicarFile(gen_labels(1, RECSIZE=1, LBLSIZE=1, NLB=1), image_area, None, tail) with self.assertRaises(Exception): # zero NLB but with binary header VicarFile(gen_labels(1, RECSIZE=1, LBLSIZE=1, NLB=0), image_area_h, None, tail) with self.assertRaises(Exception): # RECSIZE of 1 but ImageArea says 2 VicarFile(gen_labels(1, RECSIZE=1, LBLSIZE=1, NLB=0), image_area_p, None, tail) # two binary_prefixes with self.assertRaises(Exception): VicarFile(gen_labels(2, RECSIZE=2, LBLSIZE=2, NBB=1), image_area_p, None, Tail(generate_block(2, 2), None)) # TODO Create a case with both binary prefixes and a # MigrationTask. # verify that these do not raise VicarFile(gen_labels(RECSIZE=1, LBLSIZE=1), image_area, None, tail) VicarFile(gen_labels(RECSIZE=2, LBLSIZE=2, NBB=1), image_area_p, None, tail) VicarFile(gen_labels(RECSIZE=1, LBLSIZE=1, NLB=1), image_area_h, None, tail) VicarFile(gen_labels(RECSIZE=2, LBLSIZE=2, NBB=1, NLB=1), image_area_hp, None, tail)