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,570 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
ColorDetector: real-time color detection and color analysis for QImage frames.
|
|
4
|
+
Features:
|
|
5
|
+
- Target colors: find every region of one of the target colors; regions (rect, center, pixel count) and
|
|
6
|
+
coverage per target. Two match modes:
|
|
7
|
+
* Similar (default): same hue (angle in Lab's a/b plane, tolerance in degrees) with enough chroma, at
|
|
8
|
+
ANY lightness - shading, shadows, highlights and anti-aliased text of the same color all match.
|
|
9
|
+
Gray / white / black targets match low-chroma pixels of close lightness instead.
|
|
10
|
+
* Exact: CIE76 delta E distance (tolerance in delta E), for checking one precise UI color.
|
|
11
|
+
Thin strokes (1 px text, lines) are kept; fragments closer than groupDistance are merged into one region.
|
|
12
|
+
- Highlight mask: label image (0 = no match, k = target k - 1) and helpers to turn it into an overlay that
|
|
13
|
+
either isolates the matches (dims everything else) or tints them with a contrasting color.
|
|
14
|
+
- Dominant palette: stable (histogram based, no random k-means flicker) list of the main colors.
|
|
15
|
+
- Color information: HEX, RGB, HSV, HSL, CMYK, CIE Lab and the nearest CSS / SVG color name.
|
|
16
|
+
- Contrast: WCAG 2.x contrast ratio and AA / AAA rating between two colors.
|
|
17
|
+
- Color codes: formatColor() in 13 formats (HEX, CSS rgb / hsl / lab, CMYK, QColor...) and sampleColor()
|
|
18
|
+
(averaged N x N pick) for color pickers.
|
|
19
|
+
Results are plain Python data (JSON safe) except 'labelMask' (QImage), so they can be sent over a network.
|
|
20
|
+
Requires NumPy; OpenCV is optional (connected components = one rect per region instead of one per target).
|
|
21
|
+
"""
|
|
22
|
+
from ManyQt.QtCore import QObject, QElapsedTimer, pyqtSignal, pyqtSlot, pyqtProperty, Q_ENUMS, qVersion, Qt
|
|
23
|
+
from ManyQt.QtGui import QImage, QColor
|
|
24
|
+
from math import sqrt, cos, radians
|
|
25
|
+
from os.path import dirname
|
|
26
|
+
from enum import IntEnum
|
|
27
|
+
from sys import path
|
|
28
|
+
|
|
29
|
+
if not __package__ and dirname(__file__) not in path:
|
|
30
|
+
path.append(dirname(__file__))
|
|
31
|
+
|
|
32
|
+
try: # The analysis itself is Qt-free (core.py), shared with the headless server.
|
|
33
|
+
from .core import ColorAnalyzer, rgbToLab, deltaE, labArray, clamp, CHROMATIC_MIN, STRONG_FACTOR, CHROMA_FACTOR, \
|
|
34
|
+
nearestColorName as _nearestName, _linear, np, cv2
|
|
35
|
+
except:
|
|
36
|
+
from core import ColorAnalyzer, rgbToLab, deltaE, labArray, clamp, CHROMATIC_MIN, STRONG_FACTOR, CHROMA_FACTOR, \
|
|
37
|
+
nearestColorName as _nearestName, _linear, np, cv2
|
|
38
|
+
if np:
|
|
39
|
+
from numpy import frombuffer, ascontiguousarray, uint8, ndarray, zeros
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def toColor(color):
|
|
43
|
+
"""
|
|
44
|
+
:param color: QColor | Qt.GlobalColor | int | str | unicode | QString
|
|
45
|
+
:return: QColor
|
|
46
|
+
"""
|
|
47
|
+
return QColor(color)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def colorToLab(color):
|
|
51
|
+
"""
|
|
52
|
+
:param color: QColor | Qt.GlobalColor | int | str | unicode | QString
|
|
53
|
+
:return: tuple[float, float, float]
|
|
54
|
+
"""
|
|
55
|
+
c = toColor(color) # type: QColor
|
|
56
|
+
return rgbToLab(c.red(), c.green(), c.blue())
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def nearestColorName(color):
|
|
60
|
+
"""
|
|
61
|
+
:param color: QColor | Qt.GlobalColor | int | str | unicode | QString
|
|
62
|
+
:return: (tuple[str, float]) Nearest CSS / SVG color name and its delta E.
|
|
63
|
+
"""
|
|
64
|
+
c = toColor(color) # type: QColor
|
|
65
|
+
return _nearestName((c.red(), c.green(), c.blue()))
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def relativeLuminance(color):
|
|
69
|
+
"""
|
|
70
|
+
:param color: QColor | Qt.GlobalColor | int | str | unicode | QString
|
|
71
|
+
:return: (float | int) WCAG relative luminance 0 .. 1.
|
|
72
|
+
"""
|
|
73
|
+
c = toColor(color) # type: QColor
|
|
74
|
+
return 0.2126 * _linear(c.redF()) + 0.7152 * _linear(c.greenF()) + 0.0722 * _linear(c.blueF())
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def contrastRatio(color1, color2):
|
|
78
|
+
"""
|
|
79
|
+
:param color1: QColor | Qt.GlobalColor | int | str | unicode | QString
|
|
80
|
+
:param color2: QColor | Qt.GlobalColor | int | str | unicode | QString
|
|
81
|
+
:return: (float | int) WCAG contrast ratio 1 .. 21.
|
|
82
|
+
"""
|
|
83
|
+
l1, l2 = relativeLuminance(color1), relativeLuminance(color2)
|
|
84
|
+
return (max(l1, l2) + 0.05) / (min(l1, l2) + 0.05)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def wcagRating(ratio):
|
|
88
|
+
"""
|
|
89
|
+
:param ratio: float
|
|
90
|
+
:return: (str | unicode | QString) 'AAA', 'AA', 'AA large' or 'Fail' for normal text.
|
|
91
|
+
"""
|
|
92
|
+
return 'AAA' if ratio >= 7.0 else 'AA' if ratio >= 4.5 else 'AA large' if ratio >= 3.0 else 'Fail'
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def colorInfo(color):
|
|
96
|
+
"""
|
|
97
|
+
Everything about one color.
|
|
98
|
+
:param color: QColor | Qt.GlobalColor | int | str | unicode | QString
|
|
99
|
+
:return: (dict) hex, rgb, hsv, hsl, cmyk, lab, name, nameDeltaE.
|
|
100
|
+
"""
|
|
101
|
+
c = toColor(color) # type: QColor
|
|
102
|
+
h, s, v, _ = c.getHsvF()
|
|
103
|
+
hh, ss, ll, _ = c.getHslF()
|
|
104
|
+
cc, mm, yy, kk, _ = c.getCmykF()
|
|
105
|
+
name, de = nearestColorName(c)
|
|
106
|
+
return {
|
|
107
|
+
'hex': u'{}'.format(c.name()).upper(),
|
|
108
|
+
'rgb': [c.red(), c.green(), c.blue()],
|
|
109
|
+
'hsv': [int(round(max(h, 0.0) * 360)), int(round(s * 100)), int(round(v * 100))],
|
|
110
|
+
'hsl': [int(round(max(hh, 0.0) * 360)), int(round(ss * 100)), int(round(ll * 100))],
|
|
111
|
+
'cmyk': [int(round(x * 100)) for x in (cc, mm, yy, kk)],
|
|
112
|
+
'lab': [round(x, 1) for x in colorToLab(c)],
|
|
113
|
+
'name': name,
|
|
114
|
+
'nameDeltaE': round(de, 1),
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
COLOR_FORMATS = [
|
|
119
|
+
('hex', 'HEX'), ('hexLower', 'hex (lower case)'), ('hexBare', 'HEX without #'), ('rgb', 'CSS rgb()'),
|
|
120
|
+
('rgbTuple', 'RGB numbers'), ('rgbFloat', 'RGB 0..1'), ('hsl', 'CSS hsl()'), ('hsv', 'HSV'),
|
|
121
|
+
('cmyk', 'CMYK'), ('lab', 'CSS lab()'), ('qcolor', 'QColor(...)'), ('int', '0xRRGGBB'), ('name', 'Nearest name'),
|
|
122
|
+
] # type: list[tuple[str, str]] # (key, label), in menu order.
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def formatColor(color, fmt='hex'):
|
|
126
|
+
"""
|
|
127
|
+
Color code in a given format, ready for the clipboard.
|
|
128
|
+
:param color: QColor | Qt.GlobalColor | int | str | unicode | QString
|
|
129
|
+
:param fmt: (str | unicode | QString) One of the COLOR_FORMATS keys.
|
|
130
|
+
:return: str | unicode | QString
|
|
131
|
+
"""
|
|
132
|
+
c = toColor(color) # type: QColor
|
|
133
|
+
r, g, b = c.red(), c.green(), c.blue() # type: int, int, int
|
|
134
|
+
fmt = u'{}'.format(fmt) # type: str
|
|
135
|
+
if fmt == 'hexLower':
|
|
136
|
+
return '#{:02x}{:02x}{:02x}'.format(r, g, b)
|
|
137
|
+
if fmt == 'hexBare':
|
|
138
|
+
return '{:02X}{:02X}{:02X}'.format(r, g, b)
|
|
139
|
+
if fmt == 'rgb':
|
|
140
|
+
return 'rgb({}, {}, {})'.format(r, g, b)
|
|
141
|
+
if fmt == 'rgbTuple':
|
|
142
|
+
return '{}, {}, {}'.format(r, g, b)
|
|
143
|
+
if fmt == 'rgbFloat':
|
|
144
|
+
return '{:.3f}, {:.3f}, {:.3f}'.format(r / 255.0, g / 255.0, b / 255.0)
|
|
145
|
+
if fmt in ('hsl', 'hsv', 'cmyk', 'lab', 'name'):
|
|
146
|
+
info = colorInfo(c) # type: dict
|
|
147
|
+
if fmt == 'hsl':
|
|
148
|
+
return 'hsl({}, {}%, {}%)'.format(*info['hsl'])
|
|
149
|
+
if fmt == 'hsv':
|
|
150
|
+
return 'hsv({}, {}%, {}%)'.format(*info['hsv'])
|
|
151
|
+
if fmt == 'cmyk':
|
|
152
|
+
return 'cmyk({}%, {}%, {}%, {}%)'.format(*info['cmyk'])
|
|
153
|
+
if fmt == 'lab':
|
|
154
|
+
return 'lab({}% {} {})'.format(*info['lab'])
|
|
155
|
+
return info['name']
|
|
156
|
+
if fmt == 'qcolor':
|
|
157
|
+
return 'QColor({}, {}, {})'.format(r, g, b)
|
|
158
|
+
if fmt == 'int':
|
|
159
|
+
return '0x{:02X}{:02X}{:02X}'.format(r, g, b)
|
|
160
|
+
return '#{:02X}{:02X}{:02X}'.format(r, g, b)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def sampleColor(image, x, y, size=1):
|
|
164
|
+
"""
|
|
165
|
+
Average color of a size x size square centred on a pixel (clipped to the image): steadier than one pixel on
|
|
166
|
+
anti-aliased text, gradients and noisy textures. Pure Qt, no NumPy needed.
|
|
167
|
+
:param image: QImage
|
|
168
|
+
:param x: int
|
|
169
|
+
:param y: int
|
|
170
|
+
:param size: (int) 1, 3, 5, 9...
|
|
171
|
+
:return: (QColor) Invalid when the pixel is outside the image.
|
|
172
|
+
"""
|
|
173
|
+
if image is None or image.isNull() or not (0 <= x < image.width() and 0 <= y < image.height()):
|
|
174
|
+
return QColor()
|
|
175
|
+
half = max(0, int(size) // 2) # type: int
|
|
176
|
+
x0, x1 = max(0, x - half), min(image.width() - 1, x + half) # type: int, int
|
|
177
|
+
y0, y1 = max(0, y - half), min(image.height() - 1, y + half) # type: int, int
|
|
178
|
+
tr = tg = tb = n = 0 # type: int
|
|
179
|
+
for yy in range(y0, y1 + 1):
|
|
180
|
+
for xx in range(x0, x1 + 1):
|
|
181
|
+
c = QColor(image.pixel(xx, yy)) # type: QColor
|
|
182
|
+
tr, tg, tb, n = tr + c.red(), tg + c.green(), tb + c.blue(), n + 1
|
|
183
|
+
return QColor(int(round(tr / float(n))), int(round(tg / float(n))), int(round(tb / float(n))))
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def refinePick(image, x, y, radius=3):
|
|
187
|
+
"""
|
|
188
|
+
Robust target color for a click: on thin text or icon edges the clicked pixel is often an anti-aliased blend
|
|
189
|
+
(e.g. pale red on white), and in noisy images single pixels are off. Averages the same-family pixels of a
|
|
190
|
+
(2r+1)^2 neighborhood that have at least 60 % of the strongest Chroma there: pale edges are left out, noise
|
|
191
|
+
averages away, and the result is the object's typical color. Gray / white / black clicks average similar pixels.
|
|
192
|
+
:param image: QImage
|
|
193
|
+
:param x: int
|
|
194
|
+
:param y: int
|
|
195
|
+
:param radius: int
|
|
196
|
+
:return: (QColor) Invalid when (x, y) is outside the image.
|
|
197
|
+
"""
|
|
198
|
+
if image is None or image.isNull() or not (0 <= x < image.width() and 0 <= y < image.height()):
|
|
199
|
+
return QColor()
|
|
200
|
+
c0 = QColor(image.pixel(x, y)) # type: QColor
|
|
201
|
+
l0 = colorToLab(c0)
|
|
202
|
+
c0Chroma = sqrt(l0[1] ** 2 + l0[2] ** 2) # type: float
|
|
203
|
+
picks = [] # type: list # (chroma, QColor)
|
|
204
|
+
for yy in range(max(0, y - radius), min(image.height(), y + radius + 1)):
|
|
205
|
+
for xx in range(max(0, x - radius), min(image.width(), x + radius + 1)):
|
|
206
|
+
c = QColor(image.pixel(xx, yy)) # type: QColor
|
|
207
|
+
lab = colorToLab(c)
|
|
208
|
+
chroma = sqrt(lab[1] ** 2 + lab[2] ** 2) # type: float
|
|
209
|
+
if c0Chroma < 6.0: # Really gray / white / black: keep similar pixels only.
|
|
210
|
+
if deltaE(lab, l0) <= 12.0:
|
|
211
|
+
picks.append((0.0, c))
|
|
212
|
+
elif lab[1] * l0[1] + lab[2] * l0[2] >= chroma * c0Chroma * cos(radians(45.0)):
|
|
213
|
+
# Same color family (blending with white / black shifts the Lab hue: wide window).
|
|
214
|
+
picks.append((chroma, c))
|
|
215
|
+
if not picks:
|
|
216
|
+
return c0
|
|
217
|
+
strongest = max(p[0] for p in picks) # type: float
|
|
218
|
+
best = [c for chroma, c in picks if chroma >= 0.6 * strongest] or [c0] # type: list
|
|
219
|
+
n = float(len(best)) # type: float
|
|
220
|
+
return QColor(int(round(sum(c.red() for c in best) / n)), int(round(sum(c.green() for c in best) / n)),
|
|
221
|
+
int(round(sum(c.blue() for c in best) / n)))
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def complementary(color):
|
|
225
|
+
"""
|
|
226
|
+
:param color: QColor | Qt.GlobalColor | int | str | unicode | QString
|
|
227
|
+
:return: (QColor) Vivid complementary color, used to tint matches so they stand out.
|
|
228
|
+
"""
|
|
229
|
+
h = toColor(color).hsvHue() # type: int
|
|
230
|
+
return QColor.fromHsv(((h if h >= 0 else 0) + 180) % 360, 255, 255)
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def qimageToArray(image):
|
|
234
|
+
"""
|
|
235
|
+
:param image: QImage
|
|
236
|
+
:return: (ndarray) Owned (H, W, 3) uint8 RGB array.
|
|
237
|
+
"""
|
|
238
|
+
img = image.convertToFormat(QImage.Format_RGB888) # type: QImage
|
|
239
|
+
w, h, bpl = img.width(), img.height(), img.bytesPerLine() # type: int, int, int
|
|
240
|
+
nbytes = img.sizeInBytes() if hasattr(img, 'sizeInBytes') else img.byteCount() # type: int
|
|
241
|
+
ptr = img.constBits()
|
|
242
|
+
if hasattr(ptr, 'setsize'):
|
|
243
|
+
ptr.setsize(nbytes)
|
|
244
|
+
return frombuffer(ptr, uint8, count=nbytes).reshape(h, bpl)[:, :w * 3].reshape(h, w, 3).copy()
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def grayToArray(image):
|
|
248
|
+
"""
|
|
249
|
+
:param image: (QImage) Format_Grayscale8 label image.
|
|
250
|
+
:return: (ndarray) Owned (H, W) uint8 array.
|
|
251
|
+
"""
|
|
252
|
+
img = image.convertToFormat(QImage.Format_Grayscale8) # type: QImage
|
|
253
|
+
w, h, bpl = img.width(), img.height(), img.bytesPerLine() # type: int, int, int
|
|
254
|
+
nbytes = img.sizeInBytes() if hasattr(img, 'sizeInBytes') else img.byteCount() # type: int
|
|
255
|
+
ptr = img.constBits()
|
|
256
|
+
if hasattr(ptr, 'setsize'):
|
|
257
|
+
ptr.setsize(nbytes)
|
|
258
|
+
return frombuffer(ptr, uint8, count=nbytes).reshape(h, bpl)[:, :w].copy()
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def maskToOverlay(labelMask, colors, alpha=0.6, mode='isolate'):
|
|
262
|
+
"""
|
|
263
|
+
Turn a label mask into an ARGB overlay of the mask size (draw it scaled over the analysed frame).
|
|
264
|
+
:param labelMask: (QImage) Grayscale8, 0 = no match, k = target k - 1.
|
|
265
|
+
:param colors: (list[QColor | str]) Target colors, in target order.
|
|
266
|
+
:param alpha: (float | int) 0 .. 1 overlay strength.
|
|
267
|
+
:param mode: (str | unicode | QString) 'isolate' dims everything that does not match, 'tint' paints the
|
|
268
|
+
matches with a vivid complementary color.
|
|
269
|
+
:return: QImage
|
|
270
|
+
"""
|
|
271
|
+
if labelMask is None or labelMask.isNull() or not np:
|
|
272
|
+
return QImage()
|
|
273
|
+
lab = grayToArray(labelMask) # type: ndarray
|
|
274
|
+
h, w = lab.shape
|
|
275
|
+
lut = zeros((256, 4), uint8) # type: ndarray # B, G, R, A = Format_ARGB32 memory order (little-endian).
|
|
276
|
+
a = int(round(clamp(float(alpha), 0.0, 1.0) * 255)) # type: int
|
|
277
|
+
if u'{}'.format(mode) == 'tint':
|
|
278
|
+
for i, color in enumerate(list(colors)[:255]):
|
|
279
|
+
c = complementary(color) # type: QColor
|
|
280
|
+
lut[i + 1] = (c.blue(), c.green(), c.red(), a)
|
|
281
|
+
else:
|
|
282
|
+
lut[0] = (16, 12, 12, a)
|
|
283
|
+
rgba = ascontiguousarray(lut[lab]) # type: ndarray
|
|
284
|
+
return QImage(rgba.data, w, h, w * 4, QImage.Format_ARGB32).copy()
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
class ColorDetector(QObject):
|
|
288
|
+
"""
|
|
289
|
+
ColorDetector class.
|
|
290
|
+
"""
|
|
291
|
+
|
|
292
|
+
class MatchMode(IntEnum):
|
|
293
|
+
"""
|
|
294
|
+
MatchMode enumeration class.
|
|
295
|
+
"""
|
|
296
|
+
Similar = 0 # type: int
|
|
297
|
+
Exact = 1 # type: int
|
|
298
|
+
|
|
299
|
+
Similar = MatchMode.Similar # type: int
|
|
300
|
+
Exact = MatchMode.Exact # type: int
|
|
301
|
+
targetsChanged = pyqtSignal() # type: pyqtSignal
|
|
302
|
+
settingsChanged = pyqtSignal() # type: pyqtSignal
|
|
303
|
+
|
|
304
|
+
def __init__(self, *args, **kwargs):
|
|
305
|
+
"""
|
|
306
|
+
:param targetColors: (list[QColor | str]) Colors to detect.
|
|
307
|
+
:param tolerance: (float | int) 1 .. 100: hue degrees (Similar mode) or delta E (Exact mode).
|
|
308
|
+
:param matchMode: (ColorDetector.MatchMode | int) Similar (any shade of the hue) or Exact.
|
|
309
|
+
:param groupDistance: (int) Fragments closer than this (px) form one region, 0 = no grouping.
|
|
310
|
+
:param minArea: (int) Smallest region (pixels) that is reported.
|
|
311
|
+
:param maxRegions: (int) Maximum regions reported per target.
|
|
312
|
+
:param paletteSize: (int) Dominant colors to compute, 0 disables the palette.
|
|
313
|
+
:param returnMask: (bool) Include the label mask (QImage) in the results.
|
|
314
|
+
:param maxPixels: (int) Bigger frames are sub-sampled to stay real time.
|
|
315
|
+
:param targetsChanged: callable | None
|
|
316
|
+
:param settingsChanged: callable | None
|
|
317
|
+
:param args: any
|
|
318
|
+
:param kwargs: any
|
|
319
|
+
"""
|
|
320
|
+
targetColors = kwargs.pop('targetColors', []) # type: list
|
|
321
|
+
tolerance = kwargs.pop('tolerance', 15.0) # type: float
|
|
322
|
+
matchMode = kwargs.pop('matchMode', ColorDetector.Similar) # type: int
|
|
323
|
+
groupDistance = kwargs.pop('groupDistance', 5) # type: int # Joins the letters / words of a text line.
|
|
324
|
+
minArea = kwargs.pop('minArea', 12) # type: int
|
|
325
|
+
maxRegions = kwargs.pop('maxRegions', 24) # type: int
|
|
326
|
+
paletteSize = kwargs.pop('paletteSize', 6) # type: int
|
|
327
|
+
returnMask = kwargs.pop('returnMask', True) # type: bool
|
|
328
|
+
maxPixels = kwargs.pop('maxPixels', 1280 * 800) # type: int # Full resolution keeps 1 px strokes.
|
|
329
|
+
targetsChanged = kwargs.pop('targetsChanged', None) # type: callable | None
|
|
330
|
+
settingsChanged = kwargs.pop('settingsChanged', None) # type: callable | None
|
|
331
|
+
super(ColorDetector, self).__init__(*args, **kwargs)
|
|
332
|
+
if callable(targetsChanged):
|
|
333
|
+
self.targetsChanged.connect(targetsChanged)
|
|
334
|
+
if callable(settingsChanged):
|
|
335
|
+
self.settingsChanged.connect(settingsChanged)
|
|
336
|
+
self.__m_targets = [toColor(c) for c in targetColors] # type: list[QColor]
|
|
337
|
+
self.__m_tolerance = clamp(float(tolerance), 1.0, 100.0) # type: float
|
|
338
|
+
self.__m_matchMode = ColorDetector.MatchMode(int(matchMode)) # type: ColorDetector.MatchMode
|
|
339
|
+
self.__m_groupDistance = max(0, int(groupDistance)) # type: int
|
|
340
|
+
self.__m_minArea = max(1, int(minArea)) # type: int
|
|
341
|
+
self.__m_maxRegions = max(1, int(maxRegions)) # type: int
|
|
342
|
+
self.__m_paletteSize = max(0, int(paletteSize)) # type: int
|
|
343
|
+
self.__m_returnMask = bool(returnMask) # type: bool
|
|
344
|
+
self.__m_maxPixels = max(4096, int(maxPixels)) # type: int
|
|
345
|
+
self.__m_clock = QElapsedTimer() # type: QElapsedTimer
|
|
346
|
+
self.__m_analyzer = ColorAnalyzer() # type: ColorAnalyzer # the Qt-free pipeline (core.py)
|
|
347
|
+
|
|
348
|
+
# ---------- Helpers ----------
|
|
349
|
+
@staticmethod
|
|
350
|
+
def isAvailable():
|
|
351
|
+
"""
|
|
352
|
+
:return: (bool) NumPy is installed.
|
|
353
|
+
"""
|
|
354
|
+
return np
|
|
355
|
+
|
|
356
|
+
@staticmethod
|
|
357
|
+
def hasRegions():
|
|
358
|
+
"""
|
|
359
|
+
:return: (bool) OpenCV is installed (one rect per region instead of one per target).
|
|
360
|
+
"""
|
|
361
|
+
return np and cv2
|
|
362
|
+
|
|
363
|
+
# ---------- Public API ----------
|
|
364
|
+
def analyze(self, image):
|
|
365
|
+
"""
|
|
366
|
+
:param image: QImage
|
|
367
|
+
:return: (dict) width, height, scale, detections, coverage, palette, labelMask (QImage | None), timeMs.
|
|
368
|
+
"""
|
|
369
|
+
result = {'width': 0, 'height': 0, 'scale': 1, 'detections': [], 'coverage': [], 'palette': [],
|
|
370
|
+
'labelMask': None, 'timeMs': 0.0} # type: dict
|
|
371
|
+
if not np or image is None or image.isNull():
|
|
372
|
+
return result
|
|
373
|
+
self.__m_clock.start()
|
|
374
|
+
self.__m_analyzer.configure(targets=[(c.red(), c.green(), c.blue()) for c in self.__m_targets],
|
|
375
|
+
tolerance=self.__m_tolerance, matchMode=int(self.__m_matchMode),
|
|
376
|
+
groupDistance=self.__m_groupDistance, minArea=self.__m_minArea,
|
|
377
|
+
maxRegions=self.__m_maxRegions, paletteSize=self.__m_paletteSize,
|
|
378
|
+
returnMask=self.__m_returnMask, maxPixels=self.__m_maxPixels)
|
|
379
|
+
result = self.__m_analyzer.analyze(qimageToArray(image)) # type: dict
|
|
380
|
+
label = result.get('labelMask')
|
|
381
|
+
if label is not None:
|
|
382
|
+
h, w = label.shape[:2]
|
|
383
|
+
result['labelMask'] = QImage(label.data, w, h, w, QImage.Format_Grayscale8).copy()
|
|
384
|
+
result['timeMs'] = round(self.__m_clock.nsecsElapsed() / 1e6, 2)
|
|
385
|
+
return result
|
|
386
|
+
|
|
387
|
+
def getTargetColors(self):
|
|
388
|
+
"""
|
|
389
|
+
:return: list[QColor]
|
|
390
|
+
"""
|
|
391
|
+
return [QColor(c) for c in self.__m_targets]
|
|
392
|
+
|
|
393
|
+
@pyqtSlot(list)
|
|
394
|
+
def setTargetColors(self, targetColors):
|
|
395
|
+
"""
|
|
396
|
+
:param targetColors: list[QColor | str]
|
|
397
|
+
:return:
|
|
398
|
+
"""
|
|
399
|
+
self.__m_targets = [toColor(c) for c in targetColors] # type: list[QColor]
|
|
400
|
+
self.targetsChanged.emit()
|
|
401
|
+
|
|
402
|
+
@pyqtSlot(QColor)
|
|
403
|
+
@pyqtSlot(Qt.GlobalColor)
|
|
404
|
+
@pyqtSlot(int)
|
|
405
|
+
@pyqtSlot(str)
|
|
406
|
+
@pyqtSlot('QString')
|
|
407
|
+
def addTargetColor(self, color):
|
|
408
|
+
"""
|
|
409
|
+
:param color: QColor | Qt.GlobalColor | int | str | unicode | QString
|
|
410
|
+
:return:
|
|
411
|
+
"""
|
|
412
|
+
c = toColor(color) # type: QColor
|
|
413
|
+
if c.isValid() and all(t.rgb() != c.rgb() for t in self.__m_targets):
|
|
414
|
+
self.__m_targets.append(c)
|
|
415
|
+
self.targetsChanged.emit()
|
|
416
|
+
|
|
417
|
+
@pyqtSlot(int)
|
|
418
|
+
def removeTargetColor(self, index):
|
|
419
|
+
"""
|
|
420
|
+
:param index: int
|
|
421
|
+
:return:
|
|
422
|
+
"""
|
|
423
|
+
if 0 <= int(index) < len(self.__m_targets):
|
|
424
|
+
del self.__m_targets[int(index)]
|
|
425
|
+
self.targetsChanged.emit()
|
|
426
|
+
|
|
427
|
+
@pyqtSlot()
|
|
428
|
+
def clearTargetColors(self):
|
|
429
|
+
"""
|
|
430
|
+
:return:
|
|
431
|
+
"""
|
|
432
|
+
if self.__m_targets:
|
|
433
|
+
self.__m_targets = [] # type: list[QColor]
|
|
434
|
+
self.targetsChanged.emit()
|
|
435
|
+
|
|
436
|
+
def getTolerance(self):
|
|
437
|
+
"""
|
|
438
|
+
:return: float | int
|
|
439
|
+
"""
|
|
440
|
+
return self.__m_tolerance
|
|
441
|
+
|
|
442
|
+
@pyqtSlot(float)
|
|
443
|
+
@pyqtSlot(int)
|
|
444
|
+
def setTolerance(self, tolerance):
|
|
445
|
+
"""
|
|
446
|
+
:param tolerance: (float | int) delta E, 1 .. 100.
|
|
447
|
+
:return:
|
|
448
|
+
"""
|
|
449
|
+
self.__m_tolerance = clamp(float(tolerance), 1.0, 100.0) # type: float
|
|
450
|
+
self.settingsChanged.emit()
|
|
451
|
+
|
|
452
|
+
def getMatchMode(self):
|
|
453
|
+
"""
|
|
454
|
+
:return: ColorDetector.MatchMode
|
|
455
|
+
"""
|
|
456
|
+
return self.__m_matchMode
|
|
457
|
+
|
|
458
|
+
@pyqtSlot(MatchMode)
|
|
459
|
+
@pyqtSlot(int)
|
|
460
|
+
def setMatchMode(self, matchMode):
|
|
461
|
+
"""
|
|
462
|
+
:param matchMode: (ColorDetector.MatchMode | int) Similar or Exact.
|
|
463
|
+
:return:
|
|
464
|
+
"""
|
|
465
|
+
self.__m_matchMode = ColorDetector.MatchMode(int(matchMode)) # type: ColorDetector.MatchMode
|
|
466
|
+
self.settingsChanged.emit()
|
|
467
|
+
|
|
468
|
+
def getGroupDistance(self):
|
|
469
|
+
"""
|
|
470
|
+
:return: int
|
|
471
|
+
"""
|
|
472
|
+
return self.__m_groupDistance
|
|
473
|
+
|
|
474
|
+
@pyqtSlot(int)
|
|
475
|
+
def setGroupDistance(self, groupDistance):
|
|
476
|
+
"""
|
|
477
|
+
:param groupDistance: (int) px, 0 = no grouping.
|
|
478
|
+
:return:
|
|
479
|
+
"""
|
|
480
|
+
self.__m_groupDistance = max(0, int(groupDistance)) # type: int
|
|
481
|
+
self.settingsChanged.emit()
|
|
482
|
+
|
|
483
|
+
def getMinArea(self):
|
|
484
|
+
"""
|
|
485
|
+
:return: int
|
|
486
|
+
"""
|
|
487
|
+
return self.__m_minArea
|
|
488
|
+
|
|
489
|
+
@pyqtSlot(int)
|
|
490
|
+
def setMinArea(self, minArea):
|
|
491
|
+
"""
|
|
492
|
+
:param minArea: int
|
|
493
|
+
:return:
|
|
494
|
+
"""
|
|
495
|
+
self.__m_minArea = max(1, int(minArea)) # type: int
|
|
496
|
+
self.settingsChanged.emit()
|
|
497
|
+
|
|
498
|
+
def getMaxRegions(self):
|
|
499
|
+
"""
|
|
500
|
+
:return: int
|
|
501
|
+
"""
|
|
502
|
+
return self.__m_maxRegions
|
|
503
|
+
|
|
504
|
+
@pyqtSlot(int)
|
|
505
|
+
def setMaxRegions(self, maxRegions):
|
|
506
|
+
"""
|
|
507
|
+
:param maxRegions: int
|
|
508
|
+
:return:
|
|
509
|
+
"""
|
|
510
|
+
self.__m_maxRegions = max(1, int(maxRegions)) # type: int
|
|
511
|
+
self.settingsChanged.emit()
|
|
512
|
+
|
|
513
|
+
def getPaletteSize(self):
|
|
514
|
+
"""
|
|
515
|
+
:return: int
|
|
516
|
+
"""
|
|
517
|
+
return self.__m_paletteSize
|
|
518
|
+
|
|
519
|
+
@pyqtSlot(int)
|
|
520
|
+
def setPaletteSize(self, paletteSize):
|
|
521
|
+
"""
|
|
522
|
+
:param paletteSize: (int) 0 disables the palette.
|
|
523
|
+
:return:
|
|
524
|
+
"""
|
|
525
|
+
self.__m_paletteSize = max(0, int(paletteSize)) # type: int
|
|
526
|
+
self.settingsChanged.emit()
|
|
527
|
+
|
|
528
|
+
def isReturnMask(self):
|
|
529
|
+
"""
|
|
530
|
+
:return: bool
|
|
531
|
+
"""
|
|
532
|
+
return self.__m_returnMask
|
|
533
|
+
|
|
534
|
+
@pyqtSlot(bool)
|
|
535
|
+
@pyqtSlot(int)
|
|
536
|
+
def setReturnMask(self, returnMask):
|
|
537
|
+
"""
|
|
538
|
+
:param returnMask: bool
|
|
539
|
+
:return:
|
|
540
|
+
"""
|
|
541
|
+
self.__m_returnMask = bool(returnMask) # type: bool
|
|
542
|
+
self.settingsChanged.emit()
|
|
543
|
+
|
|
544
|
+
def getMaxPixels(self):
|
|
545
|
+
"""
|
|
546
|
+
:return: int
|
|
547
|
+
"""
|
|
548
|
+
return self.__m_maxPixels
|
|
549
|
+
|
|
550
|
+
@pyqtSlot(int)
|
|
551
|
+
def setMaxPixels(self, maxPixels):
|
|
552
|
+
"""
|
|
553
|
+
:param maxPixels: int
|
|
554
|
+
:return:
|
|
555
|
+
"""
|
|
556
|
+
self.__m_maxPixels = max(4096, int(maxPixels)) # type: int
|
|
557
|
+
self.settingsChanged.emit()
|
|
558
|
+
|
|
559
|
+
# ---------- Properties ----------
|
|
560
|
+
targetColors = pyqtProperty(
|
|
561
|
+
list, fget=getTargetColors, fset=setTargetColors, notify=targetsChanged) # type: pyqtProperty
|
|
562
|
+
Q_ENUMS('MatchMode' if qVersion()[0] == '4' else MatchMode)
|
|
563
|
+
tolerance = pyqtProperty(float, fget=getTolerance, fset=setTolerance) # type: pyqtProperty
|
|
564
|
+
matchMode = pyqtProperty(int, fget=getMatchMode, fset=setMatchMode) # type: pyqtProperty
|
|
565
|
+
groupDistance = pyqtProperty(int, fget=getGroupDistance, fset=setGroupDistance) # type: pyqtProperty
|
|
566
|
+
minArea = pyqtProperty(int, fget=getMinArea, fset=setMinArea) # type: pyqtProperty
|
|
567
|
+
maxRegions = pyqtProperty(int, fget=getMaxRegions, fset=setMaxRegions) # type: pyqtProperty
|
|
568
|
+
paletteSize = pyqtProperty(int, fget=getPaletteSize, fset=setPaletteSize) # type: pyqtProperty
|
|
569
|
+
returnMask = pyqtProperty(bool, fget=isReturnMask, fset=setReturnMask) # type: pyqtProperty
|
|
570
|
+
maxPixels = pyqtProperty(int, fget=getMaxPixels, fset=setMaxPixels) # type: pyqtProperty
|