-
Notifications
You must be signed in to change notification settings - Fork 0
/
play_save_video.py
95 lines (73 loc) · 2.18 KB
/
play_save_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
87
88
89
90
91
92
93
94
import numpy as np
import cv2
import matplotlib.pyplot as plt
#clear output
from IPython.display import clear_output
import time
import threading
import time
dir="F:\\OneDrive - USNH\\Documents\\ActivePresenter\\Untitled1\\Video\\"
fn="dain1.mp4"
file_path=dir+fn
video=cv2.VideoCapture(file_path)
imgs=[]
while(video.isOpened()):
ret, frame = video.read()
if ret==False: break
imgs.append(frame)
video.release()
frame_no=0
closed=False
key_pressed=None
paused=False
frame_ids=[]
def play_images(imgs):
global frame_no
global closed
global key_pressed
global paused
global frame_ids
info="space: pause/play \nq: quit \na: previous frame \nd: next frame\ns: save frame"
while True:
img=imgs[frame_no].copy()
font = cv2.FONT_HERSHEY_SIMPLEX
text = 'Frame: ' + str(frame_no)
cv2.putText(img, text, (10, 50), font, 1, (0, 255, 255), 2, cv2.LINE_AA)
y0, dy = 50, 30
for i, line in enumerate(info.split('\n')):
y = y0 + i*dy
cv2.putText(img, line, (200, y ), font, 1, (0, 255, 255), 1, cv2.LINE_AA)
# cv2.putText(img, line, (100, y ), cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
cv2.imshow('Video', img)
#get key pressed
key_pressed=cv2.waitKey(1)
if not paused and key_pressed & 0xFF == ord(' '):
paused=True
elif paused and key_pressed & 0xFF == ord(' '):
paused=False
elif key_pressed & 0xFF == ord('a'):
frame_no-=1
if frame_no<0: frame_no=0
elif key_pressed & 0xFF == ord('d'):
frame_no+=1
if frame_no>=len(imgs): frame_no=len(imgs)-1
elif key_pressed & 0xFF == ord('s'):
frame_ids.append(frame_no)
print(f'frame_ids={frame_ids}')
if key_pressed & 0xFF == ord('q'):
closed=True
cv2.destroyAllWindows()
break
thread = threading.Thread(target=play_images, args=(imgs, ))
thread.start()
frame_no=0
while True:
if closed: break
if frame_no==1:
paused=True
time.sleep(delay)
if paused:
# print('paused')
pass
else:
frame_no+=1