|
| 1 | +import sys; sys.path.append('../../') |
| 2 | + |
| 3 | +import ttkbootstrap as ttk |
| 4 | +import scitk.ttk as sttk |
| 5 | +from scitk.widgets.toolbar import ToolBar |
| 6 | +from scitk.canvas import MCanvas as Canvas |
| 7 | + |
| 8 | +class Classmanager(ttk.Frame): |
| 9 | + cmap = [ |
| 10 | + "#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", |
| 11 | + "#fb9a99", "#e31a1c", "#fdbf6f", "#ff7f00", |
| 12 | + "#cab2d6", "#6a3d9a", "#ffff99", "#b15928" |
| 13 | + ] |
| 14 | + def __init__(self, parent, **key): |
| 15 | + super().__init__(parent, **key) |
| 16 | + self.toolbar = ttk.Frame(self) |
| 17 | + self.toolbar.pack(side='top', fill='x') |
| 18 | + btn_new = ttk.Button(self.toolbar, text='new', bootstyle='info-outline', padding=2) |
| 19 | + btn_new.pack(side='left', padx=3, pady=3) |
| 20 | + |
| 21 | + btn_edit = ttk.Button(self.toolbar, text='edit', bootstyle='info-outline', padding=2) |
| 22 | + btn_edit.pack(side='left', padx=3, pady=3) |
| 23 | + |
| 24 | + btn_del = ttk.Button(self.toolbar, text='del', bootstyle='info-outline', padding=2) |
| 25 | + btn_del.pack(side='left', padx=3, pady=3) |
| 26 | + |
| 27 | + btn_clear = ttk.Button(self.toolbar, text='clear', bootstyle='info-outline', padding=2) |
| 28 | + btn_clear.pack(side='left', padx=3, pady=3) |
| 29 | + |
| 30 | + btn_unsel = ttk.Button(self.toolbar, text='unsel', bootstyle='info-outline', padding=2) |
| 31 | + btn_unsel.pack(side='left', padx=3, pady=3) |
| 32 | + |
| 33 | + cls_lst = self.cls_lst = sttk.Gridview(self, indexwidth=False, mode='row', selmode='single', stripe=True, edit=True) |
| 34 | + cls_lst.set([], columns=['Name']) |
| 35 | + cls_lst.set_column_width(0, 80, 1) |
| 36 | + cls_lst.pack(fill='both', expand=True) |
| 37 | + |
| 38 | + btn_new.config(command=self.on_new) |
| 39 | + btn_clear.config(command=self.on_clear) |
| 40 | + btn_del.config(command=self.on_delete) |
| 41 | + btn_edit.config(command=self.on_edit) |
| 42 | + btn_unsel.config(command=self.on_unsel) |
| 43 | + self.classes = [] |
| 44 | + |
| 45 | + def on_new(self): |
| 46 | + self.classes.append('new class') |
| 47 | + self.set(self.classes) |
| 48 | + self.cls_lst.active(len(self.classes)-1, 0) |
| 49 | + |
| 50 | + def on_clear(self): self.set([]) |
| 51 | + |
| 52 | + def on_unsel(self): |
| 53 | + self.cls_lst.select(None) |
| 54 | + self.set(self.classes) |
| 55 | + |
| 56 | + def on_delete(self): |
| 57 | + if self.cls_lst.selection is None: return |
| 58 | + self.classes.pop(self.cls_lst.selection[0]) |
| 59 | + self.set(self.classes) |
| 60 | + |
| 61 | + def on_edit(self): |
| 62 | + if self.cls_lst.selection is None: return |
| 63 | + self.cls_lst.active(self.cls_lst.selection[0], 0) |
| 64 | + |
| 65 | + def set(self, classes): |
| 66 | + self.classes = classes |
| 67 | + self.cls_lst.set([[i] for i in classes], columns=['Name']) |
| 68 | + for i in range(len(classes)): |
| 69 | + self.cls_lst.set_cell_color(i, 'x', self.cmap[i%12]) |
| 70 | + |
| 71 | + def default(self): |
| 72 | + i = self.cls_lst.selection |
| 73 | + if i is None: return |
| 74 | + return self.classes[i], self.cmap[i%12] |
| 75 | + |
| 76 | + def get_classes(self): |
| 77 | + return [(c, self.cmap[i%12]) for (i,c) in enumerate(self.classes)] |
| 78 | + |
| 79 | +classes = ['human', 'car', 'dog', 'cat', 'tree'] |
| 80 | + |
| 81 | +# from plugins import Gaussian |
| 82 | + |
| 83 | +class MainFrame(ttk.Frame): |
| 84 | + def __init__(self, parent, **key): |
| 85 | + super().__init__(parent, **key) |
| 86 | + |
| 87 | + toolbar = self.toolbar = ToolBar(self) |
| 88 | + toolbar.add_tools('IO', [('Open', None, print), |
| 89 | + ('Open Folder', None, print), |
| 90 | + ('Save', None, print)], True) |
| 91 | + |
| 92 | + toolbar.add_tools('Browse', [('Next', None, print), |
| 93 | + ('Prev', None, print), |
| 94 | + ('Zoom', None, print), |
| 95 | + ('Move', None, print), |
| 96 | + ('Auto Fit', None, print)], True) |
| 97 | + |
| 98 | + toolbar.add_tools('Edit', [('Rectangle', None, print), |
| 99 | + ('Polygon', None, print), |
| 100 | + ('Line', None, print), |
| 101 | + ('Point', None, print), |
| 102 | + ('Editor', None, print), |
| 103 | + ('Clear', None, print)], True) |
| 104 | + |
| 105 | + toolbar.add_tools('AI', [('Sam', None, print), |
| 106 | + ('xxx', None, print)], True) |
| 107 | + |
| 108 | + toolbar.pack(fill='x', side='top') |
| 109 | + |
| 110 | + |
| 111 | + self.status = ttk.Label(self) |
| 112 | + self.status.config(text='状态栏') |
| 113 | + self.status.pack(side='bottom', fill='x') |
| 114 | + ttk.Separator(self).pack(side='bottom', fill='x') |
| 115 | + |
| 116 | + |
| 117 | + self.fmanager = sttk.FileManager(self) |
| 118 | + self.fmanager.pack(side='left', fill='y') |
| 119 | + |
| 120 | + self.canvas = Canvas(self) |
| 121 | + self.canvas.pack(side='left', fill='both', expand=True) |
| 122 | + |
| 123 | + |
| 124 | + rp = self.rpanel = sttk.Panedbook(self) |
| 125 | + panel = rp.add('Class Manager', weight=1) |
| 126 | + clsmanager = Classmanager(panel) |
| 127 | + clsmanager.pack(fill='both', expand=True, padx=1, pady=1) |
| 128 | + clsmanager.set(classes) |
| 129 | + |
| 130 | + panel = rp.add('Label Manager', weight=1) |
| 131 | + lab_lst = sttk.Gridview(panel, indexwidth=False) |
| 132 | + lab_lst.set([], columns=['Name']) |
| 133 | + lab_lst.set_column_width(0, 80, 1) |
| 134 | + lab_lst.pack(fill='both', expand=True, padx=1, pady=1) |
| 135 | + |
| 136 | + rp.config(width=200) |
| 137 | + rp.pack(side='right', fill='y') |
| 138 | + |
| 139 | + self.fmanager.bind('<<File-Select>>', self.on_open) |
| 140 | + |
| 141 | + def get_img(self): return self.canvas.image |
| 142 | + |
| 143 | + # def alert( |
| 144 | + def info(self, info): |
| 145 | + print(info) |
| 146 | + |
| 147 | + def on_open(self, path): |
| 148 | + from imageio import imread |
| 149 | + img = imread(path) |
| 150 | + self.canvas.set_img(img) |
| 151 | + |
| 152 | + def info(self, info): |
| 153 | + self.status.config(text=info) |
| 154 | + |
| 155 | + |
| 156 | +if __name__ == '__main__': |
| 157 | + app = ttk.Window('标注工具') |
| 158 | + ttk.Style('darkly') |
| 159 | + frame = MainFrame(app) |
| 160 | + frame.pack(fill='both', expand=True) |
| 161 | + app.mainloop() |
0 commit comments