Uploading images with XML labels
Hi everyone,
I am trying to upload images along with XML labels using lending lens API. but i am getting error.
Error: {"message":"Filename should not be empty"}
Please find the code base below. Please help what i am doing wrong.
import requests
import os
from tqdm import tqdm
from collections import defaultdict
API_KEY = "placeholder"
LAI_BASE_URL = "https://api.landing.ai/v1/"
# Set the authorization header with your API key
headers = {"apikey": API_KEY}
projects_url = LAI_BASE_URL + "projects"
project_id = "placeholder"
ALLOWED_FILE_FORMATS = [".png", ".jpeg", ".jpg", ".xml", ".bmp"]
def find_similarly_named_files(directory: str):
similar_files = defaultdict(list)
for root, dirs, files in os.walk(directory):
for file in files:
# Exclude the file extension
file_name, ext = os.path.splitext(file)
if ext in ALLOWED_FILE_FORMATS:
similar_files[file_name].append(os.path.join(root, file))
return similar_files
def post_request(url, data, headers, files):
return requests.post(url, headers=headers, files=files, data=data)
# Define the image upload endpoint
images_url = f"{projects_url}/{project_id}/images"
cwd = os.getcwd()
my_data_folder = "test_data"
path = os.path.join(cwd, my_data_folder)
error_count = 0
duplicated_count = 0
medias = []
CONCURRENCY_LIMIT = 16
# thread_pool = ThreadPoolExecutor(max_workers=CONCURRENCY_LIMIT)
# tasks = []
labeled_files = find_similarly_named_files(path)
labeled_file_pairs = list(labeled_files.values())
num_labeled = len(labeled_file_pairs)
# Iterate through the folders
for pair in tqdm(labeled_file_pairs, total=num_labeled):
# print(pair)
image_path = pair[1]
image_name = image_path.split("/")[-1]
files=[
('file',(image_name,open(image_path,'rb'),'image/jpeg')),
]
if len(pair) > 1:
label_path = pair[0]
label_name = label_path.split("/")[-1]
# print(label_path)
files.append(('label',(label_name,open(label_path,'rb'),'application/xml')))
print(files)
# Prepare request data
data = {
"name": "test" # Required: File name from the loop
}
print(data)
print(files)
print(headers)
res = post_request(
images_url,
data,
headers,
files)
print(res.text)