-
Notifications
You must be signed in to change notification settings - Fork 1
/
video.py
86 lines (70 loc) · 2.91 KB
/
video.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
# Author: Selahaddin HONI | 001honi@github
# March, 2021
# ============================================================================================
import numpy as np
import cv2
class Video():
FONT = cv2.FONT_HERSHEY_SIMPLEX
BLUE = (255,0,0)
GREEN= (0,255,0)
RED = (0,0,255)
def __init__(self,path):
self.path = path
self.frames_inp = []
self.frames_out = []
self.total_frame = None
self.shape = (None,None) # H,W
self.gray = False
def read_frames(self,gray=False):
"""
stores all the frames in the given video source in
self.frames_inp (list) as [frame0, frame1, ...]
where frame# is numpy array
"""
try:
source = cv2.VideoCapture(self.path)
prop = cv2.CAP_PROP_FRAME_COUNT
self.total_frame = int(source.get(prop))
except:
print("Error in Path or Frame Count")
exit()
for i in range(self.total_frame):
ret, frame = source.read()
if not (ret or frame):
print("Error in Frame Read")
break
if gray:
self.gray = True
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if not self.shape[0]:
self.shape = frame.shape[:2]
self.frames_inp.append(frame)
print("[INFO] Video Import Completed")
def visualize(self,anchor,target,motionField,anchorP,text,a,t):
"""Put 4 frames together to show gui."""
h = 70 ; w = 10
H,W = anchor.shape
HH,WW = h+2*H+20, 2*(W+w)
frame = np.ones((HH,WW), dtype="uint8")*255
cv2.putText(frame, text[0], (w, 23), Video.FONT, 0.5, 0, 1)
cv2.putText(frame, text[1], (w, 40), Video.FONT, 0.4, 0, 1)
cv2.line(frame, (w, 46), (WW-w, 46),0)
cv2.putText(frame, f"anchor-{a:03d}", (w, h-4), Video.FONT, 0.4, 0, 1)
cv2.putText(frame, f"target-{t:03d}", (w+W, h-4), Video.FONT, 0.4, 0, 1)
cv2.putText(frame, "motion field", (w, h+2*H+10), Video.FONT, 0.4, 0, 1)
cv2.putText(frame, "predicted anchor", (w+W, h+2*H+10), Video.FONT, 0.4, 0, 1)
frame[h:h+H, w:w+W] = anchor
frame[h:h+H, w+W:w+2*W] = target
frame[h+H:h+2*H, w:w+W] = motionField
frame[h+H:h+2*H, w+W:w+2*W] = anchorP
return frame
def write(self, path="out.avi",fps=30):
H,W = self.frames_out[0].shape
# fourcc = cv2.VideoWriter_fourcc(*"MJPG")
fourcc = -1
writer = cv2.VideoWriter(path,fourcc,fps,(W,H),True)
for frame in self.frames_out:
if self.gray:
frame = cv2.cvtColor(frame,cv2.COLOR_GRAY2RGB)
writer.write(frame)
print("[INFO] Video Export Completed")