Skip to content

Instantly share code, notes, and snippets.

@atinfinity
Last active October 31, 2022 00:28
Show Gist options
  • Save atinfinity/7f59020c83e69354bafb6419b074294b to your computer and use it in GitHub Desktop.
Save atinfinity/7f59020c83e69354bafb6419b074294b to your computer and use it in GitHub Desktop.
VPI Python APIを使ってみる

VPI Python APIを使ってみる

サンプルスクリプト

を行うスクリプトは以下の通りです。

import cv2
import vpi

capture = cv2.VideoCapture("/opt/nvidia/vpi2/share/demos/dog.mp4")
if not capture.isOpened():
    raise IOError("can't open capture!")

while True:
    result, img = capture.read()
    if result is False:
        cv2.waitKey(0)
        break

    # vpi.Imageに変換する
    input = vpi.asimage(img)
    
    # CUDA backendで画像処理を実行する
    with vpi.Backend.CUDA:
        output = input.convert(vpi.Format.U8).rescale(factor=0.25).eqhist()

    # ホストに画像データを転送する
    with output.rlock_cpu() as output_host:
        cv2.imshow("output", output_host)
        key = cv2.waitKey(20)
        if key == ord('q'):
            break

cv2.destroyAllWindows()

実行結果は以下の通りです。

Screenshot from 2022-10-30 23-16-40

動作確認環境

  • Ubuntu 20.04
  • CUDA 11.4
  • VPI 2.1.6
  • Python 3.8.10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment