MCPcopy Index your code
hub / github.com/FrameworkComputer/inputmodule-rs / camera

Function camera

python/inputmodule/inputmodule/ledmatrix.py:105–141  ·  view source on GitHub ↗

Play a live view from the webcam, for fun

(dev)

Source from the content-addressed store, hash-verified

103
104
105def camera(dev):
106 """Play a live view from the webcam, for fun"""
107 with serial.Serial(dev.device, 115200) as s:
108 import cv2
109
110 capture = cv2.VideoCapture(0)
111 ret, frame = capture.read()
112
113 scale_y = HEIGHT / frame.shape[0]
114
115 # Scale the video to 34 pixels height
116 dim = (HEIGHT, int(round(frame.shape[1] * scale_y)))
117 # Find the starting position to crop the width to be centered
118 # For very narrow videos, make sure to stay in bounds
119 start_x = max(0, int(round(dim[1] / 2 - WIDTH / 2)))
120 end_x = min(dim[1], start_x + WIDTH)
121
122 # Pre-process the video into resized, cropped, grayscale frames
123 while True:
124 ret, frame = capture.read()
125 if not ret:
126 print("Failed to capture video frames")
127 break
128
129 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
130
131 resized = cv2.resize(gray, (dim[1], dim[0]))
132 cropped = resized[0:HEIGHT, start_x:end_x]
133
134 for x in range(0, cropped.shape[1]):
135 vals = [0 for _ in range(HEIGHT)]
136
137 for y in range(0, HEIGHT):
138 vals[y] = cropped[y, x]
139
140 send_col(dev, s, x, vals)
141 commit_cols(dev, s)
142
143
144def video(dev, video_file):

Callers 1

main_cliFunction · 0.90

Calls 2

send_colFunction · 0.70
commit_colsFunction · 0.70

Tested by

no test coverage detected