-
Notifications
You must be signed in to change notification settings - Fork 127
/
main.py
69 lines (55 loc) · 2.33 KB
/
main.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
import argparse
import cv2
import numpy as np
import torch
from densepose import add_densepose_config
from densepose.vis.densepose_results import (
DensePoseResultsFineSegmentationVisualizer as Visualizer,
)
from densepose.vis.extractor import DensePoseResultExtractor
from detectron2.config import get_cfg
from detectron2.engine import DefaultPredictor
def main(input_video_path="./input_video.mp4", output_video_path="./output_video.mp4"):
# Initialize Detectron2 configuration for DensePose
cfg = get_cfg()
add_densepose_config(cfg)
cfg.merge_from_file("detectron2/projects/DensePose/configs/densepose_rcnn_R_50_FPN_s1x.yaml")
cfg.MODEL.WEIGHTS = "https://dl.fbaipublicfiles.com/densepose/densepose_rcnn_R_50_FPN_s1x/165712039/model_final_162be9.pkl"
cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
predictor = DefaultPredictor(cfg)
# Open the input video
cap = cv2.VideoCapture(input_video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Initialize video writer
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
# Process each frame
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
with torch.no_grad():
outputs = predictor(frame)["instances"]
results = DensePoseResultExtractor()(outputs)
# MagicAnimate uses the Viridis colormap for their training data
cmap = cv2.COLORMAP_VIRIDIS
# Visualizer outputs black for background, but we want the 0 value of
# the colormap, so we initialize the array with that value
arr = cv2.applyColorMap(np.zeros((height, width), dtype=np.uint8), cmap)
out_frame = Visualizer(alpha=1, cmap=cmap).visualize(arr, results)
out.write(out_frame)
# Release resources
cap.release()
out.release()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-i", "--input_video_path", type=str, default="./input_video.mp4"
)
parser.add_argument(
"-o", "--output_video_path", type=str, default="./output_video.mp4"
)
args = parser.parse_args()
main(args.input_video_path, args.output_video_path)