-
Notifications
You must be signed in to change notification settings - Fork 0
/
mycamera.py
53 lines (40 loc) · 1.37 KB
/
mycamera.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
from picamera2 import Picamera2 # required for camera module v3
import numpy as np
class MyPiCamera():
def __init__(self,width,height):
self.cap = Picamera2()
self.width = width;
self.height = height
self.is_open = True
try:
self.config = self.cap.create_video_configuration(main={"format":"RGB888","size":(width,height)})
self.cap.align_configuration(self.config)
self.cap.configure(self.config)
self.cap.start()
except:
self.is_open = False
return
def read(self,dst=None):
# allocate blank image to avoid returning a "None"
if dst is None:
dst = np.empty((self.height, self.width, 3), dtype=np.uint8)
if self.is_open:
dst = self.cap.capture_array()
# dst is either blank or the previously captured image at this point
return self.is_open,dst
def isOpened(self):
return self.is_open
def release(self):
if self.is_open is True:
self.cap.close()
self.is_open = False
return
if __name__ == "__main__":
import cv2
camera = MyPiCamera(640,480)
while camera.isOpened():
_, image = camera.read()
cv2.imshow("mycamera", image)
if cv2.waitKey(1) == ord('q'):
break
cv2.destroyAllWindows()