【python】error: (-215) ssize.width > 0 && ssize.height > 0 in function cv::resize

1.Code

import os
import numpy as np
import sys
import cv2
import time
import random


def detect(imgPath, imgLabel):
    # Read Image
    frame = cv2.imread(imgPath)

    if True:
        # Subtraction from mean file
        crop = cv2.resize(frame, (256, 256))

        # Showing output
        cv2.imshow("Gender Detection",crop)
        cv2.waitKey(0)

def main(argv):
    assert (len(argv) == 2)

    original_txt = argv[1]

    with open(original_txt) as f:
        def_lines = f.readlines()

    def_lines.pop(0)

    full_list = []
    for def_line in def_lines:
        def_dic = {}

        image_dir = def_line.split(' ')[0]
        temp = def_line.split(' ')[1]
        image_label = temp.split('\n')[0]
        image_name = '/home/zhuxiuhong/dataset/age_gender/DATA/aligned/{0}'.format(image_dir)

        def_dic['image_name'] = image_name
        def_dic['image_label'] = image_label
        full_list.append(def_dic)

    images_num = len(full_list)

    for ind in range(images_num):
        image_name = full_list[ind]['image_name']
        image_label = full_list[ind]['image_label']
        detect(image_name, image_label)


if __name__ == '__main__':
    print(__doc__)
    import sys
    main(sys.argv)

2.Error



3.Solution

当cv2.imread()读入图像的路径不存在时,frame为None,再对其进行cv2.resize(),发生错误

frame = cv2.imread(imgPath)

加入语句:

if(frame == None):
  return

修改后如下:

import os
import numpy as np
import sys
import cv2
import time
import random


def detect(imgPath, imgLabel):
    # Read Image
    frame = cv2.imread(imgPath)

    if(frame == None):
        return
        
    if True:
        # Subtraction from mean file
        crop = cv2.resize(frame, (256, 256))

        # Showing output
        cv2.imshow("Gender Detection",crop)
        cv2.waitKey(0)

def main(argv):
    assert (len(argv) == 2)

    original_txt = argv[1]
        
    with open(original_txt) as f:
        def_lines = f.readlines()

    def_lines.pop(0)

    full_list = []
    for def_line in def_lines:
        def_dic = {}

        image_dir = def_line.split(' ')[0]
        temp = def_line.split(' ')[1]
        image_label = temp.split('\n')[0]
        image_name = '/home/zhuxiuhong/dataset/age_gender/DATA/aligned/{0}'.format(image_dir)

        def_dic['image_name'] = image_name
        def_dic['image_label'] = image_label
        full_list.append(def_dic)

    images_num = len(full_list)

    for ind in range(images_num):
        image_name = full_list[ind]['image_name']
        image_label = full_list[ind]['image_label']
        detect(image_name, image_label)


if __name__ == '__main__':
    print(__doc__)
    import sys
    main(sys.argv)


注意:码代码要把意外情况考虑全面

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
在车牌检测识别过程中,遇到报错"error: (-215:Assertion failed) !ssize.empty() in functioncv::resize‘"。这个错误是由于在调用cv2.resize函数时,输入图像尺寸为空所引起的。根据引用和引用的内容,可以确定这个错误是由于图像的尺寸大小不合适导致的。为了解决这个问题,可以根据引用中提供的修改后的代码进行调整。在代码中,通过对图像尺寸进行判断,如果图像高度大于36像素,则将图像尺寸调整为(136, 36 * 2),否则将图像尺寸调整为(136, 36)。这样可以确保输入图像的尺寸不为空,避免了报错。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [cpp:3720: error: (-215:Assertion failed) !ssize.empty() in function 'resize' 错误解决](https://blog.csdn.net/WellTung_666/article/details/105551802)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [error: (-215:Assertion ...ssize.empty() in functioncv::resize‘ 或者(ssize.width > 0 && ssize.h](https://blog.csdn.net/m0_70240540/article/details/129665366)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值