-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.py
More file actions
29 lines (20 loc) · 1.1 KB
/
Copy pathcamera.py
File metadata and controls
29 lines (20 loc) · 1.1 KB
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
import cv2 as cv
class Camera:
def __init__(self): #constructor
self.camera = cv.VideoCapture(0) # 0 indicates only one web cam is used(if 2 then(0,1) is used)
if not self.camera.isOpened(): # if camera is not opened then raise an error
raise ValueError("Unable to open camera!")
self.width = self.camera.get(cv.CAP_PROP_FRAME_WIDTH) #width of a camera
self.height = self.camera.get(cv.CAP_PROP_FRAME_HEIGHT)
def __del__(self): #destructor
if self.camera.isOpened(): #if camera is opened/and is not in used then
self.camera.release() # it is released or closed
def get_frame(self):
if self.camera.isOpened(): # if camera is opened then
ret, frame = self.camera.read() #read the camera
if ret:
return (ret, cv.cvtColor(frame, cv.COLOR_BGR2RGB)) #if return exists then,color is set to red,green,blue
else:
return (ret, None) #if return donot exist then none
else:
return None #none of these cases then return none