如何使用 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_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")
現(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)確度?
請輸入評論內(nèi)容...
請輸入評論/評論長度6~500個字
最新活動更多
-
即日-10.29立即報名>> 2024德州儀器嵌入式技術(shù)創(chuàng)新發(fā)展研討會
-
10月31日立即下載>> 【限時免費下載】TE暖通空調(diào)系統(tǒng)高效可靠的組件解決方案
-
即日-11.13立即報名>>> 【在線會議】多物理場仿真助跑新能源汽車
-
11月14日立即報名>> 2024工程師系列—工業(yè)電子技術(shù)在線會議
-
12月19日立即報名>> 【線下會議】OFweek 2024(第九屆)物聯(lián)網(wǎng)產(chǎn)業(yè)大會
-
即日-12.26火熱報名中>> OFweek2024中國智造CIO在線峰會
推薦專題
- 1 Intel宣布40年來最重大轉(zhuǎn)型:年底前裁員15000人、拋掉2/3房產(chǎn)
- 2 因美封殺TikTok,字節(jié)股價骨折!估值僅Meta1/5
- 3 宏山激光重磅發(fā)布行業(yè)解決方案,助力智能制造產(chǎn)業(yè)新飛躍
- 4 國產(chǎn)AI芯片公司破產(chǎn)!白菜價拍賣
- 5 具身智能火了,但規(guī)模落地還需時間
- 6 國產(chǎn)英偉達(dá)們,抓緊沖刺A股
- 7 三次錯失風(fēng)口!OpenAI前員工殺回AI編程賽道,老東家捧金相助
- 8 英特爾賦能智慧醫(yī)療,共創(chuàng)數(shù)字化未來
- 9 英偉達(dá)的麻煩在后頭?
- 10 將“網(wǎng)紅”變成“商品”,AI“爆改”實力拉滿
- 高級軟件工程師 廣東省/深圳市
- 自動化高級工程師 廣東省/深圳市
- 光器件研發(fā)工程師 福建省/福州市
- 銷售總監(jiān)(光器件) 北京市/海淀區(qū)
- 激光器高級銷售經(jīng)理 上海市/虹口區(qū)
- 光器件物理工程師 北京市/海淀區(qū)
- 激光研發(fā)工程師 北京市/昌平區(qū)
- 技術(shù)專家 廣東省/江門市
- 封裝工程師 北京市/海淀區(qū)
- 結(jié)構(gòu)工程師 廣東省/深圳市