My code is crashing with the following error:
TypeError: the JSON object must be str, not 'tuple'
I have printed the communicate from ALPR and I receive the following:
(b'plate0: 10 results\n - SBG984\t confidence: 85.7017\n - SBG98\t confidence: 83.3453\n - S8G984\t confidence: 78.3329\n - 5BG984\t confidence: 76.6761\n - S8G98\t confidence: 75.9766\n - SDG984\t confidence: 75.5532\n - 5BG98\t confidence: 74.3198\n - SG984\t confidence: 73.3743\n - SDG98\t confidence: 73.1969\n - BG984\t confidence: 71.7671\n', None)
I wonder how I make the code read this and break it down? I have taken the following code from another example I found online and it works for them so I am not sure what I am doing wrong. I have attached my code below.
# Setting up Pyrebase config below
config = {
}
camera = PiCamera()
global alpr_command_args
def Take_an_Image():
global alpr_command_args
camera.start_preview()
sleep(5)
camera.capture('picture.jpg')
camera.stop_preview()
#alpr subprocess args
alpr_command = "alpr -c gb pictureold.jpg"
alpr_command_args = shlex.split(alpr_command)
read_plate()
def alpr_subprocess():
global alpr_command_args
return subprocess.Popen(alpr_command_args, stdout=subprocess.PIPE)
def alpr_json_results():
alpr_out, alpr_error = alpr_subprocess().communicate()
if not alpr_error is None:
return None, alpr_error
elif b"No license plates found." in alpr_out:
return None, None
try:
return json.loads(alpr_out), None
except (ValueError) as e:
return None, e
def read_plate():
alpr_json, alpr_error = alpr_json_results()
if not alpr_error is None:
print (alpr_error)
return
if alpr_json is None:
print ("No results!")
return
results = alpr_json["results"]
print(results)
ordinal = 0
for result in results:
candidates = result["candidates"]
for candidate in candidates:
if candidate["matches_template"] == 1:
ordinal += 1
print ("PLATE " + candidate["plate"] + candidate["confidence"])
firebase = pyrebase.initialize_app(config)
db = firebase.database()
# Setting initial values to Firebase Database on startup
data = {
"CAMERA": "OFF",
}
# Setting default values on Pi
results = db.update(data)
# This is the handler when Firebase database changes
def stream_handler(message):
path = str(message["path"]) # This is what sensor changes, e.g path returns /LED
status = str(message["data"]) # This is what the sensor says, e.g /LED says "ON"
# Getting global values
if path =="/CAMERA":
if status == "ON":
print("**TAKE PIC**")
data = {
"CAMERA": "OFF",
}
results = db.update(data)
Take_an_Image();
# Starting stream for Firebase
my_stream = db.stream(stream_handler)
UPDATE:
With trying Denis' method I receive the following error:
Exception in thread Thread-1: Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner self.run() File "/usr/lib/python3.5/threading.py", line 862, in run self._target(*self._args, **self._kwargs) File "/home/pi/.local/lib/python3.5/site-packages/pyrebase/pyrebase.py", line 563, in start_stream self.stream_handler(msg_data) File "camera.py", line 96, in stream_handler Take_an_Image(); File "camera.py", line 29, in Take_an_Image read_plate() File "camera.py", line 50, in read_plate alpr_json, alpr_error = alpr_json_results() File "camera.py", line 36, in alpr_json_results elif "No plates found." in alpr_out: TypeError: a bytes-like object is required, not 'str'
UPDATE:
After fixing the byte issue by adding a b before "No license plates found." I am now getting the following error:
Exception in thread Thread-1: Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner self.run() File "/usr/lib/python3.5/threading.py", line 862, in run self._target(*self._args, **self._kwargs) File "/home/pi/.local/lib/python3.5/site-packages/pyrebase/pyrebase.py", line 563, in start_stream self.stream_handler(msg_data) File "camera.py", line 96, in stream_handler Take_an_Image(); File "camera.py", line 29, in Take_an_Image read_plate() File "camera.py", line 52, in read_plate alpr_json, alpr_error = alpr_json_results() File "camera.py", line 46, in alpr_json_results return json.loads(alpr_out), None File "/usr/lib/python3.5/json/init.py", line 312, in loads s.class.name)) TypeError: the JSON object must be str, not 'bytes'