【发布时间】:2017-08-13 08:51:12
【问题描述】:
我编写了这段代码来使用网络摄像头拍照。当我在树莓派上运行它时,它给了我这个错误:Corrupt jpeg data: 1 extraneous bytes before marker 0xd6 我该如何解决这个问题?
i=0
cam=cv2.VideoCapture(0)
while(True):
ret,img=cam.read();
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces=faceDetec.detectMultiScale(gray,1.3,10)
for (x,y,w,h) in faces:
i+=1
#il faut que le fichier dataSet existe déjà...
# on enregistre l'image dans le fichier dataSet sous le nom User.id.i.jpg
#gray[y:y+h,x:x+w] est l'image croppée on ne sauve que le visage.
cv2.imwrite("dataSet/User."+str(id)+"."+str(i)+".jpg",gray[y:y+h,x:x+w])
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
# on attend 500 milli secondes pour pouvoir changer de tête grimace, profil, avec lunettes,...
cv2.waitKey(1000)
cv2.waitKey()
cv2.imshow("Face",img);
cv2.waitKey(1)
if i>=20:
break
cam.release()
cv2.destroyAllWindows()
os.system("pause")
【问题讨论】:
-
这是网络摄像头和 Raspberry Pi 的常见问题:raspberrypi.org/forums/viewtopic.php?t=47586&p=372981。建议是手动更新内核。此外,一些用户报告说,即使出现这些错误,仍然可以查看图像数据。你试过打开图片吗?
-
这里出现错误{ret,img=cam.read();}
-
是的,我可以打开图像,但过程变慢了。
-
好的,但是如何隐藏这些警告?
-
我不会将减速归因于损坏的 JPEG 数据错误。请记住,您还将图像写入 SD 卡,当然您的 RPi 正在耗尽,因此可能会导致它变慢。 SD卡的读/写时间是否非常快? Not all SD cards are created equal。另请记住,SD 卡的读/写性能是影响 RPi 运行的一个因素。
标签: python opencv raspberry-pi