File size: 1,477 Bytes
ed29c11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import cv2
import numpy as np
import os
from qtpy.QtGui import QImage, QPixmap

class mp4Io_reader():
	def __init__(self, filename, info=[]):
		self.filename = filename
		self.file = cv2.VideoCapture(filename)

		if self.file.isOpened()==False:
			print("Error in opening video file.")

		self.header={}
		if info==[]:
			self.readHeader()

	def readHeader(self):

		self.header = {
			'width': int(self.file.get(cv2.CAP_PROP_FRAME_WIDTH)),
			'height': int(self.file.get(cv2.CAP_PROP_FRAME_HEIGHT)),
			'fps': self.file.get(cv2.CAP_PROP_FPS),
			'numFrames': int(self.file.get(cv2.CAP_PROP_FRAME_COUNT))
		}

	def seek(self, index):

		self.file.set(cv2.CAP_PROP_POS_FRAMES, index)

	def getTs(self,n=None):
		if n==None:
			n = self.header['numFrames']

		ts = np.zeros(n+1)
		for i in np.arange(1,n+1):
			self.seek(i)
			self.file.read()
			ts[i] = self.file.get(cv2.CAP_PROP_POS_MSEC)/1000.

		self.ts = ts[1:]
		return self.ts

	def getFrame(self, index, decode=True):

		self.seek(index)
		ret, frame = self.file.read()

		ts = self.file.get(cv2.CAP_PROP_POS_MSEC)/1000.
		return frame, ts

	def getFrameAsQPixmap(self, index, decode=True):
		image, _ = self.getFrame(index, decode)
		h, w, ch = image.shape
		bytes_per_line = ch * w
		convert_to_Qt_format = QImage(image.data, w, h, bytes_per_line, QImage.Format_BGR888)
		return QPixmap.fromImage(convert_to_Qt_format)

	def close(self):
		self.file.release()