33import json
44import os
55import urllib
6+ import datetime
67import warnings
78import cv2
89import requests
1213
1314#version class that should return
1415class Project ():
15- def __init__ (self , api_key , dataset_slug , type , workspace ):
16- self .api_key = api_key
17- self .name = dataset_slug
18- self .category = type
19- self .workspace = workspace
20- self .all_versions = []
16+ def __init__ (self , api_key , a_project ):
17+ self .__api_key = api_key
18+ self .annotation = a_project ['annotation' ]
19+ self .classes = a_project ['classes' ]
20+ self .colors = a_project ['colors' ]
21+ self .created = datetime .datetime .fromtimestamp (a_project ['created' ])
22+ self .id = a_project ['id' ]
23+ self .images = a_project ['images' ]
24+ self .name = a_project ['name' ]
25+ self .public = a_project ['public' ]
26+ self .splits = a_project ['splits' ]
27+ self .type = a_project ['type' ]
28+ self .unannotated = a_project ['unannotated' ]
29+ self .updated = datetime .datetime .fromtimestamp (a_project ['updated' ])
30+
31+ temp = self .id .rsplit ("/" )
32+ self .__workspace = temp [0 ]
33+ self .__project_name = temp [1 ]
2134
2235 def get_version_information (self ):
23-
24- slug_splitted = self .name .rsplit ("/" )
25- p , w = slug_splitted [0 ], slug_splitted [1 ]
26-
27- dataset_info = requests .get (API_URL + "/" + p + "/" + w + "?api_key=" + self .api_key )
36+ dataset_info = requests .get (API_URL + "/" + self .__workspace + "/" + self .__project_name + "?api_key=" + self .__api_key )
2837
2938 # Throw error if dataset isn't valid/user doesn't have permissions to access the dataset
3039 if dataset_info .status_code != 200 :
3140 raise RuntimeError (dataset_info .text )
3241
33- dataset_info = dataset_info .json ()[ 'project' ]
42+ dataset_info = dataset_info .json ()
3443 return dataset_info ['versions' ]
3544
3645 def list_versions (self ):
@@ -41,9 +50,8 @@ def versions(self):
4150 version_info = self .get_version_information ()
4251 version_array = []
4352 for a_version in version_info :
44- version_object = Version ((self .category if 'model' in a_version else None ), self .api_key , self .name , a_version ['id' ], local = False )
53+ version_object = Version (a_version , (self .type if 'model' in a_version else None ), self .__api_key , self .name , a_version ['id' ], local = False )
4554 version_array .append (version_object )
46-
4755 return version_array
4856
4957 def version (self , version_number ):
@@ -53,8 +61,8 @@ def version(self, version_number):
5361 for version_object in version_info :
5462
5563 current_version_num = os .path .basename (version_object ['id' ])
56- if current_version_num == version_number :
57- vers = Version (self .category , self .api_key , self .name , current_version_num , local = False )
64+ if current_version_num == str ( version_number ) :
65+ vers = Version (version_object , self .type , self .__api_key , self .name , current_version_num , local = False )
5866 return vers
5967
6068 raise RuntimeError ("Version number {} is not found." .format (version_number ))
@@ -63,12 +71,15 @@ def __image_upload(self, image_path, hosted_image=False, split="train"):
6371
6472 # If image is not a hosted image
6573 if not hosted_image :
66- project_name = os .path .basename (self .name )
74+
75+ project_name = self .id .rsplit ("/" )[1 ]
6776 image_name = os .path .basename (image_path )
77+
6878 # Construct URL for local image upload
79+
6980 self .image_upload_url = "" .join ([
7081 "https://api.roboflow.com/dataset/" , project_name , "/upload" ,
71- "?api_key=" , self .api_key ,
82+ "?api_key=" , self .__api_key ,
7283 "&name=" + image_name ,
7384 "&split=" + split
7485 ])
@@ -93,9 +104,11 @@ def __image_upload(self, image_path, hosted_image=False, split="train"):
93104
94105 else :
95106 # Hosted image upload url
107+ project_name = self .id .rsplit ("/" )[1 ]
108+
96109 upload_url = "" .join ([
97- "https://api.roboflow.com/dataset/" + self .name + "/upload" ,
98- "?api_key=" + self .api_key ,
110+ "https://api.roboflow.com/dataset/" + self .project_name + "/upload" ,
111+ "?api_key=" + self .__api_key ,
99112 "&name=" + os .path .basename (image_path ),
100113 "&split=" + split ,
101114 "&image=" + urllib .parse .quote_plus (image_path )
@@ -112,7 +125,7 @@ def __annotation_upload(self, annotation_path, image_id):
112125 # Set annotation upload url
113126 self .annotation_upload_url = "" .join ([
114127 "https://api.roboflow.com/dataset/" , self .name , "/annotate/" , image_id ,
115- "?api_key=" , self .api_key ,
128+ "?api_key=" , self .__api_key ,
116129 "&name=" + os .path .basename (annotation_path )
117130 ])
118131 # Get annotation response
@@ -128,7 +141,6 @@ def upload(self, image_path=None, annotation_path=None, hosted_image=False, imag
128141 if image_path is not None :
129142 # Upload Image Response
130143 response = self .__image_upload (image_path , hosted_image = hosted_image , split = split )
131-
132144 # Get JSON response values
133145 try :
134146 success , image_id = response .json ()['success' ], response .json ()['id' ]
@@ -166,9 +178,9 @@ def upload(self, image_path=None, annotation_path=None, hosted_image=False, imag
166178 def __str__ (self ):
167179 # String representation of project
168180 json_str = {
169- "dataset_slug " : self .name ,
170- "task_type " : self .category ,
171- "workspace" : self .workspace
181+ "name " : self .name ,
182+ "type " : self .type ,
183+ "workspace" : self .__workspace ,
172184 }
173185
174186 return json .dumps (json_str , indent = 2 )
0 commit comments