Home > Net >  Improper activity of a function (probably due to slowness) - OpenCV, Python
Improper activity of a function (probably due to slowness) - OpenCV, Python

Time:07-07

I am trying to open a video through OpenCV and record timestamps according to the user's actions (in ms, I'll write a different function to convert them to time format) So, for example, when the user clicks "f", the timestamp of exactly that seconds should be added to a list of timestamps called f_list which will be returned at the end.

I have a few problems:

  1. Not every time I click on my keyboard the timestamp is recorded! it's very random, and I really need this to work properly. Even to close the window (press q) takes time and I need to press a few times for the window to close
  2. In accordance to problem 1 - I am not sure it is recording the correct timestamps, that they are not delayed..
  3. When the video is opening it is not the window in the front, I need to "pull" it to the front, and every click on any other window removes the focus from it
  4. I think the video is running to slow, but I can deal with it

I have been working on my personal computer - i5-7200 (2.5 GHz) with 8 GM RAM, with other things open on the computer. CPU and disk are pretty low while running, CPU between 0-10%, sometimes jumps to 20%, disk at 0%. Memory on the other hand is very high - around 90%-95% where it is mostly taken by PyCharm and Chrome. The video I need to work on is pretty heavy - around an hour (~900MB) - but I have also been testing and having the same problems with a 45 seconds video (~6MB). I've been using PyCharm, and beside the piece of code I will post here everything else is commented out, so I believe that when the rest of the code will be activated it will be even worse..

I think that my computer is at least one of the problems, the thing is, other computers we have in my workplace are not much better.

Attaching a piece of my code down here, if anyone have any thoughts on how to make it more efficient or what causes the issues I have, I'll highly appreciate it!

THANK YOU!

def open_video(video_name):
    f_list = []
    p_list = []
    cap = cv2.VideoCapture(video_name)

    while cap.isOpened() == False:
        cap = cv2.VideoCapture(input('There was a problem opening the video. Please try again.\n'
                                     'Make sure you copy the name including the extension and the correct address if needed \n\t'))

    while(cap.isOpened()):
        ret, frame = cap.read()
        if ret == True:
            cv2.imshow('Frame', frame)
            if cv2.waitKey(15) & 0xFF == ord('f'):
                print(cap.get(cv2.CAP_PROP_POS_MSEC))
                f_list.append(cap.get(cv2.CAP_PROP_POS_MSEC))
            elif cv2.waitKey(15) & 0xFF == ord('p'):
                print(cap.get(cv2.CAP_PROP_POS_MSEC))
                p_list.append(cap.get(cv2.CAP_PROP_POS_MSEC))
            elif cv2.waitKey(15) & 0xFF == ord('q'):
                break
        else:
            break

    cap.release()
    print(f_list)
    print(p_list)

CodePudding user response:

To solve problem 1:

You shouldn't call the waitKey() function multiple times, this can lead to those kind of delays.

Call it once, store the output, and then create you if-else statements to check for the letters

key_stroke = cv2.waitKey(15)

if key_stroke & 0xFF == ord('f'):
    print(cap.get(cv2.CAP_PROP_POS_MSEC))
    f_list.append(cap.get(cv2.CAP_PROP_POS_MSEC))
elif key_stroke & 0xFF == ord('p'):
    print(cap.get(cv2.CAP_PROP_POS_MSEC))
    p_list.append(cap.get(cv2.CAP_PROP_POS_MSEC))
elif key_stroke & 0xFF == ord('q'):
    break
  • Related