Some Magic Words

It may takes about 20 seconds... Be patient.

In [ ]:
from pynq.overlays.base import BaseOverlay
from pynq.lib.video import *
base = BaseOverlay("base.bit")

Video Definitions

In [ ]:
# Camera resolution

width_in = 640
height_in = 480

# HDMI output resolution

# width_out = 640
# height_out = 480
# or
width_out = 1920
height_out = 1080

# Other constants

bits_per_pixel = 24

Start HDMI Output

You will see black screen on HDMI output

In [ ]:
mode = VideoMode(width_out, height_out, bits_per_pixel)
hdmi_out = base.video.hdmi_out
hdmi_out.configure(mode, PIXEL_BGR)
hdmi_out.start()

Start USB Camera (OpenCV)

It may takes a couple of seconds again.

In [ ]:
import cv2

cap = cv2.VideoCapture(0)

n_retry = 3

for i in range(n_retry):
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, width_in)
    ret = cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height_in)
    if ret:
        print("Video input configuration: done")
        break
    cap.release()
else:
    print("Video input configuration: failed")

Video Pass-through Loop

In [ ]:
import numpy as np
import sys
import time

start_time = time.time()
last_printed_time = 0.0
n_frames = 0

while True:
    ret, inframe = cap.read()
    if ret:
        outframe = hdmi_out.newframe()
        outframe[0:height_in, 0:width_in, :] = inframe[0:height_in, 0:width_in, :]
        hdmi_out.writeframe(outframe)
        
        n_frames += 1
        passed_time = time.time() - start_time
        if int(last_printed_time) != int(passed_time):
            sys.stdout.write("Frame rate so far: %.1f in %d frames\r" % (n_frames / passed_time, n_frames))
            last_printed_time = passed_time
    else:
        print("Read error occurred")

Cleaning up

In [ ]:
cap.release()
del cap
In [ ]:
hdmi_out.stop()
del hdmi_out