-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPictureManagement.py
225 lines (177 loc) · 6.85 KB
/
PictureManagement.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# manage SkyCam Pictures
import os
import time
import datetime
import config
import MySQLdb as mdb
import traceback
import shutil
import subprocess
#No of days before which the files are to be deleted
DELETE_FILES_OLDER_THAN_DAYS = 14
#Start Time Lapse at this Time
TIME_LAPSE_START_HOUR = 5
# no of days to keep time lapses
DELETE_TIME_LAPSES_OLDER_THAN_DAYS = 14
def cleanPictures(source):
#folder to clear from
dir_path = 'static/SkyCam/'
threshold = time.time() -DELETE_FILES_OLDER_THAN_DAYS *86400
print(f"threshold = {time.ctime(threshold)} ")
devices = os.listdir(dir_path)
print ("devices=", devices)
for device in devices:
device_dir_path = dir_path+device+"/"
days = os.listdir(device_dir_path)
print ("days=", days)
for day in days:
day_dir_path = device_dir_path+day+"/"
print ("day_dir_path=", day_dir_path)
files = os.listdir(day_dir_path)
#creation_time = os.stat(os.path.join(dir_path,day)).st_ctime
for myFile in files:
myFilePath = day_dir_path+myFile
creation_time = os.stat(myFilePath).st_ctime
if creation_time < threshold:
print(f"threshold = {time.ctime(threshold)} ")
print(f"{myFilePath} is created on {time.ctime(creation_time)} and will be deleted")
os.remove(myFilePath)
# check for no files left
files = os.listdir(day_dir_path)
if (len(files) == 0):
print("delete empty directory ", day_dir_path)
os.rmdir(day_dir_path)
files = os.listdir(device_dir_path)
if (len(files) == 0):
print("delete empty directory ", device_dir_path)
os.rmdir(device_dir_path)
return
def cleanTimeLapses(source):
try:
#folder to clear from
dir_path = 'static/TimeLapses/'
threshold = time.time() -DELETE_TIME_LAPSES_OLDER_THAN_DAYS *86400
print(f"threshold = {time.ctime(threshold)} ")
devices = os.listdir(dir_path)
print ("devices=", devices)
for device in devices:
device_dir_path = dir_path+device+"/"
files = os.listdir(device_dir_path)
print ("files=", files)
for myFile in files:
myFilePath = device_dir_path+myFile
creation_time = os.stat(myFilePath).st_ctime
if creation_time < threshold:
print(f"threshold = {time.ctime(threshold)} ")
print(f"{myFilePath} is created on {time.ctime(creation_time)} and will be deleted")
os.remove(myFilePath)
except:
pass
return
def addzeros(count):
if count < 10:
return "000"
if count < 100:
return "00"
if count < 1000:
return "0"
return ""
def buildTimeLapse(source):
# grab a list of the file names from mySQL
# start time
#5am previous day
yesterday = datetime.date.today () - datetime.timedelta (days=1)
hourtime = datetime.time(hour=5, minute=0)
starttime = datetime.datetime.combine(yesterday, hourtime)
print("startime=", starttime)
# end time
#5am today
today = datetime.date.today()
hourtime = datetime.time(hour=5, minute=0)
endtime = datetime.datetime.combine(today, hourtime)
print("endtime=", endtime)
# loop thorough all of the devices (grab the directory)
my_dir_path = 'static/SkyCam/'
devices = os.listdir(my_dir_path)
print ("devices=", devices)
os.makedirs("static/BuildTimeLapse", exist_ok=True)
os.makedirs("static/TimeLapses", exist_ok=True)
filerecords = []
for device in devices:
print ("device=", device)
print ("config.=",config.enable_MySQL_Logging)
if (config.enable_MySQL_Logging == True):
# open mysql database
# write log
# commit
# close
try:
print("before query")
con = mdb.connect(
"localhost",
"root",
config.MySQL_Password,
"SmartGarden3"
)
cur = con.cursor()
query = "SELECT timestamp, cameraID, picturename FROM SkyCamPictures WHERE cameraID = '%s' AND timestamp >= '%s' AND timestamp < '%s' ORDER BY timestamp" % (device, starttime, endtime)
print("query=", query)
cur.execute(query)
filerecords = cur.fetchall()
cur.execute(query)
con.commit()
except mdb.Error as e:
traceback.print_exc()
# sys.exit(1)
finally:
cur.close()
con.close()
del cur
del con
# rename them into the ffmpeg number format and copy them into the temp directory
if (len(filerecords) == 0):
print("device %s has no current pictures"%device)
if (len(filerecords) > 0):
dirpath = 'static/BuildTimeLapse'
try:
shutil.rmtree(dirpath)
except:
traceback.print_exc()
os.makedirs(dirpath, exist_ok=True)
count = 0
for record in filerecords:
# get date for dirpath
recordname = record[2]
splitName= recordname.split("_")
recordname2 = splitName[2]
splitName= recordname2.split("-")
dayname = splitName[0]+"-"+splitName[1]+"-"+splitName[2]
fromPath = my_dir_path+device+"/"+dayname+"/"+recordname
toPath = dirpath+"/pic_"+addzeros(count)+"%d"%(count)+".jpg"
print("cp %s %s" % (fromPath, toPath))
try:
shutil.copyfile(fromPath, toPath)
count = count +1
except:
pass
# build the video
# get full path name
cwdir =os.getcwd()
print(cwdir)
inputFiles = cwdir+"/"+dirpath+"/pic_%04d.jpg"
outDir = cwdir+"/"+"static/TimeLapses/"+device
os.makedirs(outDir, exist_ok=True)
outputFile = outDir+ "/"+device+"_"+dayname+".mp4"
try:
os.remove(outputFile)
except:
pass
command ="/usr/bin/ffmpeg -r 20 -i %s -c:v libx264 %s " % (inputFiles, outputFile)
print(command)
cmd = command.split()
print(cmd )
os.system(command+"> /dev/null 2>&1" )
#p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
#print (p)
# save it to timelapse directory
return