Advertisement
NoThanksMan

Meh frame extractor

Nov 20th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. import os
  2. import cv2
  3. import numpy as np
  4.  
  5. os.system('cls')
  6.  
  7. dir = "./"
  8. dirs = ["Videos/", "Video images/"]
  9.  
  10.  
  11. def folders():
  12.     dirEx = False
  13.     for i in dirs:
  14.         _dir = dir + i
  15.         if not os.path.isdir(_dir):
  16.             os.mkdir(_dir)
  17.             dirEx = True
  18.     if dirEx:
  19.         print("Missing folders")
  20.         exit()
  21.  
  22.  
  23. folders()
  24.  
  25. def autocrop(image, threshold=0):
  26.     """Crops any edges below or equal to threshold
  27.  
  28.    Crops blank image to 1x1.
  29.  
  30.    Returns cropped image.
  31.  
  32.    """
  33.     if len(image.shape) == 3:
  34.         flatImage = np.max(image, 2)
  35.     else:
  36.         flatImage = image
  37.     assert len(flatImage.shape) == 2
  38.  
  39.     rows = np.where(np.max(flatImage, 0) > threshold)[0]
  40.     if rows.size:
  41.         cols = np.where(np.max(flatImage, 1) > threshold)[0]
  42.         image = image[cols[0]: cols[-1] + 1, rows[0]: rows[-1] + 1]
  43.     else:
  44.         image = image[:1, :1]
  45.  
  46.     return image
  47.  
  48.  
  49. def isPortret(_img):
  50.     gray = cv2.cvtColor(_img, cv2.COLOR_BGR2GRAY)
  51.     image = _img.copy()
  52.     size = gray.size
  53.     black = np.sum(gray <= 1)
  54.     white = np.sum(gray == 255)
  55.     if (black/size)*100 >= 65:
  56.         h, w = gray.shape
  57.         wc = int(0.9 * w)
  58.         image[0:h, wc:w] = 1
  59.     return image
  60.  
  61. def scalePic(_pic):
  62.     scale_percent = 60 # percent of original size
  63.     width = int(_pic.shape[1] * scale_percent / 100)
  64.     height = int(_pic.shape[0] * scale_percent / 100)
  65.     dim = (width, height)
  66.     # resize image
  67.     return cv2.resize(_pic, dim, interpolation = cv2.INTER_AREA)
  68.  
  69.  
  70.  
  71.  
  72. def checkOrMake(_vid, _pic, name):
  73.     print("Getting files from: " + _vid)
  74.     print("Putting files in: " + _pic)
  75.     cap = cv2.VideoCapture(_vid)
  76.     length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
  77.     success, image = cap.read()
  78.     count = 0
  79.     while success:
  80.         # Crop the image before saving it
  81.         image = autocrop(isPortret(image), 5)
  82.         cv2.imshow(name, scalePic(image))
  83.         # save frame as JPEG file
  84.         #cv2.imwrite(_pic+"/frame%d.jpg" % count, image)
  85.         percentage = count / length * 100
  86.         print('Frame:%d/%d' % (count, length), '%d%% done' % percentage)
  87.         count += 1
  88.         success, image = cap.read()
  89.     cap.release()
  90.     cv2.destroyAllWindows()
  91.  
  92.  
  93. def getVideos(_vid, _pic, name):
  94.     if not os.path.isdir(_pic + name):
  95.         os.mkdir(_pic + name)
  96.     checkOrMake(_vid + name, _pic + name, name)
  97.  
  98.  
  99. for filename in os.listdir(dir + dirs[0]):
  100.     getVideos(dir + dirs[0], dir + dirs[1], filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement