def main():
    # initialize an empty list
    list_of_csv = []
    # arg: set on config(csv file)
    # put all args in the list
    for arg in sys.argv[1:]:
        list_of_csv.append(arg)

    #iterate over the list
    for csv_file in list_of_csv:
        #save the content of csv file in a df
        df = pd.read_csv(csv_file)
        #links = df['youtube_id']
        #file without .cvs extention
        folder_name = get_filename_without_extension(csv_file)
        # The folder where the downloaded videos will be saved
        video_downloaded_path = os.path.join(UNTRIMMED_VIDEOS, folder_name)
        # The folder where the downloaded videos will be trimmed and saved
        trimmed_video_path = os.path.join(TRIMMED_VIDEOS, folder_name)
        # make foloder according to given path if already not exist
        EnsurePath(video_downloaded_path)
        EnsurePath(trimmed_video_path)
        #iterate over the df
        index = 0
        for index, row in df.iterrows():
            try:
                #make the string of youtube link
                link = "https://www.youtube.com/watch?v=" + row['youtube_id']
                # object creation using YouTube which was imported in the beginning
                yt = YouTube(link)
                startTime = row['time_start']
                endTime = row['time_end']
            except Exception as e:
                print(str(e))  # to handle exception
            stream = yt.streams.filter(file_extension='mp4').first()
            noError = True
            try:
                # downloading the video and save as index(0.mp4,1.mp4)
                fname_index = str(index)
                downloaded_path = stream.download(
                    output_path=video_downloaded_path, filename=fname_index)
            except Exception as e:
                print(str(e))
                noError = False
            finally:
                if noError:
                    #If there is noError then only go for trimming the video
                    file_name = os.path.basename(downloaded_path)
                    target_path = os.path.join(trimmed_video_path, file_name)
                    command_name = "ffmpeg -i " + downloaded_path + " -ss  " + str(
                        startTime) + " -to " + str(
                            endTime) + " -c copy " + target_path
                    print(command_name)
                    os.system(command_name)
                    index = index + 1
def save_npy(list_of_folders):
    '''
    For each action folder in the list of folders save the numpy array files for further processing
    :param list_of_folders:
    :return:
    '''
    for folders in list_of_folders:
        action_base_name = os.path.basename(folders)
        parent_action_folder_path = os.path.join(EXTRACTED_FEATURES_FOLDER,
                                                 action_base_name)
        EnsurePath(parent_action_folder_path)
        onlyfolders = [
            f for f in listdir(folders) if not isfile(join(folders, f))
        ]
        model = VGG19Extractor()
        for numbered_folder in onlyfolders:
            images = get_frames_for_sample(source_folder=folders,
                                           numbered_folder=numbered_folder)
            path = os.path.join(parent_action_folder_path,
                                str(numbered_folder))
            if os.path.isfile(path + '.npy'):
                continue
            frames = RescaleList(images, 60)
            if frames is not None:
                sequence = []
                for image in frames:
                    features = model.extract(image)
                    sequence.append(features)
                # Save the sequence.
                np.save(path, sequence)
Ejemplo n.º 3
0
def saved_npy(list_of_folders):
    for folders in list_of_folders:
        action_base_name = os.path.basename(folders)
        parent_action_folder_path = os.path.join(EXTRACTED_FEATURES_FOLDER,
                                                 action_base_name)
        EnsurePath(parent_action_folder_path)
        onlyfolders = [
            f for f in listdir(folders) if not isfile(join(folders, f))
        ]
        model = InceptionV3Extractor()
        for numbered_folder in onlyfolders:
            images = get_frames_for_sample(source_folder=folders,
                                           numbered_folder=numbered_folder)
            path = os.path.join(parent_action_folder_path,
                                str(numbered_folder))
            if os.path.isfile(path + '.npy'):
                continue
            frames = RescaleList(images, 100)
            if frames is not None:
                sequence = []
                for image in frames:
                    features = model.extract(image)
                    sequence.append(features)
                # Save the sequence.
                np.save(path, sequence)
Ejemplo n.º 4
0
def main():
    #initialize an empty list
    list_of_folder_with_videos = []
    #arg: set on config(csv file)
    #put all args in the list
    for arg in sys.argv[1:]:
        list_of_folder_with_videos.append(arg)

    #Extract frames
    for folders in list_of_folder_with_videos:
        main_folder_name = os.path.basename(folders)
        frames_extracted_main_folder = os.path.join(EXTRACTED_FRAMES_FOLDER,
                                                    main_folder_name)
        EnsurePath(frames_extracted_main_folder)
        onlyfiles = [f for f in listdir(folders) if isfile(join(folders, f))]
        for file_name in onlyfiles:
            folder_base_name = os.path.splitext(file_name)[0]
            new_folder_path = os.path.join(frames_extracted_main_folder,
                                           folder_base_name)
            EnsurePath(new_folder_path)
            source_file_path = os.path.join(folders, file_name)
            vidcap = cv2.VideoCapture(source_file_path)

            # #Create the directory into for the file
            video_length = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT)) - 1
            print("Number of frames: ", video_length)
            count = 0
            print("Converting video..\n")
            # Start converting the video
            while vidcap.isOpened():
                # Extract the frame
                ret, frame = vidcap.read()
                # Write the results back to output location.
                cv2.imwrite(new_folder_path + "/%#05d.jpg" % (count + 1),
                            frame)
                count = count + 1
                # If there are no more frames left
                if (count > (video_length - 1)):
                    # Log the time again

                    # Release the feed
                    vidcap.release()
                    # Print stats
                    print("Done extracting frames.\n%d frames extracted" %
                          count)
                    #print("It took %d seconds forconversion." % (time_end - time_start))
                    break
def main():
    #load the data and its classes
    list_of_folder_with_extracted_feature = []
    for arg in sys.argv[1:]:
        list_of_folder_with_extracted_feature.append(arg)
    X, y = [], []
    index = 0
    for folders in list_of_folder_with_extracted_feature:
        onlyfiles = [
            f for f in listdir(folders) if join(folders, f).endswith(".npy")
        ]
        for files in onlyfiles:
            path = os.path.join(folders, files)
            img_data = np.load(path)
            X.append(img_data)
            output_list = []
            for i in range(len(list_of_folder_with_extracted_feature)):
                if (i == index):
                    output_list.append(1)
                else:
                    output_list.append(0)
            y.append(output_list)

        index = index + 1

    X = np.array(X)
    y = np.array(y)
    print(X.shape)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
    input_shape = (60, 4096)
    model = Sequential()

    model = Sequential()
    model.add(
        LSTM(
            1024,  # kernel_regularizer=l1(0.01), recurrent_regularizer=l1(0.01), bias_regularizer=l1(0.01),
            return_sequences=False,
            input_shape=input_shape,
            dropout=0.5))
    model.add(BatchNormalization())
    # model.add(Dense(512, activation='tanh'))
    # model.add(Dropout(0.5))
    model.add(Dense(256, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(2, activation='softmax'))
    optimizer = Adam(lr=1e-5, decay=1e-6)
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=['accuracy'])
    history = model.fit(X_train,
                        y_train,
                        batch_size=100,
                        validation_data=(X_test, y_test),
                        verbose=1,
                        epochs=150)

    #Now its time to save the model
    RESULT_FOLDER_TS = os.path.join(
        RESULT_FOLDER,
        datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
    EnsurePath(RESULT_FOLDER_TS)

    model_folder_path = os.path.join(RESULT_FOLDER_TS, 'model_dir')
    EnsurePath(model_folder_path)
    model_json = model.to_json()

    json_path = os.path.join(model_folder_path, "model_num.json")
    with open(json_path, "w") as json_file:
        json_file.write(model_json)

    weight_path = os.path.join(model_folder_path, "model_weight.h5")
    model.save_weights(weight_path)
    entire_model_path = os.path.join(model_folder_path, "entire_model.h5")
    model.save(entire_model_path)

    history_file_path = os.path.join(RESULT_FOLDER_TS, 'value_history.json')
    with open(history_file_path, 'w') as f:
        json.dump(str(history.history), f)

    [test_loss, test_acc] = model.evaluate(X_test, y_test)
    print("Evaluation result on Test Data : Loss = {}, accuracy = {}".format(
        test_loss, test_acc))
    y_pred = model.predict_classes(X_test)
    y_pred = to_categorical(y_pred)
    cm = confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))

    print("confusion_matrix")
    print(cm)
    print("Accuracy", cm.diagonal() / cm.sum(axis=1))
    print(
        "Precision",
        precision_score(y_test.argmax(axis=1),
                        y_pred.argmax(axis=1),
                        average=None))
    print(
        "Recall",
        recall_score(y_test.argmax(axis=1),
                     y_pred.argmax(axis=1),
                     average=None))
    print("f1_score",
          f1_score(y_test.argmax(axis=1), y_pred.argmax(axis=1), average=None))

    test_eval_dict = {}
    test_eval_dict['accuracy'] = (cm.diagonal() / cm.sum(axis=1)).tolist()
    test_eval_dict['precision'] = (precision_score(y_test.argmax(axis=1),
                                                   y_pred.argmax(axis=1),
                                                   average=None)).tolist()
    test_eval_dict['recall'] = (recall_score(y_test.argmax(axis=1),
                                             y_pred.argmax(axis=1),
                                             average=None)).tolist()
    test_eval_dict['f1_score'] = (f1_score(y_test.argmax(axis=1),
                                           y_pred.argmax(axis=1),
                                           average=None)).tolist()
    test_eval_dict[
        'description_of_model'] = 'Add some description about the model here'

    test_eval_file_path = os.path.join(RESULT_FOLDER_TS, 'test_eval.json')

    with open(test_eval_file_path, 'w') as fp:
        json.dump(test_eval_dict, fp)

    model_summary_file_path = os.path.join(RESULT_FOLDER_TS,
                                           'model_summary.txt')
    with open(model_summary_file_path, 'w') as f:
        with redirect_stdout(f):
            model.summary()
Ejemplo n.º 6
0
plt.ylabel('Loss', fontsize=16)
plt.title('Loss Curves', fontsize=16)

# Plot the Accuracy Curves
plt.figure(figsize=[8, 6])
plt.plot(history.history['accuracy'], 'r', linewidth=3.0)
plt.plot(history.history['val_accuracy'], 'b', linewidth=3.0)
plt.legend(['Training Accuracy', 'Validation Accuracy'], fontsize=18)
plt.xlabel('Epochs ', fontsize=16)
plt.ylabel('Accuracy', fontsize=16)
plt.title('Accuracy Curves', fontsize=16)

##SAVE EVERYTHING
RESULT_FOLDER_TS = os.path.join(RESULT_FOLDER,
                                datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
EnsurePath(RESULT_FOLDER_TS)

model_folder_path = os.path.join(RESULT_FOLDER_TS, 'model_dir')
EnsurePath(model_folder_path)
model_json = model.to_json()

json_path = os.path.join(model_folder_path, "model_num.json")
with open(json_path, "w") as json_file:
    json_file.write(model_json)

weight_path = os.path.join(model_folder_path, "model_weight.h5")
model.save_weights(weight_path)
entire_model_path = os.path.join(model_folder_path, "entire_model.h5")
model.save(entire_model_path)

history_file_path = os.path.join(RESULT_FOLDER_TS, 'value_history.json')
def clean_tmp_dir():
    rmtree(TESTING_DIR)
    EnsurePath(TESTING_DIR)
##Clean the directory
def clean_tmp_dir():
    rmtree(TESTING_DIR)
    EnsurePath(TESTING_DIR)


for sample_file in test_video_list:
    total_time = 0

    start_time = time.time()
    clean_tmp_dir()

    ##Extract images from videos
    IMG_DIR = os.path.join(TESTING_DIR, 'extracted_frames')
    EnsurePath(IMG_DIR)
    vidcap = cv2.VideoCapture(sample_file)

    # #Create the directory into for the file
    video_length = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT)) - 1
    #print("Number of frames: ", video_length)
    count = 0
    #print("Converting video..\n")
    # Start converting the video
    while vidcap.isOpened():
        # Extract the frame
        ret, frame = vidcap.read()
        # Write the results back to output location.
        cv2.imwrite(IMG_DIR + "/%#05d.jpg" % (count + 1), frame)
        count = count + 1
        # If there are no more frames left