-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraft.py
More file actions
63 lines (50 loc) · 1.79 KB
/
draft.py
File metadata and controls
63 lines (50 loc) · 1.79 KB
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
62
63
from io import BytesIO
from PIL import Image
import logging
import logzero
from logzero import logger
from picamera import PiCamera
import numpy
import datetime
from time import sleep
import random
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
# Set a logfile name
logzero.logfile(dir_path+"/data01.csv")
# Set a custom formatter
formatter = logging.Formatter('%(name)s - %(asctime)-15s - %(levelname)s: %(message)s');
logzero.formatter(formatter)
# Set up camera for 4:3 aspect ratio
cam = PiCamera()
cam.resolution = (2592,1944)
# datetime variable to store the start time
start_time = datetime.datetime.now()
# create a datetime variable to store the current time
now_time = datetime.datetime.now()
# setting photo counter for start
photo_counter = 1
# initialise binary stream which will receive image data
imageBytes = BytesIO()
# collecting data (gps data and photographs for 2 minutes)
while (now_time < start_time + datetime.timedelta(minutes=2)):
try:
# Capture photograph
# cam.capture(dir_path+"/photo_"+ str(photo_counter).zfill(4)+".jpg")
cam.capture(imageBytes, format='jpeg')
im = Image.open(imageBytes)
imageBrightness = numpy.mean(im)
if imageBrightness > 20:
logger.info("Saving photo %s", photo_counter)
im.save(dir_path+"/photo_"+ str(photo_counter).zfill(4)+".jpg", "JPEG")
photo_counter+=1
sleep(5)
else:
logger.info("Photo rejected, waiting...")
sleep(60)
imageBytes.truncate(0)
imageBytes.seek(0)
# update the current time
now_time = datetime.datetime.now()
except Exception as e:
logger.error("An error occurred: " + str(e))