如何使用opencv调整youtube实时流输出帧的大小
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用opencv调整youtube实时流输出帧的大小相关的知识,希望对你有一定的参考价值。
这是我从编码和本网站开始的。我正在研究要使用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)
返回正确的大小,但是我认为释放的窗口错误。如何将此代码更改为正确大小的释放窗口?
答案
您在调整大小之前显示了框架
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
以上是关于如何使用opencv调整youtube实时流输出帧的大小的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 opencv VideoCapture 方法获取实时帧?