-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqr_weaver_vid.py
129 lines (100 loc) · 3.81 KB
/
qr_weaver_vid.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
import cv2
import numpy as np
import qrcode
import gzip
import base64
import os
import json
import time # For adding delay
def generate_qr_code(data):
qr = qrcode.QRCode(
error_correction=qrcode.constants.ERROR_CORRECT_M,
box_size=10,
border=4,
)
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img_cv = np.array(img.convert('RGB'))
img_cv = cv2.cvtColor(img_cv, cv2.COLOR_RGB2BGR)
# Resize the image to 730x730
img_cv = cv2.resize(img_cv, (730, 730))
return img_cv
def compress_and_generate_base64_qr_images(file_path, chunk_size=1500):
with open(file_path, 'rb') as f:
data = f.read()
compressed_data = gzip.compress(data)
encoded_data_base64 = base64.urlsafe_b64encode(compressed_data).decode("utf-8")
print(f"Total size of base64 data before splitting: {len(encoded_data_base64)}")
chunks = [encoded_data_base64[i:i+chunk_size] for i in range(0, len(encoded_data_base64), chunk_size)]
# Write chunks to a JSON file
with open('chunks.json', 'w') as json_file:
json.dump({"chunks": chunks}, json_file) # Save the chunks as an array within a JSON object
os.makedirs('qrs', exist_ok=True) # Create the directory if it doesn't exist
for i, chunk in enumerate(chunks):
print(f"Size of chunk {i}: {len(chunk)}")
qr_img = generate_qr_code(chunk)
cv2.imwrite(f'qrs/qr_{i:09d}.png', qr_img) # Save each QR code as a PNG file
img_file_path = 'test.txt'
compress_and_generate_base64_qr_images(img_file_path)
# Add ffmpeg command to generate the video
os.system('ffmpeg -framerate 30 -i qrs/qr_%09d.png -vf "scale=730:730,setsar=1" -an -c:v libx264 -pix_fmt yuv420p output.mp4')
import cv2
from pyzbar.pyzbar import decode
import base64
import gzip
# Open the video capture
video_capture = cv2.VideoCapture('output.mp4')
def safe_base64_decode(data):
if isinstance(data, str):
# If data is already a string, it doesn't need to be decoded
return data
try:
data = data.decode("utf-8") # Decode the bytes to a string
except UnicodeDecodeError:
# If data is not valid UTF-8, it's probably already decoded
return data
missing_padding = 4 - len(data) % 4
if missing_padding:
data += '=' * missing_padding
try:
return base64.urlsafe_b64decode(data)
except Exception as e:
print(f"Exception during decoding: {e}")
print(f"Data: {data}")
return None
# Initialize an empty list to hold the data from each QR code in the video
data_chunks = []
prev_chunk = None
while True:
# Read a frame from the video
ret, frame = video_capture.read()
# Check if the frame was read successfully
if not ret:
break
# Convert the frame to grayscale
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Decode QR codes from the frame
decoded_objects = decode(gray_frame)
# Process the decoded data and append to data_chunks
for obj in decoded_objects:
decoded_data = safe_base64_decode(obj.data)
if decoded_data is not None and decoded_data != prev_chunk:
data_chunks.append(decoded_data)
prev_chunk = decoded_data
# Exit the loop if 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
print("Finished processing frames, releasing video capture...")
video_capture.release()
print("Concatenating and decompressing data...")
data = b''.join(data_chunks)
try:
# Decompress the full data
decompressed_data = gzip.decompress(data)
with open("decoded_test.txt", "wb") as out_file:
out_file.write(decompressed_data)
print("Data decompressed and written to 'decoded_test.txt'.")
except Exception as e:
print(f"Exception occurred during decompression: {e}")
print("Finished.")