Spaces:
				
			
			
	
			
			
		Running
		
			on 
			
			Zero
	
	
	
			
			
	
	
	
	
		
		
		Running
		
			on 
			
			Zero
	Create inference_video.py
Browse files- inference_video.py +164 -0
    	
        inference_video.py
    ADDED
    
    | @@ -0,0 +1,164 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import cv2
         | 
| 2 | 
            +
            import numpy as np
         | 
| 3 | 
            +
            import glob
         | 
| 4 | 
            +
            from os.path import isfile, join
         | 
| 5 | 
            +
            import subprocess
         | 
| 6 | 
            +
            from IPython.display import clear_output
         | 
| 7 | 
            +
            import os
         | 
| 8 | 
            +
            from google.colab import files
         | 
| 9 | 
            +
            import shutil
         | 
| 10 | 
            +
            from io import BytesIO
         | 
| 11 | 
            +
            import io
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            IMAGE_FORMATS = ('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif')
         | 
| 14 | 
            +
             | 
| 15 | 
            +
             | 
| 16 | 
            +
            model_scale = "2" #@param ["2", "4", "8"] {allow-input: false}
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            model = RealESRGAN(device, scale=int(model_scale))
         | 
| 19 | 
            +
            model.load_weights(f'weights/RealESRGAN_x{model_scale}.pth', download=False)
         | 
| 20 | 
            +
             | 
| 21 | 
            +
             | 
| 22 | 
            +
            def process_input(filename):
         | 
| 23 | 
            +
              result_image_path = os.path.join('results/restored_imgs', os.path.basename(filename))
         | 
| 24 | 
            +
              image = Image.open(filename).convert('RGB')
         | 
| 25 | 
            +
              sr_image = model.predict(np.array(image))
         | 
| 26 | 
            +
              sr_image.save(result_image_path)
         | 
| 27 | 
            +
              print(f'Finished! Frame of the Video saved to {result_image_path}')
         | 
| 28 | 
            +
             | 
| 29 | 
            +
             | 
| 30 | 
            +
            # assign directory
         | 
| 31 | 
            +
            directory = 'videos' #PATH_WITH_INPUT_VIDEOS
         | 
| 32 | 
            +
            zee = 0
         | 
| 33 | 
            +
             | 
| 34 | 
            +
            def convert_frames_to_video(pathIn,pathOut,fps):
         | 
| 35 | 
            +
                frame_array = []
         | 
| 36 | 
            +
                files = [f for f in os.listdir(pathIn) if isfile(join(pathIn, f))]
         | 
| 37 | 
            +
                #for sorting the file names properly
         | 
| 38 | 
            +
                files.sort(key = lambda x: int(x[5:-4]))
         | 
| 39 | 
            +
                size2 = (0,0)
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                for i in range(len(files)):
         | 
| 42 | 
            +
                    filename=pathIn + files[i]
         | 
| 43 | 
            +
                    #reading each files
         | 
| 44 | 
            +
                    img = cv2.imread(filename)
         | 
| 45 | 
            +
                    height, width, layers = img.shape
         | 
| 46 | 
            +
                    size = (width,height)
         | 
| 47 | 
            +
                    size2 = size
         | 
| 48 | 
            +
                    print(filename)
         | 
| 49 | 
            +
                    #inserting the frames into an image array
         | 
| 50 | 
            +
                    frame_array.append(img)
         | 
| 51 | 
            +
                out = cv2.VideoWriter(pathOut,cv2.VideoWriter_fourcc(*'DIVX'), fps, size2)
         | 
| 52 | 
            +
                for i in range(len(frame_array)):
         | 
| 53 | 
            +
                    # writing to a image array
         | 
| 54 | 
            +
                    out.write(frame_array[i])
         | 
| 55 | 
            +
                out.release()
         | 
| 56 | 
            +
             | 
| 57 | 
            +
             | 
| 58 | 
            +
            for filename in os.listdir(directory):
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                f = os.path.join(directory, filename)
         | 
| 61 | 
            +
                # checking if it is a file
         | 
| 62 | 
            +
                if os.path.isfile(f):
         | 
| 63 | 
            +
             | 
| 64 | 
            +
             | 
| 65 | 
            +
                  print("PROCESSING :"+str(f)+"\n")
         | 
| 66 | 
            +
                  # Read the video from specified path
         | 
| 67 | 
            +
             | 
| 68 | 
            +
                  #video to frames
         | 
| 69 | 
            +
                  cam = cv2.VideoCapture(str(f))
         | 
| 70 | 
            +
             | 
| 71 | 
            +
                  try:
         | 
| 72 | 
            +
             | 
| 73 | 
            +
                      # PATH TO STORE VIDEO FRAMES
         | 
| 74 | 
            +
                      if not os.path.exists('upload'):
         | 
| 75 | 
            +
                          os.makedirs('upload')
         | 
| 76 | 
            +
             | 
| 77 | 
            +
                  # if not created then raise error
         | 
| 78 | 
            +
                  except OSError:
         | 
| 79 | 
            +
                      print ('Error: Creating directory of data')
         | 
| 80 | 
            +
             | 
| 81 | 
            +
                  # frame
         | 
| 82 | 
            +
                  currentframe = 0
         | 
| 83 | 
            +
             | 
| 84 | 
            +
             | 
| 85 | 
            +
                  while(True):
         | 
| 86 | 
            +
             | 
| 87 | 
            +
                      # reading from frame
         | 
| 88 | 
            +
                      ret,frame = cam.read()
         | 
| 89 | 
            +
             | 
| 90 | 
            +
                      if ret:
         | 
| 91 | 
            +
                          # if video is still left continue creating images
         | 
| 92 | 
            +
                          name = 'upload/frame' + str(currentframe) + '.jpg'
         | 
| 93 | 
            +
             | 
| 94 | 
            +
                          # writing the extracted images
         | 
| 95 | 
            +
                          cv2.imwrite(name, frame)
         | 
| 96 | 
            +
             | 
| 97 | 
            +
             | 
| 98 | 
            +
                            # increasing counter so that it will
         | 
| 99 | 
            +
                            # show how many frames are created
         | 
| 100 | 
            +
                          currentframe += 1
         | 
| 101 | 
            +
                          print(currentframe)
         | 
| 102 | 
            +
                      else:
         | 
| 103 | 
            +
                          #deletes all the videos you uploaded for upscaling
         | 
| 104 | 
            +
                          #for f in os.listdir(video_folder):
         | 
| 105 | 
            +
                          #  os.remove(os.path.join(video_folder, f))
         | 
| 106 | 
            +
             | 
| 107 | 
            +
                          break
         | 
| 108 | 
            +
             | 
| 109 | 
            +
                    # Release all space and windows once done
         | 
| 110 | 
            +
                  cam.release()
         | 
| 111 | 
            +
                  cv2.destroyAllWindows()
         | 
| 112 | 
            +
             | 
| 113 | 
            +
                  #apply super-resolution on all frames of a video
         | 
| 114 | 
            +
             | 
| 115 | 
            +
                  # Specify the directory path
         | 
| 116 | 
            +
                  all_frames_path = "upload"
         | 
| 117 | 
            +
             | 
| 118 | 
            +
                  # Get a list of all files in the directory
         | 
| 119 | 
            +
                  file_names = os.listdir(all_frames_path)
         | 
| 120 | 
            +
             | 
| 121 | 
            +
                  # process the files
         | 
| 122 | 
            +
                  for file_name in file_names:
         | 
| 123 | 
            +
                    process_input(f"upload/{file_name}")
         | 
| 124 | 
            +
             | 
| 125 | 
            +
             | 
| 126 | 
            +
                  #convert super res frames to .avi
         | 
| 127 | 
            +
                  pathIn = 'results/restored_imgs/'
         | 
| 128 | 
            +
             | 
| 129 | 
            +
                  zee = zee+1
         | 
| 130 | 
            +
                  fName = "video"+str(zee)
         | 
| 131 | 
            +
                  filenameVid = f"{fName}.avi"
         | 
| 132 | 
            +
             | 
| 133 | 
            +
                  pathOut = "results_videos/"+filenameVid
         | 
| 134 | 
            +
             | 
| 135 | 
            +
                  fps = 25.0 #change this to FPS of your source video
         | 
| 136 | 
            +
             | 
| 137 | 
            +
                  convert_frames_to_video(pathIn, pathOut, fps)
         | 
| 138 | 
            +
             | 
| 139 | 
            +
             | 
| 140 | 
            +
                  #convert .avi to .mp4
         | 
| 141 | 
            +
                  src = 'results_videos/'
         | 
| 142 | 
            +
                  dst = 'results_mp4_videos/'
         | 
| 143 | 
            +
             | 
| 144 | 
            +
                  for root, dirs, filenames in os.walk(src, topdown=False):
         | 
| 145 | 
            +
                      #print(filenames)
         | 
| 146 | 
            +
                      for filename in filenames:
         | 
| 147 | 
            +
                          print('[INFO] 1',filename)
         | 
| 148 | 
            +
                          try:
         | 
| 149 | 
            +
                              _format = ''
         | 
| 150 | 
            +
                              if ".flv" in filename.lower():
         | 
| 151 | 
            +
                                  _format=".flv"
         | 
| 152 | 
            +
                              if ".mp4" in filename.lower():
         | 
| 153 | 
            +
                                  _format=".mp4"
         | 
| 154 | 
            +
                              if ".avi" in filename.lower():
         | 
| 155 | 
            +
                                  _format=".avi"
         | 
| 156 | 
            +
                              if ".mov" in filename.lower():
         | 
| 157 | 
            +
                                  _format=".mov"
         | 
| 158 | 
            +
             | 
| 159 | 
            +
                              inputfile = os.path.join(root, filename)
         | 
| 160 | 
            +
                              print('[INFO] 1',inputfile)
         | 
| 161 | 
            +
                              outputfile = os.path.join(dst, filename.lower().replace(_format, ".mp4"))
         | 
| 162 | 
            +
                              subprocess.call(['ffmpeg', '-i', inputfile, outputfile])
         | 
| 163 | 
            +
                          except:
         | 
| 164 | 
            +
                              print("An exception occurred")
         | 
 
			
