简体   繁体   English

opencv 形状错误 [utils] 使图像和徽标一起出现

[英]opencv error in the shape [utils] to make the image and the logo come out together

i follow a tutorial to learn how to use opencv which and a practical library with tensorflow notably for the cnn but I have an error我按照教程学习如何使用 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

code utils.py代码实用程序.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']

watermark.py水印.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()

thanks for helping me found the error the expected result is that the logo image 1.png when the camera turns on and the video download shows both the logo + the standard result感谢您帮助我找到错误预期结果是当相机打开时徽标图像 1.png 和视频下载同时显示徽标 + 标准结果

Here's your problem, you're reading an image from a non-existent file, hence the empty image that has no shape and is NoneType :这是你的问题,你正在从一个不存在的文件中读取图像,因此空图像没有shape并且是NoneType

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

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

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