【发布时间】: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