diff options
author | Anthony Wang | 2024-05-07 13:40:44 -0400 |
---|---|---|
committer | Anthony Wang | 2024-05-07 13:40:44 -0400 |
commit | 7a2c685c156b3aff30c736ca80765b9e9ba153b8 (patch) | |
tree | c1d7a26f44cd23f849b633bba731a33f8e5ed622 | |
parent | 49e4564566bbfd8435f390bba602eb97cc214502 (diff) |
Space out elements in each size 255 chunk
-rw-r--r-- | decoder.py | 34 | ||||
-rw-r--r-- | encoder.py | 6 |
2 files changed, 25 insertions, 15 deletions
@@ -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)) @@ -45,7 +45,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)) |