forked from meet-soni5720/Fight-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame_extraction.py
73 lines (58 loc) · 1.84 KB
/
frame_extraction.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
import sys
import os
import cv2
import imutils
from imutils.video import FPS
import numpy as np
from threading import Thread
import time
from tqdm import tqdm
if sys.version_info >= (3, 0):
from queue import Queue
else:
from Queue import Queue
class FileVideoStream:
def __init__(self,path,queueSize=128):
self.stream = cv2.VideoCapture(path)
self.stopped = False
self.Q = Queue(maxsize = queueSize)
def start(self):
t = Thread(target=self.update,args=())
t.daemon = True
t.start()
return self
def update(self):
while True:
if self.stopped:
return
if not self.Q.full():
(grabbed, frame) = self.stream.read()
if not grabbed:
self.stop()
return
self.Q.put(frame)
def read(self):
return self.Q.get()
def more(self):
return self.Q.qsize() > 0
def stop(self):
self.stopped = True
data_path = r"D:\real-life-violence-situations-dataset\Real Life Violence Dataset\NonViolence"
save_path = "D:/real-life-violence-situations-dataset/non_violence"
video_list = os.listdir(data_path)
print(video_list)
for video in tqdm(video_list):
video_path = os.path.join(data_path,video)
name = video.split('.')[0]
saving_at = os.path.join(save_path,name)
if not os.path.exists(saving_at):
os.makedirs(saving_at)
fvs = FileVideoStream(video_path).start()
time.sleep(1) #allowing buffer to fill
counter = 0
while fvs.more():
frame = fvs.read()
frame = cv2.resize(frame,(224,224),interpolation = cv2.INTER_NEAREST)
frame_name = os.path.join(saving_at,str(counter)+'.JPG')
cv2.imwrite(frame_name,frame)
counter+=1