首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带有Face_Recognition编码图像的Python

带有Face_Recognition编码图像的Python
EN

Stack Overflow用户
提问于 2021-03-14 12:11:19
回答 2查看 1.1K关注 0票数 0

我使用识别软件包进行人脸识别

输入图像文件是base64编码的,

我试着破译数据

代码语言:javascript
运行
复制
face_recognition.face_encodings(decodedBase64Data)

我有脸编码的数据列表要比较。

问题是,我需要将base64数据转换为可以使用face_encodings编码的图像。

我试过

代码语言:javascript
运行
复制
decodedData = base64.b64decode(data)
encodeFace = np.frombuffer(decodedData, np.uint8)

并将encodedFace传递给

代码语言:javascript
运行
复制
face_recognition.face_encodings(decodedBase64Data)

我得到了错误Unsupported image type, must be 8bit gray or RGB image.

如何将base64转换成与face_encodings兼容的图像?

编辑:

附供参考的代码

代码语言:javascript
运行
复制
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)
EN

Stack Overflow用户

发布于 2021-03-14 12:24:19

请共享代码以更好地理解问题,或者您可以使用下面的代码作为参考

代码语言:javascript
运行
复制
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```
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66624383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档