5
我正在嘗試使用中文whispering算法進行人臉聚類。我已經使用dlib和python爲每個臉部提取特徵,並將其映射到128 D矢量,如Davisking在https://github.com/davisking/dlib/blob/master/examples/dnn_face_recognition_ex.cpp中所述。使用中文Whispers算法的人臉聚類
然後我按照給出的指示構建了一張圖。我實現了中國耳語算法並應用於此圖。任何人都可以告訴我我做了什麼錯誤嗎?任何人都可以使用中文耳語算法上傳python代碼進行人臉聚類嗎?這是我的中國低語代碼:
import networkx as nx
import random
from random import shuffle
import math
def chinese_whispers(nodes,edges,iterations):
G = nx.Graph()
G.add_nodes_from(nodes)
#print(G.node)
for n, v in enumerate(nodes):
G.node[n]['class'] = v
#print(n,v)
G.add_edges_from(edges)
#gn=G.nodes()
#for node in gn:
#print((node,G[node],G.node,G.node[node]))
#(0, {16: {'weight': 0.49846761956907698}, 14: {'weight': 0.55778036559581601}, 7: {'weight': 0.43902511314524784}}, {'class': 0})
for z in range(0, iterations):
gn = G.nodes()
# I randomize the nodes to give me an arbitrary start point
shuffle(gn)
for node in gn:
neighs = G[node]
classes = {}
# do an inventory of the given nodes neighbours and edge weights
for ne in neighs:
if isinstance(ne, int):
key=G.node[ne]['class']
if key in classes:
classes[key] += G[node][ne]['weight']
else:
classes[key] = G[node][ne]['weight']
# find the class with the highest edge weight sum
max = 0
maxclass = 0
for c in classes:
if classes[c] > max:
max = classes[c]
maxclass = c
# set the class of target node to the winning local class
G.node[node]['class'] = maxclass
n_clusters = []
for node in G.nodes():
n_clusters.append(G.node[node]['class'])
return(n_clusters)
這裏被面部特徵提取和在128 d矢量,並從這些構造圖的用於施加中國低語各面的編碼的代碼。
from sklearn import cluster
import cv2
import sys
import os
import dlib
import glob
from skimage import io
import numpy as np
from sklearn.cluster import KMeans
from sklearn.manifold import TSNE
from matplotlib import pyplot as plt
import chinese
from chinese import chinese_whispers
predictor_path = "/home/deeplearning/Desktop/face_recognition
examples/shape_predictor_68_face_landmarks.dat"
face_rec_model_path = "/home/deeplearning/Desktop/face_recognition
examples/dlib_face_recognition_resnet_model_v1.dat"
faces_folder_path = "/home/deeplearning/Desktop/face_recognition
examples/test11/"
# Load all the models we need: a detector to find the faces, a shape predictor
# to find face landmarks so we can precisely localize the face, and finally the
# face recognition model.
detector = dlib.get_frontal_face_detector()
#print (detector)
sp = dlib.shape_predictor(predictor_path)
facerec = dlib.face_recognition_model_v1(face_rec_model_path)
#win = dlib.image_window()
# Now process all the images
dict={}
for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
print("Processing file: {}".format(f))
img = io.imread(f)
dets = detector(img, 3)
for k, d in enumerate(dets):
shape = sp(img, d)
face_descriptor = facerec.compute_face_descriptor(img, shape)
a=np.array(face_descriptor)
dict[(f,d)] = (a,f)
answ=np.array(list(dict.values()))
tmp=answ.shape[0]
ans=np.zeros((tmp,128))
for i in range(tmp):
ans[i]=np.array(answ[i][0])
nodes=[]
for i in range(tmp):
nodes.append(i)
edges=[]
for i in range(tmp):
for j in range(i+1,tmp):
dist=np.sqrt(np.sum((ans[i]-ans[j])**2))
if dist < 0.6:
edges.append((i,j,{'weight': dist}))
iterations=10
cluster=chinese_whispers(nodes,edges,iterations)
我不明白我在做什麼錯誤。誰能幫我解決這個問題? 在此先感謝。
歡迎來到Stack Overflow。請花些時間閱讀[The Tour](http://stackoverflow.com/tour),並參閱[幫助中心](http://stackoverflow.com/help/asking)中的資料,瞭解您可以在這裏問。 –
「但我沒有得到很好的準確性」不是一個非常有用的問題描述。堆棧溢出最好用於解決特定問題和這類問題 – Rook