Some Magic Words

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

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

Video Definitions

In [2]:
width_in = 640
height_in = 480

# width_out = 640
# height_out = 480

width_out = 1920
height_out = 1080

bits_per_pixel = 24

Start HDMI Output

You will see black screen on HDMI output

In [3]:
mode = VideoMode(width_out, height_out, 24)
hdmi_out = base.video.hdmi_out
hdmi_out.configure(mode, PIXEL_BGR)
hdmi_out.start()
Out[3]:
<contextlib._GeneratorContextManager at 0xaf0aa270>

Start USB Camera (OpenCV)

It may takes a couple of seconds again.

In [4]:
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 input configuration: done
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\r" % (n_frames / passed_time))
            last_printed_time = passed_time
    else:
        print("Read error")
Frame rate so far: 4.22

Cleaning up

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