forked from cta-wave/device-observation-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdpctf_qr_decoder.py
294 lines (254 loc) · 9.11 KB
/
dpctf_qr_decoder.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# -*- coding: utf-8 -*-
"""WAVE DPCTF QR code decoder
Translates the detected QR codes into the different QR codes type.
DPCTF specific QR codes - MezzanineDecodedQr, TestStatusDecodedQr and PreTestDecodedQr
The Software is provided to you by the Licensor under the License, as
defined below, subject to the following condition.
Without limiting other conditions in the License, the grant of rights under
the License will not include, and the License does not grant to you, the
right to Sell the Software.
For purposes of the foregoing, “Sell” means practicing any or all of the
rights granted to you under the License to provide to third parties, for a
fee or other consideration (including without limitation fees for hosting
or consulting/ support services related to the Software), a product or
service whose value derives, entirely or substantially, from the
functionality of the Software. Any license notice or attribution required
by the License must also include this Commons Clause License Condition
notice.
Software: WAVE Observation Framework
License: Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0.txt
Licensor: Consumer Technology Association
Contributor: Resillion UK Limited
"""
import json
import logging
import re
from datetime import datetime
from fractions import Fraction
from qr_recognition.qr_decoder import DecodedQr, QrDecoder
logger = logging.getLogger(__name__)
_mezzanine_qr_data_re = re.compile(
r"(.+);(\d{2}:[0-6][0-9]:[0-6][0-9].\d{3});(\d{7});([0-9.]+)"
)
class MezzanineDecodedQr(DecodedQr):
"""A decoded QR code from Mezzanine content
ID;HH:MM:SS.MMM;<frame #>;<frame-rate>
"""
data: str
"""qr code string"""
location: list
"""qr code location"""
detection_count: int
"""qr code detection count"""
content_id: str
"""The content id encoded in this QR code"""
media_time: float
"""The media time encoded in this QR code"""
frame_number: int
"""The media time encoded in this QR code"""
frame_rate: Fraction
"""The frame rate encoded in this QR code"""
first_camera_frame_num: int
"""recorded camera frame number that the QR code is detected on"""
last_camera_frame_num: int
"""recorded camera frame number that the QR code last appears on"""
def __init__(
self,
data: str,
location: list,
detection_count: int,
content_id: str,
media_time: float,
frame_number: int,
frame_rate: Fraction,
camera_frame_num: int,
):
super().__init__(data, location)
self.data = data
self.location = location
self.detection_count = detection_count
self.content_id = content_id
self.media_time = media_time
self.frame_number = frame_number
self.frame_rate = frame_rate
self.first_camera_frame_num = camera_frame_num
self.last_camera_frame_num = camera_frame_num
class TestStatusDecodedQr(DecodedQr):
"""A decoded QR code for Test Runner status
QR code in json format contain following info
"""
data: str
""" qr code string"""
location: list
"""qr code location"""
status: str
last_action: str
current_time: float
delay: int
camera_frame_num: int
"""recorded camera frame number that the QR code is detected on"""
def __init__(
self,
data: str,
location: list,
status: str,
last_action: str,
current_time: float,
delay: int,
camera_frame_num: int,
):
super().__init__(data, location)
self.data = data
self.location = location
self.status = status
self.last_action = last_action
self.current_time = current_time
self.delay = delay
self.camera_frame_num = camera_frame_num
class PreTestDecodedQr(DecodedQr):
"""A decoded QR code for pre test
QR code in json format contain following info
"""
data: str
""" qr code string"""
location: list
"""qr code location"""
session_token: str
"""session token encoded in the test runner QR code.
"""
test_id: str
"""test id encoded in the test runner QR code.
"""
first_camera_frame_num: int
"""recorded camera frame number that the QR code is detected on"""
last_camera_frame_num: int
"""recorded camera frame number that the QR code last appears on"""
def __init__(
self,
data: str,
location: list,
session_token: str,
test_id: str,
camera_frame_num: int,
):
super().__init__(data, location)
self.data = data
self.location = location
self.session_token = session_token
self.test_id = test_id
self.first_camera_frame_num = camera_frame_num
self.last_camera_frame_num = camera_frame_num
class DPCTFQrDecoder(QrDecoder):
"""DPCTF QR code Decoder"""
@staticmethod
def translate_qr_test_runner(
data: str, location: list, json_data, camera_frame_num: int
) -> DecodedQr:
"""translate different type of test runner qr code"""
code = DecodedQr("", [])
try:
code = TestStatusDecodedQr(
data,
location,
json_data["s"],
json_data["a"],
float(json_data["ct"]),
int(json_data["d"]),
camera_frame_num,
)
except KeyError:
try:
code = TestStatusDecodedQr(
data,
location,
json_data["s"],
json_data["a"],
0,
int(json_data["d"]),
camera_frame_num,
)
except KeyError:
try:
code = TestStatusDecodedQr(
data,
location,
json_data["s"],
json_data["a"],
0,
0,
camera_frame_num,
)
except KeyError:
try:
code = PreTestDecodedQr(
data,
location,
json_data["session_token"],
json_data["test_id"],
camera_frame_num,
)
except KeyError:
logger.debug(
"Unrecognized QR code detected: %s is ignored.", data
)
return code
@staticmethod
def media_time_str_to_ms(media_time_str: str) -> float:
"""Change media time string to ms
return media time from mezzanine QR code in milliseconds
"""
media_time_datetime = datetime.strptime(media_time_str, "%H:%M:%S.%f")
ms = media_time_datetime.microsecond / 1000
s_to_ms = media_time_datetime.second * 1000
min_to_ms = media_time_datetime.minute * 60 * 1000
h_to_ms = media_time_datetime.hour * 60 * 60 * 1000
media_time = ms + s_to_ms + min_to_ms + h_to_ms
return media_time
@staticmethod
def frame_rate_str_to_fraction(frame_rate_str: str) -> Fraction:
"""Convert string frame rate to float
fractional frame rate fund match from map to get accurate number"""
frame_rate_map = {}
with open("frame_rate_map.json", encoding="utf-8") as f:
frame_rate_map = json.load(f)
try:
res = frame_rate_map[frame_rate_str].split("/")
frame_rate = Fraction(int(res[0]), int(res[1]))
except KeyError:
frame_rate = Fraction(float(frame_rate_str))
return frame_rate
def translate_qr(
self, data: str, location: list, camera_frame_num: int
) -> DecodedQr:
"""Given a QR code as reported by pyzbar, parse the data and convert it to
the format we use.
Returns the translated QR code, or None if it's not a valid QR code.
Mezzanine QR code is higher priority and test status than the start test QR code.
"""
code = DecodedQr("", [])
match = _mezzanine_qr_data_re.match(data)
if match:
# matches a mezzanine signature so decode it as such
media_time = DPCTFQrDecoder.media_time_str_to_ms(match.group(2))
frame_rate = DPCTFQrDecoder.frame_rate_str_to_fraction(match.group(4))
code = MezzanineDecodedQr(
data,
location,
1,
match.group(1),
media_time,
int(match.group(3)),
frame_rate,
camera_frame_num,
)
else:
try:
json_data = json.loads(data)
code = DPCTFQrDecoder.translate_qr_test_runner(
data, location, json_data, camera_frame_num
)
except json.decoder.JSONDecodeError:
logger.debug(
"QR code '%s' is not recognized by the system, ignored.", data
)
return code