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.

Files changed (117) hide show
  1. ScreenMagnifier/__init__.py +68 -0
  2. ScreenMagnifier/__main__.py +17 -0
  3. ScreenMagnifier/advancedoverlay.py +593 -0
  4. ScreenMagnifier/advancedpanel.py +521 -0
  5. ScreenMagnifier/analysiscontroller.py +533 -0
  6. ScreenMagnifier/analysisserver.py +404 -0
  7. ScreenMagnifier/colordetector/__init__.py +28 -0
  8. ScreenMagnifier/colordetector/colordetector.py +570 -0
  9. ScreenMagnifier/colordetector/core.py +434 -0
  10. ScreenMagnifier/colordetector/main.py +23 -0
  11. ScreenMagnifier/colordetector/mainwindow.py +182 -0
  12. ScreenMagnifier/colorpanel.py +424 -0
  13. ScreenMagnifier/colorswatches/__init__.py +16 -0
  14. ScreenMagnifier/colorswatches/colorswatches.py +316 -0
  15. ScreenMagnifier/colorswatches/main.py +23 -0
  16. ScreenMagnifier/colorswatches/mainwindow.py +60 -0
  17. ScreenMagnifier/controlpanel.py +234 -0
  18. ScreenMagnifier/detectionpanel.py +790 -0
  19. ScreenMagnifier/facedetector/__init__.py +24 -0
  20. ScreenMagnifier/facedetector/core.py +314 -0
  21. ScreenMagnifier/facedetector/facedetector.py +288 -0
  22. ScreenMagnifier/facedetector/main.py +23 -0
  23. ScreenMagnifier/facedetector/mainwindow.py +122 -0
  24. ScreenMagnifier/facedetector/models/__init__.py +4 -0
  25. ScreenMagnifier/facepointer.py +1121 -0
  26. ScreenMagnifier/features.py +50 -0
  27. ScreenMagnifier/hotkeys/__init__.py +16 -0
  28. ScreenMagnifier/hotkeys/hotkeys.py +379 -0
  29. ScreenMagnifier/hotkeys/main.py +23 -0
  30. ScreenMagnifier/hotkeys/mainwindow.py +56 -0
  31. ScreenMagnifier/imagefilters/__init__.py +24 -0
  32. ScreenMagnifier/imagefilters/core.py +172 -0
  33. ScreenMagnifier/imagefilters/imagefilters.py +220 -0
  34. ScreenMagnifier/imagefilters/main.py +23 -0
  35. ScreenMagnifier/imagefilters/mainwindow.py +120 -0
  36. ScreenMagnifier/magnifierview/__init__.py +16 -0
  37. ScreenMagnifier/magnifierview/magnifierview.py +1249 -0
  38. ScreenMagnifier/magnifierview/main.py +23 -0
  39. ScreenMagnifier/magnifierview/mainwindow.py +91 -0
  40. ScreenMagnifier/main.py +157 -0
  41. ScreenMagnifier/mainwindow.py +1368 -0
  42. ScreenMagnifier/mousecatcher/__init__.py +16 -0
  43. ScreenMagnifier/mousecatcher/main.py +23 -0
  44. ScreenMagnifier/mousecatcher/mainwindow.py +78 -0
  45. ScreenMagnifier/mousecatcher/mousecatcher.py +251 -0
  46. ScreenMagnifier/mousemove.py +259 -0
  47. ScreenMagnifier/netprotocol/__init__.py +25 -0
  48. ScreenMagnifier/netprotocol/main.py +23 -0
  49. ScreenMagnifier/netprotocol/mainwindow.py +91 -0
  50. ScreenMagnifier/netprotocol/netprotocol.py +372 -0
  51. ScreenMagnifier/netprotocol/wire.py +189 -0
  52. ScreenMagnifier/regionselector/__init__.py +16 -0
  53. ScreenMagnifier/regionselector/main.py +23 -0
  54. ScreenMagnifier/regionselector/mainwindow.py +70 -0
  55. ScreenMagnifier/regionselector/regionselector.py +549 -0
  56. ScreenMagnifier/remoteclient.py +636 -0
  57. ScreenMagnifier/resources/icon-128.png +0 -0
  58. ScreenMagnifier/resources/icon-16.png +0 -0
  59. ScreenMagnifier/resources/icon-24.png +0 -0
  60. ScreenMagnifier/resources/icon-256.png +0 -0
  61. ScreenMagnifier/resources/icon-32.png +0 -0
  62. ScreenMagnifier/resources/icon-48.png +0 -0
  63. ScreenMagnifier/resources/icon-512.png +0 -0
  64. ScreenMagnifier/resources/icon-64.png +0 -0
  65. ScreenMagnifier/resources/icon.icns +0 -0
  66. ScreenMagnifier/resources/icon.ico +0 -0
  67. ScreenMagnifier/screengrabber/__init__.py +16 -0
  68. ScreenMagnifier/screengrabber/main.py +23 -0
  69. ScreenMagnifier/screengrabber/mainwindow.py +79 -0
  70. ScreenMagnifier/screengrabber/screengrabber.py +558 -0
  71. ScreenMagnifier/server.py +150 -0
  72. ScreenMagnifier/serverpanel.py +379 -0
  73. ScreenMagnifier/shortcuteditor/__init__.py +16 -0
  74. ScreenMagnifier/shortcuteditor/main.py +23 -0
  75. ScreenMagnifier/shortcuteditor/mainwindow.py +54 -0
  76. ScreenMagnifier/shortcuteditor/shortcuteditor.py +411 -0
  77. ScreenMagnifier/shortcuts.py +118 -0
  78. ScreenMagnifier/shortcutspanel.py +140 -0
  79. ScreenMagnifier/support.py +127 -0
  80. ScreenMagnifier/translations/ar.json +387 -0
  81. ScreenMagnifier/translations/de.json +387 -0
  82. ScreenMagnifier/translations/es.json +387 -0
  83. ScreenMagnifier/translations/fr.json +387 -0
  84. ScreenMagnifier/translations/it.json +387 -0
  85. ScreenMagnifier/translations/ja.json +387 -0
  86. ScreenMagnifier/translations/ko.json +387 -0
  87. ScreenMagnifier/translations/pt_BR.json +387 -0
  88. ScreenMagnifier/translations/ru.json +387 -0
  89. ScreenMagnifier/translations/tr.json +387 -0
  90. ScreenMagnifier/translations/zh_CN.json +387 -0
  91. ScreenMagnifier/translator/__init__.py +16 -0
  92. ScreenMagnifier/translator/main.py +23 -0
  93. ScreenMagnifier/translator/mainwindow.py +62 -0
  94. ScreenMagnifier/translator/translator.py +284 -0
  95. ScreenMagnifier/upscaler/__init__.py +24 -0
  96. ScreenMagnifier/upscaler/core.py +315 -0
  97. ScreenMagnifier/upscaler/main.py +23 -0
  98. ScreenMagnifier/upscaler/mainwindow.py +128 -0
  99. ScreenMagnifier/upscaler/models/FSRCNN-small_x2.pb +0 -0
  100. ScreenMagnifier/upscaler/models/FSRCNN-small_x3.pb +0 -0
  101. ScreenMagnifier/upscaler/models/FSRCNN-small_x4.pb +0 -0
  102. ScreenMagnifier/upscaler/models/FSRCNN_x2.pb +0 -0
  103. ScreenMagnifier/upscaler/models/FSRCNN_x3.pb +0 -0
  104. ScreenMagnifier/upscaler/models/FSRCNN_x4.pb +0 -0
  105. ScreenMagnifier/upscaler/models/LICENSE-FSRCNN +201 -0
  106. ScreenMagnifier/upscaler/models/__init__.py +4 -0
  107. ScreenMagnifier/upscaler/upscaler.py +285 -0
  108. ScreenMagnifier/wincheck.py +65 -0
  109. ScreenMagnifier/zoomoverlay/__init__.py +16 -0
  110. ScreenMagnifier/zoomoverlay/main.py +23 -0
  111. ScreenMagnifier/zoomoverlay/mainwindow.py +99 -0
  112. ScreenMagnifier/zoomoverlay/zoomoverlay.py +1779 -0
  113. screenmagnifier-1.11.0.post8.dist-info/METADATA +591 -0
  114. screenmagnifier-1.11.0.post8.dist-info/RECORD +117 -0
  115. screenmagnifier-1.11.0.post8.dist-info/WHEEL +5 -0
  116. screenmagnifier-1.11.0.post8.dist-info/entry_points.txt +5 -0
  117. screenmagnifier-1.11.0.post8.dist-info/top_level.txt +1 -0
@@ -0,0 +1,24 @@
1
+ # coding=utf-8
2
+ """
3
+ facedetector package: the Qt-free part (core.py) is always available - the headless server uses it, also where Qt is
4
+ not installed (e.g. Android); the Qt classes are available when a Qt binding is installed (None otherwise).
5
+ """
6
+ from os.path import dirname
7
+ from sys import path
8
+
9
+ if not __package__ and dirname(__file__) not in path:
10
+ path.append(dirname(__file__))
11
+
12
+ try:
13
+ from .core import FaceEngine
14
+ except:
15
+ from core import FaceEngine
16
+ try:
17
+ from .facedetector import FaceDetector
18
+ except:
19
+ try:
20
+ from facedetector import FaceDetector
21
+ except: # No Qt binding: the Qt-free part only.
22
+ FaceDetector = None
23
+
24
+ __all__ = ['FaceEngine', 'FaceDetector'] # type: list[str]
@@ -0,0 +1,314 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Face detection on NumPy arrays - pure Python + NumPy + OpenCV, no Qt: used by the Qt FaceDetector and by the
4
+ headless server (which also runs without Qt, e.g. on Android).
5
+ - YuNet (OpenCV >= 4.5.4, model downloaded on demand, SHA-256 verified): accurate, 5 landmarks, scores.
6
+ - Haar cascade (bundled with OpenCV): no download, frontal faces.
7
+ """
8
+ from os.path import dirname, join, isfile, getsize
9
+ from os import makedirs
10
+ from hashlib import sha256
11
+ from time import time
12
+ from math import exp
13
+ import sys
14
+
15
+ try:
16
+ from os import replace
17
+ except:
18
+ from os import remove, rename
19
+ from os.path import exists
20
+
21
+
22
+ def replace(src, dst):
23
+ """
24
+ Replace the destination file with the source file.
25
+ """
26
+ if exists(dst):
27
+ remove(dst)
28
+ rename(src, dst)
29
+ try:
30
+ from urllib.request import Request, urlopen
31
+ except: # Python 2.
32
+ from urllib2 import Request, urlopen
33
+ try:
34
+ from numpy import ndarray
35
+
36
+ np = True # type: bool
37
+ except Exception:
38
+ np = False # type: bool
39
+ try:
40
+ from cv2 import CascadeClassifier, cvtColor, resize, createCLAHE, COLOR_RGB2GRAY, COLOR_RGB2BGR, INTER_AREA
41
+ from cv2.data import haarcascades
42
+
43
+ cv2 = True # type: bool
44
+ except Exception:
45
+ haarcascades = '' # type: str
46
+ cv2 = False # type: bool
47
+ try:
48
+ from cv2 import FaceDetectorYN # OpenCV >= 4.5.4.
49
+ except Exception:
50
+ FaceDetectorYN = None
51
+
52
+ MODELS_DIR = join(dirname(__file__), 'models') # type: str
53
+ YUNET_FILE = 'face_detection_yunet_2023mar.onnx' # type: str
54
+ YUNET_URL = 'https://github.com/opencv/opencv_zoo/raw/main/models/face_detection_yunet/' + YUNET_FILE # type: str
55
+ YUNET_SHA256 = '8f2383e4dd3cfbb4553ea8718107fc0423210dc964f9f4280604804ed2552fa4' # type: str
56
+ YUNET_SIZE = 232589 # type: int
57
+ HAAR_FILE = 'haarcascade_frontalface_default.xml' # type: str
58
+ HAAR_MAX_INPUT = 400 # type: int # Haar cost grows fast with size: 400 px + scale step 1.2 keeps faces >= ~20 px.
59
+
60
+
61
+ def clamp(x, a, b):
62
+ """
63
+ :param x: float | int
64
+ :param a: float | int
65
+ :param b: float | int
66
+ :return: float | int
67
+ """
68
+ return a if x < a else b if x > b else x
69
+
70
+
71
+ def modelDirs(dataDir=''):
72
+ """
73
+ :param dataDir: (str | unicode | QString) Writable per-user data folder ('' = none).
74
+ :return: (list[str | unicode | QString]) Folders searched for the YuNet model, in order: package folder,
75
+ user data folder (user data first in a packaged application, whose own folder is read-only or temporary).
76
+ """
77
+ dirs = [MODELS_DIR] # type: list[str]
78
+ if dataDir:
79
+ dirs.insert(0 if getattr(sys, 'frozen', False) else 1, join(dataDir, 'models'))
80
+ return dirs
81
+
82
+
83
+ def verifyModel(path):
84
+ """
85
+ :param path: str | unicode | QString
86
+ :return: (bool) The file exists and its SHA-256 matches the official YuNet model.
87
+ """
88
+ try:
89
+ if not isfile(path) or getsize(path) != YUNET_SIZE:
90
+ return False
91
+ with open(path, 'rb') as f:
92
+ return sha256(f.read()).hexdigest() == YUNET_SHA256
93
+ except Exception:
94
+ return False
95
+
96
+
97
+ def downloadModel(target, progress=None):
98
+ """
99
+ Fetch, verify (SHA-256) and atomically install the YuNet model.
100
+ :param target: (str | unicode | QString) Destination file.
101
+ :param progress: (callable | None) f(percent).
102
+ :return:
103
+ :raise Exception: on a network error or a failed check (nothing is installed then).
104
+ """
105
+ request = Request(YUNET_URL, headers={'User-Agent': 'ScreenMagnifier'})
106
+ data = bytearray() # type: bytearray
107
+ response = urlopen(request, timeout=30)
108
+ try:
109
+ total = int(response.headers.get('Content-Length') or YUNET_SIZE) # type: int
110
+ while True:
111
+ chunk = response.read(16384) # type: bytes
112
+ if not chunk:
113
+ break
114
+ data.extend(chunk)
115
+ if callable(progress):
116
+ progress(int(clamp(100.0 * len(data) / max(1, total), 0, 100)))
117
+ finally:
118
+ response.close()
119
+ if sha256(bytes(data)).hexdigest() != YUNET_SHA256:
120
+ raise ValueError(u'Downloaded file failed the SHA-256 check ({} bytes)'.format(len(data)))
121
+ makedirs(dirname(target), exist_ok=True)
122
+ with open(target + '.part', 'wb') as f:
123
+ f.write(bytes(data))
124
+ replace(target + '.part', target)
125
+
126
+
127
+ class FaceEngine(object):
128
+ """
129
+ FaceEngine class: options are plain attributes (backend 'auto' / 'yunet' / 'haar', scoreThreshold,
130
+ nmsThreshold, minFaceSize, maxInputSize); setModelPath() / resetModel() after changing the model.
131
+ """
132
+
133
+ def __init__(self, **options):
134
+ """
135
+ :param options: backend, scoreThreshold, nmsThreshold, minFaceSize, maxInputSize, haarMaxInput (Haar frames
136
+ are downscaled to at most this, default 400: it is slower than YuNet), modelPath, dataDir,
137
+ onError (callable f(message)).
138
+ """
139
+ self.backend = u'{}'.format(options.get('backend', 'auto')) # type: str
140
+ self.scoreThreshold = clamp(float(options.get('scoreThreshold', 0.6)), 0.1, 0.99) # type: float
141
+ self.nmsThreshold = clamp(float(options.get('nmsThreshold', 0.3)), 0.05, 0.95) # type: float
142
+ self.minFaceSize = max(8, int(options.get('minFaceSize', 20))) # type: int
143
+ self.maxInputSize = max(160, int(options.get('maxInputSize', 640))) # type: int
144
+ self.haarMaxInput = max(160, int(options.get('haarMaxInput', HAAR_MAX_INPUT))) # type: int # Haar is slower
145
+ self.dataDir = u'{}'.format(options.get('dataDir', '')) # type: str
146
+ self.onError = options.get('onError')
147
+ self.__m_modelPath = u'{}'.format(options.get('modelPath', '')) # type: str
148
+ self.__m_resolvedModel = None # type: str | None # Cached verified model path ('' = none).
149
+ self.__m_yunet = None
150
+ self.__m_yunetSize = (0, 0) # type: tuple[int, int]
151
+ self.__m_haar = None
152
+ self.__m_clahe = None
153
+
154
+ @staticmethod
155
+ def isAvailable():
156
+ """
157
+ :return: (bool) Face detection is possible (NumPy + OpenCV).
158
+ """
159
+ return np and cv2
160
+
161
+ @staticmethod
162
+ def hasYuNetSupport():
163
+ """
164
+ :return: bool
165
+ """
166
+ return FaceEngine.isAvailable() and FaceDetectorYN is not None
167
+
168
+ def getModelPath(self):
169
+ """
170
+ :return: str | unicode | QString
171
+ """
172
+ return self.__m_modelPath
173
+
174
+ def setModelPath(self, modelPath):
175
+ """
176
+ :param modelPath: (str | unicode | QString) '' = search modelDirs().
177
+ :return:
178
+ """
179
+ self.__m_modelPath = u'{}'.format(modelPath) # type: str
180
+ self.resetModel()
181
+
182
+ def resetModel(self):
183
+ """
184
+ Forget the cached model (after a download or a path change).
185
+ :return:
186
+ """
187
+ self.__m_resolvedModel = None
188
+ self.__m_yunet = None
189
+
190
+ def findModel(self):
191
+ """
192
+ :return: (str | unicode | QString) Path of a verified YuNet model, '' if none is available.
193
+ """
194
+ if self.__m_resolvedModel is None:
195
+ candidates = [self.__m_modelPath] if self.__m_modelPath else [join(d, YUNET_FILE) for d in
196
+ modelDirs(self.dataDir)]
197
+ self.__m_resolvedModel = next((p for p in candidates if verifyModel(p)), '') # type: str
198
+ return self.__m_resolvedModel
199
+
200
+ def isModelReady(self):
201
+ """
202
+ :return: bool
203
+ """
204
+ return FaceEngine.hasYuNetSupport() and bool(self.findModel())
205
+
206
+ def getActiveBackend(self):
207
+ """
208
+ :return: (str | unicode | QString) 'yunet', 'haar' or '' (nothing usable).
209
+ """
210
+ if not FaceEngine.isAvailable():
211
+ return ''
212
+ if self.backend in ('auto', 'yunet') and self.isModelReady():
213
+ return 'yunet'
214
+ return 'haar' if self.backend in ('auto', 'haar') or not self.isModelReady() else ''
215
+
216
+ def _detectYuNet(self, arr, minSize):
217
+ """
218
+ :param arr: (ndarray) RGB frame.
219
+ :param minSize: (int) Smallest face in this frame's pixels.
220
+ :return: list[dict]
221
+ """
222
+ h, w = arr.shape[:2]
223
+ if self.__m_yunet is None:
224
+ self.__m_yunet = FaceDetectorYN.create(self.findModel(), '', (w, h), self.scoreThreshold,
225
+ self.nmsThreshold, 5000)
226
+ self.__m_yunetSize = (w, h) # type: tuple[int, int]
227
+ elif self.__m_yunetSize != (w, h):
228
+ self.__m_yunet.setInputSize((w, h))
229
+ self.__m_yunetSize = (w, h) # type: tuple[int, int]
230
+ self.__m_yunet.setScoreThreshold(self.scoreThreshold)
231
+ self.__m_yunet.setNMSThreshold(self.nmsThreshold)
232
+ _, faces = self.__m_yunet.detect(cvtColor(arr, COLOR_RGB2BGR))
233
+ out = [] # type: list
234
+ for f in ([] if faces is None else faces):
235
+ x, y, fw, fh = [float(v) for v in f[:4]]
236
+ if min(fw, fh) < minSize:
237
+ continue
238
+ out.append({'rect': [x, y, fw, fh], 'score': round(float(f[14]), 3),
239
+ 'landmarks': [[float(f[4 + 2 * k]), float(f[5 + 2 * k])] for k in range(5)]})
240
+ return out
241
+
242
+ def _detectHaar(self, arr, minSize):
243
+ """
244
+ :param arr: (ndarray) RGB frame.
245
+ :param minSize: (int) Smallest face in this frame's pixels.
246
+ :return: list[dict]
247
+ """
248
+ if self.__m_haar is None:
249
+ self.__m_haar = CascadeClassifier(join(haarcascades, HAAR_FILE))
250
+ if self.__m_haar.empty():
251
+ raise RuntimeError(u'Haar cascade not found in {}'.format(haarcascades))
252
+ if self.__m_clahe is None: # local equalization: a large plain area (letterbox bars, a desktop around a
253
+ self.__m_clahe = createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) # video) no longer washes faces out
254
+ gray = self.__m_clahe.apply(cvtColor(arr, COLOR_RGB2GRAY)) # type: ndarray
255
+ neighbors = 2 + int(round(self.scoreThreshold * 5)) # type: int # Stricter with higher confidence.
256
+ rects, _, weights = self.__m_haar.detectMultiScale3(
257
+ gray, scaleFactor=1.2, minNeighbors=neighbors, minSize=(minSize, minSize),
258
+ outputRejectLevels=True)
259
+ out = [] # type: list
260
+ for k, (x, y, fw, fh) in enumerate([] if rects is None else rects):
261
+ weight = float(weights[k]) if weights is not None and len(weights) > k else 4.0 # type: float
262
+ out.append({'rect': [float(x), float(y), float(fw), float(fh)],
263
+ 'score': round(1.0 - exp(-max(weight, 0.0) / 3.0), 3), 'landmarks': []})
264
+ return out
265
+
266
+ def detect(self, arr):
267
+ """
268
+ :param arr: (numpy.ndarray) (H, W, 3) uint8 RGB frame.
269
+ :return: (dict) faces [{rect, score, landmarks}], backend, width, height, timeMs.
270
+ """
271
+ result = {'faces': [], 'backend': '', 'width': 0, 'height': 0, 'timeMs': 0.0} # type: dict
272
+ backend = self.getActiveBackend() # type: str
273
+ if not backend or arr is None or arr.size == 0:
274
+ return result
275
+ started = time() # type: float
276
+ h0, w0 = arr.shape[:2]
277
+ limit = self.maxInputSize if backend == 'yunet' else min(self.maxInputSize, self.haarMaxInput) # type: int
278
+ scale = min(1.0, limit / float(max(w0, h0))) # type: float
279
+ if scale < 1.0:
280
+ arr = resize(arr, (max(1, int(w0 * scale)), max(1, int(h0 * scale))), interpolation=INTER_AREA)
281
+ try:
282
+ minSize = max(8, int(round(self.minFaceSize * scale))) # type: int # minFaceSize is in frame px.
283
+ faces = self._detectYuNet(arr, minSize) if backend == 'yunet' else self._detectHaar(arr,
284
+ minSize) # type: list
285
+ except Exception as e:
286
+ if callable(self.onError):
287
+ self.onError('Face detection ({}) failed: {}'.format(backend, e))
288
+ if backend == 'yunet':
289
+ self.__m_resolvedModel = '' # type: str # Fall back to Haar from now on.
290
+ self.__m_yunet = None
291
+ faces = [] # type: list
292
+ inv = 1.0 / scale # type: float
293
+ for f in faces:
294
+ f['rect'] = [round(v * inv, 1) for v in f['rect']]
295
+ f['landmarks'] = [[round(x * inv, 1), round(y * inv, 1)] for x, y in f['landmarks']]
296
+ result.update(faces=faces, backend=backend, width=w0, height=h0,
297
+ timeMs=round((time() - started) * 1000.0, 2))
298
+ return result
299
+
300
+
301
+ def userDataDir(appName='Screen Magnifier'):
302
+ """
303
+ The per-user data folder of the application, as Qt's QStandardPaths.AppDataLocation names it (no organization
304
+ name): the server, which has no Qt, finds the YuNet model downloaded by the application, and vice versa.
305
+ :param appName: str | unicode | QString
306
+ :return: str | unicode | QString
307
+ """
308
+ from os.path import expanduser
309
+ from os import environ
310
+ if sys.platform == 'win32':
311
+ return join(environ.get('APPDATA') or expanduser('~'), appName)
312
+ if sys.platform == 'darwin':
313
+ return join(expanduser('~'), 'Library', 'Application Support', appName)
314
+ return join(environ.get('XDG_DATA_HOME') or join(expanduser('~'), '.local', 'share'), appName)
@@ -0,0 +1,288 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ FaceDetector: optional AI face detection for QImage frames.
4
+ Backends:
5
+ - yunet : OpenCV FaceDetectorYN + YuNet ONNX model (MIT license, 232 KB): accurate, rotated / small faces,
6
+ confidence score and 5 landmarks (eyes, nose, mouth corners). The model is downloaded on demand
7
+ from the official opencv_zoo repository and verified with SHA-256 before use.
8
+ - haar : OpenCV Haar cascade shipped with opencv-python: no download, frontal faces only, no landmarks.
9
+ - auto : YuNet when its model is available, otherwise Haar.
10
+ Results are plain Python data (JSON safe) so they can be sent over a network.
11
+ """
12
+ from ManyQt.QtCore import QObject, QStandardPaths, pyqtSignal, pyqtSlot, pyqtProperty
13
+ from ManyQt.QtGui import QImage
14
+ from os import makedirs, access, W_OK
15
+ from os.path import dirname, join
16
+ from threading import Thread
17
+ from sys import path
18
+
19
+ if not __package__ and dirname(__file__) not in path:
20
+ path.append(dirname(__file__))
21
+
22
+ try: # Detection, model search, verification and download are Qt-free (core.py), shared with the server.
23
+ from .core import FaceEngine, MODELS_DIR, YUNET_FILE, YUNET_URL, YUNET_SHA256, YUNET_SIZE, HAAR_FILE, \
24
+ HAAR_MAX_INPUT, clamp, verifyModel, downloadModel, modelDirs as _modelDirs, np, cv2
25
+ except:
26
+ from core import FaceEngine, MODELS_DIR, YUNET_FILE, YUNET_URL, YUNET_SHA256, YUNET_SIZE, HAAR_FILE, \
27
+ HAAR_MAX_INPUT, clamp, verifyModel, downloadModel, modelDirs as _modelDirs, np, cv2
28
+ if np:
29
+ from numpy import frombuffer, uint8, ndarray
30
+
31
+
32
+ def qimageToArray(image):
33
+ """
34
+ :param image: QImage
35
+ :return: (ndarray) Owned (H, W, 3) uint8 RGB array.
36
+ """
37
+ img = image.convertToFormat(QImage.Format_RGB888) # type: QImage
38
+ w, h, bpl = img.width(), img.height(), img.bytesPerLine() # type: int, int, int
39
+ nbytes = img.sizeInBytes() if hasattr(img, 'sizeInBytes') else img.byteCount() # type: int
40
+ ptr = img.constBits()
41
+ if hasattr(ptr, 'setsize'):
42
+ ptr.setsize(nbytes)
43
+ return frombuffer(ptr, uint8, count=nbytes).reshape(h, bpl)[:, :w * 3].reshape(h, w, 3).copy()
44
+
45
+
46
+ def appDataDir():
47
+ """
48
+ :return: (str | unicode | QString) The writable per-user data folder of the application.
49
+ """
50
+ location = QStandardPaths.AppDataLocation if hasattr(QStandardPaths, 'AppDataLocation') else \
51
+ QStandardPaths.DataLocation # (AppDataLocation first: on Windows it is the folder the server uses too)
52
+ return u'{}'.format(QStandardPaths.writableLocation(location))
53
+
54
+
55
+ def modelDirs():
56
+ """
57
+ :return: (list[str | unicode | QString]) Folders searched for the YuNet model (package folder, user data folder).
58
+ """
59
+ return _modelDirs(appDataDir())
60
+
61
+
62
+ class FaceDetector(QObject):
63
+ """
64
+ FaceDetector class.
65
+ """
66
+ downloadProgress = pyqtSignal(int) # type: pyqtSignal
67
+ downloadFinished = pyqtSignal(bool, 'QString') # type: pyqtSignal
68
+ errorOccurred = pyqtSignal('QString') # type: pyqtSignal
69
+ backendChanged = pyqtSignal('QString') # type: pyqtSignal
70
+
71
+ def __init__(self, *args, **kwargs):
72
+ """
73
+ :param backend: (str | unicode | QString) 'auto', 'yunet' or 'haar'.
74
+ :param scoreThreshold: (float | int) 0.1 . 0.99 minimum confidence.
75
+ :param nmsThreshold: (float | int) YuNet non-maximum suppression threshold.
76
+ :param minFaceSize: (int) Smallest face (px, in the analysed frame).
77
+ :param maxInputSize: (int) Frames are downscaled so their longest side is at most this (speed).
78
+ :param modelPath: (str | unicode | QString) YuNet model file; '' = search the default folders.
79
+ :param downloadProgress: callable | None
80
+ :param downloadFinished: callable | None
81
+ :param errorOccurred: callable | None
82
+ :param args: any
83
+ :param kwargs: any
84
+ """
85
+ backend = kwargs.pop('backend', 'auto') # type: str
86
+ scoreThreshold = kwargs.pop('scoreThreshold', 0.6) # type: float
87
+ nmsThreshold = kwargs.pop('nmsThreshold', 0.3) # type: float
88
+ minFaceSize = kwargs.pop('minFaceSize', 20) # type: int
89
+ maxInputSize = kwargs.pop('maxInputSize', 640) # type: int
90
+ modelPath = kwargs.pop('modelPath', '') # type: str
91
+ downloadProgress = kwargs.pop('downloadProgress', None) # type: callable | None
92
+ downloadFinished = kwargs.pop('downloadFinished', None) # type: callable | None
93
+ errorOccurred = kwargs.pop('errorOccurred', None) # type: callable | None
94
+ super(FaceDetector, self).__init__(*args, **kwargs)
95
+ for signal, slot in ((self.downloadProgress, downloadProgress), (self.downloadFinished, downloadFinished),
96
+ (self.errorOccurred, errorOccurred)):
97
+ if callable(slot):
98
+ signal.connect(slot)
99
+ self.__m_engine = FaceEngine(backend=u'{}'.format(backend) if u'{}'.format(backend) in ('auto', 'yunet', 'haar') else 'auto',
100
+ scoreThreshold=scoreThreshold, nmsThreshold=nmsThreshold,
101
+ minFaceSize=minFaceSize, maxInputSize=maxInputSize, modelPath=u'{}'.format(modelPath),
102
+ dataDir=appDataDir(), onError=self.errorOccurred.emit) # type: FaceEngine
103
+ self.__m_downloading = False # type: bool
104
+
105
+ # ---------- Helpers ----------
106
+ @staticmethod
107
+ def isAvailable():
108
+ """
109
+ :return: (bool) OpenCV and NumPy are installed.
110
+ """
111
+ return FaceEngine.isAvailable()
112
+
113
+ @staticmethod
114
+ def hasYuNetSupport():
115
+ """
116
+ :return: (bool) This OpenCV build has FaceDetectorYN (4.5.4+).
117
+ """
118
+ return FaceEngine.hasYuNetSupport()
119
+
120
+ def findModel(self):
121
+ """
122
+ :return: (str | unicode | QString) Path of a verified YuNet model, '' if none is available.
123
+ """
124
+ return self.__m_engine.findModel()
125
+
126
+ def isModelReady(self):
127
+ """
128
+ :return: bool
129
+ """
130
+ return self.__m_engine.isModelReady()
131
+
132
+ def getActiveBackend(self):
133
+ """
134
+ :return: (str | unicode | QString) 'yunet', 'haar' or '' (nothing usable).
135
+ """
136
+ return self.__m_engine.getActiveBackend()
137
+
138
+ def _download(self, target):
139
+ """
140
+ Worker thread: fetch, verify and atomically install the YuNet model.
141
+ :param target: str | unicode | QString
142
+ :return:
143
+ """
144
+ try:
145
+ downloadModel(target, self.downloadProgress.emit)
146
+ self.__m_engine.resetModel()
147
+ self.__m_downloading = False # type: bool
148
+ self.downloadFinished.emit(True, target)
149
+ except Exception as e:
150
+ self.__m_downloading = False # type: bool
151
+ self.downloadFinished.emit(False, u'YuNet model download failed: {}'.format(e))
152
+
153
+ # ---------- Public API ----------
154
+ def detect(self, image):
155
+ """
156
+ :param image: QImage
157
+ :return: (dict) faces [{rect, score, landmarks}], backend, width, height, timeMs.
158
+ """
159
+ if image is None or image.isNull() or not self.__m_engine.getActiveBackend():
160
+ return {'faces': [], 'backend': '', 'width': 0, 'height': 0, 'timeMs': 0.0}
161
+ return self.__m_engine.detect(qimageToArray(image))
162
+
163
+ @pyqtSlot()
164
+ def downloadModel(self):
165
+ """
166
+ Download the YuNet model in the background (downloadProgress / downloadFinished report the result).
167
+ :return:
168
+ """
169
+ if self.__m_downloading:
170
+ return
171
+ folder = modelDirs()[-1] # type: str
172
+ for d in modelDirs(): # First folder that exists (or can be created) and is writable.
173
+ try:
174
+ makedirs(d, exist_ok=True)
175
+ if access(d, W_OK):
176
+ folder = d # type: str
177
+ break
178
+ except Exception:
179
+ continue
180
+ self.__m_downloading = True # type: bool
181
+ worker = Thread(target=self._download, args=(join(folder, YUNET_FILE),), name='YuNetDownload')
182
+ worker.daemon = True
183
+ worker.start()
184
+
185
+ def isDownloading(self):
186
+ """
187
+ :return: bool
188
+ """
189
+ return self.__m_downloading
190
+
191
+ def getBackend(self):
192
+ """
193
+ :return: str | unicode | QString
194
+ """
195
+ return self.__m_engine.backend
196
+
197
+ @pyqtSlot('QString')
198
+ @pyqtSlot(str)
199
+ def setBackend(self, backend):
200
+ """
201
+ :param backend: (str | unicode | QString) 'auto', 'yunet' or 'haar'.
202
+ :return:
203
+ """
204
+ backend = u'{}'.format(backend) if u'{}'.format(backend) in ('auto', 'yunet', 'haar') else 'auto' # type str
205
+ if backend != self.__m_engine.backend:
206
+ self.__m_engine.backend = backend # type str
207
+ self.backendChanged.emit(backend)
208
+
209
+ def getScoreThreshold(self):
210
+ """
211
+ :return: float | int
212
+ """
213
+ return self.__m_engine.scoreThreshold
214
+
215
+ @pyqtSlot(float)
216
+ @pyqtSlot(int)
217
+ def setScoreThreshold(self, scoreThreshold):
218
+ """
219
+ :param scoreThreshold: float
220
+ :return:
221
+ """
222
+ self.__m_engine.scoreThreshold = clamp(float(scoreThreshold), 0.1, 0.99) # type float
223
+
224
+ def getNmsThreshold(self):
225
+ """
226
+ :return: float | int
227
+ """
228
+ return self.__m_engine.nmsThreshold
229
+
230
+ @pyqtSlot(float)
231
+ @pyqtSlot(int)
232
+ def setNmsThreshold(self, nmsThreshold):
233
+ """
234
+ :param nmsThreshold: float | int
235
+ :return:
236
+ """
237
+ self.__m_engine.nmsThreshold = clamp(float(nmsThreshold), 0.05, 0.95) # type float
238
+
239
+ def getMinFaceSize(self):
240
+ """
241
+ :return: int
242
+ """
243
+ return self.__m_engine.minFaceSize
244
+
245
+ @pyqtSlot(int)
246
+ def setMinFaceSize(self, minFaceSize):
247
+ """
248
+ :param minFaceSize: int
249
+ :return:
250
+ """
251
+ self.__m_engine.minFaceSize = max(8, int(minFaceSize)) # type int
252
+
253
+ def getMaxInputSize(self):
254
+ """
255
+ :return: int
256
+ """
257
+ return self.__m_engine.maxInputSize
258
+
259
+ @pyqtSlot(int)
260
+ def setMaxInputSize(self, maxInputSize):
261
+ """
262
+ :param maxInputSize: int
263
+ :return:
264
+ """
265
+ self.__m_engine.maxInputSize = max(160, int(maxInputSize)) # type int
266
+
267
+ def getModelPath(self):
268
+ """
269
+ :return: str | unicode | QString
270
+ """
271
+ return self.__m_engine.getModelPath()
272
+
273
+ @pyqtSlot('QString')
274
+ @pyqtSlot(str)
275
+ def setModelPath(self, modelPath):
276
+ """
277
+ :param modelPath: (str | unicode | QString) '' = search the default folders.
278
+ :return:
279
+ """
280
+ self.__m_engine.setModelPath(u'{}'.format(modelPath))
281
+
282
+ # ---------- Properties ----------
283
+ backend = pyqtProperty('QString', fget=getBackend, fset=setBackend, notify=backendChanged) # type: pyqtProperty
284
+ scoreThreshold = pyqtProperty(float, fget=getScoreThreshold, fset=setScoreThreshold) # type: pyqtProperty
285
+ nmsThreshold = pyqtProperty(float, fget=getNmsThreshold, fset=setNmsThreshold) # type: pyqtProperty
286
+ minFaceSize = pyqtProperty(int, fget=getMinFaceSize, fset=setMinFaceSize) # type: pyqtProperty
287
+ maxInputSize = pyqtProperty(int, fget=getMaxInputSize, fset=setMaxInputSize) # type: pyqtProperty
288
+ modelPath = pyqtProperty('QString', fget=getModelPath, fset=setModelPath) # type: pyqtProperty
@@ -0,0 +1,23 @@
1
+ # coding=utf-8
2
+ """
3
+ Entry point for the FaceDetector 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_())