worldveil / dejavu

Audio fingerprinting and recognition in Python
MIT License
6.36k stars 1.43k forks source link

dejavu to an App #208

Closed Alfnitacoder closed 4 years ago

Alfnitacoder commented 4 years ago

can anyone be helpful and give some ways to do deavu like SoundHound or some other apps.

gitteraz commented 4 years ago

You can fork Dejavu and add a Flask server with a single endpoint for audio recognition. Record a 5 sec sound from your app and send it as Form Data to that endpoint. In that endpoint receive the file try to recognize it and send it back to the client app. This is how i did it.

@bp.route("/recognize", methods=["POST"])
def recognize_audio():
  # load config
  with open("dejavu.cnf.SAMPLE") as f:
    config = json.load(f)

  # check if the post request has the audio part
  if "audio" not in request.files:
    return bad_request("Audio to fingerprint is missing.")
  else:
    audio = request.files["audio"]
    if audio.filename == "":
      return bad_request("Audio to fingerprint is missing.")
    else:
      # Save file in a temporary location
      try:
        audio.save(os.path.join(str(Path.home()), "echoprint/test/temp.mp3"))
      except Exception as ex:
        template = "An 1 exception of type {0} occurred. Arguments:\n{1!r}"
        message = template.format(type(ex).__name__, ex.args)
        return message

      # Create a Dejavu instance
      djv = Dejavu(config)

      # Recognize audio from a file
      try:
        song = djv.recognize(FileRecognizer, os.path.join(str(Path.home()), "echoprint/test/temp.mp3"))
        if song:
          return song[0]
      except Exception as ex:
        template = "An 2 exception of type {0} occurred. Arguments:\n{1!r}"
        message = template.format(type(ex).__name__, ex.args)
        return message
  return Response(status = 200)
worldveil commented 4 years ago

thank you for the wonderful code example @gitteraz