訂閱
糾錯
加入自媒體

如何使用 Python比較兩張圖像并獲得準(zhǔn)確度?

本文,將帶你了解如何使用 Python、OpenCV 和人臉識別模塊比較兩張圖像并獲得這些圖像之間的準(zhǔn)確度水平。

首先,你需要了解我們是如何比較兩個圖像的。我們正在使用Face Recognition python 模塊來獲取兩張圖像的128 個面部編碼,我們將比較這些編碼。比較結(jié)果返回 True 或 False。如果結(jié)果為True ,那么兩個圖像將是相同的。如果是False,則兩個圖像將不相同。

128 種面部編碼將如下所示

128 個人臉編碼(人臉標(biāo)志)

僅當(dāng)比較結(jié)果返回 True 值時,才會打印準(zhǔn)確度級別。

現(xiàn)在,讓我們進(jìn)入本主題的編碼部分,

為了實現(xiàn)這一點,我們需要安裝幾個 python 模塊。為此,只需打開命令提示符或終端,鍵入以下內(nèi)容。

pip install opencv-python

pip install face-recognition

安裝后,現(xiàn)在是時候?qū)脒@些模塊了。然后,我們需要創(chuàng)建一個名為 find_face_encodings(image_path) 的新函數(shù),它獲取圖像位置(路徑)并返回 128 個面部編碼,這在比較圖像時非常有用。

find_face_encodings(image_path) 函數(shù)將使用 OpenCV 模塊,從我們作為參數(shù)傳遞的路徑中讀取圖像,然后返回使用 face_recognition 模塊中的 face_encodings() 函數(shù)獲得的 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]

現(xiàn)在,使用兩個不同的圖像路徑調(diào)用 find_face_encodings(image_path) 函數(shù),并將其存儲在兩個不同的變量中,image_1image_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")

現(xiàn)在,我們可以使用編碼執(zhí)行比較和查找這些圖像的準(zhǔn)確性等操作。

· 比較將通過使用 face_recognition 中的 compare_faces() 函數(shù)來完成。

· 通過找到 100 和 face_distance 之間的差異來確定準(zhǔn)確性。

# 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


       原文標(biāo)題 : 如何使用 Python比較兩張圖像并獲得準(zhǔn)確度?

聲明: 本文由入駐維科號的作者撰寫,觀點僅代表作者本人,不代表OFweek立場。如有侵權(quán)或其他問題,請聯(lián)系舉報。

發(fā)表評論

0條評論,0人參與

請輸入評論內(nèi)容...

請輸入評論/評論長度6~500個字

您提交的評論過于頻繁,請輸入驗證碼繼續(xù)

暫無評論

暫無評論

人工智能 獵頭職位 更多
掃碼關(guān)注公眾號
OFweek人工智能網(wǎng)
獲取更多精彩內(nèi)容
文章糾錯
x
*文字標(biāo)題:
*糾錯內(nèi)容:
聯(lián)系郵箱:
*驗 證 碼:

粵公網(wǎng)安備 44030502002758號