簡體   English   中英

如何解決這個“OSError: cannot identify image file” Opencv imwrite 實際上並沒有保存圖像

[英]how to fix this“OSError: cannot identify image file ” Opencv imwrite doesn't actually save image

我現在正在從這個 git 學習 Opencv

https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py

並添加一些代碼來捕獲圖像,它確實捕獲並將圖像保存到我指定的路徑,但它不顯示它保存的圖像(對不起,我不知道該怎么稱呼它)它說不支持這種格式文件

:[1]: https://ibb.co/zXp7PmY

我嘗試將格式文件更改為 jpg、bmp、png 將這些代碼移入/移出 for 循環添加

top = 200
right = 200
bottom = 200
left = 200

如果不在 imshow for 循環中

poor_match_index = np.argmax(face_distances)            
           if matches[poor_match_index]:
               cv2.imwrite("tanapat/unknown_" + str(count) + ".jpg", frame[right:left,top:bottom]) 
               unknown = face_recognition.load_image_file("tanapat/unknown_"+ str(count) +".jpg")
               unknown_encoding = face_recognition.face_encodings(unknown )[0]
               known_face_encodings.append(unknown_encoding)
               known_face_names.append("unknown_"+str(count))
               name = known_face_names[poor_match_index]
               count +=1
               break

我試圖讓它重復捕捉新面孔並識別它(盡量不要讓它繼續捕捉同一個人太多幀)

但在線出錯

unknown = face_recognition.load_image_file("tanapat/unknown_"+ str(count) +".jpg")

OSError:無法識別圖像文件“tanapat/unknown_0.jpg”

如果你使用

top = 200
right = 200
bottom = 200
left = 200

然后frame[200:200,200:200]創建空數組。

 width = right - left = 200 - 200 = 0
 height = bottom - top = 200 - 200 = 0

當您保存空數組時,您會得到空文件 - 我的 Linux 顯示此.jpg的大小為 0 - 您無法打開空文件。

你至少需要

top = 200
bottom = top + 1  # 201

left = 200
right = left + 1  # 201

創建一個像素的文件 - frame[200:201,200:201]


如果您使用print()顯示imwrite()的結果

print(cv2.imwrite(...))

如果保存文件有問題,那么你會得到False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM