I'm trying to make a simple program to navigate through the frames of a video. Those frames are already extracted as PNG files. I'm using OpenCV 4.5.4.58 alongside Python 3.7.11. I already made a program that could navigate through all my frames using arrow keys, but for convenience I want to add a trackbar to navigate faster in those frames. This trackbar is in seconds (frame number divided by my video frame rate). It works well until I need to use the arrow keys after using the trackbar to navigate frame by frame again.
Here is a simplified version of my code to show you the problem:
import cv2 as cv
import numpy as np
def empty(no_val):
pass
img = np.zeros((255, 255, 3))
clone = img.copy()
cv.namedWindow("image")
cv.createTrackbar("track", "image", 0, 100//25, empty) #here 100 represents my frame number and 25 my fps
i=0
old_track_pos = 0
while True:
img = clone.copy()
cv.putText(img, str(i), (125,125), cv.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 1)
cv.imshow("image", img)
key = cv.waitKeyEx(1)
track_pos = cv.getTrackbarPos("track", "image")
if key != -1:
print(key)
if key == 65361 and i > 0:
i -= 1
if key == 65363 and i < 100:
i += 1
if track_pos != old_track_pos:
old_track_pos = track_pos
i = track_pos*25
if key == ord("c"):
break
cv.destroyAllWindows()
If you run this code, you'll see that when using arrow keys before the trackbar, it increases and decreases the number by 1. Then if you use the trackbar, it goes 25 by 25. After that, if you try to use arrows again, it will go 25 by 25 because the trackbar is still selected, but once you click on the image, arrows have no effect. Other keys still work well, such as the c that exits the window.