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

3

我正在尝试使用树莓派3和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 warning: Image width is zero in 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 字节(空的)。如果这篇文章与某个人的问题完全相同,我很抱歉。然而,我自己找不到它,所以让我在这里问一个问题。

1
你的工作环境如何同时包含Python 2.7.13和Python 3.5.3? - desertnaut
抱歉,我打错了 pi@raspberrypi:~ $ python --version Python 2.7.13 pi@raspberrypi:~ $ python3 --version Python 3.5.3 - bobby
我的评论背后的想法是,你要么使用Python 2.7.13,要么使用Python 3.5.3。列出机器上所有可用的Python版本对于这个问题毫无意义。 - desertnaut
1个回答

1
尝试测试一下,看看 dst[0] 是否不等于 0。请检查此更正并告诉我它是否有效。
  dst = img[y:y+height, x:x+width]
  if dst[0].size > 0:
         cv2.imwrite("output.png", dst)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接