<jupyter_start><jupyter_text>Initialize Constants<jupyter_code>train_val_video_path = "..\\SURA\\dataset\\raw\\MSR-VTT_2016\\TrainValVideo\\"
test_video_path = "..\\SURA\\dataset\\raw\\MSR-VTT_2016\\TestVideo/"
base_save_path = ".\\"
num_frames_per_video = 100
vid_filter = lambda x: x.endswith('mp4')
train_filter =  lambda x: 0 <= int(x[5:][:-4]) <= 6512
val_filter =  lambda x: 6513 <= int(x[5:][:-4]) <= 7009
test_filter =  lambda x: 7010 <= int(x[5:][:-4]) <= 9999
videos = os.listdir(train_val_video_path) +  os.listdir(test_video_path)
videos = list(filter(vid_filter,videos))<jupyter_output><empty_output><jupyter_text>Inception Model Declare and restore Weights``<jupyter_code>class Inception_Model(object):
    def __init__(self,learning_rate=1e-3,initializer_scale=0.5):     
        self.learning_rate = learning_rate
        self.initializer = tf.random_uniform_initializer(
                                            minval=-initializer_scale,
                                            maxval=initializer_scale)
        self.images = None
        # Global step Tensor.
        self.global_step = None
    def build_inputs_outputs(self):
        self.image_feed = tf.placeholder(dtype=tf.float32,shape=[None,299,299,3],name="image_feed")        
    def build_image_embedding(self):
        self.inception_output = inception_base.get_base_model(self.image_feed)
        self.fine_tune_input = tf.placeholder_with_default(self.inception_output,self.inception_output.get_shape(),name="fine_tune_input")
        self.inception_variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,scope="InceptionV4")
#     def setup_global_step(self):
#         """Sets up the global step Tensor."""
#         global_step = tf.Variable(
#             initial_value=0,
#             name="global_step",
#             trainable=False,
#             collections=[tf.GraphKeys.GLOBAL_STEP, tf.GraphKeys.GLOBAL_VARIABLES])
#         self.global_step = global_step
        
    def build(self):
        self.build_inputs_outputs()
        self.build_image_embedding()
model = Inception_Model()
model.build()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver(var_list=model.inception_variables)
saver.restore(sess,"./inception_v4.ckpt")
#out = sess.run(model.inception_output,feed_dict={model.image_feed:IMAGE_BATCH_ARRAY)})<jupyter_output><empty_output><jupyter_text>Main Function to read video, trim frames, and preprocess images<jupyter_code>def read_video(video_path):
    #p = os.path.join(train_val_video_path+x)
    cap  = cv2.VideoCapture( video_path )
    frame_count = 0
    frame_list = []
    while True:
        ret, frame = cap.read()
        if ret is False:
            break
        frame_list.append(frame)
        frame_count += 1
    frame_list = np.array(frame_list)
    if frame_count > num_frames_per_video:
            frame_indices = np.linspace(0, frame_count-1, num=num_frames_per_video, endpoint=True).astype(int)
            frame_list = frame_list[frame_indices]
    frame_list = [cv2.cvtColor(x, cv2.COLOR_BGR2RGB) for x in frame_list]
    frame_list_resized = [scipy.misc.imresize(frame,[299,299,3]) for frame in frame_list]
    frame_list_processed = [((2*(frame.astype(np.float32) / 255 ))-1) for frame in frame_list_resized]
    frame_list = np.array(frame_list_processed)
    return frame_list
import numpy as np
np.linspace(0,3,5,endpoint=False)
np.ones([5],dtype=np.uint8)
z=  [1]*5+[0]*3
np.asarray(z,dtype=np.uint8)
z[0] = 2
import cv2
# cap  = cv2.VideoCapture("/home/ozym4nd145/Coding/Notebook/SURA/dataset/raw/MSR-VTT_2016/TrainValVideo/video1648.mp4")
cap  = cv2.VideoCapture("../dataset/raw/MSR-VTT_2016/TrainValVideo/video16.mp4")
frame_count = 0
frame_list = []
while True:
    ret, frame = cap.read()
    if ret is False:
        break
    frame_list.append(frame)
    frame_count += 1
cv2.VideoCapture()
frame_list<jupyter_output><empty_output><jupyter_text>Train, Val and Test<jupyter_code>count = 1
for vid in  filter(train_filter,videos):
    read_path = os.path.join(train_val_video_path,vid)
    save_path = os.path.join(base_save_path,"train",vid+".npy")
    if(not os.path.exists(save_path)):
        preprocessed_frames = read_video(read_path)
        completed_frames = sess.run(model.inception_output,feed_dict={model.image_feed:preprocessed_frames})
        np.save(save_path,completed_frames)
        if count%100 == 0:
            print("Processed 100 videos, currently at "+vid+"\n")
        count+=1
count = 1
for vid in  filter(test_filter,videos):
    read_path = os.path.join(test_video_path,vid)
    save_path = os.path.join(base_save_path,"test",vid+".npy")
    if(not os.path.exists(save_path)):
        preprocessed_frames = read_video(read_path)
        completed_frames = sess.run(model.inception_output,feed_dict={model.image_feed:preprocessed_frames})
        np.save(save_path,completed_frames)
        if count%100 == 0:
            print("Processed 100 videos, currently at "+vid+"\n")
        count+=1
count = 1
for vid in  filter(val_filter,videos):
    read_path = os.path.join(train_val_video_path,vid)
    save_path = os.path.join(base_save_path,"val",vid+".npy")
    if(not os.path.exists(save_path)):
        preprocessed_frames = read_video(read_path)
        completed_frames = sess.run(model.inception_output,feed_dict={model.image_feed:preprocessed_frames})
        np.save(save_path,completed_frames)
        if count%100 == 0:
            print("Processed 100 videos, currently at "+vid+"\n")
        count+=1
preprocessed_frames = read_video(read_path)
preprocessed_frames[8][77]<jupyter_output><empty_output><jupyter_text>Cv2 gives height, width, channel<jupyter_code>cv2.imshow('image',frame_list[0])
cv2.waitKey(0)
x.shape
x = frame_list[0]
d = cv2.cvtColor(x, cv2.COLOR_BGR2RGB)
from matplotlib import pyplot as plt
plt.imshow(preprocessed_frames[7])
plt.show()
from matplotlib import pyplot as plt
plt.imshow(d)
plt.show()
frame_list = np.array(frame_list)
if frame_count > num_frames_per_video:
            frame_indices = np.linspace(0, frame_count-1, num=num_frames_per_video, endpoint=True).astype(int)
            frame_list = frame_list[frame_indices]
frame_list.shape
def 
t = scipy.ndimage.imread("12447.jpg")
t.shape
t = scipy.misc.imresize(d,[300,300,3])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12521739130434784, 'fraction_numerical': 0.026719367588932806, 'mean_word_length': 3.9537979639780736, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '534260', 'license': 'Unlicense', 'n_tokens_mistral': 2339, 'n_tokens_neox': 2227, 'n_words': 335, 'path': 'code/Process Videos.ipynb', 'repo_name': 'ozym4nd145/SURA', 'size': '6325'}