Skip to content

Commit 163b0c0

Browse files
Video and camera interface (#86)
Add webcam interface --------- Signed-off-by: Daniel Schaefer <[email protected]> Co-authored-by: Daniel Schaefer <[email protected]>
1 parent b766b4d commit 163b0c0

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

ledmatrix_control.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ def main():
198198
type=argparse.FileType('rb'))
199199
parser.add_argument("--image-grey", help="Display a PNG or GIF image in greyscale",
200200
type=argparse.FileType('rb'))
201+
parser.add_argument("--camera", help="Stream from the webcam",
202+
action="store_true")
201203
parser.add_argument("--video", help="Play a video",
202204
type=str)
203205
parser.add_argument("--percentage", help="Fill a percentage of the screen",
@@ -343,6 +345,8 @@ def main():
343345
image_bl(dev, args.image)
344346
elif args.image_grey is not None:
345347
image_greyscale(dev, args.image_grey)
348+
elif args.camera:
349+
camera(dev)
346350
elif args.video is not None:
347351
video(dev, args.video)
348352
elif args.all_brightnesses:
@@ -563,6 +567,44 @@ def image_bl(dev, image_file):
563567

564568
send_command(dev, CommandVals.Draw, vals)
565569

570+
def camera(dev):
571+
"""Play a live view from the webcam, for fun"""
572+
with serial.Serial(dev.device, 115200) as s:
573+
import cv2
574+
575+
capture = cv2.VideoCapture(0)
576+
ret, frame = capture.read()
577+
578+
scale_y = HEIGHT/frame.shape[0]
579+
580+
# Scale the video to 34 pixels height
581+
dim = (HEIGHT, int(round(frame.shape[1]*scale_y)))
582+
# Find the starting position to crop the width to be centered
583+
# For very narrow videos, make sure to stay in bounds
584+
start_x = max(0, int(round(dim[1]/2-WIDTH/2)))
585+
end_x = min(dim[1], start_x + WIDTH)
586+
587+
# Pre-process the video into resized, cropped, grayscale frames
588+
while True:
589+
ret, frame = capture.read()
590+
if not ret:
591+
print("Failed to capture video frames")
592+
break
593+
594+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
595+
596+
resized = cv2.resize(gray, (dim[1], dim[0]))
597+
cropped = resized[0:HEIGHT, start_x:end_x]
598+
599+
for x in range(0, cropped.shape[1]):
600+
vals = [0 for _ in range(HEIGHT)]
601+
602+
for y in range(0, HEIGHT):
603+
vals[y] = cropped[y, x]
604+
605+
send_col(s, x, vals)
606+
commit_cols(s)
607+
566608
def video(dev, video_file):
567609
"""Resize and play back a video"""
568610
with serial.Serial(dev.device, 115200) as s:
@@ -587,6 +629,7 @@ def video(dev, video_file):
587629
while True:
588630
ret, frame = capture.read()
589631
if not ret:
632+
print("Failed to read video frames")
590633
break
591634

592635
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)

0 commit comments

Comments
 (0)