形状[utils]中的opencv错误,使图像和徽标一起显示出来

2024-06-13 07:38:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我按照一个教程学习如何使用opencv和一个带有tensorflow的实用库,尤其是cnn,但我有一个错误

Traceback (most recent call last): File "watermark.py", line 15, in <module> watermark = 
image_resize(logo, height=50) File "C:\Users\HP\Desktop\Private\project\cv\utils.py", line 9, in 
image_resize (h, w) = image.shape:2 AttributeError: 'NoneType' object has no attribute 'shape' WARN:0 
global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (674) 
SourceReaderCB::~SourceReaderCB terminating async callback

代码 utils.py

import cv2
import os
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
    dim = None
    (h, w) = image.shape[:2]
    if width is None and height is None:
        return image
    if width is None:
        r = height / float(h)
        dim = (int(w * r), height)
    else:
        r = width / float(w)
        dim = (width, int(h * r))
    resized = cv2.resize(image, dim, interpolation = inter)
    return resized
class CFEVideoConf(object):
    STD_DIMENSIONS =  {
        "360p": (480, 360),
        "480p": (640, 480),
        "720p": (1280, 720),
        "1080p": (1920, 1080),
        "4k": (3840, 2160),
    }
    VIDEO_TYPE = {
        'avi': cv2.VideoWriter_fourcc(*'XVID'),
        'mp4': cv2.VideoWriter_fourcc(*'XVID'),
    }

    width           = 640
    height          = 480
    dims            = (640, 480)
    capture         = None
    video_type      = None
    def __init__(self, capture, filepath, res="480p", *args, **kwargs):
        self.capture = capture
        self.filepath = filepath
        self.width, self.height = self.get_dims(res=res)
        self.video_type = self.get_video_type()
    def change_res(self, width, height):
        self.capture.set(3, width)
        self.capture.set(4, height)
    def get_dims(self, res='480p'):
        width, height = self.STD_DIMENSIONS['480p']
        if res in self.STD_DIMENSIONS:
            width, height = self.STD_DIMENSIONS[res]
        self.change_res(width, height)
        self.dims = (width, height)
        return width, height
    def get_video_type(self):
        filename, ext = os.path.splitext(self.filepath)
        if ext in self.VIDEO_TYPE:
          return  self.VIDEO_TYPE[ext]
        return self.VIDEO_TYPE['avi']

水印.py

import numpy as np
import cv2
from utils import CFEVideoConf, image_resize
cap = cv2.VideoCapture(0)
save_path = 'saved-media/watermark.mp4'
frames_per_seconds = 24
config = CFEVideoConf(cap, filepath=save_path, res='720p')
out = cv2.VideoWriter(save_path, config.video_type, frames_per_seconds, config.dims)
img_path = 'images/logo/1.png'
logo = cv2.imread(img_path, -1)
watermark = image_resize(logo, height=50)
cv2.imshow('watermark',watermark)
while(True):
    ret, frame = cap.read()
    print(frame[50,150])
    color=(255,0,0)
    start_cord_x=50
    start_cord_y=150
    stroke=2
    w=100
    h=200
    end_cord_x=start_cord_x+w
    end_cord_y=start_cord_y+h
    cv2.rectangle(frame,(x,y),(end_cord_x,end_cord_y),color,stroke)
    cv2.imshow('frame',frame)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break
cap.release()
out.release()
cv2.destroyAllWindows()

谢谢你帮我找到了错误 预期的结果是,当相机打开并且视频下载显示徽标+标准结果时,徽标图像将显示为1.png


Tags: pathimageimportselfnonedefreswidth
1条回答
网友
1楼 · 发布于 2024-06-13 07:38:30

这是您的问题,您正在从一个不存在的文件读取一个图像,因此空图像没有shape,并且是NoneType

img_path = 'images/logo/1.png'
logo = cv2.imread(img_path, -1)

相关问题 更多 >