Skip to main content
 首页 » 编程设计

python之'函数 imshow 中的 libpng 错误 : Invalid IHDR data' and cpp:331: error: (-215) size. width>0 && size.height>0

2024年12月31日24think

我正在尝试通过 Raspberry pi3 和 pi 相机使用人脸识别。 工作环境如下。

Python 2.7.13 
Python3 3.5.3 
scikit-learn 0.18 
numpy 1.12.1 
scipy 0.18.1 
matplotlib 2.0.0 
PIL(Pillow) 4.0.0 
keras 2.2.0 
theano 1.0.2 

当我执行程序时,出现以下错误。 在 stackoverflow 和其他网站上搜索错误,有一种解决方案是使用“VideoCapture(1)”而不是“VideoCapture(0)”,但就我而言,它不起作用。 然后,当我搜索有关“libpng 警告:IHDR 中的图像宽度为零”的错误时,建议插入 time.sleep() 以等待相机启动。我试过了,但对我不起作用。

pi@raspberrypi:~/20180717_WebScrb $ python3 face_detection.py 
Using Theano backend. 
libpng warning: Image width is zero in IHDR 
libpng warning: Image height is zero in IHDR 
libpng error: Invalid IHDR data 
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/pi/opencv-3.4.0/modules/highgui/src/window.cpp, line 331 
Traceback (most recent call last): 
  File "face_detection.py", line 75, in <module> 
    main() 
  File "face_detection.py", line 63, in main 
    cv2.imshow("Show FLAME Image", frame) 
cv2.error: /home/pi/opencv-3.4.0/modules/highgui/src/window.cpp:331: error: (-215) size.width>0 && size.height>0 in function imshow 

Python代码如下。

import face_keras as face 
import sys, os 
from keras.preprocessing.image import load_img, img_to_array 
import numpy as np 
import cv2 
import time 
 
 
cascade_path = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" 
#cascade_path = "haarcascade_frontalface_alt.xml" 
cascade = cv2.CascadeClassifier(cascade_path) 
cam = cv2.VideoCapture(0) 
color = (255, 255, 255) 
 
image_size = 32 
categories = ["obama", "trump"] 
 
def main(): 
    while(True): 
        ret, frame = cam.read() 
        facerect = cascade.detectMultiScale(frame, scaleFactor=1.2, minNeighbors=2, minSize=(10, 10)) 
        cv2.imwrite("frontalface.png", frame) 
        img = cv2.imread("frontalface.png") 
        for rect in facerect: 
            cv2.rectangle(frame, tuple(rect[0:2]),tuple(rect[0:2] + rect[2:4]), color, thickness=2) 
            x = rect[0] 
            y = rect[1] 
            width = rect[2] 
            height = rect[3] 
            dst = img[y:y+height, x:x+width] 
            cv2.imwrite("output.png", dst) 
            cv2.imread("output.png") 
            X = [] 
 
            img = load_img("./output.png", target_size=(image_size,image_size)) 
            in_data = img_to_array(img) 
 
            X.append(in_data) 
            X = np.array(X) 
            X  = X.astype("float")  / 256 
 
            model = face.build_model(X.shape[1:]) 
            model.load_weights("./image/face-model.h5") 
 
            pre = model.predict(X) 
            print(pre) 
            if pre[0][0] > 0.9: 
                print(categories[0]) 
                text = categories[0] + str(pre[0][0]*100) + "%" 
                font = cv2.FONT_HERSHEY_PLAIN 
                cv2.putText(frame,text,(rect[0],rect[1]-10),font, 2, color, 2, cv2.LINE_AA) 
            elif pre[0][1] > 0.9: 
                print(categories[1]) 
                text = categories[1] + str(pre[0][1]*100) + "%" 
                font = cv2.FONT_HERSHEY_PLAIN 
                cv2.putText(frame,text,(rect[0],rect[1]-10),font, 2, color, 2, cv2.LINE_AA) 
            elif pre[0][2] > 0.9: 
                print(categories[2]) 
                text = categories[2] + str(pre[0][2]*100) + "%" 
                font = cv2.FONT_HERSHEY_PLAIN 
                cv2.putText(frame,text,(rect[0],rect[1]-10),font, 2, color, 2, cv2.LINE_AA) 
 
        cv2.imshow("Show FLAME Image", frame) 
        time.sleep(0.4) 
 
        k = cv2.waitKey(1) 
 
        if k == ord('q'): 
            break 
 
    cam.release() 
    cv2.destroyAllWindows() 
 
if __name__ == '__main__': 
    main() 

我认为“cv2.imwrite("frontalface.png", frame)”不起作用,因为它在当前文件夹中输出 frontalface.png,但它是 0 字节(空)。 如果这篇文章与某人的问题完全相同,我很抱歉。但是,我无法通过自己的尝试找到它。所以让我在这里问你一个问题。

请您参考如下方法:

尝试测试 dst[0] 是否不等于 0。请检查此更正并告诉我它是否有效。

  dst = img[y:y+height, x:x+width] 
  if dst[0].size > 0: 
         cv2.imwrite("output.png", dst)