I am trying to show my annotations and show the video frame by frame. However, I get the frame by frame but not the annotation. Here is my code:
import cv2
import mediapipe as mp
import video_tools as vt
# Video prep
file_name = 'OpenTurn'
video_path = 'C:\\Users\\jcoli\\PycharmProjects\\SwimCodeProject\\PoseVideos'
video_ext = 'mp4'
# Annotation Prep
mpDraw = mp.solutions.drawing_utils
mpPose = mp.solutions.pose
pose = mpPose.Pose()
# Video Path
cap = vt.open_video(video_path + '/' + file_name + '.' + video_ext)
# Total Frame Count
total_frames = vt.get_total_frames(cap)
# Current Frame
cur_frame_num = 0
# Define Frame
frame = vt.get_frame(cap, cur_frame_num)
key_pressed = ''
while True:
success, img = cap.read()
if key_pressed == ord('a'):
if cur_frame_num > 0:
cur_frame_num = cur_frame_num - 1
else:
print('This is the first frame!')
elif key_pressed == ord('d'):
if cur_frame_num < total_frames:
cur_frame_num = cur_frame_num + 1
else:
print('This is the last frame!')
elif key_pressed == ord('q'):
break
else:
print('Please try one of these: a (pref), d (next)')
imgRGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = pose.process(imgRGB)
# Annotate
if results.pose_landmarks:
mpDraw.draw_landmarks(frame, results.pose_landmarks, mpPose.POSE_CONNECTIONS)
# General Annotation
for id, lm in enumerate(results.pose_landmarks.landmark):
h, w, c = frame.shape
print(id, lm)
cx, cy = int(lm.x * w), int(lm.y * h)
points = cv2.circle(frame, (cx, cy), 5, (255, 0, 0), cv2.FILLED)
# show the frame and get the next keyboard input
frame = vt.get_frame(cap, cur_frame_num)
frame = vt.show_frame_number(frame, cur_frame_num, total_frames)
key_pressed = vt.set_frame(frame)
cv2.imshow("Image", img)
What I have tried:
I have tried changing where I put:
<pre> success, img = cap.read()
if key_pressed == ord('a'):
if cur_frame_num > 0:
cur_frame_num = cur_frame_num - 1
else:
print('This is the first frame!')
elif key_pressed == ord('d'):
if cur_frame_num < total_frames:
cur_frame_num = cur_frame_num + 1
else:
print('This is the last frame!')
elif key_pressed == ord('q'):
break
else:
print('Please try one of these: a (pref), d (next)')
inside the while loop