-
Notifications
You must be signed in to change notification settings - Fork 37
/
camera_pi.py
executable file
·61 lines (49 loc) · 1.33 KB
/
camera_pi.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
from builtins import object
import time
import io
import threading
try:
__import__("cv2")
except ImportError:
ISRPI=False
else:
import cv2
ISRPI=True
class Camera(object):
thread = None # background thread that reads frames from camera
frame = None # current frame is stored here by background thread
last_access = 0 # time of last client access to the camera
def initialize(self):
if Camera.thread is None:
# start background frame thread
Camera.thread = threading.Thread(target=self._thread)
Camera.thread.start()
# wait until frames start to be available
while self.frame is None:
time.sleep(0)
def get_frame(self):
Camera.last_access = time.time()
self.initialize()
return self.frame
@classmethod
def _thread(cls):
camera_port = 0
camera=cv2.VideoCapture(camera_port)
# camera setup
camera.set(5,20) #frame rate
camera.set(3,640)
camera.set(4,360)
# max resolution 1280x720
# let camera warm up
time.sleep(2)
while True:
rc,img = camera.read()
retval,buf=cv2.imencode(".jpg",img,(cv2.IMWRITE_JPEG_QUALITY,75))
cls.frame=buf.tostring()
# if there hasn't been any clients asking for frames in
# the last 10 seconds stop the thread
if time.time() - cls.last_access > 10:
#del(camera)
camera.release()
break
cls.thread = None