Uploading Images#
To train our machine learning model, we'll need a well-structured dataset. We’ve created this dataset for this tutorial: cereal.
Because we’re using a Classification project, we can assign classes to images during the upload process based on the folders the images are in. We’ve already organized our images into two folders: clean and contaminated.
At upload, all images in the clean folder will be assigned to the clean
class, and all images in the contaminated folder will be assigned to the contaminated
class.

Sample Code: Upload Images#
The following code snippet demonstrates how to upload images to LandingLens. As a reminder, your project_id
was included in the API response when you created a project.
import os
import requests
# Replace "YOUR_API_KEY" and "YOUR_PROJECT_ID" with the actual values
api_key = "YOUR_API_KEY"
project_id = "YOUR_PROJECT_ID"
# Define the image upload endpoint
url = f"https://api.landing.ai/v1/projects/{project_id}/images"
# Set the authorization header
headers = {"apikey": api_key}
# Iterate through the clean and contaminated folders
for folder in ["clean", "contaminated"]:
folder_path = os.path.join("cereal", folder)
images = [
image_file
for image_file in os.listdir(folder_path)
if image_file.lower().endswith(".jpg") or image_file.lower().endswith(".jpeg")
] # Filter for JPEG extensions
for image_file in images:
image_path = os.path.join(folder_path, image_file)
# Prepare request data
with open(image_path, "rb") as image_data:
files = {
"file": (
image_file,
image_data.read(),
)
}
data = {
"name": image_file, # Required: File name from the loop
"label": folder, # Optional label based on folder
}
# Send the POST request to upload the image
response = requests.post(url, headers=headers, files=files, data=data)
# Check for successful upload and handle any errors
if response.status_code == 201:
print(f"Image {image_path} uploaded successfully!")
else:
print(f"Error uploading image {image_path}: {response.text}")