Qengineering / Face-Recognition-with-Mask-Jetson-Nano

Recognize 2000+ faces on your Jetson Nano with additional mask detection, auto-fill and anti-spoofing
https://qengineering.eu/deep-learning-examples-on-raspberry-32-64-os.html
BSD 3-Clause "New" or "Revised" License
35 stars 6 forks source link

Playing a welcome audio whenever someone comes infront of camera (either person/stranger or recognized person) #13

Closed rsingh2083 closed 3 years ago

rsingh2083 commented 3 years ago

Hi Sir,

Ive got a .wav file which Im playing whenever any person's Face is detected in camera. Please have a look at the playsound section of this snippet (main.cpp). The sound is playing but I think its slowing down the FaceRecognition also. Is system command slowing down the recognition/detection process ? Is there a faster /better way to play audio without slowing down the recognition/detection ?

#ifdef RECOGNIZE_FACE
        cv::String Str;
        cv::Scalar color;
        int  baseLine = 0;

        switch(obj.Color){
            case 0 : color = cv::Scalar(255, 255, 255); break;  //default white -> face ok
            case 1 : color = cv::Scalar( 80, 255, 255); break;  //yellow ->stranger
            case 2 : color = cv::Scalar(255, 237, 178); break;  //blue -> too tiny
            case 3 : color = cv::Scalar(127, 127, 255); break;  //red -> fake
            case 4 : color = cv::Scalar( 32,  32, 255); break;  //pure red -> mask
            default: color = cv::Scalar(255, 255, 255);
        }

        switch(obj.NameIndex){
            case -1: Str="Stranger"; break;
            case -2: Str="too tiny"; break;
            case -3: Str="Fake !";   break;
            case -4: Str="Mask !";   break;
            default: Str=NameFaces[obj.NameIndex];
        }
        cv::Size label_size = cv::getTextSize(Str, cv::FONT_HERSHEY_SIMPLEX, 0.6, 1, &baseLine);

        /////// ======== PLAY SOUND ============================ ///////////////
        // DETECT NAME & OUTPUT ON CONSOLE LOG
        cout << "NAME DETECTED AS \n\n " << Str << std::endl;

      //PLAY WELCOME SOUND   
          string str1 = "aplay ";
          str1 = str1 + "-Dhw:0,3 Welcome.wav" + " & ";
          const char *command = str1.c_str();
          system(command);
        } 

On the FaceMaskRecognition console im getting this error. I think whenever a face is detected audio is being called to play while the previous audio file hasnt finished playing. Is there a way to solve this ? aplay : audio open error: device or resource busy

Qengineering commented 3 years ago

A system call takes indeed a lot of computer time. The operating system has to start a new process every time you call system. Not a fastest solution. Two possible better options: A extra C++ library: link And again GStreamer: link. Please note, you have already some gst libraries on the Jetson.

rsingh2083 commented 3 years ago

From the portaudio page : - How do I play or record a file using PortAudio? We do not recommend doing file I/O in the PortAudio callback because the callback is run at a very high priority. We recommend reading the data in a normal priority thread then writing it to a FIFO which is read by the PortAudio callback. V19 has special functions called Pa_WriteStream() and Pa_ReadStream() for doing stream I/O though a FIFO.

Qengineering commented 3 years ago

Sorry @rsingh2083,

I've no expertise in playing audio. I've just googled playing wav Raspberry PI c++ and follow some promising suggestions.