0

I want to upload a .wav file from react frontend to node server and then send the file to a python file for speech recognition. I used multer to get the file in the post route. The file I get looks like this.

{
    "fieldname": "file",
    "originalname": "00f0204f_nohash_0.wav",
    "encoding": "7bit",
    "mimetype": "audio/wave",
    "destination": "./public/",
    "filename": "IMAGE-1635358708022.wav",
    "path": "public\\IMAGE-1635358708022.wav",
    "size": 32044
}

Now I want to fork a child process for python from index.js and want to sent the file for ASR.

The python file code looks like this:

import speech_recognition as sr

def read_in():
  lines = sys.stdin.readlines()
  return json.loads(lines[0])

def main():
  a = read_in()

  r = sr.Recognizer()

  with sr.AudioFile(a) as source:
    audio_text = r.listen(source)

    try:
        text = r.recognize_google(audio_text)
        print('Conversion Started')
        print(text)
    except:
        print('Error Occurred - Try Again')

How should I send the uploaded file from node to this python file for computation? I am new at this, so I am really confused.

1 Answers1

0

if you are post processing the file, then get the file content with REST API:

# Postman generated
import http.client

host = "ur ip"
path = "server path:port" # port match as the express server
def webhooktriggered(path): # i haven't done any project about py web before, so goodluck
 conn = http.client.HTTPSConnection(host)
 payload = ''
 headers = {}
 conn.request("GET", path, payload, headers)
 res = conn.getresponse()
 data = res.read()
 return (data.decode("utf-8"))

before, stream your media with express:

// Original: https://www.codegrepper.com/code-examples/javascript/node+js+express+sendfile+fs+filestream
var http = require('http'),
    fileSystem = require('fs'),
    path = require('path');

http.createServer(function(request, response) {
    var filePath = path.join(__dirname, 'pathto/yourfilehere.wav');
    var stat = fileSystem.statSync(filePath);

    response.writeHead(200, {
        'Content-Type': 'audio/x-wav', // change to whatever u want
        'Content-Length': stat.size
    });

    var readStream = fileSystem.createReadStream(filePath);
    // We replaced all the event handlers with a simple call to readStream.pipe()
    readStream.pipe(response);
})
.listen(2000); //as soon we will use this port in rest client

Trigger when process, using axios

const axios = require('axios')
function uploadSuccess(path){ // must be the streamed express media path, so python rest can download it then
 axios.get('http://pythonbackserverip/?path=' + encodeURI(path))
  .then({
   console.log("upload successfully!")
  })
}

note: my code is not accurate, modify it before use

hUwUtao
  • 76
  • 8