AttributeError: 'NoneType' object has no attribute 'shape'

#使用OpenCV报错:File "E:\project\Scrape Center\opencv识别缺口.py", line 72, in main
image_height, image_width, _ = image_raw.shape
^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'shape'

#代码:

import time

from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from selenium import webdriver
import cv2
import requests
GAUSSIAN_BLUR_KERNEL_SIZE = (5,5)
GAUSSIAN_BLUR_SIGMA_X = 0
CANNY_THRESHOLD1 = 200
CANNY_THRESHOLD2 = 450
def sava_image():
    chrome=webdriver.Chrome()
    chrome.get('https://www.douban.com/')
    iframe_node = chrome.find_element(By.XPATH,'//*[@id="anony-reg-new"]/div/div[1]/iframe')
    chrome.switch_to.frame(iframe_node)
    chrome.find_element(By.XPATH,'/html/body/div[1]/div[1]/ul[1]/li[2]').click()
    chrome.find_element(By.XPATH,'//*[@id="username"]').send_keys('18885410657')
    chrome.find_element(By.XPATH,'//*[@id="password"]').send_keys('708090.')
    chrome.find_element(By.XPATH,'/html/body/div[1]/div[2]/div[1]/div[5]/a').click()
    time.sleep(3)
    iframe_node_2 = chrome.find_element(By.XPATH,'//*[@id="tcaptcha_iframe_dy"]')
    chrome.switch_to.frame(iframe_node_2)
    # chrome.find_element(By.XPATH,'//*[@id="slideBg"]').screenshot('douban.png')
    # chrome.get('//*[@id="slideBg"]').content


    ele_bj = chrome.find_element(By.XPATH,'//*[@id="slideBg"]')
    # src_bj = ele_bj.get_attribute('src')# 获取bj图片下载地址
    # print(src_bj)
    src_bj='https://t.captcha.qq.com/cap_union_new_getcapbysig?img_index=1&image=0279050000c7192c0000000bb5e3112734d3&sess=s0Lh-PZ_E3lygc40HLJaThwzfMSb3YpHk--uMnOoHEvQDa5ANDG9V4lip-D1jqYLb50kXKk8SG8yATPo_2aC8oDj_l4Ve2DCMKQ7BMCVkvN7t03MvT2jBURQx8GpGye4keqG669dQnGVX9rT8auf9Co55MWzf0tf8O8JODxID0gf5lzt7o9V8QnA2BKqQNa6IWuccAGbYbIWLW5R7pY0uy_GWu7YyHGQZvXHf22smWRo8kI41x37gcQESHNU_3iwihegimxxyOZcozz7RpEjIlJde3Azaj0WHVrktxEiSbynlBP1JJo-dJWR1ZIqK1REz4fxxOKvmSCNFF85dMGo0oPTMGmnZsh4rNSIRhlohXWUkGC5ofZjSVwg**'
    content = requests.get(src_bj).content  # 下载背景
    f = open('douban.png',mode='wb')
    f.write(content)
    f.close()
    print('下载完成背景图片')

    time.sleep(3)
def get_gaussian_blur_image(image):
    return cv2.GaussianBlur(image,GAUSSIAN_BLUR_KERNEL_SIZE,GAUSSIAN_BLUR_SIGMA_X,0)

def get_canny_image(image):
    return cv2.Canny(image,CANNY_THRESHOLD1,CANNY_THRESHOLD2)

def get_contours(image):
    contours, _ = cv2.findContours(image, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
    # print(contours)
    return contours

def get_contour_area_threshold(image_width, image_height):
    # 20%的误差
    # 100%-20%
    contour_area_min = (image_width * 0.15) * (image_height * 0.25) * 0.8
    # 100%+20%
    contour_area_max = (image_width * 0.15) * (image_height * 0.25) * 1.2
    return contour_area_min, contour_area_max

def get_arc_length_threshold(image_width, image_height):
    arc_length_min = ((image_width * 0.15) + (image_height * 0.25)) * 2 * 0.8
    arc_length_max = ((image_width * 0.15) + (image_height * 0.25)) * 2 * 1.2
    return arc_length_min, arc_length_max

def get_offset_threshold(image_width):
    offset_min = 0.2 * image_width
    offset_max = 0.85 * image_width
    return offset_min, offset_max

def main():
    sava_image()
    time.sleep(5)
    image_raw = cv2.imread('douban.png')
    image_height, image_width, _ = image_raw.shape
    print(image_width,image_height)
    print('#'*50)
    image_gaussian_blur = get_gaussian_blur_image(image_raw)
    image_canny = get_canny_image(image_gaussian_blur)
    contours =get_contours(image_canny)
    cv2.imwrite('image_canny.png', image_canny)
    cv2.imwrite('image_gaussian_blur.png', image_gaussian_blur)
    contour_area_min, contour_area_max =get_contour_area_threshold(image_width, image_height)
    arc_length_min, arc_length_max =get_arc_length_threshold(image_width, image_height)
    offset_min, offset_max =get_offset_threshold(image_width)
    offset = None
    for contour in contours:
        # x,y坐标、宽度,高度
        x, y, w, h = cv2.boundingRect(contour)
        print(x,y,w,h)
        if contour_area_min < cv2.contourArea(contour) < contour_area_max and \
                arc_length_min < cv2.arcLength(contour, True) < arc_length_max and \
                offset_min < x < offset_max:
            # 在原有图片绘制矩形。在图片img上画长方形,坐标原点是图片左上角,向右为x轴正方向,向下为y轴正方向。左上角(x,y),右下角(x,y) ,颜色(B,G,R), 线的粗细
            # 返回:在原图像上画矩阵。1
            # 参数说明:img表示图片,(x, y)表示矩阵左上角的位置,(x+w, y+h)表示矩阵右下角的位置, (0, 0, 255)表示颜色,2表示线条
            cv2.rectangle(image_raw, (x, y), (x + w, y + h), (0, 0, 255), 2)
            offset = x
            # print(offset)
            # print('offset', offset)
    cv2.imwrite('image_label.png', image_raw)
    print('offset', offset)

if __name__ == '__main__':
# spider=douban()
    main()




#怎么回事呢?

你这个debug一下就知道了

报错是由于在获取图像数据时出现了空值,即image_raw为None,导致后续无法调用shape属性。出现这种情况可能是因为读取图片的路径有问题或者图片下载未成功。请确认douban.png位于正确的路径下,并且下载成功了。

这个错误是由于您尝试使用一个NoneType对象的形状属性造成的。这可能是因为您尝试访问一个未定义的变量或对象的形状属性。您可以检查一下代码中是否有拼写错误或其他错误,或者确保您正在使用一个有效的变量或对象

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^