aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md4
-rw-r--r--decoder.py6
-rw-r--r--encoder.py6
3 files changed, 9 insertions, 7 deletions
diff --git a/README.md b/README.md
index 98441c9..cafe92a 100644
--- a/README.md
+++ b/README.md
@@ -19,7 +19,3 @@ Copy the flags printed by the encoder and pass them to the decoder: `python deco
Formatting: `black -l 120 *.py`
Use phone as webcam for higher quality video: `scrcpy --v4l2-sink=/dev/video4 --video-source=camera --no-video-playback --camera-size 1920x1440`
-
-## TODO
-
-- Reorder bits
diff --git a/decoder.py b/decoder.py
index d1065b7..e557361 100644
--- a/decoder.py
+++ b/decoder.py
@@ -1,8 +1,6 @@
import argparse
import traceback
import cv2
-
-# import matplotlib.pyplot as plt
import numpy as np
from creedsolo import RSCodec
from raptorq import Decoder
@@ -57,12 +55,14 @@ while data is None:
break
# raw_frame is a uint8 BE CAREFUL
if type(args.input) == int:
+ # Crop image to reduce camera distortion
X, Y = raw_frame.shape[:2]
raw_frame = raw_frame[X // 4 : 3 * X // 4, Y // 4 : 3 * Y // 4]
cv2.imshow("", raw_frame)
cv2.waitKey(1)
raw_frame = cv2.cvtColor(raw_frame, cv2.COLOR_BGR2RGB)
+ # 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))
@@ -95,7 +95,9 @@ while data is None:
),
)
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
# plt.imshow(frame * 255)
# plt.show()
frame = np.concatenate(
diff --git a/encoder.py b/encoder.py
index bd44876..634a166 100644
--- a/encoder.py
+++ b/encoder.py
@@ -39,12 +39,14 @@ rcorner = np.pad(np.dstack((ones, zeros, zeros)), ((0, 1), (1, 0), (0, 0)))
gcorner = np.pad(np.dstack((zeros, ones, zeros)), ((1, 0), (0, 1), (0, 0)))
bcorner = np.pad(np.dstack((zeros, zeros, ones)), ((1, 0), (1, 0), (0, 0)))
+# Output flags for decoder
print(f"-x {args.height} -y {args.width} -l {args.level} -s {len(data)} -p {len(packets[0])}", end="")
def mkframe(packet):
frame = np.array(rsc.encode(bytearray(packet)))
frame = np.unpackbits(np.pad(frame, (0, frame_bytes - len(frame))) ^ frame_xor)
+ # 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))
frame = np.concatenate(
@@ -66,6 +68,7 @@ def mkframe(packet):
if args.mix:
+ # Mix frames with original video
cap = cv2.VideoCapture(args.input)
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
@@ -81,12 +84,13 @@ if args.mix:
vidframe[: hscale * cheight, wscale * (args.width - cwidth) :] = 0
vidframe[hscale * (args.height - cheight) :, : wscale * cwidth] = 0
vidframe[hscale * (args.height - cheight) :, wscale * (args.width - cwidth) :] = 0
-
frame = np.repeat(np.repeat(mkframe(packets[i]), hscale, 0), wscale, 1)
+ # Set edges in original video to black
frame[vidframe % 255 != 0] = 0
out.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
i = (i + 1) % len(packets)
else:
+ # Create a new video
out = cv2.VideoWriter(args.output, cv2.VideoWriter_fourcc(*"FFV1"), args.fps, (args.width, args.height))
for packet in packets:
out.write(cv2.cvtColor(mkframe(packet), cv2.COLOR_RGB2BGR))