Last active
January 10, 2018 02:11
-
-
Save metaphox/15c145045205bba23ec97b891f18b422 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyQt5.QtGui import QPixmap, QImage | |
from PyQt5.QtWidgets import QApplication, QLabel | |
import sys | |
from cv2 import VideoCapture | |
if __name__ == '__main__': | |
qa = QApplication(sys.argv) | |
ql = QLabel() | |
video = VideoCapture('./upgradeyourwinter.mp4') | |
retval, frame = video.read() | |
im = frame | |
qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_RGB888) | |
ql.setPixmap(QPixmap.fromImage(qim)) | |
ql.adjustSize() | |
ql.show() | |
sys.exit(qa.exec_()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyQt5.QtCore import QThread | |
from cv2 import VideoCapture, CAP_PROP_FRAME_COUNT, CAP_PROP_FPS, CAP_PROP_POS_MSEC, CAP_PROP_FRAME_HEIGHT, \ | |
CAP_PROP_FRAME_WIDTH | |
# in OpenCV version 4.8 (used by gi?), VideoCapture.get() can't use CAP_PROP_FPS etc | |
# CAP_PROP_POS_MSEC = 0 | |
# CAP_PROP_FPS = 5 | |
# CAP_PROP_FRAME_COUNT = 7 | |
class CV2Player(QThread): | |
def __init__(self, parent=None): | |
super(CV2Player, self).__init__(parent) | |
self.video = VideoCapture() | |
self.stop = True | |
self.mutex = None | |
self.reset_frame_props() | |
self.frame_height = 0 | |
self.frame_width = 0 | |
self.frame_count = 0 | |
self.fps = -1 | |
self.frame_delay = 0 | |
def load_video(self, filepath): | |
self.video.open(filepath) | |
if self.video.isOpened(): | |
self.frame_count = int(self.video.get(CAP_PROP_FRAME_COUNT)) | |
self.fps = int(self.video.get(CAP_PROP_FPS)) | |
self.frame_delay = self.video.get(CAP_PROP_POS_MSEC) | |
self.frame_height = self.video.get(CAP_PROP_FRAME_HEIGHT) | |
self.frame_width = self.video.get(CAP_PROP_FRAME_WIDTH) | |
return True | |
return False | |
def play(self): | |
if not self.isRunning(): | |
if self.isStopped(): | |
self.stop = False | |
self.start(QThread.NormalPriority) | |
def stop(self): | |
self.stop = True | |
def run(self): | |
if self.frame_rate <= 0: | |
return | |
while not self.stop: | |
success, frame = self.video.read() | |
if not success: | |
self.stop = True | |
return | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment