aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Zhao2024-05-07 14:03:20 -0400
committerKevin Zhao2024-05-07 14:03:20 -0400
commit9d9c891d74737f363029164b4e431bb620406a2a (patch)
tree5941f4716092d444e01645973f5ad0904ec148ca
parent9205312ad56d5d04987797b1c796cf0b57cd1835 (diff)
parent7a2c685c156b3aff30c736ca80765b9e9ba153b8 (diff)
Merge branch 'main' of https://git.exozy.me/k/6.8301-Project into CNN
-rw-r--r--decoder.py34
-rw-r--r--encoder.py6
2 files changed, 25 insertions, 15 deletions
diff --git a/decoder.py b/decoder.py
index e557361..43d06a5 100644
--- a/decoder.py
+++ b/decoder.py
@@ -1,5 +1,5 @@
import argparse
-import traceback
+import time
import cv2
import numpy as np
from creedsolo import RSCodec
@@ -47,6 +47,7 @@ if args.input.isdecimal():
args.input = int(args.input)
cap = cv2.VideoCapture(args.input)
data = None
+start_time = time.time()
while data is None:
try:
ret, raw_frame = cap.read()
@@ -65,7 +66,7 @@ while data is None:
# Find positions and colors of corners
X, Y = raw_frame.shape[:2]
cx, cy = X // 3, Y // 3
- widx, wcol = find_corner(raw_frame[:cx, :cy], lambda B: np.sum(B, axis=2) - np.std(B, axis=2))
+ widx, wcol = find_corner(raw_frame[:cx, :cy], lambda B: np.sum(B, axis=2) - 2 * np.std(B, axis=2))
ridx, rcol = find_corner(raw_frame[:cx, Y - cy :], lambda B: B[:, :, 0] - B[:, :, 1] - B[:, :, 2])
ridx[1] += Y - cy
gidx, gcol = find_corner(raw_frame[X - cx :, :cy], lambda B: B[:, :, 1] - B[:, :, 2] - B[:, :, 0])
@@ -89,30 +90,35 @@ while data is None:
[
[ccw, cch],
[args.width - ccw - 1, cch],
- [ccw, args.height - ccw - 1],
+ [ccw, args.height - cch - 1],
[args.width - ccw - 1, args.height - cch - 1],
]
),
)
frame = cv2.warpPerspective(raw_frame, M, (args.width, args.height))
# Convert to new color space
- frame = (np.squeeze(F @ (frame - origin)[..., np.newaxis]) >= 128).astype(np.uint8)
- # import matplotlib.pyplot as plt
+ frame = (np.squeeze(F @ (frame - origin)[..., np.newaxis]) >= 192).astype(np.uint8)
+ # import matplotlib.pyplot as pltc
# plt.imshow(frame * 255)
# plt.show()
- frame = np.concatenate(
- (
- frame[:cheight, cwidth : args.width - cwidth].flatten(),
- frame[cheight : args.height - cheight].flatten(),
- frame[args.height - cheight :, cwidth : args.width - cwidth].flatten(),
+ frame = np.packbits(
+ np.concatenate(
+ (
+ frame[:cheight, cwidth : args.width - cwidth].flatten(),
+ frame[cheight : args.height - cheight].flatten(),
+ frame[args.height - cheight :, cwidth : args.width - cwidth].flatten(),
+ )
)
)
- data = decoder.decode(bytes(rsc.decode(bytearray(np.packbits(frame) ^ frame_xor))[0][: args.psize]))
+ reshape_len = frame_bytes // 255 * 255
+ frame[:reshape_len] = np.ravel(frame[:reshape_len].reshape(255, reshape_len // 255), "F")
+ data = decoder.decode(bytes(rsc.decode(bytearray(frame ^ frame_xor))[0][: args.psize]))
print("Decoded frame")
except KeyboardInterrupt:
break
- except:
- traceback.print_exc()
+ except Exception as e:
+ print(e)
+cap.release()
with open(args.output, "wb") as f:
f.write(data)
-cap.release()
+print(8 * len(data) / (time.time() - start_time))
diff --git a/encoder.py b/encoder.py
index fd7958b..caf9b59 100644
--- a/encoder.py
+++ b/encoder.py
@@ -67,7 +67,11 @@ print(f"-x {args.height} -y {args.width} -l {args.level} -s {len(data)} -p {len(
def mkframe(packet):
frame = np.array(rsc.encode(bytearray(packet)))
- frame = np.unpackbits(np.pad(frame, (0, frame_bytes - len(frame))) ^ frame_xor)
+ frame = np.pad(frame, (0, frame_bytes - len(frame))) ^ frame_xor
+ reshape_len = frame_bytes // 255 * 255
+ # Space out elements in each size 255 chunk
+ frame[:reshape_len] = np.ravel(frame[:reshape_len].reshape(reshape_len // 255, 255), "F")
+ frame = np.unpackbits(frame)
# Pad to be multiple of 3 so we can reshape into RGB channels
frame = np.pad(frame, (0, (3 - len(frame)) % 3))
frame = np.reshape(frame, (frame_size, 3))