Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
Richard MacCutchan 1-Oct-21 3:48am    
You need to use the debugger to find out what is going on. I have something similar and it works fine.
Junwoo Kim 3-Oct-21 12:53pm    
I've used the debugger but still not sure how to use it/find the error
Richard MacCutchan 4-Oct-21 3:44am    
Sorry, there is not enough information here to begin to guess what your problem could be. The debugger should show you what is happening at each step. you need to use that information to try and figure out what is not being actioned correctly in your code.
Junwoo Kim 16-Oct-21 7:55am    
How do I use the debugger for that? When I use it, the code just runs, so I can't tell what is happening each step.
Richard MacCutchan 16-Oct-21 8:11am    
See pdb — The Python Debugger — Python 3.10.0 documentation[^] for details on how to set a breakpoint and single step through the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900