I am working on a Facial Recognition system using InsightFace. I want to store my labels and faces into a numpy array using np.array() and applying some filtering on them to ensure that each label has embeddings.
This is my filtering function
def filter_empty_embs(img_set: List, img_labels: List[str]):
# filtering where insightface could not generate an embedding
good_idx = [i for i,x in enumerate(img_set) if x]
if len(good_idx) == len(img_set):
clean_embs = [e[0].embedding for e in img_set]
clean_labels = img_labels
else:
# filtering eval set and labels based on good idx
clean_labels = np.array(img_labels)[good_idx]
clean_set = np.array(img_set, dtype=object)[good_idx]
# generating embs for good idx
clean_embs = [e[0].embedding for e in clean_set]
return clean_embs, clean_labels
This is the function where I extract embeddings:
# sorting files
files = os.listdir(YALE_DIR)
files.sort()
eval_set = list()
eval_labels = list()
probe_set = list()
probe_labels = list()
IMAGES_PER_IDENTITY = 11
for i in tqdm(range(1, len(files), IMAGES_PER_IDENTITY), unit_divisor=True): # ignore the README.txt file at files[0]
# print(i)
probe, eval = create_probe_eval_set(files[i:i+IMAGES_PER_IDENTITY])
# store eval embs and labels
eval_set_t, eval_labels_t = generate_embs(eval)
eval_set.extend(eval_set_t)
eval_labels.extend(eval_labels_t)
# store probe embs and labels
probe_set_t, probe_labels_t = generate_embs(probe)
probe_set.extend(probe_set_t)
probe_labels.extend(probe_labels_t)
Lastly, here where I call the functions and everything should work:
evaluation_embs, evaluation_labels = filter_empty_embs(eval_set, eval_labels)
probe_embs, probe_labels = filter_empty_embs(probe_set, probe_labels)
However, I am facing the following error in the filter_empty_embs function
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_488/1310330242.py in <module>
----> 1 evaluation_embs, evaluation_labels = filter_empty_embs(eval_set, eval_labels)
2 probe_embs, probe_labels = filter_empty_embs(probe_set, probe_labels)
~\AppData\Local\Temp/ipykernel_488/117740786.py in filter_empty_embs(img_set, img_labels)
10 # filtering eval set and labels based on good idx
11 clean_labels = np.array(img_labels)[good_idx]
---> 12 clean_set = np.array(img_set, dtype=object)[good_idx]
13
14 # generating embs for good idx
ValueError: invalid __array_struct__
Apparently the problem is in the img_set variable that I am using. which is a list of the type object that will contain images but I don't know what exactly the problem is and how to fix it.
Numpy version: 1.21.2 and I cant go back with it due to other decencies.
Thanks in advance !!
np.array(img_set)img_setgood_idx, a scalar or list/array? You sayimg_setis a list of some sort of image objects. What are those objects? Looks like it's trying to make an object dtype array of those images, specifically for the purpose of applying thegood_idxindexing.good_idxis a list of indices.img_setare the images. So, basically I am retrieving the indices were the embeddings were generated and mapping them with the corresponding images.