forked from italojs/Mask-rcnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-video.py
58 lines (48 loc) · 1.49 KB
/
generate-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
# import the necessary packages
import os
import cv2
import utils
import time
import argparse
import mask_rcnn
import numpy as np
args = mask_rcnn.get_args(video=True)
labels = mask_rcnn.get_labels(args["classes_path"])
net = mask_rcnn.get_net( args["frozen_inference"], args["inception"])
vs = cv2.VideoCapture(args["video_path"])
writer = None
try:
total = int(vs.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)) if utils.is_cv2() \
else int(vs.get(cv2.CAP_PROP_FRAME_COUNT))
print("[INFO] {} total frames in video".format(total))
except:
print("[INFO] could not determine # of frames in video")
total = -1
while True:
(grabbed, frame) = vs.read()
if not grabbed:
break
start = time.time()
(boxes, masks) = mask_rcnn.process_image(net,frame)
end = time.time()
drawed_image = mask_rcnn.draw_boxes_masks(
frame,
boxes,
masks,
labels,
float(args["confidence"]),
float(args["threshold"])
)
if writer is None:
fourcc = cv2.VideoWriter_fourcc(*"MJPG")
writer = cv2.VideoWriter(args["output"], fourcc, 30,
(drawed_image.shape[1], drawed_image.shape[0]), True)
if total > 0:
elap = (end - start)
print("[INFO] {:.2f} seconds to predict a unique frame".format(elap))
print("[INFO] estimated total time to finish: {:.2f} minutes".format(
(elap * total)/60))
writer.write(drawed_image)
print("[INFO] cleaning up...")
writer.release()
vs.release()