Resize and play back a video
(dev, video_file)
| 142 | |
| 143 | |
| 144 | def video(dev, video_file): |
| 145 | """Resize and play back a video""" |
| 146 | with serial.Serial(dev.device, 115200) as s: |
| 147 | import cv2 |
| 148 | |
| 149 | capture = cv2.VideoCapture(video_file) |
| 150 | ret, frame = capture.read() |
| 151 | |
| 152 | scale_y = HEIGHT / frame.shape[0] |
| 153 | |
| 154 | # Scale the video to 34 pixels height |
| 155 | dim = (HEIGHT, int(round(frame.shape[1] * scale_y))) |
| 156 | # Find the starting position to crop the width to be centered |
| 157 | # For very narrow videos, make sure to stay in bounds |
| 158 | start_x = max(0, int(round(dim[1] / 2 - WIDTH / 2))) |
| 159 | end_x = min(dim[1], start_x + WIDTH) |
| 160 | |
| 161 | processed = [] |
| 162 | |
| 163 | # Pre-process the video into resized, cropped, grayscale frames |
| 164 | while True: |
| 165 | ret, frame = capture.read() |
| 166 | if not ret: |
| 167 | print("Failed to read video frames") |
| 168 | break |
| 169 | |
| 170 | gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) |
| 171 | |
| 172 | resized = cv2.resize(gray, (dim[1], dim[0])) |
| 173 | cropped = resized[0:HEIGHT, start_x:end_x] |
| 174 | |
| 175 | processed.append(cropped) |
| 176 | |
| 177 | # Write it out to the module one frame at a time |
| 178 | # TODO: actually control for framerate |
| 179 | for frame in processed: |
| 180 | for x in range(0, cropped.shape[1]): |
| 181 | vals = [0 for _ in range(HEIGHT)] |
| 182 | |
| 183 | for y in range(0, HEIGHT): |
| 184 | vals[y] = frame[y, x] |
| 185 | |
| 186 | send_col(dev, s, x, vals) |
| 187 | commit_cols(dev, s) |
| 188 | |
| 189 | |
| 190 | def pixel_to_brightness(pixel): |
no test coverage detected