Skip to content

Commit

Permalink
separated threaded camera
Browse files Browse the repository at this point in the history
  • Loading branch information
wroscoe committed Dec 13, 2016
1 parent ea6f418 commit d6b969f
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,43 @@ class Camera():
img=None #last image from the stream

def __init__(self):
print('Loading PiCamera... ', end='')
self.stream = io.BytesIO()


print('Loading PiCamera... ', end='')
from picamera import PiCamera
self.cam = PiCamera()
#let camera warm up.
time.sleep(1)
time.sleep(1) #let camera warm up.
self.cam.resolution = (640, 480)
print('success')

print('starting camera stream')
self.start_stream()

def capture_img(self):
'''return PIL image from camera'''
self.cam.capture(self.stream, format='jpeg')
img = Image.open(self.stream)
return normalize(img)


def capture_binary(self):
'''return binary stream of image for webserver'''
img = self.capture_img()
return image_utils.img_to_binary(img)


class ThreadedCamera(Camera):
'''
This is an attempt to solve the issue of many threads accessing
the same stream and getting corrupt streams before they are written completely.
It curretnly does not work.
'''

def __init__(self):
Camera.__init__(self)
self.start_stream()
print('starting camera stream')

def start_stream(self):
t = threading.Thread(target=self.update, args=())
t.daemon = True
Expand All @@ -47,19 +70,12 @@ def update(self):

time.sleep(.1)



def capture_img(self):
img = Image.open(self.stream)

return normalize(img)


def capture_binary(self):
img = self.capture_img()
return image_utils.img_to_binary(img)



class FakeCamera():
'''
Expand Down

0 comments on commit d6b969f

Please sign in to comment.