ScreenMagnifier 1.11.0.post8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of ScreenMagnifier might be problematic. Click here for more details.
- ScreenMagnifier/__init__.py +68 -0
- ScreenMagnifier/__main__.py +17 -0
- ScreenMagnifier/advancedoverlay.py +593 -0
- ScreenMagnifier/advancedpanel.py +521 -0
- ScreenMagnifier/analysiscontroller.py +533 -0
- ScreenMagnifier/analysisserver.py +404 -0
- ScreenMagnifier/colordetector/__init__.py +28 -0
- ScreenMagnifier/colordetector/colordetector.py +570 -0
- ScreenMagnifier/colordetector/core.py +434 -0
- ScreenMagnifier/colordetector/main.py +23 -0
- ScreenMagnifier/colordetector/mainwindow.py +182 -0
- ScreenMagnifier/colorpanel.py +424 -0
- ScreenMagnifier/colorswatches/__init__.py +16 -0
- ScreenMagnifier/colorswatches/colorswatches.py +316 -0
- ScreenMagnifier/colorswatches/main.py +23 -0
- ScreenMagnifier/colorswatches/mainwindow.py +60 -0
- ScreenMagnifier/controlpanel.py +234 -0
- ScreenMagnifier/detectionpanel.py +790 -0
- ScreenMagnifier/facedetector/__init__.py +24 -0
- ScreenMagnifier/facedetector/core.py +314 -0
- ScreenMagnifier/facedetector/facedetector.py +288 -0
- ScreenMagnifier/facedetector/main.py +23 -0
- ScreenMagnifier/facedetector/mainwindow.py +122 -0
- ScreenMagnifier/facedetector/models/__init__.py +4 -0
- ScreenMagnifier/facepointer.py +1121 -0
- ScreenMagnifier/features.py +50 -0
- ScreenMagnifier/hotkeys/__init__.py +16 -0
- ScreenMagnifier/hotkeys/hotkeys.py +379 -0
- ScreenMagnifier/hotkeys/main.py +23 -0
- ScreenMagnifier/hotkeys/mainwindow.py +56 -0
- ScreenMagnifier/imagefilters/__init__.py +24 -0
- ScreenMagnifier/imagefilters/core.py +172 -0
- ScreenMagnifier/imagefilters/imagefilters.py +220 -0
- ScreenMagnifier/imagefilters/main.py +23 -0
- ScreenMagnifier/imagefilters/mainwindow.py +120 -0
- ScreenMagnifier/magnifierview/__init__.py +16 -0
- ScreenMagnifier/magnifierview/magnifierview.py +1249 -0
- ScreenMagnifier/magnifierview/main.py +23 -0
- ScreenMagnifier/magnifierview/mainwindow.py +91 -0
- ScreenMagnifier/main.py +157 -0
- ScreenMagnifier/mainwindow.py +1368 -0
- ScreenMagnifier/mousecatcher/__init__.py +16 -0
- ScreenMagnifier/mousecatcher/main.py +23 -0
- ScreenMagnifier/mousecatcher/mainwindow.py +78 -0
- ScreenMagnifier/mousecatcher/mousecatcher.py +251 -0
- ScreenMagnifier/mousemove.py +259 -0
- ScreenMagnifier/netprotocol/__init__.py +25 -0
- ScreenMagnifier/netprotocol/main.py +23 -0
- ScreenMagnifier/netprotocol/mainwindow.py +91 -0
- ScreenMagnifier/netprotocol/netprotocol.py +372 -0
- ScreenMagnifier/netprotocol/wire.py +189 -0
- ScreenMagnifier/regionselector/__init__.py +16 -0
- ScreenMagnifier/regionselector/main.py +23 -0
- ScreenMagnifier/regionselector/mainwindow.py +70 -0
- ScreenMagnifier/regionselector/regionselector.py +549 -0
- ScreenMagnifier/remoteclient.py +636 -0
- ScreenMagnifier/resources/icon-128.png +0 -0
- ScreenMagnifier/resources/icon-16.png +0 -0
- ScreenMagnifier/resources/icon-24.png +0 -0
- ScreenMagnifier/resources/icon-256.png +0 -0
- ScreenMagnifier/resources/icon-32.png +0 -0
- ScreenMagnifier/resources/icon-48.png +0 -0
- ScreenMagnifier/resources/icon-512.png +0 -0
- ScreenMagnifier/resources/icon-64.png +0 -0
- ScreenMagnifier/resources/icon.icns +0 -0
- ScreenMagnifier/resources/icon.ico +0 -0
- ScreenMagnifier/screengrabber/__init__.py +16 -0
- ScreenMagnifier/screengrabber/main.py +23 -0
- ScreenMagnifier/screengrabber/mainwindow.py +79 -0
- ScreenMagnifier/screengrabber/screengrabber.py +558 -0
- ScreenMagnifier/server.py +150 -0
- ScreenMagnifier/serverpanel.py +379 -0
- ScreenMagnifier/shortcuteditor/__init__.py +16 -0
- ScreenMagnifier/shortcuteditor/main.py +23 -0
- ScreenMagnifier/shortcuteditor/mainwindow.py +54 -0
- ScreenMagnifier/shortcuteditor/shortcuteditor.py +411 -0
- ScreenMagnifier/shortcuts.py +118 -0
- ScreenMagnifier/shortcutspanel.py +140 -0
- ScreenMagnifier/support.py +127 -0
- ScreenMagnifier/translations/ar.json +387 -0
- ScreenMagnifier/translations/de.json +387 -0
- ScreenMagnifier/translations/es.json +387 -0
- ScreenMagnifier/translations/fr.json +387 -0
- ScreenMagnifier/translations/it.json +387 -0
- ScreenMagnifier/translations/ja.json +387 -0
- ScreenMagnifier/translations/ko.json +387 -0
- ScreenMagnifier/translations/pt_BR.json +387 -0
- ScreenMagnifier/translations/ru.json +387 -0
- ScreenMagnifier/translations/tr.json +387 -0
- ScreenMagnifier/translations/zh_CN.json +387 -0
- ScreenMagnifier/translator/__init__.py +16 -0
- ScreenMagnifier/translator/main.py +23 -0
- ScreenMagnifier/translator/mainwindow.py +62 -0
- ScreenMagnifier/translator/translator.py +284 -0
- ScreenMagnifier/upscaler/__init__.py +24 -0
- ScreenMagnifier/upscaler/core.py +315 -0
- ScreenMagnifier/upscaler/main.py +23 -0
- ScreenMagnifier/upscaler/mainwindow.py +128 -0
- ScreenMagnifier/upscaler/models/FSRCNN-small_x2.pb +0 -0
- ScreenMagnifier/upscaler/models/FSRCNN-small_x3.pb +0 -0
- ScreenMagnifier/upscaler/models/FSRCNN-small_x4.pb +0 -0
- ScreenMagnifier/upscaler/models/FSRCNN_x2.pb +0 -0
- ScreenMagnifier/upscaler/models/FSRCNN_x3.pb +0 -0
- ScreenMagnifier/upscaler/models/FSRCNN_x4.pb +0 -0
- ScreenMagnifier/upscaler/models/LICENSE-FSRCNN +201 -0
- ScreenMagnifier/upscaler/models/__init__.py +4 -0
- ScreenMagnifier/upscaler/upscaler.py +285 -0
- ScreenMagnifier/wincheck.py +65 -0
- ScreenMagnifier/zoomoverlay/__init__.py +16 -0
- ScreenMagnifier/zoomoverlay/main.py +23 -0
- ScreenMagnifier/zoomoverlay/mainwindow.py +99 -0
- ScreenMagnifier/zoomoverlay/zoomoverlay.py +1779 -0
- screenmagnifier-1.11.0.post8.dist-info/METADATA +591 -0
- screenmagnifier-1.11.0.post8.dist-info/RECORD +117 -0
- screenmagnifier-1.11.0.post8.dist-info/WHEEL +5 -0
- screenmagnifier-1.11.0.post8.dist-info/entry_points.txt +5 -0
- screenmagnifier-1.11.0.post8.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Color analysis on NumPy arrays - pure Python + NumPy, OpenCV optional, no Qt: used by the Qt ColorDetector and by
|
|
4
|
+
the headless server (which also runs without Qt, e.g. on Android).
|
|
5
|
+
- Color maths: sRGB -> CIE Lab, delta E, CSS color names (static table, same names and order as Qt).
|
|
6
|
+
- ColorAnalyzer: target color regions (Similar shades: same hue at any lightness, with hysteresis against
|
|
7
|
+
look-alikes; Exact: delta E), grouped fragments, coverage, dominant palette, label mask.
|
|
8
|
+
Colors are (r, g, b) tuples or '#RRGGBB' strings.
|
|
9
|
+
"""
|
|
10
|
+
from math import ceil, sqrt, cos, radians
|
|
11
|
+
from time import time
|
|
12
|
+
|
|
13
|
+
try:
|
|
14
|
+
from numpy import array, zeros, where, cbrt, bincount, argsort, ascontiguousarray, ndarray, float32, uint8, \
|
|
15
|
+
int32, ones, full, minimum, maximum
|
|
16
|
+
from numpy import sqrt as npsqrt
|
|
17
|
+
|
|
18
|
+
np = True # type: bool
|
|
19
|
+
except:
|
|
20
|
+
np = False # type: bool
|
|
21
|
+
try:
|
|
22
|
+
from cv2 import connectedComponents, dilate, boxFilter, blur, cvtColor, COLOR_RGB2Lab
|
|
23
|
+
|
|
24
|
+
cv2 = True # type: bool
|
|
25
|
+
except:
|
|
26
|
+
cv2 = False # type: bool
|
|
27
|
+
scipy = False # type: bool # SciPy replaces OpenCV for regions (e.g. Android: Termux / Pydroid have SciPy).
|
|
28
|
+
if not cv2:
|
|
29
|
+
try:
|
|
30
|
+
from scipy.ndimage import label as _label, binary_dilation as _dilation, uniform_filter as _uniform
|
|
31
|
+
|
|
32
|
+
scipy = True # type: bool
|
|
33
|
+
except:
|
|
34
|
+
pass
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _components(mask):
|
|
38
|
+
"""
|
|
39
|
+
:param mask: (ndarray) uint8 0/1.
|
|
40
|
+
:return: (tuple[int, ndarray]) Number of labels (background included) and the 8-connected label image.
|
|
41
|
+
"""
|
|
42
|
+
if cv2:
|
|
43
|
+
return connectedComponents(mask, connectivity=8)[:2]
|
|
44
|
+
labels, n = _label(mask, structure=ones((3, 3), int32))
|
|
45
|
+
return n + 1, labels
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _neighbours(mask):
|
|
49
|
+
"""
|
|
50
|
+
:param mask: (ndarray) uint8 0/1.
|
|
51
|
+
:return: (ndarray) Matching pixels in each 3 x 3 window (self included).
|
|
52
|
+
"""
|
|
53
|
+
if cv2:
|
|
54
|
+
return boxFilter(mask, -1, (3, 3), normalize=False)
|
|
55
|
+
return (_uniform(mask.astype(float32), 3, mode='constant') * 9.0 + 0.5).astype(int32)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _grow(mask, radius):
|
|
59
|
+
"""
|
|
60
|
+
:param mask: (ndarray) uint8 0/1.
|
|
61
|
+
:param radius: (int) Square dilation radius.
|
|
62
|
+
:return: (ndarray) uint8 0/1.
|
|
63
|
+
"""
|
|
64
|
+
if cv2:
|
|
65
|
+
return dilate(mask, ones((2 * radius + 1, 2 * radius + 1), uint8))
|
|
66
|
+
return _dilation(mask > 0, structure=ones((2 * radius + 1, 2 * radius + 1), bool)).astype(uint8)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
SIMILAR, EXACT = 0, 1 # match modes (ColorDetector.Similar / ColorDetector.Exact)
|
|
70
|
+
|
|
71
|
+
CSS_COLORS = [ # (name, RRGGBB): the CSS / SVG color names, in Qt's order (ties keep the first)
|
|
72
|
+
('aliceblue', 'F0F8FF'), ('antiquewhite', 'FAEBD7'), ('aqua', '00FFFF'), ('aquamarine', '7FFFD4'),
|
|
73
|
+
('azure', 'F0FFFF'), ('beige', 'F5F5DC'), ('bisque', 'FFE4C4'), ('black', '000000'),
|
|
74
|
+
('blanchedalmond', 'FFEBCD'), ('blue', '0000FF'), ('blueviolet', '8A2BE2'), ('brown', 'A52A2A'),
|
|
75
|
+
('burlywood', 'DEB887'), ('cadetblue', '5F9EA0'), ('chartreuse', '7FFF00'), ('chocolate', 'D2691E'),
|
|
76
|
+
('coral', 'FF7F50'), ('cornflowerblue', '6495ED'), ('cornsilk', 'FFF8DC'), ('crimson', 'DC143C'),
|
|
77
|
+
('darkblue', '00008B'), ('darkcyan', '008B8B'), ('darkgoldenrod', 'B8860B'), ('darkgray', 'A9A9A9'),
|
|
78
|
+
('darkgreen', '006400'), ('darkkhaki', 'BDB76B'), ('darkmagenta', '8B008B'), ('darkolivegreen', '556B2F'),
|
|
79
|
+
('darkorange', 'FF8C00'), ('darkorchid', '9932CC'), ('darkred', '8B0000'), ('darksalmon', 'E9967A'),
|
|
80
|
+
('darkseagreen', '8FBC8F'), ('darkslateblue', '483D8B'), ('darkslategray', '2F4F4F'),
|
|
81
|
+
('darkturquoise', '00CED1'), ('darkviolet', '9400D3'), ('deeppink', 'FF1493'), ('deepskyblue', '00BFFF'),
|
|
82
|
+
('dimgray', '696969'), ('dodgerblue', '1E90FF'), ('firebrick', 'B22222'), ('floralwhite', 'FFFAF0'),
|
|
83
|
+
('forestgreen', '228B22'), ('fuchsia', 'FF00FF'), ('gainsboro', 'DCDCDC'), ('ghostwhite', 'F8F8FF'),
|
|
84
|
+
('gold', 'FFD700'), ('goldenrod', 'DAA520'), ('gray', '808080'), ('green', '008000'),
|
|
85
|
+
('greenyellow', 'ADFF2F'), ('honeydew', 'F0FFF0'), ('hotpink', 'FF69B4'), ('indianred', 'CD5C5C'),
|
|
86
|
+
('indigo', '4B0082'), ('ivory', 'FFFFF0'), ('khaki', 'F0E68C'), ('lavender', 'E6E6FA'),
|
|
87
|
+
('lavenderblush', 'FFF0F5'), ('lawngreen', '7CFC00'), ('lemonchiffon', 'FFFACD'), ('lightblue', 'ADD8E6'),
|
|
88
|
+
('lightcoral', 'F08080'), ('lightcyan', 'E0FFFF'), ('lightgoldenrodyellow', 'FAFAD2'), ('lightgray', 'D3D3D3'),
|
|
89
|
+
('lightgreen', '90EE90'), ('lightpink', 'FFB6C1'), ('lightsalmon', 'FFA07A'), ('lightseagreen', '20B2AA'),
|
|
90
|
+
('lightskyblue', '87CEFA'), ('lightslategray', '778899'), ('lightsteelblue', 'B0C4DE'),
|
|
91
|
+
('lightyellow', 'FFFFE0'), ('lime', '00FF00'), ('limegreen', '32CD32'), ('linen', 'FAF0E6'),
|
|
92
|
+
('maroon', '800000'), ('mediumaquamarine', '66CDAA'), ('mediumblue', '0000CD'), ('mediumorchid', 'BA55D3'),
|
|
93
|
+
('mediumpurple', '9370DB'), ('mediumseagreen', '3CB371'), ('mediumslateblue', '7B68EE'),
|
|
94
|
+
('mediumspringgreen', '00FA9A'), ('mediumturquoise', '48D1CC'), ('mediumvioletred', 'C71585'),
|
|
95
|
+
('midnightblue', '191970'), ('mintcream', 'F5FFFA'), ('mistyrose', 'FFE4E1'), ('moccasin', 'FFE4B5'),
|
|
96
|
+
('navajowhite', 'FFDEAD'), ('navy', '000080'), ('oldlace', 'FDF5E6'), ('olive', '808000'),
|
|
97
|
+
('olivedrab', '6B8E23'), ('orange', 'FFA500'), ('orangered', 'FF4500'), ('orchid', 'DA70D6'),
|
|
98
|
+
('palegoldenrod', 'EEE8AA'), ('palegreen', '98FB98'), ('paleturquoise', 'AFEEEE'), ('palevioletred', 'DB7093'),
|
|
99
|
+
('papayawhip', 'FFEFD5'), ('peachpuff', 'FFDAB9'), ('peru', 'CD853F'), ('pink', 'FFC0CB'), ('plum', 'DDA0DD'),
|
|
100
|
+
('powderblue', 'B0E0E6'), ('purple', '800080'), ('red', 'FF0000'), ('rosybrown', 'BC8F8F'),
|
|
101
|
+
('royalblue', '4169E1'), ('saddlebrown', '8B4513'), ('salmon', 'FA8072'), ('sandybrown', 'F4A460'),
|
|
102
|
+
('seagreen', '2E8B57'), ('seashell', 'FFF5EE'), ('sienna', 'A0522D'), ('silver', 'C0C0C0'),
|
|
103
|
+
('skyblue', '87CEEB'), ('slateblue', '6A5ACD'), ('slategray', '708090'), ('snow', 'FFFAFA'),
|
|
104
|
+
('springgreen', '00FF7F'), ('steelblue', '4682B4'), ('tan', 'D2B48C'), ('teal', '008080'),
|
|
105
|
+
('thistle', 'D8BFD8'), ('tomato', 'FF6347'), ('turquoise', '40E0D0'), ('violet', 'EE82EE'),
|
|
106
|
+
('wheat', 'F5DEB3'), ('white', 'FFFFFF'), ('whitesmoke', 'F5F5F5'), ('yellow', 'FFFF00'),
|
|
107
|
+
('yellowgreen', '9ACD32'),
|
|
108
|
+
] # type: list[tuple[str, str]]
|
|
109
|
+
_SRGB_TO_XYZ = ((0.4124564, 0.3575761, 0.1804375),
|
|
110
|
+
(0.2126729, 0.7151522, 0.0721750),
|
|
111
|
+
(0.0193339, 0.1191920, 0.9503041)) # type: tuple
|
|
112
|
+
_WHITE = (0.95047, 1.0, 1.08883) # type: tuple # D65 reference white.
|
|
113
|
+
CHROMATIC_MIN = 12.0 # type: float # Lab chroma under which a target counts as gray / white / black.
|
|
114
|
+
STRONG_FACTOR = 0.6 # type: float # Strong evidence (hysteresis seeds): >= 60 % chroma and half the hue window.
|
|
115
|
+
CHROMA_FACTOR = 0.45 # type: float # Similar mode: a pixel needs >= 45 % of the target chroma (shadows of the
|
|
116
|
+
# same color keep ~55-65 %, dull look-alikes such as brown for red do not).
|
|
117
|
+
_NAME_TABLE = [] # type: list # Lazily built [(name, (L, a, b))] for nearestColorName().
|
|
118
|
+
_LINEAR_LUT = [] # type: list # Lazily built 256-entry sRGB -> linear table (ndarray).
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def parseColor(color):
|
|
122
|
+
"""
|
|
123
|
+
:param color: ((r, g, b) | '#RRGGBB' | '#RGB' | CSS name) A colour.
|
|
124
|
+
:return: (tuple[int, int, int]) (r, g, b).
|
|
125
|
+
"""
|
|
126
|
+
if isinstance(color, (tuple, list)):
|
|
127
|
+
return int(color[0]), int(color[1]), int(color[2])
|
|
128
|
+
text = u'{}'.format(color).strip() # type: str
|
|
129
|
+
if not text.startswith('#'):
|
|
130
|
+
for name, code in CSS_COLORS:
|
|
131
|
+
if name == text.lower():
|
|
132
|
+
text = '#' + code
|
|
133
|
+
break
|
|
134
|
+
text = text.lstrip('#')
|
|
135
|
+
if len(text) == 3:
|
|
136
|
+
text = ''.join(ch * 2 for ch in text)
|
|
137
|
+
return int(text[0:2], 16), int(text[2:4], 16), int(text[4:6], 16)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def hexName(color):
|
|
141
|
+
"""
|
|
142
|
+
:param color: (r, g, b) | str
|
|
143
|
+
:return: (str | unicode | QString) '#RRGGBB'.
|
|
144
|
+
"""
|
|
145
|
+
return '#{:02X}{:02X}{:02X}'.format(*parseColor(color))
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def clamp(x, a, b):
|
|
149
|
+
"""
|
|
150
|
+
:param x: float | int
|
|
151
|
+
:param a: float | int
|
|
152
|
+
:param b: float | int
|
|
153
|
+
:return: float | int
|
|
154
|
+
"""
|
|
155
|
+
return a if x < a else b if x > b else x
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _linear(c):
|
|
159
|
+
"""
|
|
160
|
+
sRGB channel (0 .. 1) -> linear light.
|
|
161
|
+
:param c: float
|
|
162
|
+
:return: float | int
|
|
163
|
+
"""
|
|
164
|
+
return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def rgbToLab(r, g, b):
|
|
168
|
+
"""
|
|
169
|
+
:param r: (int) 0 .. 255
|
|
170
|
+
:param g: (int) 0 .. 255
|
|
171
|
+
:param b: (int) 0 .. 255
|
|
172
|
+
:return: (tuple[float, float, float]) CIE L*a*b* (D65).
|
|
173
|
+
"""
|
|
174
|
+
lr, lg, lb = _linear(r / 255.0), _linear(g / 255.0), _linear(b / 255.0)
|
|
175
|
+
xyz = [(m[0] * lr + m[1] * lg + m[2] * lb) / w for m, w in zip(_SRGB_TO_XYZ, _WHITE)] # type: list
|
|
176
|
+
f = [v ** (1.0 / 3.0) if v > 0.008856 else 7.787 * v + 16.0 / 116.0 for v in xyz] # type: list
|
|
177
|
+
return 116.0 * f[1] - 16.0, 500.0 * (f[0] - f[1]), 200.0 * (f[1] - f[2])
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def deltaE(lab1, lab2):
|
|
181
|
+
"""
|
|
182
|
+
:param lab1: tuple[float, float, float]
|
|
183
|
+
:param lab2: tuple[float, float, float]
|
|
184
|
+
:return: (float | int) CIE76 color difference (about 2.3 = just noticeable).
|
|
185
|
+
"""
|
|
186
|
+
return sqrt(sum((p - q) ** 2 for p, q in zip(lab1, lab2)))
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def nearestColorName(color):
|
|
190
|
+
"""
|
|
191
|
+
:param color: (r, g, b) | str
|
|
192
|
+
:return: (tuple[str, float]) Nearest CSS / SVG color name and its delta E.
|
|
193
|
+
"""
|
|
194
|
+
if not _NAME_TABLE:
|
|
195
|
+
for name, code in CSS_COLORS:
|
|
196
|
+
_NAME_TABLE.append((name, rgbToLab(*parseColor('#' + code))))
|
|
197
|
+
lab = rgbToLab(*parseColor(color))
|
|
198
|
+
best = min(_NAME_TABLE, key=lambda item: deltaE(lab, item[1]))
|
|
199
|
+
return best[0], deltaE(lab, best[1])
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def labArray(arr):
|
|
203
|
+
"""
|
|
204
|
+
Vectorised sRGB -> CIE Lab (OpenCV float conversion when available: about 3x faster, < 0.2 delta E).
|
|
205
|
+
:param arr: (ndarray) (H, W, 3) uint8 RGB.
|
|
206
|
+
:return: (ndarray) (H, W, 3) float32 Lab.
|
|
207
|
+
"""
|
|
208
|
+
if cv2:
|
|
209
|
+
rgb = arr.astype(float32) # type: ndarray
|
|
210
|
+
rgb *= 1.0 / 255.0
|
|
211
|
+
return cvtColor(rgb, COLOR_RGB2Lab)
|
|
212
|
+
if not _LINEAR_LUT:
|
|
213
|
+
_LINEAR_LUT.append(array([_linear(i / 255.0) for i in range(256)], float32))
|
|
214
|
+
lin = _LINEAR_LUT[0][arr] # type: ndarray
|
|
215
|
+
xyz = lin.dot(array(_SRGB_TO_XYZ, float32).T) / array(_WHITE, float32) # type: ndarray
|
|
216
|
+
f = where(xyz > 0.008856, cbrt(xyz), 7.787 * xyz + 16.0 / 116.0) # type: ndarray
|
|
217
|
+
out = zeros(arr.shape, float32) # type: ndarray
|
|
218
|
+
out[..., 0] = 116.0 * f[..., 1] - 16.0
|
|
219
|
+
out[..., 1] = 500.0 * (f[..., 0] - f[..., 1])
|
|
220
|
+
out[..., 2] = 200.0 * (f[..., 1] - f[..., 2])
|
|
221
|
+
return out
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
class ColorAnalyzer(object):
|
|
225
|
+
"""
|
|
226
|
+
ColorAnalyzer class: options are plain attributes (set them, then analyze()).
|
|
227
|
+
"""
|
|
228
|
+
|
|
229
|
+
def __init__(self, **options):
|
|
230
|
+
"""
|
|
231
|
+
:param options: targets ([(r, g, b) | str]), tolerance, matchMode (SIMILAR / EXACT), groupDistance, minArea,
|
|
232
|
+
maxRegions, paletteSize, returnMask, maxPixels - the ColorDetector options.
|
|
233
|
+
"""
|
|
234
|
+
self.targets = [] # type: list
|
|
235
|
+
self.tolerance = 15.0 # type: float
|
|
236
|
+
self.matchMode = SIMILAR # type: int
|
|
237
|
+
self.groupDistance = 5 # type: int
|
|
238
|
+
self.minArea = 12 # type: int
|
|
239
|
+
self.maxRegions = 24 # type: int
|
|
240
|
+
self.paletteSize = 6 # type: int
|
|
241
|
+
self.returnMask = True # type: bool
|
|
242
|
+
self.maxPixels = 1280 * 800 # type: int
|
|
243
|
+
self.configure(**options)
|
|
244
|
+
|
|
245
|
+
def configure(self, **options):
|
|
246
|
+
"""
|
|
247
|
+
:param options: see __init__ (unknown names are ignored, values are clamped).
|
|
248
|
+
:return:
|
|
249
|
+
"""
|
|
250
|
+
if 'targets' in options:
|
|
251
|
+
self.targets = [parseColor(c) for c in options['targets']][:254] # type: list
|
|
252
|
+
if 'tolerance' in options:
|
|
253
|
+
self.tolerance = clamp(float(options['tolerance']), 1.0, 100.0) # type: float
|
|
254
|
+
if 'matchMode' in options:
|
|
255
|
+
self.matchMode = EXACT if int(options['matchMode']) == EXACT else SIMILAR # type: int
|
|
256
|
+
if 'groupDistance' in options:
|
|
257
|
+
self.groupDistance = max(0, int(options['groupDistance'])) # type: int
|
|
258
|
+
if 'minArea' in options:
|
|
259
|
+
self.minArea = max(1, int(options['minArea'])) # type: int
|
|
260
|
+
if 'maxRegions' in options:
|
|
261
|
+
self.maxRegions = max(1, int(options['maxRegions'])) # type: int
|
|
262
|
+
if 'paletteSize' in options:
|
|
263
|
+
self.paletteSize = max(0, int(options['paletteSize'])) # type: int
|
|
264
|
+
if 'returnMask' in options:
|
|
265
|
+
self.returnMask = bool(options['returnMask']) # type: bool
|
|
266
|
+
if 'maxPixels' in options:
|
|
267
|
+
self.maxPixels = max(4096, int(options['maxPixels'])) # type: int
|
|
268
|
+
|
|
269
|
+
def _palette(self, arr):
|
|
270
|
+
"""
|
|
271
|
+
:param arr: (ndarray) RGB pixels.
|
|
272
|
+
:return: list[dict]
|
|
273
|
+
"""
|
|
274
|
+
flat = arr.reshape(-1, 3) # type: ndarray
|
|
275
|
+
step = max(1, int(flat.shape[0] // 65536)) # type: int
|
|
276
|
+
px = flat[::step].astype(int32) # type: ndarray
|
|
277
|
+
bins = (px[:, 0] >> 4) * 256 + (px[:, 1] >> 4) * 16 + (px[:, 2] >> 4) # type: ndarray
|
|
278
|
+
counts = bincount(bins, minlength=4096) # type: ndarray
|
|
279
|
+
sums = [bincount(bins, weights=px[:, k], minlength=4096) for k in range(3)] # type: list
|
|
280
|
+
total = float(px.shape[0]) # type: float
|
|
281
|
+
groups = [] # type: list # [lab, (r, g, b), count]
|
|
282
|
+
for b in argsort(counts)[::-1][:64]:
|
|
283
|
+
if counts[b] == 0:
|
|
284
|
+
break
|
|
285
|
+
rgb = tuple(int(round(sums[k][b] / counts[b])) for k in range(3)) # type: tuple
|
|
286
|
+
lb = rgbToLab(*rgb)
|
|
287
|
+
for grp in groups:
|
|
288
|
+
if deltaE(grp[0], lb) < 12.0:
|
|
289
|
+
grp[2] += int(counts[b])
|
|
290
|
+
break
|
|
291
|
+
else:
|
|
292
|
+
groups.append([lb, rgb, int(counts[b])])
|
|
293
|
+
groups.sort(key=lambda g: -g[2])
|
|
294
|
+
out, byName = [], {} # type: list, dict # Groups sharing a color name are merged (gradients).
|
|
295
|
+
for lb, rgb, count in [g for g in groups if g[2] / total >= 0.003]:
|
|
296
|
+
name = nearestColorName(rgb)[0] # type: str
|
|
297
|
+
if name in byName:
|
|
298
|
+
byName[name]['fraction'] = round(byName[name]['fraction'] + count / total, 4)
|
|
299
|
+
continue
|
|
300
|
+
byName[name] = {'color': hexName(rgb), 'fraction': round(count / total, 4), 'name': name}
|
|
301
|
+
out.append(byName[name])
|
|
302
|
+
out.sort(key=lambda c: -c['fraction']) # Merging changed the shares: sort again.
|
|
303
|
+
return out[:self.paletteSize]
|
|
304
|
+
|
|
305
|
+
def _match(self, lab, chroma, ab, abChroma, target):
|
|
306
|
+
"""
|
|
307
|
+
Pixels matching one target.
|
|
308
|
+
:param lab: (ndarray) (H, W, 3) Lab frame.
|
|
309
|
+
:param chroma: (ndarray) (H, W) Lab chroma of the frame.
|
|
310
|
+
:param ab: (ndarray) (H, W, 2) a/b plane smoothed 3 x 3: hue direction with ~3x less noise (blending
|
|
311
|
+
with a neutral background shrinks the vector but keeps its direction, so thin text keeps its hue).
|
|
312
|
+
:param abChroma: (ndarray) (H, W) length of the smoothed a/b vector.
|
|
313
|
+
:param target: (tuple) (r, g, b)
|
|
314
|
+
:return: (tuple[ndarray, ndarray | None]) Weak mask (all matches) and strong mask (hysteresis seeds, None
|
|
315
|
+
when every match counts as strong: Exact mode, gray targets), uint8 0/1.
|
|
316
|
+
"""
|
|
317
|
+
tl = rgbToLab(*target)
|
|
318
|
+
tol = self.tolerance # type: float
|
|
319
|
+
L, A, B = lab[..., 0], lab[..., 1], lab[..., 2]
|
|
320
|
+
if self.matchMode == EXACT:
|
|
321
|
+
dl, da, db = L - tl[0], A - tl[1], B - tl[2]
|
|
322
|
+
return (dl * dl + da * da + db * db <= tol * tol).astype(uint8), None
|
|
323
|
+
ct = sqrt(tl[1] * tl[1] + tl[2] * tl[2]) # type: float
|
|
324
|
+
if ct < CHROMATIC_MIN:
|
|
325
|
+
# Gray / white / black target: hue is meaningless, match low chroma and close lightness.
|
|
326
|
+
return ((chroma <= max(8.0, 0.6 * tol)) & (abs(L - tl[0]) <= 1.2 * tol)).astype(uint8), None
|
|
327
|
+
# Chromatic target: same hue (angle in the a/b plane) and enough chroma, ANY lightness, so shading,
|
|
328
|
+
# shadows and highlights of the same color match. cos(angle) test avoids a per-pixel atan2.
|
|
329
|
+
cosTol = cos(radians(min(tol, 89.0))) # type: float
|
|
330
|
+
dot = ab[..., 0] * tl[1] + ab[..., 1] * tl[2] # type: ndarray
|
|
331
|
+
bright = L >= 3.0 # type: ndarray
|
|
332
|
+
weak = (chroma >= max(8.0, CHROMA_FACTOR * ct)) & (dot >= abChroma * (ct * cosTol)) & bright # type: ndarray
|
|
333
|
+
strong = weak & (chroma >= STRONG_FACTOR * ct) & (dot >= abChroma * (ct * cos(radians(min(tol, 89.0) / 2.0))))
|
|
334
|
+
return weak.astype(uint8), strong.astype(uint8)
|
|
335
|
+
|
|
336
|
+
def _regions(self, mask, step, strong=None):
|
|
337
|
+
"""
|
|
338
|
+
Regions of a mask: fragments closer than groupDistance are merged (text line, noisy object = one box) and
|
|
339
|
+
scattered specks (noise, isolated look-alike pixels) are dropped. A pixel with at least 2 matching
|
|
340
|
+
neighbors is "core" (true for every pixel of a 1 px stroke or line, rarely for noise); only matches
|
|
341
|
+
within groupDistance of a core pixel are kept.
|
|
342
|
+
:param mask: (ndarray) uint8 0/1, all matches.
|
|
343
|
+
:param step: (int) Sub-sampling step of the frame (coordinates are scaled back).
|
|
344
|
+
:param strong: (ndarray | None) Strong matches: a group (built from all matches, so anti-aliased pixels
|
|
345
|
+
bridge the letters of a text line) is kept only if it holds >= 3 strong pixels (hysteresis), so
|
|
346
|
+
borderline look-alike pixels (e.g. noisy brown for a red target) never form regions on their own.
|
|
347
|
+
:return: (tuple[list, ndarray]) Regions (pixels, [x, y, w, h], [cx, cy]) in frame pixels, cleaned mask.
|
|
348
|
+
"""
|
|
349
|
+
out = [] # type: list
|
|
350
|
+
count = int(mask.sum()) # type: int
|
|
351
|
+
if not cv2 and not scipy: # NumPy only: one box per target color
|
|
352
|
+
if count * step * step >= self.minArea:
|
|
353
|
+
ys, xs = where(mask > 0)
|
|
354
|
+
x0, y0 = int(xs.min()), int(ys.min())
|
|
355
|
+
out.append((count, [x0 * step, y0 * step, (int(xs.max()) - x0 + 1) * step,
|
|
356
|
+
(int(ys.max()) - y0 + 1) * step],
|
|
357
|
+
[float(xs.mean()) * step, float(ys.mean()) * step]))
|
|
358
|
+
return out, mask
|
|
359
|
+
core = mask & (_neighbours(mask) >= 3).astype(uint8) # type: ndarray # self + 2 neighbors
|
|
360
|
+
gap = max(1, int(round(self.groupDistance / float(step)))) # type: int
|
|
361
|
+
grouped = _grow(core, gap) # type: ndarray
|
|
362
|
+
n, labels = _components(grouped)
|
|
363
|
+
keep = bincount(labels[core > 0], minlength=n) > 0 # type: ndarray # groups with core pixels
|
|
364
|
+
if strong is not None: # Hysteresis: a group must contain strong evidence, not only borderline pixels.
|
|
365
|
+
keep &= bincount(labels[(strong & mask) > 0], minlength=n) >= 3
|
|
366
|
+
keep[0] = False
|
|
367
|
+
mask = mask & keep[labels].astype(uint8) # type: ndarray # Specks / look-alike-only groups are dropped.
|
|
368
|
+
ys, xs = where(mask > 0)
|
|
369
|
+
ls = labels[ys, xs] # type: ndarray # Group of every matching pixel (dilation pixels are not counted).
|
|
370
|
+
pixels = bincount(ls, minlength=n) # type: ndarray
|
|
371
|
+
sx, sy = bincount(ls, weights=xs, minlength=n), bincount(ls, weights=ys, minlength=n)
|
|
372
|
+
x0, y0 = full(n, mask.shape[1], int32), full(n, mask.shape[0], int32)
|
|
373
|
+
x1, y1 = full(n, -1, int32), full(n, -1, int32)
|
|
374
|
+
minimum.at(x0, ls, xs)
|
|
375
|
+
minimum.at(y0, ls, ys)
|
|
376
|
+
maximum.at(x1, ls, xs)
|
|
377
|
+
maximum.at(y1, ls, ys)
|
|
378
|
+
for k in range(1, n):
|
|
379
|
+
area = int(pixels[k]) * step * step # type: int
|
|
380
|
+
if area >= self.minArea:
|
|
381
|
+
out.append((area, [int(x0[k]) * step, int(y0[k]) * step, int(x1[k] - x0[k] + 1) * step,
|
|
382
|
+
int(y1[k] - y0[k] + 1) * step],
|
|
383
|
+
[float(sx[k] / pixels[k]) * step, float(sy[k] / pixels[k]) * step]))
|
|
384
|
+
return out, mask
|
|
385
|
+
|
|
386
|
+
def analyze(self, arr):
|
|
387
|
+
"""
|
|
388
|
+
:param arr: (numpy.ndarray) (H, W, 3) uint8 RGB frame.
|
|
389
|
+
:return: (dict) width, height, scale, detections, coverage, palette, labelMask ((H, W) uint8 array of
|
|
390
|
+
target index + 1, or None), timeMs.
|
|
391
|
+
"""
|
|
392
|
+
result = {'width': 0, 'height': 0, 'scale': 1, 'detections': [], 'coverage': [], 'palette': [],
|
|
393
|
+
'labelMask': None, 'timeMs': 0.0} # type: dict
|
|
394
|
+
if not np or arr is None or arr.size == 0:
|
|
395
|
+
return result
|
|
396
|
+
started = time() # type: float
|
|
397
|
+
h0, w0 = arr.shape[:2]
|
|
398
|
+
step = max(1, int(ceil(sqrt(w0 * h0 / float(self.maxPixels))))) # type: int
|
|
399
|
+
if step > 1:
|
|
400
|
+
arr = arr[::step, ::step] # type: ndarray
|
|
401
|
+
h, w = arr.shape[:2]
|
|
402
|
+
label = zeros((h, w), uint8) # type: ndarray
|
|
403
|
+
if self.targets:
|
|
404
|
+
lab = labArray(arr) # type: ndarray
|
|
405
|
+
chroma = npsqrt(lab[..., 1] * lab[..., 1] + lab[..., 2] * lab[..., 2]) # type: ndarray
|
|
406
|
+
if cv2:
|
|
407
|
+
ab = blur(ascontiguousarray(lab[..., 1:3]), (3, 3)) # type: ndarray
|
|
408
|
+
elif scipy:
|
|
409
|
+
ab = _uniform(lab[..., 1:3], (3, 3, 1), mode='nearest') # type: ndarray
|
|
410
|
+
else:
|
|
411
|
+
ab = lab[..., 1:3] # type: ndarray
|
|
412
|
+
abChroma = npsqrt(ab[..., 0] * ab[..., 0] + ab[..., 1] * ab[..., 1]) # type: ndarray
|
|
413
|
+
for i, target in enumerate(self.targets[:254]):
|
|
414
|
+
weak, strong = self._match(lab, chroma, ab, abChroma, target)
|
|
415
|
+
regions, mask = self._regions(weak, step, strong)
|
|
416
|
+
count = int(mask.sum()) # type: int
|
|
417
|
+
code = hexName(target) # type: str
|
|
418
|
+
name = nearestColorName(target)[0] # type: str
|
|
419
|
+
result['coverage'].append({'target': i, 'color': code, 'name': name,
|
|
420
|
+
'fraction': round(count / float(w * h), 5)})
|
|
421
|
+
if not count:
|
|
422
|
+
continue
|
|
423
|
+
label[(mask > 0) & (label == 0)] = i + 1
|
|
424
|
+
regions.sort(key=lambda rg: -rg[0])
|
|
425
|
+
for area, rect, centre in regions[:self.maxRegions]:
|
|
426
|
+
result['detections'].append({'target': i, 'color': code, 'name': name, 'rect': rect,
|
|
427
|
+
'center': [round(centre[0], 1), round(centre[1], 1)],
|
|
428
|
+
'pixels': int(area)})
|
|
429
|
+
if self.returnMask:
|
|
430
|
+
result['labelMask'] = ascontiguousarray(label)
|
|
431
|
+
if self.paletteSize:
|
|
432
|
+
result['palette'] = self._palette(arr)
|
|
433
|
+
result.update(width=w0, height=h0, scale=step, timeMs=round((time() - started) * 1000.0, 2))
|
|
434
|
+
return result
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
"""
|
|
3
|
+
Entry point for the ColorDetector demo.
|
|
4
|
+
"""
|
|
5
|
+
from os.path import dirname
|
|
6
|
+
from sys import path
|
|
7
|
+
|
|
8
|
+
if not __package__ and dirname(__file__) not in path:
|
|
9
|
+
path.append(dirname(__file__))
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
from .mainwindow import MainWindow
|
|
13
|
+
except:
|
|
14
|
+
from mainwindow import MainWindow
|
|
15
|
+
|
|
16
|
+
if __name__ == '__main__':
|
|
17
|
+
from ManyQt.QtWidgets import QApplication
|
|
18
|
+
from sys import argv, exit
|
|
19
|
+
|
|
20
|
+
a = QApplication(argv) # type: QApplication
|
|
21
|
+
w = MainWindow() # type: MainWindow
|
|
22
|
+
w.show()
|
|
23
|
+
exit(a.exec_())
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
"""
|
|
3
|
+
Demo window for ColorDetector: click the scene to add target colors, tune the tolerance, see regions, mask,
|
|
4
|
+
palette and color information.
|
|
5
|
+
"""
|
|
6
|
+
from ManyQt.QtWidgets import QMainWindow, QVBoxLayout, QHBoxLayout, QWidget, QLabel, QSlider, QPushButton, QComboBox
|
|
7
|
+
from ManyQt.QtGui import QImage, QPainter, QColor, QFont, QPen, QLinearGradient
|
|
8
|
+
from ManyQt.QtCore import Qt, QRect, QRectF, QPointF, pyqtSignal, pyqtSlot
|
|
9
|
+
from os.path import dirname
|
|
10
|
+
from sys import path
|
|
11
|
+
|
|
12
|
+
if not __package__ and dirname(__file__) not in path:
|
|
13
|
+
path.append(dirname(__file__))
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
from .colordetector import ColorDetector, colorInfo, maskToOverlay
|
|
17
|
+
except:
|
|
18
|
+
from colordetector import ColorDetector, colorInfo, maskToOverlay
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def makeScene():
|
|
22
|
+
"""
|
|
23
|
+
:return: (QImage) A UI-like scene with several reds, blues and greens.
|
|
24
|
+
"""
|
|
25
|
+
img = QImage(640, 400, QImage.Format_RGB32) # type: QImage
|
|
26
|
+
g = QLinearGradient(0, 0, 640, 400) # type: QLinearGradient
|
|
27
|
+
g.setColorAt(0, QColor('#1B2A3A'))
|
|
28
|
+
g.setColorAt(1, QColor('#2E4A2E'))
|
|
29
|
+
p = QPainter(img) # type: QPainter
|
|
30
|
+
p.fillRect(img.rect(), g)
|
|
31
|
+
for rect, color in ((QRect(40, 40, 90, 60), '#E01020'), (QRect(200, 70, 40, 40), '#F03030'),
|
|
32
|
+
(QRect(470, 300, 120, 50), '#C81414'), (QRect(330, 180, 110, 80), '#1E5AD7'),
|
|
33
|
+
(QRect(60, 250, 70, 110), '#2AA845'), (QRect(520, 60, 60, 60), '#F2C12E')):
|
|
34
|
+
p.fillRect(rect, QColor(color))
|
|
35
|
+
p.setPen(QColor('#FFFFFF'))
|
|
36
|
+
p.setFont(QFont('Sans Serif', 14, QFont.Bold))
|
|
37
|
+
p.drawText(QRect(160, 330, 300, 40), Qt.AlignCenter, u'Click a color to detect it')
|
|
38
|
+
p.end()
|
|
39
|
+
return img
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class SceneView(QWidget):
|
|
43
|
+
"""
|
|
44
|
+
SceneView class: the scene with the detection result painted over it.
|
|
45
|
+
"""
|
|
46
|
+
colorClicked = pyqtSignal(QColor) # type: pyqtSignal
|
|
47
|
+
|
|
48
|
+
def __init__(self, *args, **kwargs):
|
|
49
|
+
"""
|
|
50
|
+
:param args: any
|
|
51
|
+
:param kwargs: any
|
|
52
|
+
"""
|
|
53
|
+
super(SceneView, self).__init__(*args, **kwargs)
|
|
54
|
+
self.__m_image = QImage() # type: QImage
|
|
55
|
+
self.__m_result = {} # type: dict
|
|
56
|
+
self.__m_overlay = QImage() # type: QImage
|
|
57
|
+
self.setMinimumSize(320, 200)
|
|
58
|
+
|
|
59
|
+
def setScene(self, image, result, overlay):
|
|
60
|
+
"""
|
|
61
|
+
:param image: QImage
|
|
62
|
+
:param result: dict
|
|
63
|
+
:param overlay: QImage
|
|
64
|
+
:return:
|
|
65
|
+
"""
|
|
66
|
+
self.__m_image, self.__m_result, self.__m_overlay = image, result, overlay
|
|
67
|
+
self.update()
|
|
68
|
+
|
|
69
|
+
def _target(self):
|
|
70
|
+
"""
|
|
71
|
+
:return: (QRectF) Where the scene is drawn (aspect ratio kept).
|
|
72
|
+
"""
|
|
73
|
+
if self.__m_image.isNull():
|
|
74
|
+
return QRectF()
|
|
75
|
+
s = min(self.width() / float(self.__m_image.width()), self.height() / float(self.__m_image.height()))
|
|
76
|
+
w, h = self.__m_image.width() * s, self.__m_image.height() * s # type: float, float
|
|
77
|
+
return QRectF((self.width() - w) / 2.0, (self.height() - h) / 2.0, w, h)
|
|
78
|
+
|
|
79
|
+
def mousePressEvent(self, e):
|
|
80
|
+
"""
|
|
81
|
+
:param e: QMouseEvent
|
|
82
|
+
:return:
|
|
83
|
+
"""
|
|
84
|
+
t = self._target() # type: QRectF
|
|
85
|
+
if t.contains(QPointF(e.pos())):
|
|
86
|
+
x = int((e.pos().x() - t.left()) * self.__m_image.width() / t.width()) # type: int
|
|
87
|
+
y = int((e.pos().y() - t.top()) * self.__m_image.height() / t.height()) # type: int
|
|
88
|
+
self.colorClicked.emit(QColor(self.__m_image.pixel(x, y)))
|
|
89
|
+
|
|
90
|
+
def paintEvent(self, event):
|
|
91
|
+
"""
|
|
92
|
+
:param event: QPaintEvent
|
|
93
|
+
:return:
|
|
94
|
+
"""
|
|
95
|
+
p = QPainter(self) # type: QPainter
|
|
96
|
+
p.fillRect(self.rect(), QColor('#15161C'))
|
|
97
|
+
t = self._target() # type: QRectF
|
|
98
|
+
if not t.isNull():
|
|
99
|
+
p.drawImage(t, self.__m_image)
|
|
100
|
+
if not self.__m_overlay.isNull():
|
|
101
|
+
p.drawImage(t, self.__m_overlay)
|
|
102
|
+
s = t.width() / float(self.__m_image.width()) # type: float
|
|
103
|
+
p.setRenderHint(QPainter.Antialiasing, True)
|
|
104
|
+
for d in self.__m_result.get('detections', []):
|
|
105
|
+
x, y, w, h = d['rect']
|
|
106
|
+
p.setPen(QPen(QColor(d['color']), 2))
|
|
107
|
+
p.drawRect(QRectF(t.left() + x * s, t.top() + y * s, w * s, h * s))
|
|
108
|
+
p.setPen(Qt.white)
|
|
109
|
+
p.drawText(QPointF(t.left() + x * s, t.top() + y * s - 4), d['name'])
|
|
110
|
+
p.end()
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class MainWindow(QMainWindow):
|
|
114
|
+
"""
|
|
115
|
+
MainWindow class.
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
def __init__(self, *args, **kwargs):
|
|
119
|
+
"""
|
|
120
|
+
:param args: any
|
|
121
|
+
:param kwargs: any
|
|
122
|
+
"""
|
|
123
|
+
super(MainWindow, self).__init__(*args, **kwargs)
|
|
124
|
+
self.setWindowTitle(self.tr(u'ColorDetector demo'))
|
|
125
|
+
self.resize(980, 640)
|
|
126
|
+
self.__m_scene = makeScene() # type: QImage
|
|
127
|
+
self.__m_detector = ColorDetector(targetColors=['#E01020'], tolerance=20,
|
|
128
|
+
targetsChanged=self.analyse) # type: ColorDetector
|
|
129
|
+
self.__m_view = SceneView() # type: SceneView
|
|
130
|
+
self.__m_view.colorClicked.connect(self.onColorClicked)
|
|
131
|
+
self.__m_info = QLabel() # type: QLabel
|
|
132
|
+
self.__m_info.setWordWrap(True)
|
|
133
|
+
self.__m_mode = QComboBox() # type: QComboBox
|
|
134
|
+
self.__m_mode.addItems(['isolate', 'tint', 'none'])
|
|
135
|
+
self.__m_mode.currentIndexChanged.connect(lambda *a: self.analyse())
|
|
136
|
+
tol = QSlider(Qt.Horizontal) # type: QSlider
|
|
137
|
+
tol.setRange(1, 60)
|
|
138
|
+
tol.setValue(20)
|
|
139
|
+
tol.valueChanged.connect(lambda v: (self.__m_detector.setTolerance(v), self.analyse()))
|
|
140
|
+
clear = QPushButton(self.tr(u'Clear targets')) # type: QPushButton
|
|
141
|
+
clear.clicked.connect(self.__m_detector.clearTargetColors)
|
|
142
|
+
bar = QHBoxLayout() # type: QHBoxLayout
|
|
143
|
+
bar.addWidget(QLabel(self.tr(u'Tolerance (delta E)')))
|
|
144
|
+
bar.addWidget(tol)
|
|
145
|
+
bar.addWidget(QLabel(self.tr(u'Mask')))
|
|
146
|
+
bar.addWidget(self.__m_mode)
|
|
147
|
+
bar.addWidget(clear)
|
|
148
|
+
wdg = QWidget() # type: QWidget
|
|
149
|
+
layout = QVBoxLayout(wdg) # type: QVBoxLayout
|
|
150
|
+
layout.addLayout(bar)
|
|
151
|
+
layout.addWidget(self.__m_view, 1)
|
|
152
|
+
layout.addWidget(self.__m_info)
|
|
153
|
+
self.setCentralWidget(wdg)
|
|
154
|
+
self.analyse()
|
|
155
|
+
|
|
156
|
+
@pyqtSlot()
|
|
157
|
+
def analyse(self):
|
|
158
|
+
"""
|
|
159
|
+
:return:
|
|
160
|
+
"""
|
|
161
|
+
r = self.__m_detector.analyze(self.__m_scene) # type: dict
|
|
162
|
+
mode = self.__m_mode.currentText() # type: str
|
|
163
|
+
overlay = maskToOverlay(r.get('labelMask'), self.__m_detector.getTargetColors(), 0.6, mode) \
|
|
164
|
+
if mode != 'none' else QImage() # type: QImage
|
|
165
|
+
self.__m_view.setScene(self.__m_scene, r, overlay)
|
|
166
|
+
self.__m_info.setText(self.tr(u'Targets: {} | regions: {} | palette: {} | {:.1f} ms').format(
|
|
167
|
+
', '.join(c['name'] for c in r['coverage']) or '-', len(r['detections']),
|
|
168
|
+
', '.join('{} {:.0f}%'.format(p['name'], 100 * p['fraction']) for p in r['palette']), r['timeMs']))
|
|
169
|
+
|
|
170
|
+
@pyqtSlot(QColor)
|
|
171
|
+
@pyqtSlot(Qt.GlobalColor)
|
|
172
|
+
@pyqtSlot(int)
|
|
173
|
+
@pyqtSlot(str)
|
|
174
|
+
@pyqtSlot('QString')
|
|
175
|
+
def onColorClicked(self, color):
|
|
176
|
+
"""
|
|
177
|
+
:param color: QColor | Qt.GlobalColor | int | str | QString
|
|
178
|
+
:return:
|
|
179
|
+
"""
|
|
180
|
+
info = colorInfo(color) # type: dict
|
|
181
|
+
self.statusBar().showMessage('{hex} {name} RGB {rgb} HSV {hsv} Lab {lab}'.format(**info))
|
|
182
|
+
self.__m_detector.addTargetColor(color)
|