【问题标题】:Cv2.error : (-215:Assertion failed) !_img.empty() in function 'imwrite'Cv2.error : (-215:Assertion failed) !_img.empty() in function 'imwrite'
【发布时间】:2020-06-07 05:36:10
【问题描述】:

我正在尝试根据它们的标签标签将图像分割为我的项目的头盔、非头盔或未处理的图像。但是有错误说图像对象为空。我已经制作了一个辅助脚本,在其中处理了正在工作的同一图像。我不知道为什么,但似乎 opencv 库无法读取一个特定的图像。任何帮助将不胜感激。

import requests
import shutil
import json
import sys
import os
import cv2
x=0
lis=[]
s=""
def batwara(filename,data):
    print("unproccesed/"+filename[6:])
    print(data)

    image = cv2.imread(filename)
    if image is None:
        print("NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN")
        return
    color = (255, 0, 0)
    thickness = 2
    totfaces = data['annotation']
    if totfaces is None:
        cv2.imwrite("unproccesed/"+filename[6:],image)
        return

    x=0
    for face in totfaces:
        x=x+1
        new_file=str(x)+filename[9:]
        print(new_file)
        label= face['label']
        print ("label=============",label)
        wid=face['imageWidth']
        hei=face['imageHeight']

        x1=int(face['points'][0]['x']*wid)
        y1=int(face['points'][0]['y']*hei)
        x2=int(face['points'][1]['x']*wid)
        y2=int(face['points'][1]['y']*hei)

        #print (x1,y1,x2,y2,wid,hei)
        start_point = (x1, y1)
        end_point = (x2, y2)
        crop_img = image[y1:y2, x1:x2]
        if len(label)==0:
            new_file= "unidentified/img"+new_file
            cv2.imwrite(new_file,crop_img)
        elif label[0] == "Without Helmet":
            new_file= "non_helmet/img"+new_file
            cv2.imwrite(new_file,crop_img)
        elif label[0] == "With Helmet":
            new_file= "helmet/img"+new_file
            cv2.imwrite(new_file,crop_img)






with open('/home/oem/Downloads/Bikers Wearing Helmet Or Not.json') as f:
    while True:
        c = f.read(1)
        s=s+c
        if c=='{' or c=='[':
            lis.append(c)
        if c==']'or c=='}':
                lis.pop()
                if len(lis)==0:
                    x=x+1
                    #print(filename)
                    #print(s)
                    data = json.loads(s)
                    filen,ex= os.path.splitext(data['content'])

                    filename= "image/img"+str(x)+ex
                    #print(data)
                    #print (data['content'])
                    # This is the image url.
                    image_url = data['content']
                    # Open the url image, set stream to True, this will return the stream content.
                    resp = requests.get(image_url, stream=True)
                    # Open a local file with wb ( write binary ) permission.
                    local_file = open(filename, 'wb')
                    # Set decode_content value to True, otherwise the downloaded image file's size will be zero.
                    resp.raw.decode_content = True
                    # Copy the response stream raw data to local image file.
                    shutil.copyfileobj(resp.raw, local_file)
                    # Remove the image url response object.
                    del resp

                    if ex!=".png":
                        batwara(filename,data)
                    s=""


        if not c:
            print ("End of file")
            print(x)
            break

终端显示的错误是:

Traceback (most recent call last):
  File "app2.py", line 91, in <module>
    batwara(filename,data)
  File "app2.py", line 46, in batwara
    cv2.imwrite(new_file,crop_img)
cv2.error: OpenCV(4.2.0) /io/opencv/modules/imgcodecs/src/loadsave.cpp:715: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'

【问题讨论】:

  • 你确定你的作物不是空的吗? IE。 x1 == x2y1 == y2 有可能吗?
  • 情况并非如此,因为只有在图像中找到人脸时才会生成这些值。
  • 保存失败的crop_img是什么形状?
  • 感谢您的指出,我刚刚整理出 y1==y2 所以这就是crop_img 为空的原因。而crop_img的形状是(0,4,3)
  • 那你能把答案标记为答案吗?

标签: python opencv


【解决方案1】:

您需要确保x1 == x2y1 == y2

【讨论】:

    猜你喜欢
    • 2021-04-14
    • 2019-08-30
    • 2021-07-30
    • 2021-08-16
    • 1970-01-01
    • 2019-06-19
    • 2021-04-09
    • 2019-11-12
    • 2021-02-08
    相关资源
    最近更新 更多