-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphoto.py
59 lines (50 loc) · 2.1 KB
/
photo.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
import cv2
import psutil
import os
import time
import logging
import threading
from picamera import PiCamera
#Initialize logging files
logging.basicConfig(filename='storage/results.log',
format='%(asctime)s-%(message)s',
level=logging.DEBUG)
def clarity(self, image):
# compute the Laplacian of the image and then return the focus
# measure, which is simply the variance of the Laplacian
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return 0 if image is None else cv2.Laplacian(gray, cv2.CV_64F).var()
def is_focused(self, image):
return clarity(image) > 100
def has_disk_space():
hdd = psutil.disk_usage('/')
return hdd.percent < 95
def capture(frame, visitation_id, detection_score, photo_type):
thread = threading.Thread(target=save, args=(frame, visitation_id, detection_score, photo_type))
logging.info("Main : before running thread")
thread.start()
def mkdirs(visitation_id):
directory = "storage/detected/{}/{}".format(time.strftime("%Y-%m-%d"), visitation_id)
logging.info("making directories")
if not os.path.exists(directory):
os.makedirs(directory)
return directory
def save(frame, visitation_id, detection_score, photo_type):
logging.info('checking disk space')
try:
if has_disk_space():
directory = mkdirs(visitation_id)
image_path = "{}/{}_{}_{}.png".format(directory, photo_type, time.strftime("%H-%M-%S"), detection_score)
if not os.path.isfile(image_path):
logging.info("writing image {}".format(image_path))
cv2.imwrite( image_path, frame )
else:
logging.info("file already exists")
# camera = PiCamera()
# camera.resolution = (3264, 2448)
# if bounding_box:
# logging.info("zooming image")
# camera.zoom = (bounding_box['x0'], bounding_box['y0'], bounding_box['x1']-bounding_box['x0'], bounding_box['y1']-bounding_box['y0'])
# camera.capture( image_path , 'png')
except:
logging.exception("Failed to save image")