-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo.py
More file actions
97 lines (72 loc) · 2.76 KB
/
video.py
File metadata and controls
97 lines (72 loc) · 2.76 KB
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
"""Copyright (C) Square Factory SA - All Rights Reserved.
This source code is protected under international copyright law. All rights
reserved and protected by the copyright holders.
This file is confidential and only available to authorized individuals with the
permission of the copyright holders. If you encounter this file and do not have
permission, please contact the copyright holders and delete this file.
"""
import argparse
import asyncio
from pathlib import Path
import cv2
from i2_client import I2Client
parser = argparse.ArgumentParser()
parser.add_argument("--url", type=str, help="", required=True)
parser.add_argument("--access_uuid", type=str, help="", required=True)
parser.add_argument("--video_path", type=str, help="", required=True)
parser.add_argument("--save_path", type=str, help="", default=None)
args = parser.parse_args()
# Check arguments
path = Path(args.video_path)
if not path.is_file():
raise FileNotFoundError(f"No file at {path}")
valid_suffixes = [".mp4"]
if path.suffix not in valid_suffixes:
raise TypeError(
"The video has an suffix. Valid suffixes: {' ,'.join(valid_suffixes)}"
)
if args.save_path is None:
save_path = path.parent / f"{path.stem}_processed.mp4"
else:
save_path = Path(args.save)
if save_path.is_file():
raise FileExistsError("The save path is already exist")
# Main function
async def main():
"""Main async function."""
async with I2Client(args.url, args.access_uuid) as client:
# Start video reader
cap = cv2.VideoCapture(str(path))
if not cap.isOpened():
raise ValueError("Error opening video")
# Make the first inference to get inference output size
ok, frame = cap.read()
if not ok:
raise ValueError("Error reading video")
outputs = await client.async_inference(frame)
# Start video writer
fourcc = cv2.VideoWriter_fourcc(*"MP4V")
fps = 25
output_shape = (outputs[0].shape[0], outputs[0].shape[1])
out = cv2.VideoWriter(str(save_path), fourcc, fps, output_shape)
out.write(outputs[0])
count = 1
# Inference until video is completly processed
while True:
ok, frame = cap.read()
if not ok:
# End of the video
break
outputs = await client.async_inference(frame)
success, output = outputs[0]
if not success:
raise RuntimeError(output)
out.write(output)
count += 1
if not bool(count % 25):
print(f"processed {count} frames")
# Release reader and writer
cap.release()
out.release()
print(f"processed video saved at: {save_path}")
asyncio.run(main())