簡體   English   中英

Python Face_Recognition with base64 編碼圖像

[英]Python Face_Recognition with base64 encoded image

我正在使用face_recognition package 進行人臉識別

輸入圖像文件是 base64 編碼的,

我正在嘗試解碼數據並

face_recognition.face_encodings(decodedBase64Data)

我有面部編碼數據列表進行比較。

問題是我需要將 base64 數據轉換為我可以使用 face_encodings 進行編碼的圖像。

我試過

decodedData = base64.b64decode(data)
encodeFace = np.frombuffer(decodedData, np.uint8)

並將 encodedFace 傳遞給

face_recognition.face_encodings(decodedBase64Data)

我得到錯誤Unsupported image type, must be 8bit gray or RGB image.

如何將 base64 轉換成與 face_encodings 兼容的圖像?

編輯:

附上代碼供參考

import base64
import numpy as np
import json
import face_recognition as fr

with open('Face_Encoding_Data.json') as f:
    EncodeJsonData = json.load(f)
    personName = list(EncodeJsonData.keys())
    encodedImgList = list(EncodeJsonData.values())
"""
EncodeJsonData = {"name1" : [encoded data 1], "name2" : [encoded data 2]}
128 byte
"""
base64Data = """ base64 encoded image with face """
encodeFace = np.frombuffer(base64.b64decode(base64Data), np.uint8)

matches = fr.compare_faces(encodedImgList, encodeFace, tolerance=0.5)

faceDist = fr.face_distance(encodedImgList, encodeFace)
matchIndex = np.argmin(faceDist)

name = "unknown"
if matches[matchIndex]:
    name = personName[matchIndex]

print(name)

請分享代碼以更好地理解問題,或者您可以使用以下代碼作為參考

import cv2
import os
import numpy as np
from PIL import Image
import time
cap = cv2.VideoCapture(1)

count=1
path='dataset2'

img=[]
imagepath = [os.path.join(path,f)for f in os.listdir(path)]
c=len(imagepath)
#for i in imagepath: i access the each images from my folder of images
while count<=c:
    image = face_recognition.load_image_file("dataset2/vrushang."+str(count)+".jpg")
    #now i will make list of the encoding parts to compare it runtime detected face
    img.append(face_recognition.face_encodings(image)[0])
    time.sleep(1)
    count=count+1

    face_locations = []
    face_encodings = []
    face_names = []
    process_this_frame = True
    img2 = []
    img2 = img[0]
    print"this is img2"
    print img

while True:
    ret, frame = cap.read()
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
    if process_this_frame:
    face_locations = face_recognition.face_locations(small_frame)
    face_encodings = face_recognition.face_encodings(small_frame, face_locations)
    face_names = []
    for face_encoding in face_encodings:
        match = face_recognition.compare_faces(img, face_encoding)
                 print match
   
                 if match[0]==True:
                       name = "vrushang"
            elif match[1]==True:
                       name = "hitu"    
            elif match[3]==True:
                      name = "sardar patel" 
            elif match[2]==True:
                      name = "yaksh"
            else:       
              name = "unknown"
                face_names.append(name)
process_this_frame = not process_this_frame   
for (top, right, bottom, left), name in zip(face_locations, face_names):
    top *= 4
    right *= 4
    bottom *= 4
    left *= 4
    cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)      
    cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)  
cv2.imshow('Video', frame)   
if cv2.waitKey(1)==27:
    break```

你解決了這個問題嗎? 你能給我程序代碼嗎?

嘗試使用此代碼加載用於 face_recognition package 的編碼圖像:

import urllib.request as ur
import face_recognition as fr
image = 'Input image file is base64 encoded'
decoded= ur.urlopen(image)
image_loaded = fr.load_image_file(decoded)

=> 測試:

face_locations = fr.face_locations(image_loaded)
print("I found {} face(s) in this photograph.".format(len(face_locations)))

暫無
暫無

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

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