Milad
Milad

Reputation: 473

Opencv play the videos too slow

I wrote a program with Python to play some videos with Opencv... It works correctly in Windows 10. But I want to run it with Raspberry, and the problem is that the videos will be played too slow. I used OMXplayer and it plays the files very smoothly. So the problem is not Raspberry.(I think so...)

I simplified my code:

print "---Playing the Videos and Images---"
cap = cv2.VideoCapture(str(VideoPath))
while cap.isOpened():
 ret, frame = cap.read()
 if ret == True:
  cv2.imshow('videoWindow',frame)
 if cv2.waitKey(25) & 0xFF == ord('q'):
  a=2
  break

cv2.destroyAllWindows()
print "!...Done...!"

Could you tell me what the problem is?

Upvotes: 4

Views: 11072

Answers (1)

al-eax
al-eax

Reputation: 732

Your loop doesnt care about the fps from your video. Looks like your video is not cached in RAM, you read it directly from your disk, thats why your video is displayed slow. Preload your video, just save all frames in a list before you display them.

Get the fps by video.get(cv2.cv.CV_CAP_PROP_FPS) for cv2.X or video.get(cv2.CAP_PROP_FPS) for cv3.X.

Now, measure the time for each loop iteration and calculate the needed sleep interval. Take a look here: https://www.learnopencv.com/how-to-find-frame-rate-or-frames-per-second-fps-in-opencv-python-cpp/

Upvotes: 5

Related Questions