-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmy_video_capture.py
47 lines (38 loc) · 1.39 KB
/
my_video_capture.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
import cv2
class MyVideoCapture:
def __init__(self, video_source=0):
self.video_source = video_source
self.vid = None
self.width = None
self.height = None
self.init()
def init(self):
# Open the video source
self.vid = cv2.VideoCapture(self.video_source)
if not self.vid.isOpened():
print("Unable to open video source")
raise ValueError("Unable to open video source", self.video_source)
self.width = self.vid.get(cv2.CAP_PROP_FRAME_WIDTH)
self.height = self.vid.get(cv2.CAP_PROP_FRAME_HEIGHT)
def get_frame(self):
if self.vid.isOpened():
ret, frame = self.vid.read()
if ret:
# Return a boolean success flag and the current frame converted to BGR
return (ret, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
else:
print("Ret not found")
return (ret, None)
else:
print("get_frame Not opened")
return (None)
# Release the video source when the object is destroyed
def __del__(self):
print("Camera capture released")
if self.vid.isOpened():
self.vid.release()
def release_camera(self):
print("Camera capture released")
if self.vid.isOpened():
self.vid.release()
DEFAULT_VIDEO_CAPTURE = MyVideoCapture(0)