gpt4 book ai didi

ios - 将较小的音频文件从iOS应用上传到Python-Flask服务器

转载 作者:行者123 更新时间:2023-12-03 02:34:09 24 4
gpt4 key购买 nike

从字面上看,我遍历了与从iOS应用程序上载音频文件有关的所有StackOverflow帖子,但是还无法使我的代码正常工作。

基本上,我正在创建一个可以录制声音的应用程序,然后将声音发送到python flask 服务器,该服务器在声音文件上运行某些机器学习算法,并将值返回给客户端(iOS应用程序)。我的录音部分正在工作,并且一直想与服务器进行有效通信,但是我无法将音频文件发送到服务器。

所以我的问题分为两个部分:1.如何将音频文件从iOS发送到服务器? 2.如何在服务器中读取接收到的音频文件?

这是我的代码:

// Code to send an audio file to a local Python-Flask server (this code has been taken from a few answers on Stack Overflow)

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setURL:[NSURL URLWithString:@"http://127.0.0.1:5000"]];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

// Audio data
NSMutableData *postData = [NSMutableData data];
NSString *header = [NSString stringWithFormat:@"--%@\r\n", boundary];
[postData appendData:[header dataUsingEncoding:NSUTF8StringEncoding]];
//add your filename entry
NSString *contentDisposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", @"filename", @"sound.wav"];
[postData appendData:[contentDisposition dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[NSData dataWithContentsOfFile:@"/Users/talhajansari/Library/Application Support/iPhone Simulator/7.1/Applications/E1061366-05C2-4412-B381-39023F3C6C18/Documents/sound.wav"]];
NSString *endItemBoundary = [NSString stringWithFormat:@"\r\n--%@\r\n",boundary];
[postData appendData:[endItemBoundary dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];

[request setTimeoutInterval:30.0];

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

我的服务器代码是:
import os
from flask import Flask, request, redirect, url_for, jsonify
import uuid
from subprocess import Popen, PIPE
app = Flask(__name__)

# for ios
@app.route('/', methods=['POST'])
def classify():
soundFile = request.file
#run the algorithm on soundFile to get a particular value specific to the sound file
return jsonify({'answer':str(value)})

if __name__ == '__main__':
app.debug = True
app.run()

目前,该代码无法正常工作。我或者没有从客户端正确发送文件,或者没有在服务器端正确读取文件。基本上,request.file为空。

感谢您的帮助!

最佳答案

相当确定它是您要查找的request.files['filename'],而不是request.file。从flask's docs:

def upload_file():
if request.method == 'POST':
file = request.files['file']
if file:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file',
filename=filename))

关于ios - 将较小的音频文件从iOS应用上传到Python-Flask服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23175819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com