Skip to content

Commit

Permalink
Merge pull request #13 from HAZET/master
Browse files Browse the repository at this point in the history
Improved logging, fix for framerate-setting
  • Loading branch information
mikevanis authored Mar 14, 2019
2 parents 72114ea + ecd82ba commit b3e0b4f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 25 deletions.
53 changes: 35 additions & 18 deletions ChangeDetector.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ class ChangeDetector(Thread):
def __init__(self, configuration):
super(ChangeDetector, self).__init__()

logging.basicConfig(filename='/home/pi/camera.log', level=logging.DEBUG)

self.config = configuration
self.daemon = True
self.cancelled = False

self.config = configuration
# initialise logging
numeric_loglevel = getattr(logging, self.config["log_level"].upper(), None)
if not isinstance(numeric_loglevel, int):
raise ValueError('Invalid log level: %s' % self.config["log_level"])
logging.basicConfig(filename='/home/pi/camera.log', level=numeric_loglevel, format='%(asctime)-15s %(levelname)s: %(message)s')
logging.info('logging initialised')

self.camera = None
self.hiResCapture = None
Expand All @@ -28,7 +32,6 @@ def __init__(self, configuration):
self.lowResStream = None
self.initialise_camera()

self.framerate = self.config["frame_rate"]
self.minWidth = self.config["min_width"]
self.maxWidth = self.config["max_width"]
self.minHeight = self.config["min_height"]
Expand All @@ -45,16 +48,16 @@ def __init__(self, configuration):
self.isMinActive = False
self.currentImage = None

# Initialise camera with splitter port.
# Initialise camera
def initialise_camera(self):
logging.info('Initialising camera ...')
if self.camera is not None:
self.camera.close()

self.camera = PiCamera()
PiCamera.CAPTURE_TIMEOUT = 60

self.camera.resolution = (self.safe_width(self.config["img_width"]), self.safe_height(self.config["img_height"]))

# setting resolution and framerate at construction saves time
self.camera = PiCamera(resolution = (self.safe_width(self.config["img_width"]), self.safe_height(self.config["img_height"])),framerate = self.config["frame_rate"])
time.sleep(1)

if self.config["fix_camera_settings"] is 1:
self.camera.iso = self.config["iso"]
time.sleep(0.2)
Expand All @@ -64,15 +67,20 @@ def initialise_camera(self):
self.camera.awb_mode = 'off'
self.camera.awb_gains = g

self.hiResCapture = PiRGBArray(self.camera)
# low resolution images for movement detection
self.lowResCapture = PiRGBArray(self.camera, size=(self.safe_width(self.config["cv_width"]),
self.safe_height(self.config["cv_height"])))
self.hiResStream = self.camera.capture_continuous(self.hiResCapture, format="bgr", use_video_port=True)
self.lowResStream = self.camera.capture_continuous(self.lowResCapture, format="bgr", use_video_port=True,
splitter_port=2,
resize=(self.safe_width(self.config["cv_width"]),
self.safe_height(self.config["cv_height"])))
# high resolution images for capturing photos
self.hiResCapture = PiRGBArray(self.camera)
self.hiResStream = self.camera.capture_continuous(self.hiResCapture, format="bgr", use_video_port=self.config["use_video_port"])

logging.debug('Camera initialised with a resolution of %s and a framerate of %s', self.camera.resolution, self.camera.framerate)
time.sleep(2)
logging.info('Ready to capture photos')

# Thread run
def run(self):
Expand All @@ -97,7 +105,7 @@ def save_photo(image):
try:
cv2.imwrite("photos/" + filename, image)
except Exception as e:
logging.debug('take_photo() error: ')
logging.error('save_photo() error: ')
logging.exception(e)
pass

Expand Down Expand Up @@ -141,10 +149,17 @@ def detect_change_contours(self, img):
return img

def capture_photo(self):
logging.info('Taking a photo...')
self.hiResCapture.truncate(0)
self.hiResCapture.seek(0)
hrs = self.hiResStream.__next__()
logging.info('Capturing a photo ...')
try:
self.hiResCapture.truncate(0)
self.hiResCapture.seek(0)
hrs = self.hiResStream.__next__()
except Exception as e:
logging.error('capture_photo() error: ')
logging.exception(e)
pass
else:
logging.debug('Captured %dx%d photo', self.hiResCapture.array.shape[1], self.hiResCapture.array.shape[0])
if self.config["rotate_camera"] is 1:
hi_res_image = imutils.rotate(hrs.array, angle=180)
else:
Expand Down Expand Up @@ -216,9 +231,11 @@ def decrease_min_max(self, increment):
self.maxHeight = self.config["cv_height"]

def arm(self):
logging.info('Starting photo capturing')
self.mode = 1

def disarm(self):
logging.info('Ending photo capturing')
self.mode = 0

def rotate_camera(self):
Expand Down Expand Up @@ -266,7 +283,7 @@ def update(self):
self.currentImage = self.detect_change_contours(self.currentImage)

except Exception as e:
logging.debug('update error')
logging.warning('update error')
logging.exception(e)
self.initialise_camera()
pass
Expand Down
16 changes: 9 additions & 7 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"cv_width": 384,
"cv_height": 216,
"img_width": 1920,
"img_height": 1080,
"frame_rate": 24,
"cv_width": 400,
"cv_height": 300,
"img_width": 3280,
"img_height": 2464,
"frame_rate": 10,
"use_video_port": false,
"camera_warmup": 2.5,
"stream_delay": 0.2,
"rotate_camera": 1,
Expand All @@ -17,5 +18,6 @@
"min_photo_interval_s": 1,
"fix_camera_settings": 0,
"iso": 800,
"shutter_speed": 8000
}
"shutter_speed": 8000,
"log_level": "INFO"
}

0 comments on commit b3e0b4f

Please sign in to comment.