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,636 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ RemoteClient: offloads upscaling and analysis to an AnalysisServer (server.py) in real time.
4
+ Flow control: per channel (e.g. 'view', 'lens') and per analysis source at most ONE request is in flight; a newer
5
+ frame replaces the waiting one ("latest frame wins"), so a slow network or server adds latency but never a
6
+ growing backlog. RemoteProcessor plugs into MagnifierViewWidget / ZoomOverlay as a normal processor: it never
7
+ blocks painting - until the server answers it shows the last server result (if recent) or a fast local scale.
8
+ """
9
+ from ManyQt.QtCore import QObject, QTimer, QSize, QElapsedTimer, Qt, pyqtSignal, pyqtSlot, pyqtProperty
10
+ from ManyQt.QtGui import QImage
11
+ from os.path import dirname
12
+ from zlib import adler32
13
+ from sys import path
14
+
15
+ if not __package__ and dirname(__file__) not in path:
16
+ path.append(dirname(__file__))
17
+
18
+ try:
19
+ from .netprotocol import FrameChannel, encodeImage, decodeImage, DEFAULT_PORT
20
+ except:
21
+ from netprotocol import FrameChannel, encodeImage, decodeImage, DEFAULT_PORT
22
+
23
+ REQUEST_TIMEOUT_MS = 3000 # type: int # An unanswered request is dropped after this (lost / server stuck).
24
+
25
+
26
+ def imageKey(image):
27
+ """
28
+ Cheap content fingerprint, so the same crop is never sent twice.
29
+ :param image: QImage
30
+ :return: int
31
+ """
32
+ ptr = image.constBits()
33
+ n = image.sizeInBytes() if hasattr(image, 'sizeInBytes') else image.byteCount() # type: int
34
+ if hasattr(ptr, 'setsize'):
35
+ ptr.setsize(n)
36
+ return adler32(bytes(ptr)) ^ (image.width() << 16) ^ image.height()
37
+
38
+
39
+ def localScale(image, size):
40
+ """
41
+ Fallback processor while waiting for the server.
42
+ :param image: QImage
43
+ :param size: QSize
44
+ :return: QImage
45
+ """
46
+ return image.scaled(size, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
47
+
48
+
49
+ class RemoteProcessor(object):
50
+ """
51
+ RemoteProcessor class: callable(QImage, QSize) -> QImage backed by a RemoteClient channel.
52
+ """
53
+
54
+ def __init__(self, client, channel, fallback=None, maxAgeMs=300):
55
+ """
56
+ :param client: RemoteClient
57
+ :param channel: (str | unicode | QString) Channel name ('view', 'lens').
58
+ :param fallback: (callable | None) Used when the server is unavailable (e.g. the local upscaler);
59
+ None = smooth Qt scaling. While waiting for the server a cheap Qt scale is always used, so the
60
+ expensive local processing is never duplicated.
61
+ :param maxAgeMs: (int) How long an older server result may still be shown while waiting.
62
+ """
63
+ self.__m_client = client # type: RemoteClient
64
+ self.__m_channel = u'{}'.format(channel) # type: str
65
+ self.__m_fallback = fallback if callable(fallback) else localScale # type: callable
66
+ self.__m_maxAgeMs = int(maxAgeMs) # type: int
67
+
68
+ def __call__(self, image, size):
69
+ """
70
+ :param image: QImage
71
+ :param size: QSize
72
+ :return: QImage
73
+ """
74
+ return self.process(image, size)
75
+
76
+ def process(self, image, size):
77
+ """
78
+ :param image: QImage
79
+ :param size: QSize
80
+ :return: QImage
81
+ """
82
+ client = self.__m_client # type: RemoteClient
83
+ if not client.isReady():
84
+ return self.__m_fallback(image, size)
85
+ key = (imageKey(image), size.width(), size.height()) # type: tuple
86
+ exact = client.cachedResult(self.__m_channel, key) # type: QImage | None
87
+ if exact is not None:
88
+ return exact
89
+ client.submitUpscale(self.__m_channel, image, size, key)
90
+ recent = client.recentResult(self.__m_channel, size, self.__m_maxAgeMs) # type: QImage | None
91
+ return recent if recent is not None else localScale(image, size)
92
+
93
+ def getChannel(self):
94
+ """
95
+ :return: str | unicode | QString
96
+ """
97
+ return self.__m_channel
98
+
99
+
100
+ class RemoteClient(QObject):
101
+ """
102
+ RemoteClient class.
103
+ """
104
+ stateChanged = pyqtSignal('QString') # type: pyqtSignal
105
+ upscaleReady = pyqtSignal('QString') # type: pyqtSignal
106
+ analysisReady = pyqtSignal('QString', object, object) # type: pyqtSignal # source, result dict, mask QImage
107
+ errorOccurred = pyqtSignal('QString') # type: pyqtSignal
108
+ statsChanged = pyqtSignal() # type: pyqtSignal
109
+
110
+ def __init__(self, *args, **kwargs):
111
+ """
112
+ :param host: str | unicode | QString
113
+ :param port: int
114
+ :param token: (str | unicode | QString) Shared secret expected by the server.
115
+ :param autoReconnect: (bool) Reconnect every 3 s after an unexpected disconnection.
116
+ :param uploadFormat: (str | unicode | QString) 'JPG' (default, fast) or 'PNG' (lossless, slow to encode).
117
+ :param resultFormat: (str | unicode | QString) 'JPG' (default, fast) or 'PNG' (lossless).
118
+ :param quality: (int) JPEG quality 1 .. 100 of the results.
119
+ :param uploadQuality: (int) JPEG quality 1 .. 100 of uploaded frames (95: near lossless, far faster than PNG).
120
+ :param method: (int) Upscaler method the server must use (Upscaler.Method value).
121
+ :param sharpness: (float | int) Server-side sharpening 0 .. 2.
122
+ :param stateChanged: callable | None
123
+ :param errorOccurred: callable | None
124
+ :param args: any
125
+ :param kwargs: any
126
+ """
127
+ host = kwargs.pop('host', '127.0.0.1') # type: str
128
+ port = kwargs.pop('port', DEFAULT_PORT) # type: int
129
+ token = kwargs.pop('token', '') # type: str
130
+ autoReconnect = kwargs.pop('autoReconnect', True) # type: bool
131
+ uploadFormat = kwargs.pop('uploadFormat', 'JPG') # type: str
132
+ resultFormat = kwargs.pop('resultFormat', 'JPG') # type: str
133
+ quality = kwargs.pop('quality', 90) # type: int
134
+ uploadQuality = kwargs.pop('uploadQuality', 95) # type: int
135
+ method = kwargs.pop('method', 3) # type: int
136
+ sharpness = kwargs.pop('sharpness', 0.25) # type: float
137
+ stateChanged = kwargs.pop('stateChanged', None) # type: callable | None
138
+ errorOccurred = kwargs.pop('errorOccurred', None) # type: callable | None
139
+ super(RemoteClient, self).__init__(*args, **kwargs)
140
+ if callable(stateChanged):
141
+ self.stateChanged.connect(stateChanged)
142
+ if callable(errorOccurred):
143
+ self.errorOccurred.connect(errorOccurred)
144
+ self.__m_host = u'{}'.format(host) # type: str
145
+ self.__m_port = int(port) # type: int
146
+ self.__m_token = token # type: str
147
+ self.__m_autoReconnect = bool(autoReconnect) # type: bool
148
+ self.__m_uploadFormat = 'JPG' if u'{}'.format(uploadFormat).upper() in ('JPG', 'JPEG') else 'PNG' # type: str
149
+ self.__m_resultFormat = 'PNG' if u'{}'.format(resultFormat).upper() == 'PNG' else 'JPG' # type: str
150
+ self.__m_quality = max(1, min(100, int(quality))) # type: int
151
+ self.__m_uploadQuality = max(1, min(100, int(uploadQuality))) # type: int
152
+ self.__m_method = int(method) # type: int
153
+ self.__m_sharpness = float(sharpness) # type: float
154
+ # State.
155
+ self.__m_state = 'disconnected' # type: str
156
+ self.__m_wanted = False # type: bool # The user asked to be connected.
157
+ self.__m_serverInfo = {} # type: dict
158
+ self.__m_nextId = 1 # type: int
159
+ self.__m_inflight = {} # type: dict # id -> (kind, name, key, sentMs, size)
160
+ self.__m_busy = {} # type: dict # (kind, name) -> id
161
+ self.__m_pending = {} # type: dict # (kind, name) -> args of the newest waiting request
162
+ self.__m_results = {} # type: dict # channel -> list of (key, QImage, size, receivedMs), newest last
163
+ self.__m_latencyMs = -1.0 # type: float
164
+ self.__m_counts = {} # type: dict # name -> results received (for fps)
165
+ self.__m_clock = QElapsedTimer() # type: QElapsedTimer
166
+ self.__m_clock.start()
167
+ self.__m_channel = FrameChannel(messageReceived=self._onMessage, connected=self._onConnected,
168
+ disconnected=self._onDisconnected, errorOccurred=self._onError,
169
+ parent=self) # type: FrameChannel
170
+ self.__m_reconnect = QTimer(self) # type: QTimer
171
+ self.__m_reconnect.setSingleShot(True)
172
+ self.__m_reconnect.timeout.connect(self._reconnect)
173
+ self.__m_pinger = QTimer(self) # type: QTimer
174
+ self.__m_pinger.timeout.connect(self._ping)
175
+
176
+ # ---------- Helpers ----------
177
+ def _setState(self, state):
178
+ """
179
+ :param state: (str | unicode | QString) disconnected | connecting | authenticating | ready | error
180
+ :return:
181
+ """
182
+ if state != self.__m_state:
183
+ self.__m_state = state # type: str
184
+ self.stateChanged.emit(state)
185
+
186
+ def _now(self):
187
+ """
188
+ :return: (int) ms since the client was created.
189
+ """
190
+ return int(self.__m_clock.elapsed())
191
+
192
+ def _reset(self):
193
+ """
194
+ Forget everything that belonged to the previous connection.
195
+ :return:
196
+ """
197
+ self.__m_inflight, self.__m_busy, self.__m_pending = {}, {}, {}
198
+
199
+ @pyqtSlot()
200
+ def _onConnected(self):
201
+ """
202
+ :return:
203
+ """
204
+ self._setState('authenticating')
205
+ self.__m_channel.send({'type': 'hello', 'token': self.__m_token, 'client': 'ScreenMagnifier',
206
+ 'version': 1})
207
+
208
+ @pyqtSlot()
209
+ def _onDisconnected(self):
210
+ """
211
+ :return:
212
+ """
213
+ self.__m_pinger.stop()
214
+ self._reset()
215
+ if self.__m_state != 'error':
216
+ self._setState('disconnected')
217
+ if self.__m_wanted and self.__m_autoReconnect:
218
+ self.__m_reconnect.start(3000)
219
+
220
+ @pyqtSlot('QString')
221
+ @pyqtSlot(str)
222
+ def _onError(self, message):
223
+ """
224
+ :param message: str | unicode | QString
225
+ :return:
226
+ """
227
+ self.errorOccurred.emit(message)
228
+ if self.__m_state in ('connecting', 'authenticating'):
229
+ self._setState('error')
230
+ if self.__m_wanted and self.__m_autoReconnect:
231
+ self.__m_reconnect.start(3000)
232
+
233
+ @pyqtSlot()
234
+ def _reconnect(self):
235
+ """
236
+ :return:
237
+ """
238
+ if self.__m_wanted and self.__m_state != 'ready':
239
+ self._setState('connecting')
240
+ self.__m_channel.connectToHost(self.__m_host, self.__m_port)
241
+
242
+ @pyqtSlot()
243
+ def _ping(self):
244
+ """
245
+ Measure latency and expire lost requests.
246
+ :return:
247
+ """
248
+ now = self._now() # type: int
249
+ for rid, item in list(self.__m_inflight.items()):
250
+ if now - item[3] > REQUEST_TIMEOUT_MS:
251
+ self._finish(rid)
252
+ self.__m_channel.send({'type': 'ping', 't': now})
253
+ self.statsChanged.emit()
254
+
255
+ def _finish(self, rid):
256
+ """
257
+ A request is answered (or expired): free its slot and send the waiting one, if any.
258
+ :param rid: int
259
+ :return: (tuple | None) The in-flight record.
260
+ """
261
+ item = self.__m_inflight.pop(rid, None)
262
+ if item is None:
263
+ return None
264
+ slot = (item[0], item[1]) # type: tuple
265
+ if self.__m_busy.get(slot) == rid:
266
+ del self.__m_busy[slot]
267
+ waiting = self.__m_pending.pop(slot, None)
268
+ if waiting is not None:
269
+ if item[0] == 'upscale':
270
+ self.submitUpscale(*waiting)
271
+ else:
272
+ self.requestAnalysis(*waiting)
273
+ return item
274
+
275
+ def _onMessage(self, header, payload):
276
+ """
277
+ :param header: dict
278
+ :param payload: bytes
279
+ :return:
280
+ """
281
+ kind = u'{}'.format(header.get('type', '')) # type: str
282
+ if kind == 'welcome':
283
+ self.__m_serverInfo = dict(header) # type: dict
284
+ self._setState('ready')
285
+ self.__m_pinger.start(2000)
286
+ self._ping()
287
+ elif kind == 'pong':
288
+ try:
289
+ self.__m_latencyMs = float(self._now() - int(header.get('t'))) # type: float
290
+ except (TypeError, ValueError):
291
+ pass
292
+ elif kind == 'upscaled':
293
+ item = self._finish(header.get('id'))
294
+ image = decodeImage(payload) # type: QImage
295
+ if item is not None and not image.isNull():
296
+ name = item[1] # type: str
297
+ results = self.__m_results.setdefault(name, []) # type: list
298
+ results.append((item[2], image, item[4], self._now()))
299
+ del results[:-3]
300
+ self.__m_counts[name] = self.__m_counts.get(name, 0) + 1
301
+ self.upscaleReady.emit(name)
302
+ elif kind == 'analysis':
303
+ item = self._finish(header.get('id'))
304
+ if item is not None:
305
+ mask = decodeImage(payload) if header.get('hasMask') else QImage() # type: QImage
306
+ name = item[1] # type: str
307
+ self.__m_counts['analysis:' + name] = self.__m_counts.get('analysis:' + name, 0) + 1
308
+ result = dict(header.get('result') or {}) # type: dict
309
+ result['origin'] = header.get('origin') # Screen position of the analysed frame (echoed).
310
+ self.analysisReady.emit(name, result, mask)
311
+ elif kind == 'error':
312
+ if header.get('code') == 'auth':
313
+ self.__m_wanted = False # type: bool # Do not hammer a server that refuses us.
314
+ self._setState('error')
315
+ else:
316
+ self._finish(header.get('id'))
317
+ self.errorOccurred.emit(u'Server: {}'.format(header.get('message', 'error')))
318
+
319
+ def _send(self, kind, name, key, size, header, image, fmt, quality):
320
+ """
321
+ :param kind: str | unicode | QString
322
+ :param name: str | unicode | QString
323
+ :param key: tuple | None
324
+ :param size: QSize | None
325
+ :param header: dict
326
+ :param image: QImage
327
+ :param fmt: str | unicode | QString
328
+ :param quality: (int) JPEG quality.
329
+ :return: bool
330
+ """
331
+ rid = self.__m_nextId # type: int
332
+ self.__m_nextId += 1
333
+ header['id'] = rid
334
+ accepted = self.__m_serverInfo.get('formats') if self.__m_serverInfo else None # older servers: all
335
+ if accepted and fmt not in accepted: # e.g. a phone server without a JPEG codec: lossless RAWZ instead
336
+ fmt = 'RAWZ' # type: str
337
+ data = encodeImage(image, fmt, quality if fmt == 'JPG' else -1) # type: bytes
338
+ if not self.__m_channel.send(header, data):
339
+ return False
340
+ self.__m_inflight[rid] = (kind, name, key, self._now(), size)
341
+ self.__m_busy[(kind, name)] = rid
342
+ return True
343
+
344
+ # ---------- Public API ----------
345
+ @pyqtSlot()
346
+ def connectToServer(self):
347
+ """
348
+ Connect with the current host / port / token (see setters).
349
+ :return:
350
+ """
351
+ self.__m_wanted = True # type: bool
352
+ self.__m_reconnect.stop()
353
+ self._reset()
354
+ self._setState('connecting')
355
+ self.__m_channel.connectToHost(self.__m_host, self.__m_port)
356
+
357
+ @pyqtSlot()
358
+ def disconnectFromServer(self):
359
+ """
360
+ :return:
361
+ """
362
+ self.__m_wanted = False # type: bool
363
+ self.__m_reconnect.stop()
364
+ self.__m_pinger.stop()
365
+ self.__m_channel.abort()
366
+ self._reset()
367
+ self._setState('disconnected')
368
+
369
+ def isReady(self):
370
+ """
371
+ :return: (bool) Connected and authenticated.
372
+ """
373
+ return self.__m_state == 'ready'
374
+
375
+ def getState(self):
376
+ """
377
+ :return: (str | unicode | QString) disconnected | connecting | authenticating | ready | error
378
+ """
379
+ return self.__m_state
380
+
381
+ def getServerInfo(self):
382
+ """
383
+ :return: (dict) The server's welcome message (methods, faces, server name...).
384
+ """
385
+ return dict(self.__m_serverInfo)
386
+
387
+ def processor(self, channel, fallback=None):
388
+ """
389
+ :param channel: (str | unicode | QString) e.g. 'view' or 'lens'.
390
+ :param fallback: (callable | None) Local processor used while the server is unavailable.
391
+ :return: RemoteProcessor
392
+ """
393
+ return RemoteProcessor(self, channel, fallback)
394
+
395
+ def cachedResult(self, channel, key):
396
+ """
397
+ :param channel: str | unicode | QString
398
+ :param key: tuple
399
+ :return: (QImage | None) Server result for exactly this crop and size.
400
+ """
401
+ for k, image, size, t in reversed(self.__m_results.get(u'{}'.format(channel), [])):
402
+ if k == key:
403
+ return image
404
+ return None
405
+
406
+ def recentResult(self, channel, size, maxAgeMs):
407
+ """
408
+ :param channel: str | unicode | QString
409
+ :param size: QSize
410
+ :param maxAgeMs: int
411
+ :return: (QImage | None) Newest server result of this size that is not older than maxAgeMs.
412
+ """
413
+ results = self.__m_results.get(u'{}'.format(channel), []) # type: list
414
+ if results:
415
+ key, image, s, t = results[-1]
416
+ if s == size and self._now() - t <= maxAgeMs:
417
+ return image
418
+ return None
419
+
420
+ def submitUpscale(self, channel, image, size, key=None):
421
+ """
422
+ Queue an upscale request ("latest frame wins").
423
+ :param channel: str | unicode | QString
424
+ :param image: QImage
425
+ :param size: QSize
426
+ :param key: (tuple | None) Content key (imageKey, w, h).
427
+ :return:
428
+ """
429
+ if not self.isReady():
430
+ return
431
+ slot = ('upscale', u'{}'.format(channel)) # type: tuple
432
+ if slot in self.__m_busy:
433
+ if self.__m_inflight.get(self.__m_busy[slot], (None, None, None))[2] != key:
434
+ self.__m_pending[slot] = (channel, image, size, key)
435
+ return
436
+ header = {'type': 'upscale', 'channel': u'{}'.format(channel), 'width': size.width(), 'height': size.height(),
437
+ 'method': self.__m_method, 'sharpness': self.__m_sharpness, 'format': self.__m_resultFormat,
438
+ 'quality': self.__m_quality} # type: dict
439
+ self._send('upscale', u'{}'.format(channel), key, QSize(size), header, image, self.__m_uploadFormat,
440
+ self.__m_uploadQuality)
441
+
442
+ def requestAnalysis(self, source, image, params):
443
+ """
444
+ Queue an analysis request ("latest frame wins").
445
+ :param source: (str | unicode | QString) e.g. 'view' or 'lens'.
446
+ :param image: QImage
447
+ :param params: (dict) {'colors': {...}, 'faces': {...}} as in the AnalysisServer protocol.
448
+ :return: None
449
+ """
450
+ if not self.isReady():
451
+ return
452
+ slot = ('analyze', u'{}'.format(source)) # type: tuple
453
+ if slot in self.__m_busy:
454
+ self.__m_pending[slot] = (source, image, params)
455
+ return
456
+ header = dict(params, type='analyze', source=u'{}'.format(source)) # type: dict
457
+ self._send('analyze', u'{}'.format(source), None, None, header, image, 'RAWZ', -1) # Lossless: exact colors.
458
+
459
+ def getLatency(self):
460
+ """
461
+ :return: (float | int) Last measured round-trip time in ms, -1 if unknown.
462
+ """
463
+ return self.__m_latencyMs
464
+
465
+ def takeCounts(self):
466
+ """
467
+ :return: (dict) Results received per channel since the previous call (for fps displays).
468
+ """
469
+ counts, self.__m_counts = self.__m_counts, {}
470
+ return counts
471
+
472
+ def getBytes(self):
473
+ """
474
+ :return: (tuple[int, int]) Total bytes sent, received.
475
+ """
476
+ return self.__m_channel.getBytesSent(), self.__m_channel.getBytesReceived()
477
+
478
+ def getHost(self):
479
+ """
480
+ :return: str | unicode | QString
481
+ """
482
+ return self.__m_host
483
+
484
+ @pyqtSlot('QString')
485
+ @pyqtSlot(str)
486
+ def setHost(self, host):
487
+ """
488
+ :param host: str | unicode | QString
489
+ :return:
490
+ """
491
+ self.__m_host = u'{}'.format(host).strip() # type: str
492
+
493
+ def getPort(self):
494
+ """
495
+ :return: int
496
+ """
497
+ return self.__m_port
498
+
499
+ @pyqtSlot(int)
500
+ def setPort(self, port):
501
+ """
502
+ :param port: int
503
+ :return:
504
+ """
505
+ self.__m_port = int(port) # type: int
506
+
507
+ def getToken(self):
508
+ """
509
+ :return: str | unicode | QString
510
+ """
511
+ return self.__m_token
512
+
513
+ @pyqtSlot('QString')
514
+ @pyqtSlot(str)
515
+ def setToken(self, token):
516
+ """
517
+ :param token: str | unicode | QString
518
+ :return:
519
+ """
520
+ self.__m_token = token # type: str
521
+
522
+ def isAutoReconnect(self):
523
+ """
524
+ :return: bool
525
+ """
526
+ return self.__m_autoReconnect
527
+
528
+ @pyqtSlot(bool)
529
+ @pyqtSlot(int)
530
+ def setAutoReconnect(self, autoReconnect):
531
+ """
532
+ :param autoReconnect: bool
533
+ :return:
534
+ """
535
+ self.__m_autoReconnect = bool(autoReconnect) # type: bool
536
+
537
+ def getUploadFormat(self):
538
+ """
539
+ :return: str | unicode | QString
540
+ """
541
+ return self.__m_uploadFormat
542
+
543
+ @pyqtSlot('QString')
544
+ @pyqtSlot(str)
545
+ def setUploadFormat(self, uploadFormat):
546
+ """
547
+ :param uploadFormat: (str | unicode | QString) 'PNG' or 'JPG'.
548
+ :return:
549
+ """
550
+ self.__m_uploadFormat = 'JPG' if u'{}'.format(uploadFormat).upper() in ('JPG', 'JPEG') else 'PNG' # type: str
551
+
552
+ def getResultFormat(self):
553
+ """
554
+ :return: str | unicode | QString
555
+ """
556
+ return self.__m_resultFormat
557
+
558
+ @pyqtSlot('QString')
559
+ @pyqtSlot(str)
560
+ def setResultFormat(self, resultFormat):
561
+ """
562
+ :param resultFormat: (str | unicode | QString) 'JPG' or 'PNG'.
563
+ :return:
564
+ """
565
+ self.__m_resultFormat = 'PNG' if u'{}'.format(resultFormat).upper() == 'PNG' else 'JPG' # type: str
566
+
567
+ def getQuality(self):
568
+ """
569
+ :return: int
570
+ """
571
+ return self.__m_quality
572
+
573
+ @pyqtSlot(int)
574
+ def setQuality(self, quality):
575
+ """
576
+ :param quality: int
577
+ :return:
578
+ """
579
+ self.__m_quality = max(1, min(100, int(quality))) # type: int
580
+
581
+ def getUploadQuality(self):
582
+ """
583
+ :return: int
584
+ """
585
+ return self.__m_uploadQuality
586
+
587
+ @pyqtSlot(int)
588
+ def setUploadQuality(self, uploadQuality):
589
+ """
590
+ :param uploadQuality: int
591
+ :return:
592
+ """
593
+ self.__m_uploadQuality = max(1, min(100, int(uploadQuality))) # type: int
594
+
595
+ def getMethod(self):
596
+ """
597
+ :return: int
598
+ """
599
+ return self.__m_method
600
+
601
+ @pyqtSlot(int)
602
+ def setMethod(self, method):
603
+ """
604
+ :param method: (int) Upscaler.Method value used by the server.
605
+ :return:
606
+ """
607
+ self.__m_method = int(method) # type: int
608
+ self.__m_results = {} # type: dict # Old results were made with another method.
609
+
610
+ def getSharpness(self):
611
+ """
612
+ :return: float | int
613
+ """
614
+ return self.__m_sharpness
615
+
616
+ @pyqtSlot(float)
617
+ @pyqtSlot(int)
618
+ def setSharpness(self, sharpness):
619
+ """
620
+ :param sharpness: float | int
621
+ :return:
622
+ """
623
+ self.__m_sharpness = float(sharpness) # type: float
624
+ self.__m_results = {} # type: dict
625
+
626
+ # ---------- Properties ----------
627
+ host = pyqtProperty('QString', fget=getHost, fset=setHost) # type: pyqtProperty
628
+ port = pyqtProperty(int, fget=getPort, fset=setPort) # type: pyqtProperty
629
+ token = pyqtProperty('QString', fget=getToken, fset=setToken) # type: pyqtProperty
630
+ autoReconnect = pyqtProperty(bool, fget=isAutoReconnect, fset=setAutoReconnect) # type: pyqtProperty
631
+ uploadFormat = pyqtProperty('QString', fget=getUploadFormat, fset=setUploadFormat) # type: pyqtProperty
632
+ resultFormat = pyqtProperty('QString', fget=getResultFormat, fset=setResultFormat) # type: pyqtProperty
633
+ quality = pyqtProperty(int, fget=getQuality, fset=setQuality) # type: pyqtProperty
634
+ uploadQuality = pyqtProperty(int, fget=getUploadQuality, fset=setUploadQuality) # type: pyqtProperty
635
+ method = pyqtProperty(int, fget=getMethod, fset=setMethod) # type: pyqtProperty
636
+ sharpness = pyqtProperty(float, fget=getSharpness, fset=setSharpness) # type: pyqtProperty
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,16 @@
1
+ # coding=utf-8
2
+ """
3
+ screengrabber package.
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 .screengrabber import ScreenGrabber, winWindowAt, winGrabWindowRect, isBlankImage
13
+ except:
14
+ from screengrabber import ScreenGrabber, winWindowAt, winGrabWindowRect, isBlankImage
15
+
16
+ __all__ = ['ScreenGrabber', 'winWindowAt', 'winGrabWindowRect', 'isBlankImage'] # type: list[str]