jeudi 31 mai 2018

Introducing a new tune to Arduino code

Okay last question for this project. We have this face-to-sound code for raspberry pi and Arduino. Basically, when the camera detects a face, it sends a signal to Arduino to play a tune. So my question is, how can I introduce another 'song' (which we already have) into Arduino? So to be clear, if = 1 face is found : play tuneA. If >1 faces are found : play tuneB

We would have to modify both the Arduino and Python parts Here are both codes

Python:

import time
import io
import os
import sys
from cStringIO import StringIO
import random
import pipan
import picamera
import cv2
import numpy
import base64
import serial

ser = serial.Serial('/dev/ttyACM0', 9600)

find_face = False
serach_time = 0

def looking_face():

    #Create a memory stream so photos doesn't need to be saved in a file
    stream = io.BytesIO()

    with picamera.PiCamera() as camera:
        camera.resolution = (1280, 720)
        camera.hflip = True
        camera.vflip = True
        camera.capture(stream, format='jpeg')

    #Convert the picture into a numpy array
    buff = numpy.fromstring(stream.getvalue(), dtype=numpy.uint8)

    #Now creates an OpenCV image
    image = cv2.imdecode(buff, 1)

    #downsize the image for face recognition
    image_small = cv2.resize(image, (0, 0), fx=0.5, fy=0.5)

    #Load a cascade file for detecting faces
    face_cascade = cv2.CascadeClassifier('face.xml')

    #Convert to grayscale
    gray = cv2.cvtColor(image_small,cv2.COLOR_BGR2GRAY)

    #Look for faces in the image using the loaded cascade file
    faces = face_cascade.detectMultiScale(gray, 1.1, 5)

    print "Found "+str(len(faces))+" face(s)"

    if len(faces) > 0:

        # If a face is found, send a meesage to Arduino
        ser.write('1')

        #Draw a rectangle around every found face
        for (x,y,w,h) in faces:
            cv2.rectangle(image,(x*2,y*2),(x*2+w*2,y*2+h*2),(255,255,0),2)

        cv2.imwrite('face_found.jpg',image)

        exit()


def main():

    raw_input("press enter to start searching")
    print 'Search Begin!'

    # Infinite Loop
    while True:
        looking_face()

if __name__ == '__main__':
    main()

Arduino:

#include <Wire.h>
#include <Zumo32U4.h>

Zumo32U4Buzzer buzzer;

// Store this song in program space using the PROGMEM macro.
// Later we will play it directly from program space, bypassing
// the need to load it all into RAM first.
const char fugue[] PROGMEM =
  "! O5 L16 agafaea dac+adaea fa<aa<bac#a dac#adaea f";

boolean play_sound = false;

void setup()       // run once, when the sketch starts
{
  Serial.begin(9600);
}

void loop()        // run over and over again
{
  //Listening from Python
  if(Serial.read() == '1'){
     play_sound = true;
  }

  if(play_sound == true){
    // Start playing a tone with frequency 440 Hz at maximum
    // volume (15) for 200 milliseconds.
    buzzer.playFrequency(440, 200, 15);

    // Delay to give the tone time to finish.
    delay(1000);

    // Start playing note A in octave 4 at maximum volume
    // volume (15) for 2000 milliseconds.
    buzzer.playNote(NOTE_A(4), 2000, 15);

    // Wait for 200 ms and stop playing note.
    delay(200);
    buzzer.stopPlaying();

    delay(1000);

    // Start playing a fugue from program space.
    buzzer.playFromProgramSpace(fugue);

    // Wait until it is done playing.
    while(buzzer.isPlaying()){ }

    delay(1000);

    play_sound = false;
  }
}

Any help would be greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire