"Let's make a natural conversation robot!"I'm studying Python based on a book called "Artificial Intelligence Made with RasberryPi and Arduino."
I have a question because an error occurred when I tried to execute the code in it as specified.
The source code and error content are listed below.
#!/usr/bin/env python
# coding:UTF-8
import requests
import json
importos
import time
GOOGLE_APIKEY='Your_Google_APIKEY' #Replace this part with the APIKEY you got yourself
VOICE_REC_PATH='/home/pi/ai/hello.flac'
def recognize():
print('recognizing...')
f=open(VOICE_REC_PATH, 'rb')
voice=f.read()
f.close()
url='https://www.google.com/speech-api/v2/recognize?xjerr=1&client=chromium&'\'lang=ja-JP&maxresults=10&pfilter=0&xjerr=1&key='+\GOOGLE_APIKEY
hds = {'Content-type': 'audio/x-flac; rate = 11025'}
try:
rsp=requests.post(url,data=voice,headers=hds).text
except IOError:
return '#CONN_ERR'
expect:
return '#ERROR'
print'results:',rsp
objs=rsp.split(os.linesep)
for obj in objs:
if not obj:
continue
alternatives=json.loads(obj) ['result']
iflen(alternates) == 0:
continue
return alternatives [0]['alternative'][0]['transcript']
US>"return"
current_milli_time():
return int(round(time.time()*1000))
if__name__=='__main__':
t0 = current_milli_time()
message=recognize().encode('utf-8')
print 'recognized:'+str(current_milli_time()-t0)+'ms'
if(message=='#CONN_ERR'):
print 'internet not available'
message = ' '
elif(message=='#ERROR'):
print 'voice recognizing failed'
message = ' '
else:
print 'your words:' + message
Traceback (most recent call last):
File "/home/pi/ai/test-recognize.py", line 46, in<module>
message=recognize().encode('utf-8')
File "/home/pi/ai/test-recognize.py", line 34, in acknowledge
alternatives=json.loads(obj) ['result']
File"/usr/lib/python 2.7/json/_init__.py", line 338, in loads
return_default_decoder.decode(s)
File "/usr/lib/python 2.7/json/decoder.py", line 366,indecode
obj,end=self.raw_decode(s,idx=_w(s,0).end())
File"/usr/lib/python 2.7/json/decoder.py", line384, in raw_decode
raise ValueError ("No JSON object could be decoded")
ValueError: No JSON object could have been decoded
I've looked into the error in my own way, but I'm a python beginner, so I don't know how to solve it.
python
Error indicating obj is not json.
As a result of requests.post, I have json.loads rsp one line at a time, but is rsp a format that requires this process?
print'results:',rsp
may also provide the value of the rsp variable displayed on the screen.
© 2024 OneMinuteCode. All rights reserved.