-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhiveMonitor.py
88 lines (72 loc) · 2.56 KB
/
hiveMonitor.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
import config
import time
from picamera import PiCamera
import uuid
import pyaudio
import wave
from subprocess import call
# paths
video_dir = config.base_dir+"/multimedia/videos/"
audio_dir = config.base_dir+"/multimedia/audios/"
image_dir = config.base_dir+"/multimedia/images/"
database_path = config.base_dir+"/multimedia/database.sqlite"
class Capture:
def __init__(self):
self.files = []
self.camera = None
def change_format(self, file_path):
new_file_path = file_path
if file_path.endswith(".h264"):
new_file_path = file_path[:-5]+".mp4"
call("MP4Box -fps 30 -add "+file_path+" "+new_file_path, shell=True)
call("rm "+file_path, shell=True)
return new_file_path
def record_video(self, capture_duration=10):
self.init_camera()
vid_path = video_dir + 'vid' + uuid.uuid4().__str__() + '.h264'
self.camera.start_recording(vid_path)
self.camera.wait_recording(capture_duration)
self.camera.stop_recording()
self.files.append([self.change_format(vid_path), "video"])
self.save_to_db()
def record_audio(self, record_seconds=10):
chunk = 1024
form = pyaudio.paInt16
channels = 2
rate = 44100
p = pyaudio.PyAudio()
stream = p.open(format=form,
channels=channels,
rate=rate,
input=True,
frames_per_buffer=chunk)
frames = []
for i in range(0, int(rate / chunk * record_seconds)):
data = stream.read(chunk)
frames.append(data)
stream.stop_stream()
stream.close()
p.terminate()
aud_path = audio_dir + 'aud' + uuid.uuid4().__str__() + '.wav'
wf = wave.open(aud_path, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(form))
wf.setframerate(rate)
wf.writeframes(b''.join(frames))
wf.close()
self.files.append([self.change_format(aud_path), "audio"])
self.save_to_db()
def init_camera(self):
if self.camera is None:
self.camera = PiCamera()
self.camera.resolution = (640, 480)
def snap(self, num=1):
self.init_camera()
time.sleep(2)
for i in range(num):
img_path = image_dir + 'img' + uuid.uuid4().__str__() + '.jpg'
self.camera.capture(img_path)
self.files.append([self.change_format(img_path), "image"])
self.save_to_db()
def save_to_db(self):
pass