forked from pinae/UnsharpDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassified_image_datatype.py
131 lines (106 loc) · 4.33 KB
/
classified_image_datatype.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# -*- coding: utf-8 -*-
from __future__ import division, print_function, unicode_literals
from PyQt5.QtCore import Qt, QObject, QPropertyAnimation, QSequentialAnimationGroup, QEasingCurve
from PyQt5.QtCore import pyqtSignal, pyqtProperty
from PyQt5.QtGui import QImage, QColor
from visualization_helpers import convert_image
class ClassifiedImageBundle(QObject):
def get_animation_progress(self):
return self._animation_progress
def set_animation_progress(self, val):
self._animation_progress = val
self.data_changed.emit(self)
UNDECIDED, CLASSIFIED, MANUAL, PROGRESS = range(4)
data_changed = pyqtSignal(QObject)
selected = pyqtSignal(QObject)
animation_progress = pyqtProperty(float, get_animation_progress, set_animation_progress)
def __init__(self, *args):
super().__init__(*args)
self.img = None
self.thumb = None
self.filename = None
self.np_array = None
self.status = ClassifiedImageBundle.UNDECIDED
self.color = None
self.keep = None
self.show_buttons = False
self._animation_progress = 1.0
self.ani = QSequentialAnimationGroup()
self.init_animation()
def init_animation(self):
ani1 = QPropertyAnimation(self, b"animation_progress")
ani1.setDuration(3700)
ani1.setEasingCurve(QEasingCurve.InOutQuad)
ani1.setStartValue(-0.001)
ani1.setEndValue(-1.0)
self.ani.addAnimation(ani1)
ani2 = QPropertyAnimation(self, b"animation_progress")
ani2.setDuration(2300)
ani2.setEasingCurve(QEasingCurve.InOutQuad)
ani2.setStartValue(0.0)
ani2.setEndValue(1.0)
self.ani.addAnimation(ani2)
self.ani.setLoopCount(-1)
def set_np_image(self, np_array, thumb_width=128):
self.np_array = np_array
self.img = QImage(convert_image(np_array), np_array.shape[1], np_array.shape[0], QImage.Format_RGB32)
self.create_thumb(thumb_width)
def set_filename(self, filename):
self.filename = filename
def set_image_from_filename(self, filename, thumb_width=128):
self.filename = filename
self.img = QImage(filename)
self.thumb = self.img.scaledToWidth(thumb_width, mode=Qt.SmoothTransformation)
def create_thumb(self, thumb_width=128):
self.thumb = self.img.scaledToWidth(thumb_width, mode=Qt.SmoothTransformation)
def set_progress(self):
self.status = ClassifiedImageBundle.PROGRESS
self.color = QColor(148, 148, 255)
self.ani.start()
self.data_changed.emit(self)
def set_manual(self, decision):
self.keep = decision
self.status = ClassifiedImageBundle.MANUAL
if self.keep:
self.color = QColor(0, 255, 0)
else:
self.color = QColor(255, 0, 0)
self.ani.stop()
self._animation_progress = 1.0
self.data_changed.emit(self)
def set_classification(self, result):
if self.status != ClassifiedImageBundle.MANUAL:
self.keep = result[1] > result[0]
self.status = ClassifiedImageBundle.CLASSIFIED
self.color = QColor(int(result[0] * 255), int(result[1] * 255), 0)
self.ani.stop()
self._animation_progress = 1.0
self.data_changed.emit(self)
def set_show_buttons(self, button_state=False):
self.show_buttons = button_state
self.data_changed.emit(self)
def get_show_buttons(self):
return self.show_buttons
def get_thumb(self):
return self.thumb
def get_image(self):
return self.img
def get_np_array(self):
return self.np_array
def is_decided(self):
return self.status in [ClassifiedImageBundle.CLASSIFIED,
ClassifiedImageBundle.MANUAL,
ClassifiedImageBundle.PROGRESS]
def has_color(self):
return self.color is not None and self.is_decided()
def get_color(self):
return self.color
def is_classified(self):
return self.status in [ClassifiedImageBundle.CLASSIFIED,
ClassifiedImageBundle.PROGRESS]
def is_undecided(self):
return self.status == ClassifiedImageBundle.UNDECIDED
def reset(self):
self.set_show_buttons(False)
def select(self):
self.selected.emit(self)