-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvlcOpencvClass.py
76 lines (61 loc) · 2.44 KB
/
vlcOpencvClass.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import vlc
import ctypes
import numpy as np
import cv2 as cv
class vlcOpencvClass():
"""
callback to override the default vlc locking function
"""
CorrectVideoLockcb = ctypes.CFUNCTYPE(ctypes.c_void_p,ctypes.c_void_p,ctypes.POINTER(ctypes.c_void_p))
@CorrectVideoLockcb
def _lock_cb(opaque,planes):
self = ctypes.cast(opaque,ctypes.POINTER(ctypes.py_object)).contents.value
planes[0] = self.vlc_video_buf_p
return
"""
callback to override the default vlc read function
"""
@vlc.CallbackDecorators.VideoDisplayCb
def _display_cb(opaque,picture):
self = ctypes.cast(opaque,ctypes.POINTER(ctypes.py_object)).contents.value
self.vlcFrameBuffer = np.ndarray(shape=(self.height,self.width,self.channels),dtype=np.ubyte,buffer=self.vlc_video_buf)
return
"""
initialise the vlc player
"""
def __init__(self,camera_settings):
self.width = camera_settings['Camera_Horizontal_Resolution']
self.height = camera_settings['Camera_Vertical_Resolution']
self.fmt = camera_settings["Camera_fmt"]
self.channels = 3
#video buffers
self.vlc_video_buf=(ctypes.c_ubyte * self.width*self.height*self.channels)()
self.vlc_video_buf_p = ctypes.cast(self.vlc_video_buf,ctypes.c_void_p)
self.vlcFrameBuffer = np.zeros((self.height,self.width,self.channels),np.uint8)
#private pointer to self for lock/display callbacks
self.ref_ref = ctypes.py_object(self)
self.ref_p = ctypes.byref(self.ref_ref)
#media player, callbacks and format
self.player=vlc.MediaPlayer(camera_settings['Camera_Interface'])
vlc.libvlc_video_set_callbacks(self.player,self._lock_cb, None, self._display_cb, self.ref_p)
self.player.video_set_format(self.fmt,self.width,self.height,self.width*self.channels)
self.player.play()
"""
replicas of opencv functions
"""
def read(self):
temp_frame = self.vlcFrameBuffer.copy()
#convert to correct color format
temp_frame = cv.cvtColor(temp_frame,cv.COLOR_RGB2BGR)
return True,temp_frame
def isOpened(self):
return self.player.is_playing()
def release(self):
self.player.stop()
self.player.release()
return
#these methods are for compatibility at the moment
def get(self,var):
return 0
def set(self,var,val):
return 0