Showing vpi.Image output

Hi, I’m trying to warp video with some Vision Programming Interface methods. I believe the below code works, except I’m not sure how to show the output. Trying with OpenCV “imshow”, I get ‘src is not a numpy array, neither a scalar’.

What is a good way to show the output variable here? Any help appriciated!

import cv2
import vpi
import numpy as np

cap = cv2.VideoCapture("v4l2src device=/dev/video0 ! video/x-raw, format=BGRx, width=2064, height=1544 ! videoconvert ! video/x-raw, format=BGR ! appsink")

xform = [[  0.5386, 0.1419, -74   ],
         [ -0.4399, 0.8662, 291.5   ],
         [ -0.0005, 0.0003, 1     ]]

while True:
    
    _, frame = cap.read()

    vpi_image = vpi.asimage(np.asarray(frame))

    with vpi.Backend.CUDA:
        output = vpi_image.perspwarp(xform)

    output = cv2.resize(output, (960,640))
    
    cv2.imshow('output', output)
    if cv2.waitKey(25) == ord('q'):
        break


cv2.destroyAllWindows()

check the handling around line 210 here

Thanks, I will have a look!

I’ve tried to follow the Python version from VPI - Vision Programming Interface: Perspective Warp but get the following error when calling output.rlock_cpu():

‘vpi.Image’ object has no attribute ‘rlock_cpu’

import cv2
import vpi
import numpy as np

# cap =  cv2.VideoCapture(0)
cap = cv2.VideoCapture("v4l2src device=/dev/video0 ! video/x-raw, format=BGRx, width=2064, height=1544 ! videoconvert ! video/x-raw, format=BGR ! appsink")

xform = [[  0.5386, 0.1419, -74   ],
         [ -0.4399, 0.8662, 291.5   ],
         [ -0.0005, 0.0003, 1     ]]

fourcc = cv2.VideoWriter_fourcc(*'MPEG')
inSize = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fps = cap.get(cv2.CAP_PROP_FPS)


outVideo = cv2.VideoWriter(filename='perspwarp_python.mp4', fourcc=fourcc, fps=fps, frameSize=inSize)

while True:
    
    _, frame = cap.read()

    vpi_image = vpi.asimage(np.asarray(frame))

    
    with vpi.Backend.CUDA:
        output = vpi_image.perspwarp(xform)

    with vpi.Backend.CUDA:
        output = output.convert(vpi.Format.RGB8)

    with output.rlock_cpu() as data:
        outVideo.write(data)

Just tried running the perspective warp python example using one of the videos from /opt/nvidia/vpi1/samples/assets folder. Had to change the output file ending from mp4 to avi, otherwise I get:

OpenCV: FFMPEG: tag 0x4745504d/ ‘MPEG’ is not supported with codec id 2

Fixing that, my original problem still persists:

AttributeError: ‘vpi.Image’ object has no attribute ‘rlock_cpu’.

EDIT

I noticed the sample from my vpi1 folder is different from the nvidia forum example. I’ve manage to get the code running, even using a webcam as input.

I’m still unsure how to show the output, not as a video, but as live video, like I’d do with cv2.imshow for example. Any suggestions? Thanks!