如何使用 Python比較兩張圖像并獲得準確度?
本文,將帶你了解如何使用 Python、OpenCV 和人臉識別模塊比較兩張圖像并獲得這些圖像之間的準確度水平。
首先,你需要了解我們是如何比較兩個圖像的。我們正在使用Face Recognition python 模塊來獲取兩張圖像的128 個面部編碼,我們將比較這些編碼。比較結果返回 True 或 False。如果結果為True ,那么兩個圖像將是相同的。如果是False,則兩個圖像將不相同。
128 種面部編碼將如下所示

128 個人臉編碼(人臉標志)
僅當比較結果返回 True 值時,才會打印準確度級別。
現在,讓我們進入本主題的編碼部分,
為了實現這一點,我們需要安裝幾個 python 模塊。為此,只需打開命令提示符或終端,鍵入以下內容。
pip install opencv-python
pip install face-recognition
安裝后,現在是時候導入這些模塊了。然后,我們需要創建一個名為 find_face_encodings(image_path) 的新函數,它獲取圖像位置(路徑)并返回 128 個面部編碼,這在比較圖像時非常有用。
find_face_encodings(image_path) 函數將使用 OpenCV 模塊,從我們作為參數傳遞的路徑中讀取圖像,然后返回使用 face_recognition 模塊中的 face_encodings() 函數獲得的 128 個人臉編碼。
import cv2
import face_recognition
def find_face_encodings(image_path):
# reading image
image = cv2.imread(image_path)
# get face encodings from the image
face_enc = face_recognition.face_encodings(image)
# return face encodings
return face_enc[0]
現在,使用兩個不同的圖像路徑調用 find_face_encodings(image_path) 函數,并將其存儲在兩個不同的變量中,image_1和image_2
# getting face encodings for first image
image_1 = find_face_encodings("image_1.jpg")
# getting face encodings for second image
image_2 = find_face_encodings("image_2.jpg")
現在,我們可以使用編碼執行比較和查找這些圖像的準確性等操作。
· 比較將通過使用 face_recognition 中的 compare_faces() 函數來完成。
· 通過找到 100 和 face_distance 之間的差異來確定準確性。
# checking both images are same
is_same = face_recognition.compare_faces([image_1], image_2)[0]
print(f"Is Same: {is_same}")
if is_same:
# finding the distance level between images
distance = face_recognition.face_distance([image_1], image_2)
distance = round(distance[0] * 100)
# calcuating accuracy level between images
accuracy = 100 - round(distance)
print("The images are same")
print(f"Accuracy Level: {accuracy}%")
else:
print("The images are not same")
輸出——案例 1


Is Same: True
The images are same
Accuracy Level: 64%
輸出——案例 2


Is Same: False
The images are not same
原文標題 : 如何使用 Python比較兩張圖像并獲得準確度?
請輸入評論內容...
請輸入評論/評論長度6~500個字
最新活動更多
- 1 AI狂歡遇上油價破百,全球股市還能漲多久? | 產聯看全球
- 2 OpenAI深夜王炸!ChatGPT Images 2.0實測:中文穩、細節炸,設計師慌了
- 3 6000億美元估值錨定:字節跳動的“去單一化”突圍與估值重構
- 4 Tesla AI5芯片最新進展總結
- 5 連夜測了一波DeepSeek-V4,我發現它可能只剩“審美”這個短板了
- 6 熱點丨AI“瑜亮之爭”:既生OpenClaw,何生Hermes?
- 7 AI界的殺豬盤:9秒刪庫跑路,全員被封號,還繼續扣錢!
- 8 2026,人形機器人只贏了面子
- 9 DeepSeek降價90%:價格屠夫不是身份,是戰略
- 10 AI Infra產業鏈卡在哪里了?


分享













