我编写这段代码是为了用网络摄像头拍照。当我在树莓派上运行它时,它给了我这个错误:损坏的 jpeg 数据:标记 0xd6 之前有 1 个无关字节 我该如何解决这个问题?
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")
最佳答案
我的程序不断获取图像并不断打印出这些错误消息,因此您在这些垃圾中看不到相关消息。我的解决方案:而不是调用
./myprogram
我打电话
unbuffer ./myprogram |& grep --line-buffered -v "Corrupt JPEG data:"
grep 命令会抑制不需要的消息。 "|&"是一个管道,将 stdout 和 stderr 结合起来,"unbuffer"和 "--line buffer"防止缓冲。否则,只有在“myprogram”终止后,我才能在终端上看到输出。程序“unbuffer”是包“expect”的一部分。 这适用于我的装有 Linux Mint 的 PC,应该也适用于 Raspbian。
关于python - Raspbian 上的 OpenCV 错误 : Corrupt jpeg data: 1 extraneous bytes before marker 0xd6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42931346/