如何使用 opencv 从 youtube 的直播流调整输出帧的大小

How to resize output frame from live stream from youtube using opencv

这是我编码和这个网站的开始。我正在做一个项目,我想在其中使用 openCV,但我对此有疑问。我需要调整输出帧的大小,以识别对象。我读过,那个框架的尺寸应该是 416x416,但是当我试图释放框架时,它仍然是常规尺寸。 这是代码:

import pafy
import youtube_dl
import cv2
import numpy as np


url = "https://www.youtube.com/watch?v=WOn7m0_aYBw"
video = pafy.new(url)
best = video.getbest(preftype="mp4")

cap = cv2.VideoCapture()
cap.open(best.url)

net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
classes = []
with open("coco.names", "r") as f:
    classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
output_layers =[layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0, 255, size=(len(classes), 3))


while True:
    ret, frame = cap.read()
 #   if ret == True:
    img = cv2.imshow('frame',frame)
    #cap.set(cv2.CAP_PROP_FRAME_WIDTH, 416)
    #cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 416)
    width = 416
    height = 416
    dim = (width, height)
    img = cv2.resize(frame, dim, interpolation = cv2.INTER_AREA)
    print(img.shape)

    if cv2.waitKey(20) & 0xFF == ord('q'):
        break

blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)

print(img.shape) returns 大小正确,但我想我放错了 window。如何将此代码更改为以正确的大小释放 window?

您在调整大小之前显示了框架

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

    width = 416
    height = 416
    dim = (width, height)
    img = cv2.resize(frame, dim, interpolation = cv2.INTER_AREA)
    print(img.shape)

    cv2.imshow('frame',img)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break