Bonjour à tous !
Je suis novice en python et je me fais un petit projet perso, sur un Rpi avec un module KY-038 qui permet de detecter un bruit, j'aimerai que à un certain seuil de son, cela me déclanche deux actions, une prise de photo et une capteur audio.
Pour cela, j'ai fais 4 programmes en python, l'un pour detecter le son "sound-detect.py" :
Ensuite un fichier "photo.py" qui permet de prendre une photo :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 import RPi.GPIO as GPIO import time sound = 4 GPIO.setmode(GPIO.BCM) GPIO.setup(sound, GPIO.IN) def callback(sound): if GPIO.input(sound): print "Sound Detected!" GPIO.add_event_detect(sound, GPIO.BOTH, bouncetime=300) # let us know when the pin goes HIGH or LOW GPIO.add_event_callback(sound, callback) # assign function to GPIO PIN, Run function on change while True: time.sleep(1)
et un fichier "micro.py" qui permet d'enregistrer un son :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 from datetime import datetime from picamera import PiCamera camera = PiCamera() camera.rotation = 180 camera.capture("/home/pi/Test/Dim-"+str(datetime.now().strftime('%Y-%m-%d-%H_%M_%S'))+".jpg")
Individuellement, ces 3 fichiers fonctionnent bien
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 from datetime import datetime import pyaudio import wave form_1 = pyaudio.paInt16 # 16-bit resolution chans = 1 # 1 channel samp_rate = 44100 # 44.1kHz sampling rate chunk = 4096 # 2^12 samples for buffer record_secs = 4 # seconds to record dev_index = 2 # device index found by p.get_device_info_by_index(ii) wav_output_filename = "/home/pi/Test/Dim-"+str(datetime.now().strftime('%Y-%m-%d-%H_%M_%S'))+".wav" # name of .wav file audio = pyaudio.PyAudio() # create pyaudio instantiation # create pyaudio stream stream = audio.open(format = form_1,rate = samp_rate,channels = chans, \ input_device_index = dev_index,input = True, \ frames_per_buffer=chunk) print("recording") frames = [] # loop through stream and append audio chunks to frame array for ii in range(0,int((samp_rate/chunk)*record_secs)): data = stream.read(chunk) frames.append(data) print("finished recording") # stop the stream, close it, and terminate the pyaudio instantiation stream.stop_stream() stream.close() audio.terminate() # save the audio frames as .wav file wavefile = wave.open(wav_output_filename,'wb') wavefile.setnchannels(chans) wavefile.setsampwidth(audio.get_sample_size(form_1)) wavefile.setframerate(samp_rate) wavefile.writeframes(b''.join(frames)) wavefile.close()
Le soucis est que lorsque je crée un fichier principal "main.py" qui permet de lier ces trois programmes, j'ai "import" mes deux fichiers "micro" et "photo" mais apès je ne sais pas comment faire
Comment puis je faire ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import RPi.GPIO as GPIO import time import micro import photo sound = 4 GPIO.setmode(GPIO.BCM) GPIO.setup(sound, GPIO.IN) def callback(sound): if GPIO.input(sound): print "Sound Detected!" GPIO.add_event_detect(sound, GPIO.BOTH, bouncetime=300) # let us know when the pin goes HIGH or LOW GPIO.add_event_callback(sound, callback) # assign function to GPIO PIN, Run function on change while True: time.sleep(1)
Cordialement
Partager