-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpretrained_keypointrcnn_video.py
More file actions
64 lines (47 loc) · 1.65 KB
/
pretrained_keypointrcnn_video.py
File metadata and controls
64 lines (47 loc) · 1.65 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
import torch
import torchvision
import cv2
import argparse
import utils
import time
from PIL import Image
from torchvision.transforms import transforms as transforms
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--input', default = 'test_video.mp4')
args = parser.parse_args()
USE_CUDA = torch.cuda.is_available()
DEVICE = torch.device("cuda" if USE_CUDA else "cpu")
transform = transforms.Compose([
transforms.ToTensor()
])
model = torchvision.models.detection.keypointrcnn_resnet50_fpn(pretrained=True, num_keypoints=17)
model.to(DEVICE)
model.eval()
cap = cv2.VideoCapture(args.input)
if (not cap.isOpened()):
print('Video is not available')
frame_count = 0
total_fps = 0
with torch.no_grad():
while (cap.isOpened()):
ret, frame = cap.read()
if ret == True:
start_time = time.time()
pil_image = Image.fromarray(frame)
outputs = model(transform(pil_image).unsqueeze(0).to(DEVICE))
end_time = time.time()
output_image = utils.draw_keypoints(outputs, frame) # 가시화
fps = 1 / (end_time - start_time)
total_fps += fps
frame_count += 1
wait_time = max(1, int(fps / 4))
cv2.imshow('Output frame', output_image)
if cv2.waitKey(wait_time) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
avg_fps = total_fps / frame_count
print(f"Average FPS: {avg_fps:.3f}")