【问题标题】:speech recognition in multiple form fields in djangodjango中多个表单字段中的语音识别
【发布时间】:2021-07-08 08:16:06
【问题描述】:

我正在为我的网站使用 localhost 服务器。我在表单字段中集成了语音识别。 由于我在表单的每个字段中都需要语音识别器,因此我为此使用了不同的视图功能并渲染回同一页面。语音工作正常,但问题是当我提交表单时语音识别开始工作,但它应该将该数据保存到数据库中。当我不使用语音识别器时,它可以完美地保存数据,但不能使用语音识别器,因为它再次开始运行语音算法。 我真的不知道此时应该做什么。 表单代码

<form action="" method="POST" >
    {% csrf_token %}
        <div class="txtb" style="color:#022739">

       <input type="text" name="" value="{{p}}" readonly  style=" font-size: 15px;color:#022739;" >
      <input type="hidden" name="patientname" value="{{pid}}" readonly style=" font-size: 15px;color:#022739;" required="">
    </div>
        <div class="txtb" style="color:#022739">
             <input type="text" name="" value="{{doc}}" readonly style="  font-size: 15px;  color:#022739;">

      <input type="hidden" name="docname" value="{{docid}}"  readonly style=" font-size: 15px;color:#022739;" required="">
    </div>

        <div class="txtb" style="display:none;color:#022739">

      <input type="hidden" name="date" value="{{d}}" readonly style=" font-size: 15px;color:#022739;" required="">
    </div>


<div class="txtb" style="color:#022739">
      <input type="text" name="cal" value="{{sp}}" placeholder="Enter total calories"  style=" font-size: 15px;color:#022739;" required="" pattern="[0-9]+" >
      <a href="/speechCal"  style="font-family: FontAwesome"  >
         <i class="fa fa-microphone" aria-hidden="true" style="margin-left:160px;"></i>
      </a>
    </div>
<div class="txtb" >
      <input placeholder="Breakfast" name="breakfast" value="{{sp2}}" required="" pattern="([^\s][A-z0-9À-ž\s]+)">
   <a href="/speechBreak"  style="font-family: FontAwesome"  >
         <i class="fa fa-microphone" aria-hidden="true" style="margin-left:160px;"></i>
      </a>
    </div>
      <div class="txtb" >
      <input placeholder="Lunch" name="lunch" required="" value="{{sp3}}" pattern="([^\s][A-z0-9À-ž\s]+)">
            <a href="/speech3"  style="font-family: FontAwesome"  >
         <i class="fa fa-microphone" aria-hidden="true" style="margin-left:160px;"></i>
      </a>
    </div>
<div class="txtb" >
      <input placeholder="Dinner" name="dinner" value="{{sp4}}" required="" pattern="([^\s][A-z0-9À-ž\s]+)">
   <a href="/speech4"  style="font-family: FontAwesome"  >
         <i class="fa fa-microphone" aria-hidden="true" style="margin-left:160px;"></i>
      </a>
    </div>




    <a href="{% url 'nutchat' id=p %}"><input class="btn" type="submit" onclick="msg()" value="Send"/></a>

</form>
观看次数
def speechCal(request):
    onlydate = datetime.date.today().strftime('%Y-%m-%d')
    field_name = 'id'
    obj = DocRecordnew.objects.get(firstname=request.session['firstname'])
    docid = getattr(obj, field_name)

    pid = pd()

    r3 = sr.Recognizer()

    with sr.Microphone() as source:
        print("speak something")
        audio = r3.listen(source)
        r3.adjust_for_ambient_noise(source)
    try:
        speak = r3.recognize_google(audio)
        print("prediction: " + r3.recognize_google(audio))
        global sp11

        def sp11():
            return speak


    except Exception:
        speak = "something went wrong"
        print("something went wrong")
    #return redirect('newnutritionistchat/?id='+pid)
    return render(request, 'newnutritionistchat.html', {'d': onlydate,'p': pid, 'doc': request.session['firstname'],'docid': docid, 'sp': speak})

        # return render(request, 'newdoctorchat.html', {​​'doc': request.session['firstname'],'pid':pid, 'docid': docid}​​)
def speechBreak(request):
    onlydate = datetime.date.today().strftime('%Y-%m-%d')
    field_name = 'id'
    obj = DocRecordnew.objects.get(firstname=request.session['firstname'])
    docid = getattr(obj, field_name)
    sp=sp11()
    p = pd()
    r3 = sr.Recognizer()

    with sr.Microphone() as source:
        print("speak something")
        audio = r3.listen(source)
        r3.adjust_for_ambient_noise(source)
    try:
        speak = r3.recognize_google(audio)
        print("prediction: " + r3.recognize_google(audio))
        global sp22

        def sp22():
            return speak
    except Exception:
        speak = "something went wrong"
        print("something went wrong")
    return render(request, 'newnutritionistchat.html', {'d': onlydate,'sp':sp,'doc': request.session['firstname'],'p':p ,'docid': docid, 'sp2': speak})


def speech3(request):
    onlydate = datetime.date.today().strftime('%Y-%m-%d')
    field_name = 'id'
    obj = DocRecordnew.objects.get(firstname=request.session['firstname'])
    docid = getattr(obj, field_name)
    sp2 = sp22()
    sp=sp11()

    p = pd()
    r3 = sr.Recognizer()

    with sr.Microphone() as source:
        print("speak something")
        audio = r3.listen(source)
        r3.adjust_for_ambient_noise(source)
    try:
        speak = r3.recognize_google(audio)
        print("prediction: " + r3.recognize_google(audio))
        global sp33

        def sp33():
            return speak
    except Exception:
        speak = "something went wrong"
        print("something went wrong")
    return render(request, 'newnutritionistchat.html', {'d': onlydate,'sp2':sp2,'sp':sp,'doc': request.session['firstname'],'p':p ,'docid': docid, 'sp3': speak})


def speech4(request):
    onlydate = datetime.date.today().strftime('%Y-%m-%d')
    field_name = 'id'
    obj = DocRecordnew.objects.get(firstname=request.session['firstname'])
    docid = getattr(obj, field_name)
    sp2 = sp22()
    sp = sp11()
    sp3=sp33()

    p = pd()
    r3 = sr.Recognizer()

    with sr.Microphone() as source:
        print("speak something")
        audio = r3.listen(source)
        r3.adjust_for_ambient_noise(source)
    try:
        speak = r3.recognize_google(audio)
        print("prediction: " + r3.recognize_google(audio))

    except Exception:
        speak = "something went wrong"
        print("something went wrong")
    return render(request, 'newnutritionistchat.html',{ 'd': onlydate,'sp3': sp3,'sp2': sp2, 'sp': sp, 'doc': request.session['firstname'], 'p': p, 'docid': docid, 'sp4': speak})

【问题讨论】:

    标签: python django django-views django-templates speech-recognition


    【解决方案1】:

    尝试像这样将视图拆分为 GET 和 POST 请求

    if request.method == 'GET':
        # get the template with the audio saving form ,, from forms.py
    elif request.method == 'POST':
        # save the form into the database 
    
    
    

    【讨论】:

    • 实际上的问题是语音识别在提交表单时开始重新加载。两者的形式相同。语音识别所做的就是听取用户使用的语音并将其转换为文本,然后在该表单字段中获取结果。
    • 我要求您将逻辑分解为 GET 和 POST 请求的原因。打破你的业务逻辑。录制完音频后,您想使用(voice_record = models.FileField())将文件发布(保存)到后端(MODEL)一旦保存音频,然后返回(RESPONSE)
    猜你喜欢
    • 2021-06-28
    • 1970-01-01
    • 2023-04-05
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多