Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Virtual Pets ->Make -> Python #180

Closed
centralparksteam opened this issue Apr 1, 2019 · 0 comments
Closed

Virtual Pets ->Make -> Python #180

centralparksteam opened this issue Apr 1, 2019 · 0 comments

Comments

@centralparksteam
Copy link

centralparksteam commented Apr 1, 2019

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))
@dalelane dalelane transferred this issue from IBM/taxinomitis-docs Apr 3, 2019
@dalelane dalelane closed this as completed Nov 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants