1. Home
  2. Docs
  3. PixelSnapAI Documentation
  4. Example Code
  5. Python Request

Python Request

PixelSnapAI API Python Example

This documentation provides an example code snippet in Python for using the PixelSnapAI API to upload and analyze an image. Follow the steps below to run the code successfully.

Prerequisites

Before running the code, ensure that you have the following:

  • Python installed on your system.
  • The requests library installed. If it is not installed, you can install it using pip install requests.

Code Example

import requests

url = "URL_END_POINT"

payload = {}
files = [
    ('picture', ('prouct_img.jpeg', open('./prouct_img.jpeg', 'rb'), 'image/jpeg'))
]
headers = {
    'Authorization': 'Token [SECRET_KEY]'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

# Check if the request was successful (HTTP status code 200)
if response.status_code == 200:
    # Parse the response JSON
    response_json = response.json()
    # Extract the relevant information
    message = response_json['message']
    product_names = response_json['product_name']
    # Print the information
    print("Message:", message)
    print("Product Names:", product_names)
else:
    # Print the error message if the request was not successful
    print("Error:", response.text)

Running the Code

Follow these steps to run the provided code:

  1. Open a text editor or an integrated development environment (IDE) of your choice.
  2. Copy and paste the code into a new Python file.
  3. Make sure you have the image file product_img.jpeg available at the specified path ./product_img.jpeg. If your image file is located in a different directory, modify the file path accordingly.
  4. Replace the placeholder value 'Token [SECRET_KEY]' in the Authorization header with your actual PixelSnapAI API token. This token can be obtained from your PixelSnapAI account after subscribing to a plan.
  5. Save the file with a .py extension, for example, pixelsnapai_example.py.
  6. Open a terminal or command prompt and navigate to the directory where the Python file is saved.
  7. Run the Python script by executing the command: python pixelsnapai_example.py.
  8. The code will send a POST request to the PixelSnapAI API, uploading the image and returning the response.
  9. The response will be printed to the console, displaying the analysis results or any error messages received from the API.

Return Example:

When the API request is successful, the code will receive a JSON response similar to the following:

{
    "message": "Picture uploaded successfully.",
    "product_name": [
        "Dualshock 4",
        "PS4 DualShock 4",
        "Pro Wireless GamePad",
        "Nuc Controller Plus"
    ]
}

The returned JSON includes a "message" field indicating the success status and a "product_name" field containing a list of product names extracted from the uploaded picture.

Make sure to adjust the code as needed to handle the response according to your application’s requirements.

Conclusion

You have successfully run the Python example code for utilizing the PixelSnapAI API to upload and analyze an image. This code demonstrates the process of making a POST request to the API endpoint and handling the response.

For further information on the available API endpoints, request parameters, and response formats, please refer to the official PixelSnapAI API documentation on the PixelSnapAI website.

If you encounter any issues or have additional questions, please contact our support team at info@pixelsnapai.com. We are here to assist you!

Was this article helpful to you? Yes No

How can we help?