-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselfie-o-matic.py
162 lines (123 loc) · 4.22 KB
/
selfie-o-matic.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# coding=utf-8
# Progetto: Selfie-O-Matic
import sys
import os
import time
import io
from consts import *
try:
from picamera.array import PiRGBArray
from picamera import PiCamera
except:
pass
try:
import RPi.GPIO as GPIO
except:
pass
import cv2
import logging
import settings
from tasks.task_countdown import CountdownTask
from tasks.task_fadetowhite import FadeToWhiteTask
from tasks.task_stillframe import StillFrameTask
from tasks.task_snapshot import SnapShotTask
from tasks.task_postonfb import PostOnFbTask
from tasks.task_pushetta import PushettaTask
from tasks.task_upload_lost import UploadLostTask
from task_manager import TaskManager
from tendo import singleton
__author__ = "Fabrizio Guglielmino"
APP_NAME = "Self-O-Matic"
VERSION = "0.3"
class DeviceContext(object):
camera = None
cap = None
custom_data = {}
def __init__(self, camera, cap):
self.camera = camera
class SelfieOMatic(object):
_is_running = False
# True quando sta processando una foto
_is_snap = False
# TODO: Coda di frame processors
_processors = []
_task_manager = TaskManager()
rawCapture = None
ctx = DeviceContext(None, None)
def __init__(self, device=0):
logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
filename='selfie-o-matic.log', level=logging.INFO)
self.ctx.camera = None
if GPIO:
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
try:
cv2.namedWindow("MainWin")
self.ctx.camera = PiCamera()
if settings.HFLIP_IMAGE:
self.ctx.camera.hflip = True
self.ctx.camera.framerate = 24
self.ctx.camera.preview_fullscreen = True
self.ctx.camera.awb_mode = 'sunlight'
self.ctx.camera.exposure_mode = 'snow'
self.ctx.camera.start_preview()
self.rawCapture = PiRGBArray(self.ctx.camera)
time.sleep(0.3)
except:
logging.error("picamera init failed")
if not os.path.exists(PUBLISHED_FOLDER):
os.mkdir(PUBLISHED_FOLDER)
# Scheduling del task di recupero immagini non uploadate
upload_lost = UploadLostTask(self.ctx)
self._task_manager.add_scheduled_task(upload_lost)
def run(self):
self._is_running = True
while self._is_running:
self.__process_input()
self.__process_tasks()
time.sleep(0.05)
def cleanup(self):
if self.ctx.camera:
self.ctx.camera.stop_preview()
self.ctx.camera.close()
cv2.destroyAllWindows()
def __process_input(self):
key = cv2.waitKey(10)
if GPIO:
input_state = GPIO.input(18)
if input_state == False:
# Remap
key = ord('s')
if key == ord('q'):
cv2.destroyAllWindows()
self._is_running = False
elif key == ord('s'):
if not self._is_snap:
self._is_snap = True
# Snapshot workflow
countdown = CountdownTask(self.ctx)
fade = FadeToWhiteTask(self.ctx)
still = StillFrameTask(self.ctx)
snap = SnapShotTask(self.ctx)
postfb = PostOnFbTask(self.ctx)
# Async snapshot, image processing ad post on fb
#still.set_on_completed(self.__after_fade_event)
self._task_manager.add_task(countdown)
self._task_manager.add_task(fade)
self._task_manager.add_task(still)
self._task_manager.add_task(snap)
self._task_manager.add_task(postfb)
def __after_fade_event(self, taskmanager):
# push = PushettaTask(self.ctx)
# taskmanager.add_async_task(push)
snap = SnapShotTask(self.ctx)
taskmanager.add_async_task(snap)
def __process_tasks(self):
self._is_snap = self._task_manager.cycle()
if __name__ == '__main__':
# Gestione della singola istanza
me = singleton.SingleInstance()
selfie = SelfieOMatic()
selfie.run()
selfie.cleanup()