-
Notifications
You must be signed in to change notification settings - Fork 0
/
autonomous_video_cropper.py
105 lines (82 loc) · 2.55 KB
/
autonomous_video_cropper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# -*- coding: utf-8 -*-
"""Autonomous Video Cropper.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1qBQFFcG7o06MaUefVgrVE6m5coCcll0V
"""
#!pip install progressbar
#!pip install pytube
#!pip install cvlib
import cvlib as cv
from cvlib.object_detection import draw_bbox
import cv2
import cvlib as cv
from cvlib.object_detection import draw_bbox
import cv2
from pytube import YouTube
vid_url = 'https://www.youtube.com/watch?v=dsOZpD1j5Ac'
YouTube(vid_url).streams.filter(progressive=True, file_extension='mp4').first().download()
from glob import glob
file = glob('*.mp4')
og_vid = file[0]
og_vid
cap = cv2.VideoCapture(og_vid)
framecount = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4v',fourcc, 30, (800,880), True)
frame = 0
boxes = []
labels = []
confs = []
frames = []
print('Processing: %s' %og_vid)
while frame < framecount:
ret , image = cap.read()
frames.append(image)
frame += 1
if (type(image) == type(None)):
break
# apply object detection
bbox, label, conf = cv.detect_common_objects(image)
boxes.append(bbox)
labels.append(label)
confs.append(conf)
#print(bbox, label, conf)
print('Frame: {} / {}'.format(frame,framecount))
# draw bounding box over detected objects
box = draw_bbox(image, bbox, label, conf)
out.write(box)
# press any key to close window
if cv2.waitKey(1) == ord('x'):
break
# release resources
cap.release()
len(frames)
def pad_img_to_fit_box(img, x1, x2, y1, y2):
img = cv2.copyMakeBorder(img, - min(0, y1), max(y2 - img.shape[0], 0),
-min(0, x1), max(x2 - img.shape[1], 0),cv2.BORDER_REPLICATE)
y2 += -min(0, y1)
y1 += -min(0, y1)
x2 += -min(0, x1)
x1 += -min(0, x1)
return img, x1, x2, y1, y2
def imcrop(img, box):
x1, y1, x2, y2 = box
if x1 < 0 or y1 < 0 or x2 > img.shape[1] or y2 > img.shape[0]:
img, x1, x2, y1, y2 = pad_img_to_fit_box(img, x1, x2, y1, y2)
return img[y1:y2, x1:x2, :]
print('Filtering out objects that are not people')
indx = [i.index('person') for i in labels]
pbox = []
for i in range(0,framecount):
pbox.append(boxes[i][indx[i]])
video_name = 'cropped.mp4v'
nframe = frames[0]
video = cv2.VideoWriter(video_name,fourcc, 20.0, (800,800))
print('Cropping Video...')
for im in range(len(frames)):
video.write(imcrop(frames[im],pbox[im]))
video.release()
print('Completed\nCropped Video Located: %s' % video_name)
import matplotlib.pyplot as plt
plt.imshow(imcrop(frames[50],pbox[50]))