Set frame rate

This commit is contained in:
jimmy 2022-07-18 18:35:03 +12:00
parent 7ccb745daf
commit 054f92aa18
1 changed files with 14 additions and 13 deletions

View File

@ -1,7 +1,5 @@
from random import randint from random import randint
import cv2 import cv2
import numpy as np
import os
import multiprocessing import multiprocessing
import time import time
@ -14,7 +12,7 @@ class Video:
self.path = path self.path = path
def start(self): def start(self):
self.run = True
self.cap = cv2.VideoCapture(self.path) self.cap = cv2.VideoCapture(self.path)
# Check if camera opened successfully # Check if camera opened successfully
if (self.cap.isOpened()== False): if (self.cap.isOpened()== False):
@ -27,7 +25,11 @@ class Video:
# self.cap.set(cv2.CAP_PROP_POS_FRAMES, randint(0, totalframecount* 0.9)) # self.cap.set(cv2.CAP_PROP_POS_FRAMES, randint(0, totalframecount* 0.9))
# Read until video is completed # Read until video is completed
while(self.cap.isOpened() and self.run): fps = self.cap.get(cv2.CAP_PROP_FPS)
delay = 1/fps * 1000
print(fps, delay)
while(self.cap.isOpened()):
# Capture frame-by-frame # Capture frame-by-frame
ret, frame = self.cap.read() ret, frame = self.cap.read()
@ -37,27 +39,26 @@ class Video:
cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN) cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
cv2.moveWindow("window", self.x, self.y) cv2.moveWindow("window", self.x, self.y)
cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN) #cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
cv2.imshow("window", frame) cv2.imshow("window", frame)
cv2.waitKey(int(delay))
# Break the loop # Break the loop
else: else:
break break
# self.cap.release()
def stop(self):
self.run = False
self.cap.release()
# Closes all the frames # # Closes all the frames
cv2.destroyAllWindows() # cv2.destroyAllWindows()
if __name__ == "__main__": if __name__ == "__main__":
video = Video(0, 0) video = Video(0, 0)
video.setVideo("slave/sample-mp4-file.mp4") video.setVideo("sample-mp4-file.mp4")
x = multiprocessing.Process(target = video.start) x = multiprocessing.Process(target = video.start)
x.start() x.start()
time.sleep(1) time.sleep(2)
x.terminate() x.terminate()
time.sleep(1) time.sleep(1)
x = multiprocessing.Process(target = video.start) x = multiprocessing.Process(target = video.start)