V-Sher / Face-Search

9 stars 7 forks source link

Numpy 1.20.0 error invalid __array_struct__ #1

Open sachadee opened 2 years ago

sachadee commented 2 years ago

Thanks a lot for your work Dr. Varshita Sher!

To avoid the issue: Invaliid __array_struct__ , you can easely bypass the filtering function with a try and extracting only the embedding array in the embedding function:

to get only the embeddings array with insight face:

emb_res = app.get(rgb_arr)
res = emb_res[0].embedding

Then the function will be :

def generate_embs(img_fpaths: List[str]):
    embs_set = list()
    embs_label = list()
    for img_fpath in img_fpaths:  
        print('tratamento: ',img_fpath)                    
        # read grayscale img
        img = Image.open(os.path.join(YALE_DIR, img_fpath)) 
        img_arr = np.asarray(img)  

        # convert grayscale to rgb
        im = Image.fromarray((img_arr * 255).astype(np.uint8))
        rgb_arr = np.asarray(im.convert('RGB'))       

        # generate Insightface embedding
        emb_res = app.get(rgb_arr)
        try:
         res = emb_res[0].embedding
         # append emb to the eval set
         embs_set.append(res)          
         # append label to eval_label set
         embs_label.append(img_fpath.split("_")[0])
        except:
         print('no embedding found for this image')
    return embs_set, embs_label

this way no need for filtering the set is updated only if the embedding exist.

Then replace these 2 lines:

evaluation_embs, evaluation_labels = filter_empty_embs(eval_set, eval_labels)
probe_embs, probe_labels = filter_empty_embs(probe_set, probe_labels)

with

evaluation_embs, evaluation_labels = eval_set, eval_labels
probe_embs, probe_labels = probe_set, probe_labels

or rename the variables name

Check my answer on stackoverflow:

https://stackoverflow.com/questions/69618467/numpy-np-array-valueerror-invalid-array-struct/70909945#70909945

V-Sher commented 2 years ago

Thanks for the changes. Can you pls make a PR and I can merge them into main?

sachadee commented 2 years ago

Hi, yes you can! I tried to make a pull request but I don't realy now how it work.