helper.py(quoc14)

 avatar
unknown
python
5 months ago
1.7 kB
2
Indexable
import numpy as np
import glob
import os
from sklearn.preprocessing import normalize

def list_all_folders(folder_path):
    subdirs = []
    for x in os.walk(folder_path):
        subdirs.append(x[0])
    return subdirs

def list_all_files(folder_path, ext = '.jpg'):
    all_file_paths = []
    for folder in list_all_folders(folder_path):
        file_paths = glob.glob(folder + "/*" + ext)
        all_file_paths += file_paths
    
    return all_file_paths

def run_pathlib_iterdir(directory):
    sub_dirs = [f.name for f in directory.iterdir() if f.is_dir()]
    return sub_dirs

def write_to_file(inp_list, file_path):
    f = open(file_path, "w")
    for item in inp_list:
        f.write("%s\n" % item)
    f.close()

def read_from_file(file_path, split = None):
    out_list = []
    f = open(file_path, "r")
    for line in f:
        # remove linebreak from a current name
        # linebreak is the last character of each line
        x = line[:-1]
        if (split != None):
            x = x.split(split)
        # add current item to the list
        out_list.append(x)
    f.close()
    return out_list

def load_embedding__(file_path):
    f= open(file_path, "r")
    Lines = f.readlines()
    embeddings = [float(elt.strip()) for elt in Lines]
    f.close()
    embeddings = normalize(np.array([embeddings]).astype("float32"))[0]
    return np.asarray(embeddings)

def load_embedding(file_path):
    f= open(file_path, "r")
    line = f.read()
    embeddings = [float(elt.strip()) for elt in line.split(',')]
    f.close()
    embeddings = normalize(np.array([embeddings]).astype("float32"))[0]
    return np.asarray(embeddings)
Editor is loading...
Leave a Comment