我正在尝试将人脸识别与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字节(空)。 很抱歉,这篇文章与某人的提问内容完全相同。但是,我自己尝试找不到。所以我在这里问你一个问题。
答案 0 :(得分:1)
尝试测试dst [0]是否不等于0。请检查此更正,并告诉我它是否有效。
dst = img[y:y+height, x:x+width]
if dst[0].size > 0:
cv2.imwrite("output.png", dst)