Skip to content
Snippets Groups Projects
Commit aad3af83 authored by Benden, Daniel's avatar Benden, Daniel
Browse files

Added persitant image counter

parent 8c71dea9
No related branches found
No related tags found
No related merge requests found
import os
import json
import time
import datetime
import numpy as np
......@@ -13,6 +14,40 @@ try:
except ImportError:
from fake_rpi.picamera import PiCamera
class ImageCounter:
def __init__(self, storage_dir):
self.__persitance_storage = \
os.path.join(storage_dir, '.counter'):
self.__current_counter = None
def __read(self):
if not os.path.exists(self.__persitance_storage):
self.__current_counter = 0
return 0
with open(self.__persitance_storage, 'r') as f:
self.__current_counter = int(f.read())
def __store(self)
with open(self.__persitance_storage, 'w') as f:
f.write(self.__current_counter)
@property
def count(self):
if self.__current_counter is None:
self.__current_counter = self.__read()
return self.__current_counter
def increase(self):
self.__current_counter = self.count + 1
self.__store()
def __str__(self):
return str(self.__current_counter)
class Camera(object):
def __init__(self, storage_dir, resolution=(800, 480), framerate=30):
self.__camera = PiCamera()
......@@ -20,12 +55,12 @@ class Camera(object):
self.__image_size = (resolution[0], resolution[1], 3)
self.__framerate = framerate
self.__camera_lock = Lock()
self.__nb_images = 0
self.__nb_images = ImageCounter()
self.__storage_dir = storage_dir
def open(self):
pass
def image(self, resolution=None):
with self.__camera_lock:
if resolution is None:
......@@ -48,16 +83,17 @@ class Camera(object):
self.__camera.resolution = resolution
ts = datetime.datetime.utcnow().strftime("%Y%m%d-%H%M%S%f")
#cap_filename = os.path.join(self.__storage_dir, str(uuid.uuid4()) + '.jpg')
cap_filename = os.path.join(self.__storage_dir, f'{self.__nb_images}-{ts}.jpg')
img_nr = self.__nb_images.count + 1
cap_filename = os.path.join(self.__storage_dir, f'{img_nr}-{ts}.jpg')
start = time.time()
self.__camera.capture(cap_filename, use_video_port=True)
end = time.time() - start
logging.debug("capturing and istoring image took %f s" % end)
self.__nb_images += 1
logging.debug("capturing and storing image took %f s" % end)
self.__nb_images.increase()
os.sync()
def nb_images(self):
return self.__nb_images
return self.__nb_images.count
def led_on(self):
......
......@@ -21,7 +21,7 @@ def filesize(kbytes):
return (kbytes, units[c])
return (0, units[0])
......@@ -65,7 +65,7 @@ class QMainWidget(QMainWindow):
def flash_off(self):
self.flash.off()
def led_on(self):
self.led.on()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment