-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
78 lines (64 loc) · 2.71 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
70
71
72
73
74
75
76
77
78
# -*- coding: UTF-8 -*-
import cv2 as cv
import argparse
import numpy as np
import time
from utils import choose_run_mode, load_pretrain_model, set_video_writer
from Pose.pose_visualizer import TfPoseVisualizer
from Action.recognizer import load_action_premodel, framewise_recognize
parser = argparse.ArgumentParser(description='Action Recognition by OpenPose')
parser.add_argument('--video', help='Path to video file.')
args = parser.parse_args()
# 导入相关模型
estimator = load_pretrain_model('VGG_origin')
action_classifier = load_action_premodel('Action/framewise_recognition.h5')
# 参数初始化
realtime_fps = '0.0000'
start_time = time.time()
fps_interval = 1
fps_count = 0
run_timer = 0
frame_count = 0
# 读写视频文件(仅测试过webcam输入)
cap = choose_run_mode(args)
video_writer = set_video_writer(cap, write_fps=int(7.0))
# # 保存关节数据的txt文件,用于训练过程(for training)
# f = open('origin_data.txt', 'a+')
while cv.waitKey(1) < 0:
has_frame, show = cap.read()
if has_frame:
fps_count += 1
frame_count += 1
# pose estimation
humans = estimator.inference(show)
# get pose info
pose = TfPoseVisualizer.draw_pose_rgb(show, humans) # return frame, joints, bboxes, xcenter
# recognize the action framewise
show = framewise_recognize(pose, action_classifier)
height, width = show.shape[:2]
# 显示实时FPS值
if (time.time() - start_time) > fps_interval:
# 计算这个interval过程中的帧数,若interval为1秒,则为FPS
realtime_fps = fps_count / (time.time() - start_time)
fps_count = 0 # 帧数清零
start_time = time.time()
fps_label = 'FPS:{0:.2f}'.format(realtime_fps)
cv.putText(show, fps_label, (width-160, 25), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3)
# 显示检测到的人数
num_label = "Human: {0}".format(len(humans))
cv.putText(show, num_label, (5, height-45), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3)
# 显示目前的运行时长及总帧数
if frame_count == 1:
run_timer = time.time()
run_time = time.time() - run_timer
time_frame_label = '[Time:{0:.2f} | Frame:{1}]'.format(run_time, frame_count)
cv.putText(show, time_frame_label, (5, height-15), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3)
cv.imshow('Action Recognition based on OpenPose', show)
video_writer.write(show)
# # 采集数据,用于训练过程(for training)
# joints_norm_per_frame = np.array(pose[-1]).astype(np.str)
# f.write(' '.join(joints_norm_per_frame))
# f.write('\n')
video_writer.release()
cap.release()
# f.close()