Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import cv2
- import numpy as np
- os.system('cls')
- dir = "./"
- dirs = ["Videos/", "Video images/"]
- def folders():
- dirEx = False
- for i in dirs:
- _dir = dir + i
- if not os.path.isdir(_dir):
- os.mkdir(_dir)
- dirEx = True
- if dirEx:
- print("Missing folders")
- exit()
- folders()
- def autocrop(image, threshold=0):
- """Crops any edges below or equal to threshold
- Crops blank image to 1x1.
- Returns cropped image.
- """
- if len(image.shape) == 3:
- flatImage = np.max(image, 2)
- else:
- flatImage = image
- assert len(flatImage.shape) == 2
- rows = np.where(np.max(flatImage, 0) > threshold)[0]
- if rows.size:
- cols = np.where(np.max(flatImage, 1) > threshold)[0]
- image = image[cols[0]: cols[-1] + 1, rows[0]: rows[-1] + 1]
- else:
- image = image[:1, :1]
- return image
- def isPortret(_img):
- gray = cv2.cvtColor(_img, cv2.COLOR_BGR2GRAY)
- image = _img.copy()
- size = gray.size
- black = np.sum(gray <= 1)
- white = np.sum(gray == 255)
- if (black/size)*100 >= 65:
- h, w = gray.shape
- wc = int(0.9 * w)
- image[0:h, wc:w] = 1
- return image
- def scalePic(_pic):
- scale_percent = 60 # percent of original size
- width = int(_pic.shape[1] * scale_percent / 100)
- height = int(_pic.shape[0] * scale_percent / 100)
- dim = (width, height)
- # resize image
- return cv2.resize(_pic, dim, interpolation = cv2.INTER_AREA)
- def checkOrMake(_vid, _pic, name):
- print("Getting files from: " + _vid)
- print("Putting files in: " + _pic)
- cap = cv2.VideoCapture(_vid)
- length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
- success, image = cap.read()
- count = 0
- while success:
- # Crop the image before saving it
- image = autocrop(isPortret(image), 5)
- cv2.imshow(name, scalePic(image))
- # save frame as JPEG file
- #cv2.imwrite(_pic+"/frame%d.jpg" % count, image)
- percentage = count / length * 100
- print('Frame:%d/%d' % (count, length), '%d%% done' % percentage)
- count += 1
- success, image = cap.read()
- cap.release()
- cv2.destroyAllWindows()
- def getVideos(_vid, _pic, name):
- if not os.path.isdir(_pic + name):
- os.mkdir(_pic + name)
- checkOrMake(_vid + name, _pic + name, name)
- for filename in os.listdir(dir + dirs[0]):
- getVideos(dir + dirs[0], dir + dirs[1], filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement