techoverload/slave/video.py

71 lines
2.0 KiB
Python
Raw Normal View History

2022-07-18 05:30:07 +00:00
from random import randint
import cv2
import multiprocessing
import time
class Video:
def __init__(self, x, y):
self.x = x
self.y = y
2022-07-18 06:47:01 +00:00
def start(self, path):
2022-07-18 06:35:03 +00:00
2022-07-18 06:47:01 +00:00
self.cap = cv2.VideoCapture(path)
2022-07-18 05:30:07 +00:00
# Check if camera opened successfully
if (self.cap.isOpened()== False):
print("Error opening video file")
totalframecount= int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
print("The total number of frames in this video is ", totalframecount)
2022-07-20 11:46:27 +00:00
i = randint(0, int(totalframecount * 0.9))
self.cap.set(cv2.CAP_PROP_POS_FRAMES, i)
2022-07-18 05:30:07 +00:00
# Read until video is completed
2022-07-18 06:35:03 +00:00
fps = self.cap.get(cv2.CAP_PROP_FPS)
delay = 1/fps * 1000
print(fps, delay)
2022-07-20 11:46:27 +00:00
i = 0
2022-07-21 12:09:36 +00:00
playframes = randint(50, 200)
2022-07-20 11:46:27 +00:00
while(self.cap.isOpened() ):
i += 1
2022-07-21 12:09:36 +00:00
if i > playframes:
2022-07-20 11:46:27 +00:00
break
2022-07-18 06:35:03 +00:00
2022-07-18 05:30:07 +00:00
# Capture frame-by-frame
ret, frame = self.cap.read()
if ret == True:
# Display the resulting frame
cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
cv2.moveWindow("window", self.x, self.y)
2022-07-18 06:51:16 +00:00
cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
2022-07-18 05:30:07 +00:00
cv2.imshow("window", frame)
2022-07-18 06:35:03 +00:00
cv2.waitKey(int(delay))
2022-07-18 05:30:07 +00:00
# Break the loop
else:
break
2022-07-21 12:09:36 +00:00
#self.cap.release()
2022-07-18 05:30:07 +00:00
2022-07-18 06:51:16 +00:00
# Closes all the frames
2022-07-20 11:46:27 +00:00
#cv2.destroyAllWindows()
2022-07-18 05:30:07 +00:00
2022-07-20 09:14:04 +00:00
2022-07-18 05:30:07 +00:00
if __name__ == "__main__":
video = Video(0, 0)
2022-07-20 11:46:27 +00:00
x = multiprocessing.Process(target = video.start, args=("slave/videos/sample-mp4-file.mp4",))
2022-07-18 05:30:07 +00:00
x.start()
2022-07-18 06:35:03 +00:00
time.sleep(2)
2022-07-18 05:30:07 +00:00
x.terminate()
time.sleep(1)
2022-07-18 06:47:01 +00:00
video = Video(4000, 0)
2022-07-20 11:46:27 +00:00
x = multiprocessing.Process(target = video.start, args=("slave/videos/sample-mp4-file.mp4",))
2022-07-18 05:30:07 +00:00
x.start()
time.sleep(1)
2022-07-20 11:46:27 +00:00
x.terminate()