Created
March 27, 2021 11:21
-
-
Save Tandon-A/42b5c21c66bb7b13edbc6a5733e99286 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import cv2 | |
import numpy as np | |
import os | |
import pandas as pd | |
def parse_args(): | |
''' Argument Parser. ''' | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--data_dir', type=str, required=True, \ | |
help='Data directory path containing folders for persons' | |
', where each person folder in turn consists of emotion folders.') | |
args = parser.parse_args() | |
return args | |
if __name__ == '__main__': | |
args = parse_args() | |
if not os.path.exists(args.data_dir): | |
raise ValueError('Please pass a valid data directory.') | |
dirs = os.listdir(args.data_dir) | |
# iterate over all persons | |
for person in dirs: | |
person_dir = os.path.join(args.data_dir, person) | |
# skip the file if it is not a directory | |
if not os.path.isdir(person_dir): | |
continue | |
person_csv = os.path.join(args.data_dir, '%s.csv' %(person)) | |
# open csv file for person | |
with open(person_csv, 'w') as f: | |
f.write('emotion,pixels,usage\n') | |
# iterate over emotion folders | |
for emotion in os.listdir(person_dir): | |
emotion_label = int(emotion) | |
emotion_dir = os.path.join(person_dir, emotion) | |
# iterate over images in the emotion folder | |
for image_name in os.listdir(emotion_dir): | |
image = cv2.imread(os.path.join(emotion_dir, image_name), cv2.IMREAD_GRAYSCALE) | |
image = image.reshape(-1) | |
image_str = [str(pix) for pix in image] # convert image pixels to str | |
image_str = " ".join(image_str) | |
f.write('%d,%s,%s' %(emotion_label, image_str, 'training')) | |
f.write('\n') | |
print ('completed writing csv for %s' %(person)) | |
print ('completed process') | |
# Reading the last csv file | |
pdata = pd.read_csv(person_csv) | |
print (pdata.head()) | |
# converting the pixels from string to np array | |
pdata['pixels'] = pdata['pixels'].apply(lambda image_px : np.fromstring(image_px, sep = ' ')) | |
images = pdata.iloc[:, 1].values | |
images = np.vstack(images).reshape((-1, 48, 48, 1)).astype('float32') # images present in the csv file | |
print ('images', images.shape) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment