0

这是我开始编码和这个网站。我正在做一个项目,我想在其中使用 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)返回正确的大小,但我认为我发布了错误的窗口。如何将此代码更改为以正确大小释放窗口?

4

1 回答 1

0

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

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
于 2019-11-19T01:58:35.710 回答