-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_stream.py
63 lines (48 loc) · 1.83 KB
/
main_stream.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
import video_dataloader.transforms as vtransforms
import torchvision
import torch
import sys
import cv2
from model_lstm import Inception3Model
def create_youtube_stream(url): # for example "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
import pafy
play = pafy.new(url).streams[-1] # '-1' means read the lowest quality of video.
assert play is not None # we want to make sure their is a input to read.
stream = cv2.VideoCapture(play.url) # create a opencv video stream.
return stream
def create_webcam_stream(cam_id):
stream = cv2.VideoCapture(cam_id) # 0 means read from local camera.
return stream
def main():
stream = None
if len(sys.argv) == 2:
url = sys.argv[1].strip()
if url.lower().startswith('webcam'):
url = url.replace('webcam', '')
cam_id = int(url) if url.isdigit() else 0
print('using webcam')
stream = create_webcam_stream(cam_id)
elif 'http' in url.lower() and 'www.youtube.com' in url.lower():
print(f'using youtube url: {url}')
stream = create_youtube_stream(url)
if stream is None:
print('USAGE: main_stream.py youtube_url/webcam')
sys.exit(0)
stt = vtransforms.StreamToTensor(stream=stream, fps=4, max_len=20, padding_mode="zero")
transform = torchvision.transforms.Compose([
stt,
vtransforms.VideoResize([224, 224]),
])
model = Inception3Model()
model = model.load_from_checkpoint("95.ckpt")
if torch.cuda.is_available():
model.to(torch.device("cuda"))
model.eval()
with torch.no_grad():
while stt.is_running:
v = transform(None)
ret = model(v[None, :, :, :, :]).flatten()
print(f'NonViolence: {ret[0]}, Violence: {ret[1]}')
stream.release()
if __name__ == '__main__':
main()