-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtkdisplay.py
99 lines (83 loc) · 2.73 KB
/
gtkdisplay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Copyright 2014 Douglas Bagnall <[email protected]> LGPL
# used by classify-gtk
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gtk, Gdk
class ClassifierDisplay(Gtk.DrawingArea):
results = []
def __init__(self, classifier, size):
Gtk.DrawingArea.__init__(self)
self.size = size
self.set_size_request (size, size)
self.classifier = classifier
self.connect('draw', self.on_draw)
def notify_results(self, results):
self.results.append(results)
self.queue_draw()
def on_draw(self, widget, cr):
if not self.results:
return
cr.save()
w = widget.get_allocated_width()
h = widget.get_allocated_height()
cr.translate (w / 2, h / 2)
cr.scale(w * 0.48, h * 0.48)
#draw a centre line
cr.set_line_width(0.05)
cr.set_dash((0.01, 0.02))
cr.set_source_rgb(0.8, 0.8, 0.8)
cr.move_to(0, -0.5)
cr.line_to(0, 0.4)
cr.stroke()
cr.set_dash(())
step = 1.0 / len(self.results)
i = step
for winner, scores in self.results:
cr.set_line_width(i * 0.08)
i += step
if len(scores) == 2:
m, e = scores
p = m - e
cr.set_source_rgb(abs(p), m, e)
cr.move_to(p, 0.1)
cr.line_to(p, 0)
elif len(scores) == 3:
m, e, n = scores
p = m - e
cr.set_source_rgb(n, m, e)
cr.move_to(p, n - 0.1)
cr.line_to(p, n - 0.2)
cr.stroke()
cr.restore()
self.results = []
def window_stop(window):
window.classifier.stop()
Gtk.main_quit()
def on_key_press_event(widget, event):
keyname = Gdk.keyval_name(event.keyval).lower()
ctrl = event.state & Gdk.ModifierType.CONTROL_MASK
if keyname == 'q' or (ctrl and keyname == 'w'):
window_stop(widget)
if keyname == 'n':
widget.classifier.load_next_file()
if keyname == 'right':
widget.classifier.seek_relative(5)
if keyname == 'left':
widget.classifier.seek_relative(-5)
#print "Key %s (%d) was pressed" % (keyname, event.keyval)
def run(classifier, files, reverse=False, fullscreen=False):
window = Gtk.Window()
window.set_title ("RNN Classifier")
app = ClassifierDisplay(classifier, 300)
classifier.widget = app
window.add(app)
#XXX mucky
window.classifier = classifier
#print dir(window)
window.connect_after('destroy', window_stop)
window.connect('key_press_event', on_key_press_event)
window.show_all()
if fullscreen:
window.fullscreen()
classifier.run(files, reverse)
Gtk.main()