-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.py
More file actions
112 lines (97 loc) · 3.9 KB
/
camera.py
File metadata and controls
112 lines (97 loc) · 3.9 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
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
#!/usr/bin/python3 -*- coding: utf-8 -*-
from picamera import PiCamera
from datetime import datetime, time
import os
from fileHandler import *
from concurrent.futures import ThreadPoolExecutor
from loggerFormat import logsFormat
#import logging
#Const
SERVER = '192.168.1.147'
USER = 'miguelangel'
#SERVER_PATH = '/sharedfolders/PiCamera'
SERVER_PATH = '/srv/dev-disk-by-uuid-19926591486E72B1'
#PI_PATH = '/media/pi/00A3-22621/'
PI_PATH = '/media/pi/884cfa6c-fbea-41fe-b344-c5c87cb550c8/'
FOLDER_NAME = 'myVideos'
REGISTER_NAME = 'register.txt'
REGISTER_NAME_TO_DELATE = 'registerToDelete.txt'
LOG_FILE_NAME = 'camera.log'
LOG_LEVEL = 'DEBUG'
#Logs
logger = logsFormat('camera',PI_PATH,LOG_FILE_NAME)
def timestamp():
#Returns a timsStam at the instant it runs in the function in ISO format.
return datetime.now().isoformat(timespec= 'seconds')
def createDirRec(folderName):
try:
os.mkdir(PI_PATH + folderName)
logger.info('Creando folder %s',folderName)
except OSError as e:
logger.info(e)
def rec (camera,path,ts,regName,regDelName):
#I hace to format the timestamp becouse Raspivid doesn't accept the ":" in a file name.
ts = ts.replace(':','')
recName = ts + '.h264'
pathRecName = path + recName
camera.resolution = (1920, 1080)
camera.exposure_mode = ('night')
camera.clock_mode = ('raw')
camera.framerate = (5)
#camera.start_preview()
camera.annotate_text = timestamp()
logger.info('Grabando archivo: %s ', recName)
camera.start_recording(pathRecName,sps_timing=True,bitrate=10000000)
camera.wait_recording(1800)
camera.stop_recording()
logger.info('Grabación finalizada archivo: %s', recName)
recRegister(path,regName,recName)
return recName
def filesManagSend(files,server,user,origin,destination,regDelName):
#This function it's exclisvie to send the file afeter record
sshClient = sshLogin(server,user)
i = 0
for f in files:
i = i + 1
scptransfer(sshClient,origin,destination,regDelName,f)
logger.info('Archivos transferidos: ' + str(i) + '/' + str(len(files)))
sshLogout(sshClient)
def fileManagSend(f,server,user,origin,destination,regDelName):
#This function it's exclisvie to send the file afeter record
sshClient = sshLogin(server,user)
scptransfer(sshClient,origin,destination,regDelName,f)
sshLogout(sshClient)
removeFile(piPath,REGISTER_NAME_TO_DELATE)
if __name__ == "__main__":
#path = '/media/pi/0113-44041/'
#logging.basicConfig(filename = PI_PATH + 'camera.log',format = 'asctime', level = 'DEBUG')
#logger = logsFormat(PI_PATH,LOG_FILE_NAME)
camera = PiCamera()
files = []
executor = ThreadPoolExecutor(max_workers=3)
try:
createDirRec(FOLDER_NAME)
ts = timestamp()
piPath = PI_PATH + FOLDER_NAME + '/'
executor.submit(removeFile,piPath,REGISTER_NAME_TO_DELATE)
#removeFile(piPath,REGISTER_NAME_TO_DELATE)
files = Files(piPath)
if len(files) > 0 :
executor.submit(filesManagSend,files,SERVER,USER,piPath,SERVER_PATH,REGISTER_NAME_TO_DELATE)
removeFile(piPath,REGISTER_NAME_TO_DELATE)
for i in range(3):
print ("Pasada antes de grabar")
recName = rec(camera,piPath,ts,REGISTER_NAME,REGISTER_NAME_TO_DELATE)
sshClient = sshLogin(SERVER,USER)
executor.submit(fileManagSend,recName,SERVER,USER,piPath,SERVER_PATH,REGISTER_NAME_TO_DELATE)
executor.submit(scptransfer,sshClient,piPath,SERVER_PATH,REGISTER_NAME_TO_DELATE,recName)
ts = timestamp()
recName = rec(camera,piPath,ts,REGISTER_NAME,REGISTER_NAME_TO_DELATE)
print ("Después de grabar")
print (str(i))
finally:
logger.info('Cerrando Cámara')
camera.close()
sshLogout(sshClient)
removeFile(piPath,REGISTER_NAME_TO_DELATE)
executor.shutdown(wait=True)