You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The suggested Python code for Virtual Pets for an image on the internet returns an error using Python3:
TypeError: Object of type bytes is not JSON serializable
Made these changes for string from bytes using str with utf-8 and it works now.
import requests, base64
# Gets the contents of an image on the Internet to be
# sent to the machine learning model for classifying
def getImageUrlData(wwwLocationOfImage):
#base64.b64encode(requests.get(wwwLocationOfImage).content)
byte_data = base64.b64encode(requests.get(wwwLocationOfImage).content)
string_data = str(byte_data, 'utf-8')
return string_data
# This function will pass your image to the machine learning model
# and return the top result with the highest confidence
def classify(imageurl):
key = "REDACTED"
url = "https://machinelearningforkids.co.uk/api/scratch/"+ key + "/classify"
response = requests.post(url, json={ "data" : getImageUrlData(imageurl) })
if response.ok:
responseData = response.json()
topMatch = responseData[0]
return topMatch
else:
response.raise_for_status()
# CHANGE THIS to the URL of the image you want to classify
demo = classify("http://www.cityfood.com/media/resampled/articleElement/216/resampled_CUTIE.jpg")
label = demo["class_name"]
confidence = demo["confidence"]
# CHANGE THIS to do something different with the result
print ("result: '%s' with %d%% confidence" % (label, confidence))
Similar error with the Python3 code for image from webcam:
import cv2, requests, base64
# Gets an image from the webcam
def getWebcamImageData():
cam = cv2.VideoCapture(0)
try:
ok, image = cam.read()
if ok != True:
raise ValueError("Problem using the webcam")
ok, data = cv2.imencode('.jpg', image)
if ok != True:
raise ValueError("Problem getting image data")
#return base64.b64encode(data)
byte_data = base64.b64encode(data)
string_data = str(byte_data, 'utf-8')
return string_data
finally:
cam.release()
# This function will pass your image to the machine learning model
# and return the top result with the highest confidence
def classify():
key = "6e614570-5431-11e9-8a47-c5f6138f27983082d58f-fe8a-4f16-8b69-0e9c1d30c7da"
url = "https://machinelearningforkids.co.uk/api/scratch/"+ key + "/classify"
response = requests.post(url, json={ "data" : getWebcamImageData() })
if response.ok:
responseData = response.json()
topMatch = responseData[0]
return topMatch
else:
response.raise_for_status()
demo = classify()
label = demo["class_name"]
confidence = demo["confidence"]
# CHANGE THIS to do something different with the result
print ("result: '%s' with %d%% confidence" % (label, confidence))
Then for the Python code for using a local file, it gave an error:
AttributeError: 'bytes' object has no attribute 'encode'
So changed it to this:
import requests, base64
#import requests
# 3/31/19 Modified the data encoding to use base64
# Gets the contents of an image file to be sent to the
# machine learning model for classifying
def getImageFileData(locationOfImageFile):
with open(locationOfImageFile, "rb") as f:
data = f.read()
#return data.encode("base64")
byte_version = base64.b64encode(data)
string_version = str(byte_version, 'utf-8')
return string_version
# This function will pass your image to the machine learning model
# and return the top result with the highest confidence
def classify(imagefile):
key = "6e614570-5431-11e9-8a47-c5f6138f27983082d58f-fe8a-4f16-8b69-0e9c1d30c7da"
url = "https://machinelearningforkids.co.uk/api/scratch/"+ key + "/classify"
response = requests.post(url, json={ "data" : getImageFileData(imagefile) })
if response.ok:
responseData = response.json()
topMatch = responseData[0]
return topMatch
else:
response.raise_for_status()
# CHANGE THIS to the name of the image file you want to classify
demo = classify("heart.jpg")
label = demo["class_name"]
confidence = demo["confidence"]
# CHANGE THIS to do something different with the result
print ("result: '%s' with %d%% confidence" % (label, confidence))
The text was updated successfully, but these errors were encountered:
The suggested Python code for Virtual Pets for an image on the internet returns an error using Python3:
TypeError: Object of type bytes is not JSON serializable
Made these changes for string from bytes using str with utf-8 and it works now.
The text was updated successfully, but these errors were encountered: