@@ -209,6 +209,7 @@ def upload_image(
209209 tag_names : Optional [List [str ]] = None ,
210210 sequence_number : Optional [int ] = None ,
211211 sequence_size : Optional [int ] = None ,
212+ metadata : Optional [Dict ] = None ,
212213 ** kwargs ,
213214):
214215 """
@@ -218,6 +219,8 @@ def upload_image(
218219 image_path (str): path to image you'd like to upload
219220 hosted_image (bool): whether the image is hosted on Roboflow
220221 split (str): the dataset split the image to
222+ metadata (dict, optional): custom key-value metadata to attach to the image.
223+ Example: {"camera_id": "cam001", "location": "warehouse"}
221224 """
222225
223226 coalesced_batch_name = batch_name or DEFAULT_BATCH_NAME
@@ -232,13 +235,14 @@ def upload_image(
232235 upload_url = _local_upload_url (
233236 api_key , project_url , coalesced_batch_name , tag_names , sequence_number , sequence_size , kwargs
234237 )
235- m = MultipartEncoder (
236- fields = {
237- "name" : image_name ,
238- "split" : split ,
239- "file" : ("imageToUpload" , imgjpeg , "image/jpeg" ),
240- }
241- )
238+ fields = {
239+ "name" : image_name ,
240+ "split" : split ,
241+ "file" : ("imageToUpload" , imgjpeg , "image/jpeg" ),
242+ }
243+ if metadata is not None :
244+ fields ["metadata" ] = json .dumps (metadata )
245+ m = MultipartEncoder (fields = fields )
242246
243247 try :
244248 response = requests .post (upload_url , data = m , headers = {"Content-Type" : m .content_type }, timeout = (300 , 300 ))
@@ -247,7 +251,12 @@ def upload_image(
247251
248252 else :
249253 # Hosted image upload url
250- upload_url = _hosted_upload_url (api_key , project_url , image_path , split , coalesced_batch_name , tag_names )
254+ hosted_kwargs = dict (kwargs )
255+ if metadata is not None :
256+ hosted_kwargs ["metadata" ] = json .dumps (metadata )
257+ upload_url = _hosted_upload_url (
258+ api_key , project_url , image_path , split , coalesced_batch_name , tag_names , hosted_kwargs
259+ )
251260
252261 try :
253262 # Get response
@@ -363,7 +372,8 @@ def _upload_url(api_key, project_url, **kwargs):
363372 return url
364373
365374
366- def _hosted_upload_url (api_key , project_url , image_path , split , batch_name , tag_names ):
375+ def _hosted_upload_url (api_key , project_url , image_path , split , batch_name , tag_names , kwargs = None ):
376+ extra = kwargs or {}
367377 return _upload_url (
368378 api_key ,
369379 project_url ,
@@ -372,6 +382,7 @@ def _hosted_upload_url(api_key, project_url, image_path, split, batch_name, tag_
372382 image = image_path ,
373383 batch = batch_name ,
374384 tag = tag_names ,
385+ ** extra ,
375386 )
376387
377388
0 commit comments