-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
64 lines (53 loc) · 1.91 KB
/
test.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
import cv2
import numpy as np
import tensorflow as tf
import utils_pose as utils
from movenet import Movenet
from data import BodyPart
import os
from def_lib import detect, get_keypoint_landmarks, landmarks_to_embedding, draw_prediction_on_image, predict_pose, draw_class_on_image, write_video
path = './videos/office1_test_fall.mp4'
def gen_video(path):
model = tf.keras.models.load_model("./models/model_fall.h5")
cap = cv2.VideoCapture(path)
time_step = 5
label = "waiting"
i = 0
lm = []
list = []
while cap.isOpened():
ret, frame = cap.read()
# Reshape Image
if ret == True:
img = frame.copy()
img = cv2.resize(img, (854, 480))
# img = cv2.resize(img, (640, 360))
img = tf.convert_to_tensor(img, dtype=tf.uint8)
i = i + 1
print(f"Start detect: frame {i}")
person = detect(img)
landmarks = get_keypoint_landmarks(person)
lm_pose = landmarks_to_embedding(tf.reshape(
tf.convert_to_tensor(landmarks), (1, 51)))
# print(lm_pose)
lm.append(lm_pose)
img = np.array(img)
img = draw_prediction_on_image(img, person, crop_region=None,
close_figure=False, keep_input_size=True)
if (len(lm) == time_step):
lm = tf.reshape(lm, (1, 34, 5))
label = predict_pose(model, lm, label)
lm = []
img = np.array(img)
img = draw_class_on_image(label, img)
list.append(img)
cv2.imshow('Fall Detection', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# cv2.destroyAllWindows()
else:
break
cap.release()
# out_path = os.path.join('./outputs/', path)
# write_video(out_path, np.array(list), 24)
gen_video(path)