Click here to Skip to main content
15,445,242 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to jump to a specific frame using openCV. I am doing this by dividing the specific number of the frame by the total number of frames. However, when I enter keyboard inputs to move forward a specific frame, the window crashes. It is not a forced close because exit code is 0, caused by the if not success: print("bruh") and break.
I am attaching my code below. If the video tool library that I created is needed, I will attach that in an edit, just let me know.
<pre>import cv2
import mediapipe as mp
import time
import video_tools as vt

mpDraw =
mpPose =
pose = mpPose.Pose()

file_name = 'OpenTurn'
video_ext = 'mp4'
video_path = 'C:\\Users\\jcoli\\PycharmProjects\\SwimCodeProject\\PoseVideos'
cap = vt.open_video(video_path + '/' + file_name + '.' + video_ext)
pTime = 0
cur_frame_num = 0

total_frames = vt.get_total_frames(cap)
show_annotation = True
key_pressed = ''

while True:
    if key_pressed == ord('a'):
        if cur_frame_num > 0:
            cur_frame_num = cur_frame_num - 1
            print('This is the first frame!')
    elif key_pressed == ord('d'):
        if cur_frame_num < total_frames:
            cur_frame_num = cur_frame_num + 1
            print('This is the last frame!')
        print('Please try one of these: a (pref), d (next)')
        key_pressed = cv2.waitKey() & 0xFF

    cap.set(2, cur_frame_num/total_frames)
    success, img =
    if not success:
    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    results = pose.process(imgRGB)
    # print(results.pose_landmarks)
    if results.pose_landmarks:
        mpDraw.draw_landmarks(img, results.pose_landmarks, mpPose.POSE_CONNECTIONS)
        for id, lm in enumerate(results.pose_landmarks.landmark):
            h, w, c = img.shape
            print(id, lm)
            cx, cy = int(lm.x * w), int(lm.y * h)
            points =, (cx, cy), 5, (255, 0, 0), cv2.FILLED)

    cTime = time.time()
    fps = 1 / (cTime - pTime)
    pTime = cTime

    cv2.putText(img, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3,
                (255, 0, 0), 3)

    key_pressed = vt.set_frame(img)

What I have tried:

I have tried looking at the Stack Overflow link [^] and asked for help from friends

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

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