How to write codes from three .py files into one

Question:

I have 3 .py files that splits a video into frames, apply saliency to those frames and then combine those frames into a video. I would like to have a single .py file that does all the above, instead of running three separate files. I have provided the codes below.

1. Spliting video.py

import cv2

capture = cv2.VideoCapture('vids/tm_test.mp4')
fps = capture.get(cv2.CAP_PROP_FPS)
print(fps)
frameNr = 0

while True:
    success, frame = capture.read()

    if success:
        cv2.imwrite(f'Output/frame_{frameNr}.jpg', frame)
    else:
        break

    frameNr = frameNr + 1

capture.release()

2. Saliency.py

import cv2
from glob import glob

source = r'Output/*.jpg'

path_to_save = 'images/'
saliency = cv2.saliency.StaticSaliencyFineGrained_create()
def main():
    i = 0
    for fn in glob(source):
        image = cv2.imread(fn)
        (ret, img_sal) = saliency.computeSaliency(image)
        saliencyMap = (img_sal * 255).astype('uint8')
        cv2.imwrite(path_to_save + 'test_salient_tm '+ str(i) + '.jpg', saliencyMap)
        i += 1

if __name__ == '__main__':
    main()
 
3. Combining after saliency.py

import cv2
import os

image_folder = 'images/'
video_name = 'vids/tm_test_salient.mp4'

images = [img for img in os.listdir(image_folder) if img.endswith('.jpg')]
frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape

fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(video_name, fourcc, 56.0, (width, height))

for image in images:
    video.write(cv2.imread(os.path.join(image_folder, image)))

cv2.destroyAllWindows()
video.release()

How can I combine these three into one file to perform those functions. Thank you

Asked By: user2381342

||

Answers:

Following is the beginning of the combination.

import cv2
from glob import glob
import os

# globals
capture = cv2.VideoCapture('vids/tm_test.mp4')
saliency = cv2.saliency.StaticSaliencyFineGrained_create()
fps = capture.get(cv2.CAP_PROP_FPS)
source = r'Output/*.jpg'
path_to_save = 'images/'
frameNr = 0
image_folder = 'images/'
video_name = 'vids/tm_test_salient.mp4'

# Functions

# Inputs

# Processing
while True:
    success, frame = capture.read()
    if success:
        cv2.imwrite(f'Output/frame_{frameNr}.jpg', frame)
    else:
        break
    frameNr = frameNr + 1
capture.release()
i = 0
for fn in glob(source):
    image = cv2.imread(fn)
    (ret, img_sal) = saliency.computeSaliency(image)
    saliencyMap = (img_sal * 255).astype('uint8')
    cv2.imwrite(path_to_save + 'test_salient_tm ' + str(i) + '.jpg',
                saliencyMap)
    i += 1

images = [img for img in os.listdir(image_folder) if img.endswith('.jpg')]
frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(video_name, fourcc, 56.0, (width, height))
for image in images:
    video.write(cv2.imread(os.path.join(image_folder, image)))
cv2.destroyAllWindows()
video.release()

# Outputs

Answered By: Carl_M