class ImageSubscriber(): def __init__(self): self.cvbridge = CvBridge() self.img_buf = None self.image_subs = rospy.Subscriber("image", Image, callback=self._callback, queue_size=1) self.image_viewer = ImageViewer() self.test = rospy.get_param("/stop_param") def wait_for_image(self): if self.test is True: self.img_buf = self.image_viewer.get_test_camera() if self.img_buf is None: return None, None key = self.image_viewer.show(self.img_buf) img = self.img_buf img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.resize(img, dsize=(HEIGHT, WIDTH)) img = (img.astype(np.float32) - 128) / 256 self.img_buf = None return img, key def _callback(self, msg): cv_img = self.cvbridge.imgmsg_to_cv2(msg) self.img_buf = cv_img
def __init__(self): self.cvbridge = CvBridge() self.img_buf = None self.image_subs = rospy.Subscriber("image", Image, callback=self._callback, queue_size=1) self.image_viewer = ImageViewer() self.test = rospy.get_param("/stop_param")
def __init__(self, config, car_list): super().__init__() self._config = config self.main_layout = QHBoxLayout() self.car_list = [] self.car_list_items = [] self.car_list_ui = QListWidget() self.update_all_btn = QPushButton("Update All") self.remove_btn = QPushButton("Remove Car") self.car_add_ui = CarAddUI(self) self.center_layout = QVBoxLayout() self.image_viewer_widget = QLabel() self.image_viewer = ImageViewer(self.image_viewer_widget) self.image_list_ui = QListWidget() # self.car_image_widget = QWidget() self.current_car = QLabel() # self.image_preview_new = QGraphicsView() # self.image_preview = QLabel() self.car_info_ui = CarInfoUI(self) # maybe temporary self._current_image = 0 self.InitUI(car_list)
def cell_double_clicked(self, row, column): filepath = self.__detailWidget.item(row,column).text() if column == 2: timestamp = self.__detailWidget.item(row,column-1).text() # print(f"you have clicked on {filepath}") self.viewer = ImageViewer(filepath,timestamp) self.viewer.view()
def __init__(self): self.running = True self.running = True self.server_host = '127.0.1.1' self.my_socket = None self.image_viewer = ImageViewer(interval=INTERVAL) self.shape = None
def click_view_raw(self): """ view the image stream """ img = getattr(skimage.data, 'rocket')() self.viewer_raw = ImageViewer(img=img, resize=(600, 600), xy=(100, 600)) self.vraw = True self.threadpool.add_to_queue(function=self.raw_queue_poll, delay=0.1, stop=self.buttons['stop'])
def __init__(self, seq_info, update_ms): image_shape = seq_info["image_size"][::-1] aspect_ratio = float(image_shape[1]) / image_shape[0] image_shape = 1024, int(aspect_ratio * 1024) if update_ms is None: update_ms = seq_info["update_ms"] self.viewer = ImageViewer(update_ms, image_shape, "Figure %s" % seq_info["sequence_name"]) self.viewer.thickness = 2 self.frame_idx = seq_info["min_frame_idx"] self.last_idx = seq_info["max_frame_idx"] self.results = seq_info["results"]
class ThumbnailDisplay(QtWidgets.QWidget): tx_showViewer = pyqtSignal() def __init__(self): super().__init__() self.__layout = QtWidgets.QVBoxLayout() self.__display_area = QtWidgets.QListWidget() self.setup_display_area() self.setup_ThumbnailDisplay() # connect thumbnail display to displaying viewer self.__display_area.itemDoubleClicked.connect(self.rx_thumbnail_double_clicked) def setup_display_area(self): self.__display_area.alternatingRowColors = True self.__display_area.setMaximumWidth(500) self.__display_area.layoutDirection = QtCore.Qt.LayoutDirection.LeftToRight self.__display_area.setGridSize(QtCore.QSize(100,100)) self.__display_area.setViewMode(QtWidgets.QListView.ViewMode.IconMode) self.__display_area.setIconSize(QtCore.QSize(85,40)) self.__display_area.setTextElideMode(QtCore.Qt.TextElideMode.ElideMiddle) self.__display_area.setAutoScroll(True) def setup_ThumbnailDisplay(self): self.__layout.addWidget(self.__display_area) self.setLayout(self.__layout) self.setStyleSheet("QWidget" "{" "border : 2px solid black;" "background : #b67162;" "}") def rx_put_thumbnail(self, image, timestamp, image_path): # print(f"putting thumbnail of {image_path}") img = QtGui.QIcon(image) thumbnail = QtWidgets.QListWidgetItem(img, timestamp) thumbnail_font = QtGui.QFont() thumbnail_font.setPointSizeF(5.5) thumbnail.setFont(thumbnail_font) thumbnail.setWhatsThis(image_path) thumbnail.setTextAlignment(64) self.__display_area.addItem(thumbnail) def rx_thumbnail_double_clicked(self, item): image_path = item.whatsThis() image_timestamp = item.text() self.viewer = ImageViewer(image_path, image_timestamp) self.viewer.view()
def main(): args = parse_args() pprint.pprint(vars(args)) runner = LatticeRunner(args) viewer = ImageViewer(width=args.size, height=args.size, updateRate=args.updateRate, runner=runner) print("Hit ESC to abort") runner.start() viewer.start() runner.stop() runner.join()
def debug_history(self): iv = ImageViewer() iv.remove_axis_values() iv.add(self.source, 'source', cmap='bgr') for img, func in zip(self.debug_out_list, self.functions): iv.add(img, str(func), cmap='bgr') return iv
def increment_page(obj: ImageViewer, change: int) -> None: old_page_number = obj.current_page obj.current_page += change current_page = obj.current_page if current_page is not old_page_number or change == 0: print("Updating Pages") obj.current_image = obj.image_list[current_page] obj.current_image_copy = obj.current_image resize_image(obj.image_display) obj.update() else: print("Page has not changed")
def __init__(self): super().__init__() self.resize(1024, 720) self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) # おまじない self.main_widget = QWidget(self) self.main_layout = QGridLayout(self.main_widget) self.setCentralWidget(self.main_widget) # 子供UI # window setup self.view_origin = ImageViewer() #self.view_origin.setFixedSize(512, 512) self.view_salient = ImageViewer() self.view3 = ImageViewer() self.view4 = ImageViewer() #self.view_salient.setFixedSize(512, 512) label1 = QLabel("Original") label1.setAlignment(Qt.AlignCenter) label1.setFont(QFont("Helvetica", 18, 2)) label2 = QLabel("Saliency") label2.setAlignment(Qt.AlignCenter) label2.setFont(QFont("Helvetica", 18, 2)) # レイアウトをセット self.main_layout.addWidget(label1, 0, 0) self.main_layout.addWidget(label2, 0, 1) self.main_layout.addWidget(self.view_origin, 1, 0) self.main_layout.addWidget(self.view_salient, 1, 1) self.main_layout.addWidget(self.view3, 2, 0) self.main_layout.addWidget(self.view4, 2, 1) #self.main_layout.addWidget(, 2, 0, 2,2 ) #self.main_layout.addWidget(label4, 1, 1) # イベント self.fileLoaded.connect(self.draw_origin_image)
class Visualization(object): """ This class shows tracking output in an OpenCV image viewer. """ def __init__(self, seq_info, update_ms): image_shape = seq_info["image_size"][::-1] aspect_ratio = float(image_shape[1]) / image_shape[0] image_shape = 1024, int(aspect_ratio * 1024) if update_ms is None: update_ms = seq_info["update_ms"] self.viewer = ImageViewer(update_ms, image_shape, "Figure %s" % seq_info["sequence_name"]) self.viewer.thickness = 2 self.frame_idx = seq_info["min_frame_idx"] self.last_idx = seq_info["max_frame_idx"] self.results = seq_info["results"] # self.detections = detections # self.iou = iou def run(self, frame_callback): self.viewer.run(lambda: self._update_fun(frame_callback)) def _update_fun(self, frame_callback): if self.frame_idx > self.last_idx: return False # Terminate frame_callback(self, self.frame_idx, self.results) self.frame_idx += 1 return True def set_image(self, image): self.viewer.image = image def draw_groundtruth(self, track_ids, boxes): self.viewer.thickness = 2 for track_id, box in zip(track_ids, boxes): self.viewer.color = create_unique_color_uchar(track_id) self.viewer.rectangle(*box.astype(np.int), label=str(track_id)) # def draw_detections(self, detections): # self.viewer.thickness = 2 # self.viewer.color = 0, 0, 255 # for i, detection in enumerate(detections): # self.viewer.rectangle(*detection.tlwh) def draw_trackers(self, tracks): self.viewer.thickness = 5 for track in tracks: self.viewer.color = create_unique_color_uchar(track.track_id) self.viewer.rectangle(*track.track_bbox.astype(np.int), label=str(track.track_id))
class DetailDisplay(QtWidgets.QWidget): def __init__(self): super().__init__() # create widget layout self.__layout = QtWidgets.QVBoxLayout() # create table widget self.__detailWidget = QtWidgets.QTableWidget() self.setup_detailWidget() self.__detailWidget.showGrid = True self.setup_DetailDisplay() def setup_detailWidget(self): self.__detailWidget.showGrid = True # show table grids # create columns column_names = ["Source", "Time", "Image Location"] for col_number in range(0, len(column_names)): self.__detailWidget.insertColumn(col_number) self.__detailWidget.setHorizontalHeaderLabels(column_names) # set size adjustment policy self.__detailWidget.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents) self.__detailWidget.resizeColumnsToContents() self.__detailWidget.horizontalHeader().setStretchLastSection(True) # set alternating row colors self.__detailWidget.setAlternatingRowColors(True) self.__detailWidget.setStyleSheet("QTableWidget { alternate-background-color: #e2bcb7;background-color: #b67162; border : 2px solid black; }") # set widget max size self.__detailWidget.setMaximumWidth(500) # connect detail display cells to cellClicked event self.__detailWidget.cellDoubleClicked.connect(self.cell_double_clicked) def setup_DetailDisplay(self): self.__layout.addWidget(self.__detailWidget) self.setLayout(self.__layout) self.setStyleSheet("QWidget" "{" "border : 2px solid black;" "background : #b67162;" "}") # write details when new alert is triggered @pyqtSlot(str,str,str) def rx_write_details(self, filepath, timestamp, stream_num): rowPosition = self.__detailWidget.rowCount() # print(rowPosition) self.__detailWidget.insertRow(rowPosition) self.__detailWidget.setItem(rowPosition,2,QtWidgets.QTableWidgetItem(filepath)) self.__detailWidget.setItem(rowPosition,1,QtWidgets.QTableWidgetItem(timestamp)) self.__detailWidget.setItem(rowPosition,0,QtWidgets.QTableWidgetItem(stream_num)) self.__detailWidget.resizeColumnToContents(1) def cell_double_clicked(self, row, column): filepath = self.__detailWidget.item(row,column).text() if column == 2: timestamp = self.__detailWidget.item(row,column-1).text() # print(f"you have clicked on {filepath}") self.viewer = ImageViewer(filepath,timestamp) self.viewer.view()
def placeholder(self): img_viewer = ImageViewer(self) img_viewer.mainloop()
class YourMainWindow(BasedMainWindow): """ BaseMainWindwの使いかた その2 ウィジェットのレイアウトの方法 """ MAINLOOP_FPS = 30 def __init__(self): super().__init__() self.resize(1024, 720) self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) # おまじない self.main_widget = QWidget(self) self.main_layout = QGridLayout(self.main_widget) self.setCentralWidget(self.main_widget) # 子供UI # window setup self.view_origin = ImageViewer() #self.view_origin.setFixedSize(512, 512) self.view_salient = ImageViewer() self.view3 = ImageViewer() self.view4 = ImageViewer() #self.view_salient.setFixedSize(512, 512) label1 = QLabel("Original") label1.setAlignment(Qt.AlignCenter) label1.setFont(QFont("Helvetica", 18, 2)) label2 = QLabel("Saliency") label2.setAlignment(Qt.AlignCenter) label2.setFont(QFont("Helvetica", 18, 2)) # レイアウトをセット self.main_layout.addWidget(label1, 0, 0) self.main_layout.addWidget(label2, 0, 1) self.main_layout.addWidget(self.view_origin, 1, 0) self.main_layout.addWidget(self.view_salient, 1, 1) self.main_layout.addWidget(self.view3, 2, 0) self.main_layout.addWidget(self.view4, 2, 1) #self.main_layout.addWidget(, 2, 0, 2,2 ) #self.main_layout.addWidget(label4, 1, 1) # イベント self.fileLoaded.connect(self.draw_origin_image) @Slot() def draw_origin_image(self): self.putStatusbar("draw_origin_image") self.origin_image_file = img_file = self.loaded_filepath self.putStatusbar(img_file) self.view_origin.setFile(img_file) # self.view_salient.setFile(img_file) # ======================================================================================= self.putStatusbar("saliency calc start ..." + self.origin_image_file) # VIEW1 self.salinecy = Saliency(self.origin_image_file) # VIEW 2 self.view_salient.setFile(self.salinecy.output_file) # VIEW 3 self.view3.setFile(self.salinecy.region_file) self.view4.setFile(self.salinecy.slgray_file) # ======================================================================================= self.putStatusbar("saliency calc end ...")
def rx_thumbnail_double_clicked(self, item): image_path = item.whatsThis() image_timestamp = item.text() self.viewer = ImageViewer(image_path, image_timestamp) self.viewer.view()
def init_ui(self): self.font_h1 = QFont("Arial", 32) self.font_p1 = QFont("Arial", 16) self.grid_top = QGridLayout() self.grid_body_upper = QGridLayout() self.grid_body = QGridLayout() self.grid_body_lower = QGridLayout() self.resize(1600, 1100) # Grid Top self.e_title = QLineEdit() self.e_title.setAlignment(Qt.AlignCenter) self.e_title.setFont(self.font_h1) self.e_title.setReadOnly(True) self.e_title.setText("Setup Sorted Filenames") self.e_title.setFrame(False) self.grid_top.addWidget(self.e_title, 0, 0) self.b_help = QPushButton("HELP") self.b_help.setDefault(True) self.b_help.setEnabled(True) self.grid_top.addWidget(self.b_help, 0, 1) # Grid BODY UPPER self.e_filename = QLineEdit() self.e_filename.setAlignment(Qt.AlignCenter) self.e_filename.setFont(self.font_p1) self.e_filename.setReadOnly(True) self.e_filename.setText("Filename: ") self.grid_body_upper.addWidget(self.e_filename, 0, 2) self.e_section = QLineEdit() self.e_section.setAlignment(Qt.AlignCenter) self.e_section.setFont(self.font_p1) self.e_section.setReadOnly(True) self.e_section.setText("Section: ") self.grid_body_upper.addWidget(self.e_section, 0, 3) # Grid BODY self.viewer = ImageViewer(self) self.grid_body.addWidget(self.viewer, 0, 0) # Grid BODY LOWER self.b_rostral = QPushButton("Rostral Limit:") self.grid_body_lower.addWidget(self.b_rostral, 0, 0) self.e_rostral = QLineEdit() self.e_rostral.setAlignment(Qt.AlignCenter) self.e_rostral.setFont(self.font_p1) self.grid_body_lower.addWidget(self.e_rostral, 0, 1) self.b_caudal = QPushButton("Caudal Limit:") self.grid_body_lower.addWidget(self.b_caudal, 1, 0) self.e_caudal = QLineEdit() self.e_caudal.setAlignment(Qt.AlignCenter) self.e_caudal.setFont(self.font_p1) self.grid_body_lower.addWidget(self.e_caudal, 1, 1) self.b_dorsal = QPushButton("Dorsal Limit:") self.grid_body_lower.addWidget(self.b_dorsal, 0, 2) self.e_dorsal = QLineEdit() self.e_dorsal.setAlignment(Qt.AlignCenter) self.e_dorsal.setFont(self.font_p1) self.grid_body_lower.addWidget(self.e_dorsal, 0, 3) self.b_ventral = QPushButton("Ventral Limit:") self.grid_body_lower.addWidget(self.b_ventral, 1, 2) self.e_ventral = QLineEdit() self.e_ventral.setAlignment(Qt.AlignCenter) self.e_ventral.setFont(self.font_p1) self.grid_body_lower.addWidget(self.e_ventral, 1, 3) self.b_first_slice = QPushButton("Mark as FIRST Slice With Brainstem:") self.grid_body_lower.addWidget(self.b_first_slice, 0, 4) self.e_first_slice = QLineEdit() self.e_first_slice.setAlignment(Qt.AlignCenter) self.e_first_slice.setFont(self.font_p1) self.grid_body_lower.addWidget(self.e_first_slice, 0, 5) self.b_last_slice = QPushButton("Mark as LAST Slice With Brainstem:") self.grid_body_lower.addWidget(self.b_last_slice, 1, 4) self.e_last_slice = QLineEdit() self.e_last_slice.setAlignment(Qt.AlignCenter) self.e_last_slice.setFont(self.font_p1) self.grid_body_lower.addWidget(self.e_last_slice, 1, 5) self.b_done = QPushButton("DONE") self.grid_body_lower.addWidget(self.b_done, 1, 6) # Super grid self.supergrid = QGridLayout() self.supergrid.addLayout(self.grid_top, 0, 0) self.supergrid.addLayout(self.grid_body_upper, 1, 0) self.supergrid.addLayout(self.grid_body, 2, 0) self.supergrid.addLayout(self.grid_body_lower, 3, 0) # Set layout and window title self.setLayout(self.supergrid) self.setWindowTitle("Q")
def render(self, mode="human"): if self.viewer is None: self.viewer = ImageViewer(caption="SnakeEnv", height=500, width=500,) # show the screen on the image viewer self.viewer.show(self.screen)
def initUI(self): self.image_viewer = ImageViewer() self.setCentralWidget(self.image_viewer) self.resize(800, 600)
def doubleClicked(self, vobj): ImageViewer(self.LithophaneImage.image)
time = evaluation['time'] kwargs = evaluation['kwargs'] test_perc = evaluation['test_perc'] all_dices.append(dice_vals) all_filenames = filenames if all_filenames is None else all_filenames pairs = list(zip(dice_vals, filenames)) pairs.sort() dice_vals = [x for x, _ in pairs] filenames = [x for _, x in pairs] plt.plot(dice_vals, label=f'dice {i}') plt.axhline(y=mean, color='rgb'[i % 3], ls='--', label=f'mean {i}') print( f'{FILENAME:<32} mean={mean:.04f} time={time:.02f}s fps={len(filenames)/time:.02f} test_perc={test_perc} kwargs={kwargs}' ) plt.title('\n'.join(show_filenames)) plt.legend() plt.show() if len(all_dices) == 2: worst = [(max(a - b, b - a), f) for a, b, f in zip(*all_dices, filenames)] worst.sort() iv = ImageViewer() for s, f in worst[-12:]: iv.add(cv2.imread(f'data_test/paintings_gt/imgs/{f}'), cmap='bgr', title=f'{s}') iv.show()
class ThreadedImgGui(QMainWindow): def __init__(self, params=None, parent=None, *args, **kwargs): super(ThreadedImgGui, self).__init__(*args, **kwargs) # setup the threadpool self.params = params self.threadpool = QueueThreads() # queues to collect images from stream # the image queue will be polled in a thread and displayed if # an image is available if 'qsize' in self.params: qsize = self.params['qsize'] else: qsize = 50 self.raw_queue = ImgQueue(obj_type='raw img', maxsize=qsize) self.parent = parent self.setWindowTitle('Threaded GUI Example') app.aboutToQuit.connect(self.closeEvent) self.layout = QGridLayout() self.layout_buttons() w = QWidget() w.setLayout(self.layout) self.setCentralWidget(w) self.setGeometry(100, 100, 50, 50) self.show() def layout_buttons(self): top_layout_2 = QHBoxLayout() ctrl_groupbox = QGroupBox('Image Stream Controls') ctrl_groupbox.setAlignment(Qt.AlignCenter) start = QPushButton('start', clicked=self.click_start) view_raw = QPushButton( 'view raw', clicked=self.click_view_raw, ) stop = QPushButton( 'stop', clicked=self.click_stop, ) top_layout_2.addWidget(start) top_layout_2.addWidget(view_raw) top_layout_2.addWidget(stop) ctrl_groupbox.setLayout(top_layout_2) self.layout.addWidget(ctrl_groupbox, 1, 0) self.buttons = { 'start': start, 'view_raw': view_raw, 'stop': stop, } def click_start(self): """ start fake image stream in a thread """ # add_to_queue # function: to be executed in a new thread # tsignal: accesible in the function # slot: passed as string, connected in thread_queue with getattr # kwargs: will be passed to the function # this is a signal defined in the thread_queue # with a custom signature (object, str) if 'delay' in self.params: delay = self.params['delay'] else: delay = 0.5 self.threadpool.add_to_queue(function=fakestream, signal='imgtime', slot=self.add_to_raw_queue, delay=delay, stop=self.buttons['stop']) self.buttons['start'].setEnabled(False) self.buttons['stop'].setEnabled(True) @pyqtSlot(object, str) def add_to_raw_queue(self, image, timestamp): # this is the slot for the add_to_queue call with function=fakestream # the decorator @pyqtSlot(object, str) matches the signal # of the self.raw_queue.check_full() self.threadpool.add_to_queue(function=self.raw_queue.add2queue, img=image, time_stamp=timestamp) def click_view_raw(self): """ view the image stream """ img = getattr(skimage.data, 'rocket')() self.viewer_raw = ImageViewer(img=img, resize=(600, 600), xy=(100, 600)) self.vraw = True self.threadpool.add_to_queue(function=self.raw_queue_poll, delay=0.1, stop=self.buttons['stop']) def raw_queue_poll(self, delay, stop, **kwargs): # run in a thread from click_view_raw # note there was no signal or slot connected # but it is necessary to include the **kwargs # poll the raw image queue while stop.isEnabled() == True: time.sleep(delay) self.update_view_raw() def update_view_raw(self): # not very useful in this example, because FPS is low, but while not self.raw_queue.empty(): img, timestamp = self.raw_queue.get() self.viewer_raw.update_img_label(img) def click_stop(self): print('\nStop the stream') self.buttons['stop'].setEnabled(False) self.buttons['start'].setEnabled(True) def closeEvent(self, event=None): #Your desired functionality here print('\nClose button pressed') self.destroy() if self.parent is None: sys.exit(0)
class Client(object): def __init__(self): self.running = True self.running = True self.server_host = '127.0.1.1' self.my_socket = None self.image_viewer = ImageViewer(interval=INTERVAL) self.shape = None # connect client with the server to start chatting def run(self): # Create Client socket self.my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the Socket to server host and same port number self.my_socket.connect((self.server_host, PORT_NUMBER)) # receive image shape self.shape = [ int(i) for i in self.my_socket.recv(10).decode()[1:-1].split(',') ] print(self.shape) # Receiving Messages from Server on separate thread threading.Thread(target=self.receive).start() # run stream_viewer # stream viewer should run in the main thread. self.image_viewer.next() self.image_viewer.slide_show() # Receive stream from Server def receive(self): size = 3 * self.shape[0] * self.shape[1] temp = None while self.running: try: # receive file file = self.my_socket.recv(size) if len(file) < size: if temp is None: temp = file else: temp += file # print(len(temp)) if len(temp) == size: self.send_mouse() self.image_viewer.add_frame( ImageTk.PhotoImage( Image.frombytes('RGB', (800, 800), temp))) temp = None else: self.image_viewer.add_frame( ImageTk.PhotoImage( Image.frombytes('RGB', (800, 800), file))) self.send_mouse() except socket.error as msg: print('EXCEPTION ' + str(msg)) def send_mouse(self): mouse_control = mouse.position() self.my_socket.send(str(mouse_control).encode()) # Ending stream def kill(self): self.running = False # Close chat socket self.my_socket.close()
from image_viewer import ImageViewer, show_vars from config import config if __name__ == "__main__": # comic = r"C:\Users\adrake.KEMPTOWN\Pictures\onepunchman\onepunchman.cbz" # comic = r"D:\Downloads\gallery.zip" my_gui = ImageViewer(comic) show_vars(config) show_vars(ImageViewer) my_gui.mainloop()
class GUICropBrainStem(QWidget): def __init__(self, stack, parent=None): super(GUICropBrainStem, self).__init__(parent) self.stack = stack self.fileLocationManager = FileLocationManager(self.stack) self.sqlController = SqlController() self.sqlController.get_animal_info(self.stack) self.valid_sections = self.sqlController.get_valid_sections(stack) self.valid_section_keys = sorted(list(self.valid_sections)) self.curr_section_index = 0 self.curr_section = None self.active_selection = '' self.img_width = 2001 self.img_height = 1001 self.rostral = -1 self.caudal = -1 self.ventral = -1 self.dorsal = -1 self.first_slice = -1 self.last_slice = -1 self.init_ui() self.b_rostral.clicked.connect( lambda: self.click_button(self.b_rostral)) self.b_caudal.clicked.connect(lambda: self.click_button(self.b_caudal)) self.b_dorsal.clicked.connect(lambda: self.click_button(self.b_dorsal)) self.b_ventral.clicked.connect( lambda: self.click_button(self.b_ventral)) self.b_first_slice.clicked.connect( lambda: self.click_button(self.b_first_slice)) self.b_last_slice.clicked.connect( lambda: self.click_button(self.b_last_slice)) self.b_done.clicked.connect(lambda: self.click_button(self.b_done)) self.viewer.click.connect(self.click_photo) def init_ui(self): self.font_h1 = QFont("Arial", 32) self.font_p1 = QFont("Arial", 16) self.grid_top = QGridLayout() self.grid_body_upper = QGridLayout() self.grid_body = QGridLayout() self.grid_body_lower = QGridLayout() self.resize(1600, 1100) # Grid Top self.e_title = QLineEdit() self.e_title.setAlignment(Qt.AlignCenter) self.e_title.setFont(self.font_h1) self.e_title.setReadOnly(True) self.e_title.setText("Setup Sorted Filenames") self.e_title.setFrame(False) self.grid_top.addWidget(self.e_title, 0, 0) self.b_help = QPushButton("HELP") self.b_help.setDefault(True) self.b_help.setEnabled(True) self.grid_top.addWidget(self.b_help, 0, 1) # Grid BODY UPPER self.e_filename = QLineEdit() self.e_filename.setAlignment(Qt.AlignCenter) self.e_filename.setFont(self.font_p1) self.e_filename.setReadOnly(True) self.e_filename.setText("Filename: ") self.grid_body_upper.addWidget(self.e_filename, 0, 2) self.e_section = QLineEdit() self.e_section.setAlignment(Qt.AlignCenter) self.e_section.setFont(self.font_p1) self.e_section.setReadOnly(True) self.e_section.setText("Section: ") self.grid_body_upper.addWidget(self.e_section, 0, 3) # Grid BODY self.viewer = ImageViewer(self) self.grid_body.addWidget(self.viewer, 0, 0) # Grid BODY LOWER self.b_rostral = QPushButton("Rostral Limit:") self.grid_body_lower.addWidget(self.b_rostral, 0, 0) self.e_rostral = QLineEdit() self.e_rostral.setAlignment(Qt.AlignCenter) self.e_rostral.setFont(self.font_p1) self.grid_body_lower.addWidget(self.e_rostral, 0, 1) self.b_caudal = QPushButton("Caudal Limit:") self.grid_body_lower.addWidget(self.b_caudal, 1, 0) self.e_caudal = QLineEdit() self.e_caudal.setAlignment(Qt.AlignCenter) self.e_caudal.setFont(self.font_p1) self.grid_body_lower.addWidget(self.e_caudal, 1, 1) self.b_dorsal = QPushButton("Dorsal Limit:") self.grid_body_lower.addWidget(self.b_dorsal, 0, 2) self.e_dorsal = QLineEdit() self.e_dorsal.setAlignment(Qt.AlignCenter) self.e_dorsal.setFont(self.font_p1) self.grid_body_lower.addWidget(self.e_dorsal, 0, 3) self.b_ventral = QPushButton("Ventral Limit:") self.grid_body_lower.addWidget(self.b_ventral, 1, 2) self.e_ventral = QLineEdit() self.e_ventral.setAlignment(Qt.AlignCenter) self.e_ventral.setFont(self.font_p1) self.grid_body_lower.addWidget(self.e_ventral, 1, 3) self.b_first_slice = QPushButton("Mark as FIRST Slice With Brainstem:") self.grid_body_lower.addWidget(self.b_first_slice, 0, 4) self.e_first_slice = QLineEdit() self.e_first_slice.setAlignment(Qt.AlignCenter) self.e_first_slice.setFont(self.font_p1) self.grid_body_lower.addWidget(self.e_first_slice, 0, 5) self.b_last_slice = QPushButton("Mark as LAST Slice With Brainstem:") self.grid_body_lower.addWidget(self.b_last_slice, 1, 4) self.e_last_slice = QLineEdit() self.e_last_slice.setAlignment(Qt.AlignCenter) self.e_last_slice.setFont(self.font_p1) self.grid_body_lower.addWidget(self.e_last_slice, 1, 5) self.b_done = QPushButton("DONE") self.grid_body_lower.addWidget(self.b_done, 1, 6) # Super grid self.supergrid = QGridLayout() self.supergrid.addLayout(self.grid_top, 0, 0) self.supergrid.addLayout(self.grid_body_upper, 1, 0) self.supergrid.addLayout(self.grid_body, 2, 0) self.supergrid.addLayout(self.grid_body_lower, 3, 0) # Set layout and window title self.setLayout(self.supergrid) self.setWindowTitle("Q") def set_curr_section(self, section_index=-1): """ Sets the current section to the section passed in. Will automatically update curr_section, prev_section, and next_section. Updates the header fields and loads the current section image. """ if section_index == -1: section_index = self.curr_section_index # Update curr, prev, and next section self.curr_section_index = section_index self.curr_section = self.valid_sections[self.valid_section_keys[ self.curr_section_index]] # Update the section and filename at the top self.e_filename.setText(self.curr_section['destination']) self.e_section.setText(str(self.curr_section['section_number'])) # Get filepath of "curr_section" and set it as viewer's photo img_fp = os.path.join(self.fileLocationManager.thumbnail_prep, self.curr_section['destination']) if os.path.isfile(img_fp): img = cv2.imread(img_fp) * 3 self.img_height, self.img_width, channel = img.shape if self.rostral != -1: x_coordinate = int(self.rostral) img[:, x_coordinate - 2:x_coordinate + 2, :] = np.ones( (self.img_height, 4, 3)) * 255 if self.caudal != -1: x_coordinate = int(self.caudal) img[:, x_coordinate - 2:x_coordinate + 2, :] = np.ones( (self.img_height, 4, 3)) * 255 if self.dorsal != -1: y_coordinate = int(self.dorsal) img[y_coordinate - 2:y_coordinate + 2, :, :] = np.ones( (4, self.img_width, 3)) * 255 if self.ventral != -1: y_coordinate = int(self.ventral) img[y_coordinate - 2:y_coordinate + 2, :, :] = np.ones( (4, self.img_width, 3)) * 255 qImg = QImage(img.data, self.img_width, self.img_height, 3 * self.img_width, QImage.Format_RGB888) self.viewer.set_photo(QPixmap(qImg)) # Update the internal crop values based on text boxes #self.updateCropVals() def get_valid_section_index(self, section_index): if section_index >= len(self.valid_sections): return 0 elif section_index < 0: return len(self.valid_sections) - 1 else: return section_index def click_button(self, button): if button in [ self.b_rostral, self.b_caudal, self.b_dorsal, self.b_ventral ]: self.viewer.set_drag_mode(0) if button == self.b_rostral: self.active_selection = 'rostral' elif button == self.b_caudal: self.active_selection = 'caudal' elif button == self.b_dorsal: self.active_selection = 'dorsal' elif button == self.b_ventral: self.active_selection = 'ventral' # Prep2 section limits elif button in [self.b_first_slice, self.b_last_slice]: if button == self.b_first_slice: self.first_slice = int(self.curr_section) self.e_first_slice.setText(str(self.curr_section)) elif button == self.b_last_slice: self.last_slice = int(self.curr_section) self.e_last_slice.setText(str(self.curr_section)) elif button == self.b_done: if -1 in [ self.rostral, self.caudal, self.dorsal, self.ventral, self.first_slice, self.last_slice ]: QMessageBox.about(self, "Popup Message", "Make sure all six fields have values!") return elif self.rostral >= self.caudal: QMessageBox.about( self, "Popup Message", "Rostral Limit must be smaller than caudal limit!") return elif self.dorsal >= self.ventral: QMessageBox.about( self, "Popup Message", "Dorsal Limit must be smaller than Ventral limit!") return elif self.first_slice >= self.last_slice: QMessageBox.about(self, "Popup Message", "Last slice must be after the first slice!") return try: QMessageBox.about( self, "Popup Message", "This operation will take roughly 1.5 minutes per image.") self.setCurrSection(self.curr_section) stain = stack_metadata[stack]['stain'] os.subprocess.call([ 'python', 'a_script_preprocess_6.py', stack, stain, '-l', str(self.rostral), str(self.caudal), str(self.dorsal), str(self.ventral), str(self.first_slice), str(self.last_slice) ]) sys.exit(app.exec_()) except Exception as e: sys.stderr.write('\n ********************************\n') sys.stderr.write(str(e)) sys.stderr.write('\n ********************************\n') def click_photo(self, pos): if self.viewer.dragMode() == QGraphicsView.NoDrag: x = pos.x() y = pos.y() print('%d, %d' % (pos.x(), pos.y())) scale_factor = 1.0 / self.viewer.scale_factor if self.active_selection == '': pass elif self.active_selection == 'rostral': self.rostral = int(x * scale_factor) self.rostral = min(self.rostral, self.img_width - 5) self.rostral = max(self.rostral, 5) self.e_rostral.setText(str(self.rostral)) elif self.active_selection == 'caudal': self.caudal = int(x * scale_factor) self.caudal = min(self.caudal, self.img_width - 5) self.caudal = max(self.caudal, 5) self.e_caudal.setText(str(self.caudal)) elif self.active_selection == 'dorsal': self.dorsal = int(y * scale_factor) self.dorsal = min(self.dorsal, self.img_height - 5) self.dorsal = max(self.dorsal, 5) self.e_dorsal.setText(str(self.dorsal)) elif self.active_selection == 'ventral': self.ventral = int(y * scale_factor) self.ventral = min(self.ventral, self.img_height - 5) self.ventral = max(self.ventral, 5) self.e_ventral.setText(str(self.ventral)) self.active_selection = '' self.viewer.set_drag_mode(1) def updateCropVals(self): if self.e_rostral.text() != '': try: self.rostral = int(self.e_rostral.text()) except: self.rostral = -1 self.e_rostral.setText("") if self.e_caudal.text() != '': try: self.caudal = int(self.e_caudal.text()) except: self.caudal = -1 self.e_caudal.setText("") if self.e_dorsal.text() != '': try: self.dorsal = int(self.e_dorsal.text()) except: self.dorsal = -1 self.e_dorsal.setText("") if self.e_ventral.text() != '': try: self.ventral = int(self.e_ventral.text()) except: self.ventral = -1 self.e_ventral.setText("") if self.e_first_slice.text() != '': try: self.first_slice = int(self.e_first_slice.text()) except: self.first_slice = -1 self.e_first_slice.setText("") if self.e_last_slice.text() != '': try: self.last_slice = int(self.e_last_slice.text()) except: self.last_slice = -1 self.e_last_slice.setText("") def keyPressEvent(self, event): try: key = event.key() except AttributeError: key = event if key == 91: # [ index = self.get_valid_section_index(self.curr_section_index - 1) self.set_curr_section(index) elif key == 93: # ] index = self.get_valid_section_index(self.curr_section_index + 1) self.set_curr_section(index) elif key == 16777220: # Enter index = self.get_valid_section_index(self.curr_section_index) self.set_curr_section(index) else: print(key) def closeEvent(self, event): sys.exit(app.exec_())
def __optionChosen(self, item: QListWidgetItem) -> NoReturn: selectedVolume = item.tags()['link'] self._parent.goTo( ImageViewer(selectedVolume, self.__backEnd, parent=self._parent))
class GameState: def __init__(self, screen_type="console"): self.game = core.Game() self.state = core.state self.screen_type = screen_type self.console = curses.initscr() self.score = 0 self.viewer = None self.screen = None pygame.init() def initial_state(self): return ( helpers.translate_state(self.state, self.game.table), 0.5, False, ) def frame_step(self, action): return self.do_action(action) def print_console(self, board): self.console.addstr(0, 0, board) self.console.refresh() def render(self, mode="human"): if self.viewer is None: self.viewer = ImageViewer(caption="SnakeEnv", height=500, width=500,) # show the screen on the image viewer self.viewer.show(self.screen) def find_loc(self, row_ix, col_ix): loc = [ (col_ix * 4.2), (row_ix * 4.2), (col_ix * 4.2 + 4.2), (row_ix * 4.2 + 4.2), ] return loc def draw(self, data): img = Image.new("RGB", (128, 128)) d = ImageDraw.Draw(img) for row_ix, row in enumerate(data): for col_ix, col in enumerate(row): if col == "#": d.rectangle( self.find_loc(row_ix, col_ix), fill=(255, 0, 0), ) elif col == "X": d.rectangle( self.find_loc(row_ix, col_ix), fill=(0, 255, 0), ) elif col == "O": d.ellipse( self.find_loc(row_ix, col_ix), fill=(0, 0, 255), ) elif col == "H": d.rectangle( self.find_loc(row_ix, col_ix), fill=(128, 0, 128), ) return np.array(img) def print_console_data(self, data): self.console.addstr(30, 0, data) self.console.refresh() def do_action(self, action): """ action: In range [0, 1, 2] 0 do nothing 1 left 2 right 1, 2 will be translated to respective value according to the snake traveling path. """ self.screen = self.draw(self.game.table) new_state, eaten, is_state_terminal = self.driveSnake(action, self.state) self.state = new_state if eaten: reward = 1 self.score += 1 elif is_state_terminal and not eaten: reward = -1 self.score = 0 else: reward = -0.05 return ( helpers.translate_state(new_state, self.game.table), reward, is_state_terminal, self.score, ) def driveSnake(self, direction, state): traveling_path = state["moves"][0] if traveling_path == core.NORTH: move = core.WEST if direction == 1 else core.EAST elif traveling_path == core.SOUTH: move = core.EAST if direction == 1 else core.WEST elif traveling_path == core.WEST: move = core.SOUTH if direction == 1 else core.NORTH elif traveling_path == core.EAST: move = core.NORTH if direction == 1 else core.SOUTH else: raise ValueError("Invalid travelling path.") if direction == 0: move = self.state["moves"][0] new_state = core.enqeue_move(state, move) new_state, eaten, is_state_terminal = core.next_state(new_state) self.game.make(new_state, new_state["apple"]) if self.screen_type == "console": board = str(self.game) self.print_console(board) else: self.render() # use a gui return new_state, eaten, is_state_terminal
def _on_click(self, event): ImageViewer(self.folder_path, self.marker_canvas).open_file(self.time)
class Main(QMainWindow): def __init__(self, port: int): super().__init__() self.initUI() self.initToolbar() self.statusBar().showMessage('Ready') self.API_URL = f'http://localhost:{port}/api/recognize' self.show() def initUI(self): self.image_viewer = ImageViewer() self.setCentralWidget(self.image_viewer) self.resize(800, 600) def initToolbar(self): toolbar = self.addToolBar('Toolbar') toolbar.setMovable(False) load_image_button = QPushButton(text='LoadImage') load_image_button.clicked.connect(self.onLoadImageClick) toolbar.addWidget(load_image_button) segment_button = QPushButton(text='Word Segment') segment_button.clicked.connect(self.image_viewer.autoSegmentation) toolbar.addWidget(segment_button) clear_bbox_button = QPushButton(text='Clear') clear_bbox_button.clicked.connect(self.image_viewer.clearBoxes) toolbar.addWidget(clear_bbox_button) predict_button = QPushButton(text='Predict') predict_button.clicked.connect(self.onPredict) toolbar.addWidget(predict_button) def onLoadImageClick(self): file_path, file_ext = QFileDialog.getOpenFileName( self, 'Select image file', '.', 'Image(*.png *.jpeg *.jpg *.tif);; All files (*.*)', 'Image(*.png *.jpeg *.jpg *.tif)') if file_path: self.loadImage(file_path) def onRotateLeftClick(self): print('Rotate L') def onRotateRightClick(self): print('Rotate R') def onPredict(self): print('Predict') bboxes = self.image_viewer.getBoxes() if len(bboxes) == 0: print('Empty boxes') return request_predicts = {'images': []} # box: QRubberBand for i, box in enumerate(bboxes): cropped: QPixmap = self.image_viewer.pixmap().copy(box) image: Image.Image = Image.fromqpixmap(cropped) # image.save('images/{}.jpg'.format(i)) buffer = BytesIO() image.save(buffer, 'jpeg') buffer.seek(0) request_predicts['images'].append({ 'id': i, 'data': base64.b64encode(buffer.getvalue()).decode(), }) result = requests.post(self.API_URL, json=request_predicts) if result.ok: result = result.json()['results'] # list of # { # 'status': 0, # No error # 'id': image_id, # 'text': text, # } predict_str = ' '.join([word['text'] for word in result]) print('predict_str: "{}"'.format(predict_str)) self.image_viewer.show_predict(predict_str) else: print(result.reason) def loadImage(self, image_path): ok = self.image_viewer.loadImage(image_path) if ok: image: QPixmap = self.image_viewer.pixmap() message = f'{image_path} | {image.width()} x {image.height()}' self.statusBar().showMessage(message)