04_get_expression.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import numpy as np
  2. from numpy import save
  3. import os
  4. # AU = ["AU01", "AU02", "AU04", "AU05", "AU06", "AU07", "AU09","AU10","AU12","AU14","AU15","AU17","AU20","AU23",
  5. # "AU25","AU26","AU45","AU01_c","AU02_c","AU04_c","AU05_c","AU06_c","AU07_c","AU09_c","AU10_c","AU12_c","AU14_c","AU15_c","AU17_c",
  6. # "AU20_c","AU23_c","AU25_c","AU26_c","AU28_c","AU45_c"]
  7. reference_au = ["File", "AU01", "AU02", "AU04", "AU05", "AU06", "AU07", "AU09","AU10","AU12","AU14","AU15","AU17","AU20","AU23",
  8. "AU25","AU26", "AU45"]
  9. # finding reference values
  10. ref_au = {}
  11. ref = []
  12. # get all offset data
  13. data_all = np.load(f'/home/valapil/Project/ForkCausal_Adithya/processed_data.npy')
  14. # data = np.concatenate(data_all[:3], data_all[4:])
  15. data = np.delete(data_all, 2,1)
  16. activation = []
  17. unique = np.unique(data[1:, 0])
  18. for i in unique:
  19. same_rows = data[data[:, 0] == i]
  20. mean = np.mean(same_rows[:, 2:19].astype(np.float64), axis=0)
  21. std = np.std(same_rows[:, 2:19].astype(np.float64), axis=0)
  22. ref_au = mean + (0.5 * std)
  23. # ref_au.append(mean + (0.5 * std))
  24. ref = np.hstack((i, ref_au))
  25. reference_au = np.row_stack((reference_au, ref))
  26. # folder where each offset data is stored
  27. folder = '/home/valapil/Project/ForkCausal_Adithya/data'
  28. csv_files = [file for file in os.listdir(folder) if file.endswith('.npy')]
  29. for file in csv_files:
  30. EXP = ["Happiness upper", "Happiness lower", "Surprise upper", "Surprise lower", "Disgust lower", "Fear upper",
  31. "Fear lower", "Sadness upper", "Sadness lower", "Anger upper", "Anger lower"]
  32. # Get data
  33. file_path = os.path.join(folder,file)
  34. # data = np.genfromtxt(file_path, delimiter=',', skip_header=1)
  35. data = np.load(file_path)
  36. # find the particular person
  37. base_name, extension = os.path.splitext(file)
  38. new_name = base_name.split('.')[0]
  39. f1 = new_name[:-2]
  40. selected_columns = data
  41. index = np.where(reference_au[1:, 0] == f1)[0]
  42. # AU activation
  43. change = np.subtract(selected_columns, reference_au[index+1, 1:].astype(np.float16))
  44. activation = np.where(change >= 0, 1, 0)
  45. expression_labels = np.zeros((activation.shape[0], 11), dtype=int)
  46. # Define expressions based on specific AUs
  47. expression_labels[:, 0] = np.any(activation[:, [4]], axis=1) # AU06 is 4th au in order. [4] to get results with []
  48. expression_labels[:, 1] = np.all(activation[:, [8, 14]], axis=1) # AU12 and AU25
  49. expression_labels[:, 2] = np.all(activation[:, [0, 1, 3]], axis=1) # AU01, AU02, AU05
  50. expression_labels[:, 3] = np.any(activation[:, [15]], axis=1) # AU26
  51. expression_labels[:, 4] = np.all(activation[:, [6, 7, 14]], axis=1) # AU09, AU10, AU25
  52. expression_labels[:, 5] = np.all(activation[:, [0, 1, 2, 3]], axis=1) # AU01, AU02, AU04, AU05
  53. expression_labels[:, 6] = np.all(activation[:, [12, 14]], axis=1) # AU20, AU25
  54. expression_labels[:, 7] = np.all(activation[:, [0, 2]], axis=1) # AU01, AU04
  55. expression_labels[:, 8] = np.all(activation[:, [10, 11]], axis=1) # AU15, AU17
  56. expression_labels[:, 9] = np.all(activation[:, [2, 3, 5]], axis=1) # AU04, AU05, AU07
  57. expression_labels[:, 10] = np.all(activation[:, [11, 13]], axis=1) # AU17, AU23
  58. EXP = np.row_stack((EXP, expression_labels))
  59. # Save the result to a new NumPy file
  60. # Each file has expressions for each frame based on activation of AU in that frame
  61. np.save(f'/home/valapil/Project/ForkCausal_Adithya/test/{new_name}.npy', EXP)