<jupyter_start><jupyter_text>OpenCV Filters WebcamIn this notebook, several filters will be applied to webcam images.Those input sources and applied filters will then be displayed either directly in the notebook or on HDMI output.To run all cells in this notebook a webcam and HDMI output monitor are required.    1. Start HDMI output  Step 1: Load the overlay<jupyter_code>from pynq.overlays.base import BaseOverlay
from pynq.lib.video import *
base = BaseOverlay("base.bit")<jupyter_output><empty_output><jupyter_text>Step 2: Initialize HDMI I/O<jupyter_code># monitor configuration: 640*480 @ 60Hz
Mode = VideoMode(640,480,24)
hdmi_out = base.video.hdmi_out
hdmi_out.configure(Mode,PIXEL_BGR)
hdmi_out.start()<jupyter_output><empty_output><jupyter_text>2. Applying OpenCV filters on Webcam input Step 1: Specify webcam resolution<jupyter_code># camera (input) configuration
frame_in_w = 640
frame_in_h = 480<jupyter_output><empty_output><jupyter_text>Step 2: Initialize camera from OpenCV<jupyter_code>import cv2

videoIn = cv2.VideoCapture(0)
videoIn.set(cv2.CAP_PROP_FRAME_WIDTH, frame_in_w);
videoIn.set(cv2.CAP_PROP_FRAME_HEIGHT, frame_in_h);
print("capture device is open: " + str(videoIn.isOpened()))<jupyter_output>capture device is open: True<jupyter_text>Step 3: Send webcam input to HDMI output<jupyter_code>import numpy as np

ret, frame_vga = videoIn.read()

if (ret):
    outframe = hdmi_out.newframe()
    outframe[:] = frame_vga
    hdmi_out.writeframe(outframe)
else:
    raise RuntimeError("Error while reading from camera.")<jupyter_output><empty_output><jupyter_text>Step 4: Edge detection Detecting edges on webcam input and display on HDMI out.<jupyter_code>import time

num_frames = 20
readError = 0

start = time.time()
for i in range (num_frames):   
    # read next image
    ret, frame_vga = videoIn.read()
    if (ret):
        outframe = hdmi_out.newframe()
        laplacian_frame = cv2.Laplacian(frame_vga, cv2.CV_8U, dst=outframe)
        hdmi_out.writeframe(outframe)
    else:
        readError += 1
end = time.time()

print("Frames per second: " + str((num_frames-readError) / (end - start)))
print("Number of read errors: " + str(readError))<jupyter_output>Frames per second: 7.096439356531233
Number of read errors: 0<jupyter_text>Step 5: Canny edge detectionDetecting edges on webcam input and display on HDMI out.Any edges with intensity gradient more than maxVal are sure to be edges and those below minVal are sure to be non-edges, so discarded. Those who lie between these two thresholds are classified edges or non-edges based on their connectivity. If they are connected to “sure-edge” pixels, they are considered to be part of edges. Otherwise, they are also discarded. As we only need a single output channel reconfigure the HDMI output to work in grayscale mode. This means that our output frame is in the correct format for the edge detection algorith,<jupyter_code>num_frames = 20

Mode = VideoMode(640,480,8)
hdmi_out = base.video.hdmi_out
hdmi_out.configure(Mode,PIXEL_GRAY)
hdmi_out.start()

start = time.time()
for i in range (num_frames):
    # read next image
    ret, frame_webcam = videoIn.read()
    if (ret):
        outframe = hdmi_out.newframe()
        cv2.Canny(frame_webcam, 100, 110, edges=outframe)
        hdmi_out.writeframe(outframe)
    else:
        readError += 1
end = time.time()

print("Frames per second: " + str((num_frames-readError) / (end - start)))
print("Number of read errors: " + str(readError))<jupyter_output>Frames per second: 8.003659968333068
Number of read errors: 0<jupyter_text>Step 6: Show resultsNow use matplotlib to show filtered webcam input inside notebook.<jupyter_code>%matplotlib inline 
from matplotlib import pyplot as plt
import numpy as np

frame_canny = cv2.Canny(frame_webcam, 100, 110)
plt.figure(1, figsize=(10, 10))
frame_vga = np.zeros((480,640,3)).astype(np.uint8)
frame_vga[:,:,0] = frame_canny
frame_vga[:,:,1] = frame_canny
frame_vga[:,:,2] = frame_canny
plt.imshow(frame_vga)
plt.show()<jupyter_output><empty_output><jupyter_text>Step 7: Release camera and HDMI<jupyter_code>videoIn.release()
hdmi_out.stop()
del hdmi_out<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09450707603741905, 'fraction_numerical': 0.028544015351403215, 'mean_word_length': 5.318181818181818, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '23194', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1511, 'n_tokens_neox': 1414, 'n_words': 412, 'path': 'Pynq-ZU/base/notebooks/video/opencv_filters_webcam.ipynb', 'repo_name': 'Xilinx/PYNQ-ZU', 'size': '4169'}