Support
Quality
Security
License
Reuse
kandi has reviewed facenet and discovered the below as its top functions. This is intended to give you an instant insight into facenet implemented functionality, and help decide if they suit your requirements.
Face recognition using Tensorflow
Sorting a tensor list in ascending order
namestodistance = [('Alice', .1), ('Bob', .3), ('Carrie', .2)]
names_top = sorted(namestodistance, key=lambda x: x[1])
print(names_top[:2])
namestodistance = list(map(lambda x: (x[0], x[1].item()), namestodistance)
names_top = sorted(namestodistance, key=lambda x: x[1])
print(names_top[:2])
-----------------------
namestodistance = [('Alice', .1), ('Bob', .3), ('Carrie', .2)]
names_top = sorted(namestodistance, key=lambda x: x[1])
print(names_top[:2])
namestodistance = list(map(lambda x: (x[0], x[1].item()), namestodistance)
names_top = sorted(namestodistance, key=lambda x: x[1])
print(names_top[:2])
Can't open "face_detector\deploy.prototxt" in function 'cv::dnn::ReadProtoFromTextFile'
import os
prototxtPath = os.path.join(os.getcwd(), 'face_detector', 'deploy.prototxt')
weightsPath = os.path.join(os.getcwd(), 'face_detector', 'res10_300x300_ssd_iter_140000.caffemodel')
faceNet = cv2.dnn.readNet(prototxtPath, weightsPath)
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
How to set up a environment for python application development in Docker Desktop
ADD $PWD/requirements.txt /requirements.txt
RUN pip3 install -r /requirements.txt
ADD $PWD/ / #This will add all your workdirectory files in container
Can DeepFace verify() accept an image array or PIL Image object?
results = DeepFace.verify(np.array(PILIMAGE), ...)
-----------------------
picture= "extracted_face_picture/single_face_picture.jpg"
picture= Image.open(picture)
.
.
df.verify(picture, np.array(frame), "Facenet")
df.verify(np.array(picture),np.array(frame), "Facenet")
-----------------------
picture= "extracted_face_picture/single_face_picture.jpg"
picture= Image.open(picture)
.
.
df.verify(picture, np.array(frame), "Facenet")
df.verify(np.array(picture),np.array(frame), "Facenet")
How to store FaceNet data efficiently?
#!pip install deepface
from deepface import DeepFace
img_list = ["img1.jpg", "img2.jpg", ...]
model = DeepFace.build_model("Facenet")
for img_path in img_list:
img_embedding = DeepFace.represent(img_path, model = model)
#store img_embedding into the redis here
Show Mask Object Detection On Screen instead of Camera
img=cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
Cannot set headers after they are sent to client
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
success = false;
message = 'Cannot detect the person.Please add name in the textbox provided below to save the person.';
res.status(200).json({message: message,success:success});
res.end();
var facialScript = new PythonShell('face_detect.py',options)
facialScript.on('message',(response)=>{
console.log(response);
res.status(200).send(response);
//res.end();
})
-----------------------
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
success = false;
message = 'Cannot detect the person.Please add name in the textbox provided below to save the person.';
res.status(200).json({message: message,success:success});
res.end();
var facialScript = new PythonShell('face_detect.py',options)
facialScript.on('message',(response)=>{
console.log(response);
res.status(200).send(response);
//res.end();
})
-----------------------
let result="";
facialScript.on('message',(response)=>{
result=result+response;//assuming that response is string you can change this as per your convenience .
})
facialScript.on("end",()=>{
res.send(result);
})
How do I make Input type and weight type same?
pred = model(img.to(device))[0]
>>> x = torch.ones(1)
>>> x.to(device)
tensor([1], device='cuda:0')
>>> x.is_cuda
False
-----------------------
pred = model(img.to(device))[0]
>>> x = torch.ones(1)
>>> x.to(device)
tensor([1], device='cuda:0')
>>> x.is_cuda
False
How to execute docker health check only one time?
HEALTHCHECK CMD sh -c "if [ ! -f /tmp/health.txt ]; then touch /tmp/health.txt && python api/initRequest.py || exit 0 ; else echo \"initRequest.py already executed\"; fi"
Unexpected error when loading the model: problem in predictor - ModuleNotFoundError: No module named 'torchvision'
REQUIRED_PACKAGES = ['torchvision==0.5.0', 'torch @ https://download.pytorch.org/whl/cpu/torch-1.4.0%2Bcpu-cp37-cp37m-linux_x86_64.whl', 'opencv-python', 'facenet-pytorch']
QUESTION
Sorting a tensor list in ascending order
Asked 2021-Dec-05 at 21:29I am working on a facial comparison app that will give me the closest n number of faces to my target face.
I have done this with dlib/face_recognition as it uses numpy arrays, however i am now trying to do the same thing with facenet/pytorch and running into an issue because it uses tensors.
I have created a database of embeddings and I am giving the function one picture to compare to them. What i would like is for it to sort the list from lowest distances to highest, and give me the lowest 5 results or so.
here is the code I am working on that is doing the comparison. at this point i am feeding it a photo and asking it to compare against the embedding database.
def face_match(img_path, data_path): # img_path= location of photo, data_path= location of data.pt
# getting embedding matrix of the given img
img_path = (os.getcwd()+'/1.jpg')
img = Image.open(img_path)
face = mtcnn(img) # returns cropped face and probability
emb = resnet(face.unsqueeze(0)).detach() # detech is to make required gradient false
saved_data = torch.load('data.pt') # loading data.pt file
embedding_list = saved_data[0] # getting embedding data
name_list = saved_data[1] # getting list of names
dist_list = [] # list of matched distances, minimum distance is used to identify the person
for idx, emb_db in enumerate(embedding_list):
dist = torch.dist(emb, emb_db)
dist_list.append(dist)
namestodistance = list(zip(name_list,dist_list))
print(namestodistance)
face_match('1.jpg', 'data.pt')
This results in giving me all the names and their distance from the target photo in alphabetical order of the names, in the form of (Adam Smith, tensor(1.2123432))
, Brian Smith, tensor(0.6545464)
etc. If the 'tensor' wasn't part of every entry I think it would be no problem to sort it. I don't quite understand why its being appended to the entries. I can cut this down to the best 5 by adding [0:5]
at the end of dist_list, but I can't figure out how to sort the list, I think the problem is the word tensor being in every entry.
I have tried
for idx, emb_db in enumerate(embedding_list): dist = torch.dist(emb, emb_db) sorteddist = torch.sort(dist)
but for whatever reason this only returns one distance value, and it isn't the smallest one.
idx_min = dist_list.index(min(dist_list))
, this works fine in giving me the lowest value and then matching it to a name using namelist[idx_min]
, therefore giving the best match, but I would like the best 5 matches in order as opposed to just the best match.
Anyone able to solve this ?
ANSWER
Answered 2021-Dec-05 at 16:43Unfortunately I cannot test your code, but to me it seems like you are operation on a python list of tuples. You can sort that by using a key:
namestodistance = [('Alice', .1), ('Bob', .3), ('Carrie', .2)]
names_top = sorted(namestodistance, key=lambda x: x[1])
print(names_top[:2])
Of course you have to modify the anonymous function in key
to return a sortable value instead of e.g. a torch.tensor
.
This can be done by using key = lambda x: x[1].item()
.
Edit: To answer the question that crept up in the comments, we can refactor our code a little. Namely
namestodistance = list(map(lambda x: (x[0], x[1].item()), namestodistance)
names_top = sorted(namestodistance, key=lambda x: x[1])
print(names_top[:2])
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Explore Related Topics
Save this library and start creating your kit