-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr_scanner_thread.py
205 lines (166 loc) · 8.05 KB
/
qr_scanner_thread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""
This file is part of the PetalVault password manager distribution. See <https://github.com/F33RNI/PetalVault>.
Copyright (C) 2024 Fern Lane
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see <http://www.gnu.org/licenses/>.
"""
import base64
import json
import logging
import threading
from typing import override
import cv2
import numpy as np
from PyQt6 import QtCore, QtGui
from get_resource_path import get_resource_path
COLOR_OK = (190, 203, 106)
COLOR_ERROR = (136, 125, 219)
WORDLIST_FILE = get_resource_path("wordlist.txt")
class QRScannerThread(threading.Thread, QtCore.QObject): # pyright: ignore[reportUnsafeMultipleInheritance]
# See ScanDialog for more info
set_image_signal = QtCore.pyqtSignal(QtGui.QImage)
received_part_flag_signal = QtCore.pyqtSignal(tuple)
finished_signal = QtCore.pyqtSignal()
def __init__(self, camera_index: int, expected_data: str):
"""Initializes QRScannerThread instance
Args:
camera_index (int): ID of camera (use 0 for default camera)
expected_data (str): "mnemonic" or "actions"
"""
threading.Thread.__init__(self)
QtCore.QObject.__init__(self)
self._camera_index = camera_index
self._expected_data = expected_data
self._scanned_idxs = []
# Dialog result
self.actions = []
self.sync_salt = None
self.mnemonic = None
self.exception = None
# Load wordlist and Mnemonic instance for checking
self._wordlist = []
logging.debug(f"Loading words from {WORDLIST_FILE} file")
with open(WORDLIST_FILE, "r", encoding="utf-8") as file:
for line in file:
line = line.strip()
if line:
self._wordlist.append(line)
logging.debug(f"Loaded {len(self._wordlist)} words")
self._exit_flag = False
@override
def run(self):
"""OpenCV scanning loop"""
# Open camera and try to read the first frame
logging.debug(f"Opening camera: {self._camera_index}")
self.exception = None
self._scanned_idxs.clear()
try:
capture = cv2.VideoCapture(self._camera_index)
ret, _ = capture.read()
if not ret:
raise Exception(f"Unable to open camera {self._camera_index}. Try to change camera index")
# Initialize QR decoder
logging.debug("Initializing QRCodeDetector instance")
detector = cv2.QRCodeDetector()
self._exit_flag = False
while not self._exit_flag:
# Read one frame
ret, frame = capture.read()
if not ret or frame is None:
raise Exception(f"Error reading frame from camera {self._camera_index}")
data = None
color = COLOR_ERROR
try:
# Read QR codes
data, points, _ = detector.detectAndDecode(frame)
if data:
# Decode actions (JSON)
if self._expected_data == "actions":
# "i": part index, "n": total parts, "acts": [{"act": sync, "id": 123, "iv": ., "enc": .}]
data_dict = json.loads("{" + data + "}")
# Check part (just in case)
part_idx = data_dict.get("i", 0)
parts_total = data_dict.get("n", 1)
if part_idx >= parts_total:
raise Exception("Wrong index or number of parts")
logging.debug(f"Received part {part_idx + 1} / {parts_total}")
# Check for salt
if "salt" in data_dict:
self.sync_salt = base64.b64decode(data_dict["salt"].encode("utf-8"))
if len(self.sync_salt) != 32:
raise Exception("Sync salt is not 32 bytes long")
logging.debug(f"Received salt: {self.sync_salt}")
# Add to the final list
for action in data_dict.get("acts", []):
if action not in self.actions:
self.actions.append(action)
# Build flags
if len(self._scanned_idxs) == 0:
self._scanned_idxs = [False for _ in range(parts_total)]
# Set received part
self._scanned_idxs[part_idx] = True
# All parts
if all(scanned_idx == True for scanned_idx in self._scanned_idxs):
logging.debug("Received all parts")
self._exit_flag = True
# Show progress
if parts_total != 1:
self.received_part_flag_signal.emit((part_idx, parts_total))
# Decode and check mnemonic phrase
elif self._expected_data == "mnemonic":
words = data.strip().lower().split(" ")
# For now, accept only 12-word mnemonic
if len(words) != 12:
raise Exception("Mnemonic phrase must be 12 words long")
# Check each word
for word in words:
if word not in self._wordlist:
raise Exception(f"{word} is not a mnemonic phrase word")
# Seems OK
self.mnemonic = words
self._exit_flag = True
else:
raise Exception(
f'expected_data must be "actions" or "mnemonic", not {self._expected_data}'
)
# Use OK color because we was able to decode it
color = COLOR_OK
except Exception as e:
logging.warning(f"Unable to read or parse QR-code data {data}: {e}. Wrong QR code?")
continue
# Draw bounding lines
if data:
points = points.astype(np.int32)
_, _, width, height = cv2.boundingRect(points)
line_thickness = max(1, (width + height) // 2 // 30)
frame = cv2.polylines(frame, points.astype(np.int32), True, color, line_thickness, cv2.LINE_AA)
# Convert to QImage and push to GUI
image = QtGui.QImage(
frame,
frame.shape[1],
frame.shape[0],
frame.strides[0],
QtGui.QImage.Format.Format_BGR888,
)
self.set_image_signal.emit(image)
# Cleanup
logging.debug("Closing camera")
capture.release()
cv2.destroyAllWindows()
except Exception as e:
logging.error("Error capturing frame", exc_info=e)
self.exception = e
self.finished_signal.emit()
def cancel(self):
"""Cancels running scanner
(clears self._exit_flag) Call this to stop QR scanning thread
"""
if self._exit_flag:
return
logging.debug("qr_scanner_thread cancel requested")
self._exit_flag = True