繁体   English   中英

如何使用OpenCV使用Python将视频捕获的输出提供给窗口?

[英]How can I feed the output of a video capture to a window with Python using OpenCV?

我的代码是:

import cv2
import numpy as np
import argparse
import gluoncv as gcv
import mxnet as mx
import camera

def parse_cli_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--video_device", dest="video_device",
                        help="Video device # of USB webcam (/dev/video?) [-1 for Jetson]",
                        default=-1, type=int)
    arguments = parser.parse_args()
    return arguments


def read_cam(video_capture):
    if video_capture.isOpened():
        windowName = "ssdObject"
        cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
        cv2.resizeWindow(windowName, 1280, 720)
        cv2.moveWindow(windowName, 0, 0)
        cv2.setWindowTitle(windowName, "SSD Object Detection")
        while True:
            # Check to see if the user closed the window
            if cv2.getWindowProperty(windowName, 0) < 0:
                # This will fail if the user closed the window; Nasties get printed to the console
                break
            ret_val, frame = video_capture.read()
            print(frame)

            # frame = mx.nd.array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)).astype('uint8')
            # rgb_nd, frame = gcv.data.transforms.presets.ssd.transform_test(frame, short=512, max_size=700)

            # # Run frame through network
            # class_IDs, scores, bounding_boxes = net(rgb_nd)

            displayBuf = frame

            cv2.imshow(windowName, displayBuf)

    else:
        print("camera open failed")


if __name__ == '__main__':
    arguments = parse_cli_args()
    print("Called with args:")
    print(arguments)
    print("OpenCV version: {}".format(cv2.__version__))
    print("Device Number:", arguments.video_device)
    if arguments.video_device == -1:
        video_capture = camera.open_camera(device_number=None)
    else:
        video_capture = camera.open_camera(
            device_number=arguments.video_device)
    read_cam(video_capture)
    video_capture.release()
    cv2.destroyAllWindows()

相机是:

import cv2

def open_camera(device_number=None):
    if device_number == None:
        return cv2.VideoCapture("nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)640, height=(int)480, format=(string)I420, framerate=(fraction)30/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink")
    else:
        return cv2.VideoCapture(device_number)

它激活了我的网络摄像头,但是视频看起来像:

在此处输入图片说明

根据Dan Masek的评论 ,所需要做的只是添加cv2.waitKey

原来如此

cv2.imshow(windowName, displayBuf)
cv2.waitKey(0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM