def Read_Input_Images(inputDir, listOfIgnoredSamples, dB, resizedFlag, table, workplace, spatial_size, channel, objective_flag): r = w = spatial_size SubperdB = [] # cross-checking parameter subperdb_id = [] for sub in sorted([infile for infile in os.listdir(inputDir)]): VidperSub = [] vid_id = np.empty([0]) for vid in sorted([inrfile for inrfile in os.listdir(inputDir + sub)]): path = inputDir + sub + '/' + vid + '/' # image loading path if path in listOfIgnoredSamples: continue imgList = readinput(path) numFrame = len(imgList) if resizedFlag == 1: col = w row = r else: img = cv2.imread(imgList[0]) [row, col, _l] = img.shape ## read the label for each input video collectinglabel(table, sub[3:], vid, workplace + 'Classification/', dB, objective_flag) for var in range(numFrame): img = cv2.imread(imgList[var]) [_, _, dim] = img.shape if channel == 1: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if resizedFlag == 1: img = cv2.resize(img, (col, row)) if var == 0: FrameperVid = img.flatten() else: FrameperVid = np.vstack((FrameperVid, img.flatten())) vid_id = np.append(vid_id, imgList[var]) # <--cross-check VidperSub.append(FrameperVid) subperdb_id.append(vid_id) # <--cross-check SubperdB.append(VidperSub) return SubperdB
def augmentation_casme(db_images, outputDir, numSamples, table, resizedFlag, r, w): for emotion in ['positive', 'negative', 'surprise', 'others']: table_emotion = pd.DataFrame(data=table[0:,0:],columns=['sub','id','emotion']) table_emotion = table_emotion[table_emotion['emotion']==emotion] for i in range(numSamples): print(emotion+"_"+str(i)) # first we ensure that every original video is processed, then we start sampling randomly until we have enough if i <= (table_emotion.shape[0]-1): random_pick = table_emotion.iloc[[i]] # not so random print("processing original video #" + str(i) + " emotion=" + emotion) else: random_pick = table_emotion.sample(n=1) # very random print("processing augmented video #" + str(i) + "emotion" + emotion) path = db_images+"sub"+str(random_pick['sub'].iloc[0])+"/"+str(random_pick['id'].iloc[0])+"/" imgList = readinput(path) numFrame = len(imgList) if resizedFlag == 1: col = w row = r else: img = cv2.imread(imgList[0]) [row,col,_l] = img.shape cropBool = 1#randint(0,1) cropStyle = randint(1,8) flipBool = randint(0,1) rotBool = 0#randint(0,1) rotAngle = 0#randint(-8,8) for var in range(numFrame): img = cv2.imread(imgList[var]) [_,_,dim] = img.shape if resizedFlag == 1: img = cv2.resize(img, (col,row)) if i > (table_emotion.shape[0]-1): img = augment_image(img,cropBool,cropStyle,flipBool,rotBool, rotAngle) writeFolder = outputDir+"sub"+str(random_pick['sub'].iloc[0])+"/"+str(random_pick['id'].iloc[0])+"."+str(i)+"/" outputPath = writeFolder + imgList[var].split('/')[-1] if not os.path.exists(writeFolder): os.makedirs(writeFolder) cv2.imwrite(outputPath, img)
def Read_Input_Images_Apex(inputDir, listOfIgnoredSamples, dB, resizedFlag, table, workplace, spatial_size, channel, objective_flag): r = w = spatial_size SubperdB = [] # cross-checking parameter subperdb_id = [] for sub in sorted([infile for infile in os.listdir(inputDir)]): VidperSub = [] for vid in sorted([inrfile for inrfile in os.listdir(inputDir + sub)]): path = inputDir + sub + '/' + vid + '/' # image loading path if path in listOfIgnoredSamples: print(path) continue imgList = readinput(path) collectinglabel(table, sub[3:], vid.split(".")[0], workplace + 'Classification/', dB, objective_flag) table_df = pd.DataFrame(table) apexInd = table_df[table_df[0] == sub[-2:]] apexInd = int(apexInd[apexInd[1] == vid][3]) imgON = cv2.imread(imgList[0]) imgApex = cv2.imread(imgList[apexInd]) if channel == 1: imgON = cv2.cvtColor(imgON, cv2.COLOR_BGR2GRAY) imgApex = cv2.cvtColor(imgApex, cv2.COLOR_BGR2GRAY) if resizedFlag == 1: imgON = cv2.resize(imgON, (spatial_size, spatial_size)) imgApex = cv2.resize(imgApex, (spatial_size, spatial_size)) FrameperVid = imgON.flatten() FrameperVid = np.vstack((FrameperVid, imgApex.flatten())) VidperSub.append(FrameperVid) SubperdB.append(VidperSub) return SubperdB
def augmenting2(): new_image = 0 for sub in sorted([infile for infile in os.listdir(img_path)]): for vid in sorted([inrfile for inrfile in os.listdir(img_path + sub)]): augmentations = np.empty([0]) path = img_path + sub + '/' + vid + '/' folder_path = img_path + sub + '/' if path in listOfIgnoredSamples: continue # print(path) imgList = readinput(path, dB) numFrame = len(imgList) # print(numFrame) # print("imglist: %i" % imgList) for var in range(numFrame): augmentations = pixel_removal(2, imgList[var]) # print("augmentations: %i" % len(augmentations)) # print("var: %i" % var) counter = 0 while counter < len(augmentations): # # split augmented data into different folders folder_name = vid + "_" + str(counter) folder_name = folder_path + folder_name + "/" # print(folder_name) if not os.path.exists(folder_name): os.makedirs(folder_name) if var < 9: filename = folder_name + "00" + str(var + 1) + ".jpg" else: filename = folder_name + "0" + str(var + 1) + ".jpg" os.chdir(folder_name) cv2.imwrite(filename, augmentations[counter]) os.chdir(path) counter += 1 print("Written to: " + filename) new_image += 1 print("Total new images: " + str(new_image))
def cube_maker(): counter = 1 for sub in sorted([infile for infile in os.listdir(img_path)]): for vid in sorted([inrfile for inrfile in os.listdir(img_path + sub)]): path = img_path + sub + '/' + vid + '/' if path in listOfIgnoredSamples: continue print(path) imgList = readinput(path, dB) numFrame = len(imgList) for var in range(numFrame): img = cv2.imread(imgList[var]) img = cv2.resize(img, (256, 256)) cv2.imwrite(imgList[var], img) counter += 1 print(str(counter) + "/5659")
else: print("NOT in the selection.") ######### Reading in the input images ######## SubperdB = [] for sub in sorted([infile for infile in os.listdir(inputDir)]): VidperSub = [] for vid in sorted([inrfile for inrfile in os.listdir(inputDir + sub)]): path = inputDir + sub + '/' + vid + '/' if path in listOfIgnoredSamples: continue imgList = readinput(path, dB) numFrame = len(imgList) if resizedFlag == 1: col = w row = r else: img = cv2.imread(imgList[0]) [row, col, _l] = img.shape ## read the label for each input video collectinglabel(table, sub[3:], vid, workplace + 'Classification/', dB) for var in range(numFrame):
def Read_Input_Images_SAMM_CASME(inputDir, filteredSamples, ignoredSamples, dB, resizedFlag, table, workplace, spatial_size, channel, objective_flag): # r=224; w=224 r = w = spatial_size SubperdB = [] # cross-checking parameter subperdb_id = [] for sub in sorted([infile for infile in os.listdir(inputDir)]): VidperSub = [] vid_id = np.empty([0]) for vid in sorted([inrfile for inrfile in os.listdir(inputDir + sub)]): path = inputDir + sub + '/' + vid + '/' # print(len(filteredSamples)) # print(filteredSamples) # print("bohaha") # filtered samples are samples needed if path not in filteredSamples: # print(path) continue # print(dB) # print(path) imgList = readinput(path) numFrame = len(imgList) # print(numFrame) if resizedFlag == 1: col = w row = r else: img = cv2.imread(imgList[0]) [row, col, _l] = img.shape ## ##read the label for each input video # print(sub[3:]) collectinglabel(table, sub, vid, workplace + 'Classification/', dB, objective_flag) for var in range(numFrame): img = cv2.imread(imgList[var]) [_, _, dim] = img.shape if channel == 1: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if resizedFlag == 1: #in resize function, [col,row] img = cv2.resize(img, (col, row)) if var == 0: FrameperVid = img.flatten() else: FrameperVid = np.vstack((FrameperVid, img.flatten())) vid_id = np.append(vid_id, imgList[var]) # <--cross-check VidperSub.append(FrameperVid) subperdb_id.append(vid_id) # <--cross-check # print(subperdb_id) # if len(VidperSub) > 0: # print(len(VidperSub)) SubperdB.append(VidperSub) # return SubperdB, vid_id, subperdb_id return SubperdB
def augmenting(): new_image = 0 for sub in sorted([infile for infile in os.listdir(img_path)]): for vid in sorted([inrfile for inrfile in os.listdir(img_path + sub)]): augmentations = np.empty([0]) path = img_path + sub + '/' + vid + '/' folder_path = img_path + sub + '/' if path in listOfIgnoredSamples: continue # print(path) imgList = readinput(path, dB) numFrame = len(imgList) # print(numFrame) # hardcoded number_of_ops = 13 folder_num = 0 for var in range(numFrame): img = cv2.imread(imgList[var]) rotated_img = rotation(5, img) rotated_img_2 = rotation(-5, img) rotated_img_3 = rotation(10, img) rotated_img_4 = rotation(-10, img) scaled_img0_9 = scaling(0.9, img) scaled_img1_0 = scaling(1.0, img) scaled_img1_1 = scaling(1.1, img) translation_1 = translation(-2, -2, img) translation_2 = translation(-2, 2, img) translation_3 = translation(2, -2, img) translation_4 = translation(2, 2, img) horizontal, vertical = flip(img) augmentations = np.append(augmentations, [ rotated_img, rotated_img_2, rotated_img_3, rotated_img_4, scaled_img0_9, scaled_img1_0, scaled_img1_1, translation_1, translation_2, translation_3, translation_4, horizontal, vertical ]) counter = 0 seq_var = -9 count_seq = 0 filename_count = 1 while counter < len(augmentations): # split augmented data into different folders folder_name = vid + "_" + str(folder_num) folder_name = folder_path + folder_name + "/" # print(folder_name) if not os.path.exists(folder_name): os.makedirs(folder_name) # filename = path + "0" + str( numFrame + counter + 1 ) + ".jpg" # filename = folder_name + "0" + str( numFrame + counter + 1 ) + ".jpg" if filename_count < 10: filename = folder_name + "00" + str( filename_count) + ".jpg" else: filename = folder_name + "0" + str(filename_count) + ".jpg" # print(str(seq_var)) print(filename) os.chdir(folder_name) cv2.imwrite(filename, augmentations[seq_var + 13]) os.chdir(path) counter += 1 new_image += 1 seq_var += 13 filename_count += 1 if counter % 10 == 0 and counter > 0: count_seq += 1 seq_var = -13 + count_seq folder_num += 1 filename_count = 1 # print("Saved to " + path) # seq_var += 1 print("Total new images: " + str(new_image))