Skip to content

Commit e4e764f

Browse files
authored
Merge pull request #57 from fastlabel/hotfix/add-example
fix readme
2 parents 24c3357 + 1d5a10f commit e4e764f

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

examples/donwload_images.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import fastlabel
2+
import os
3+
import time
4+
import urllib.request
5+
from multiprocessing import Pool, cpu_count
6+
7+
8+
client = fastlabel.Client()
9+
IMAGE_DIR = "images"
10+
PROJECT_SLUG = "YOUR_PROJECT_SLUG"
11+
12+
def get_all_tasks() -> list:
13+
# Iterate pages until new tasks are empty.
14+
all_tasks = []
15+
offset = None
16+
while True:
17+
time.sleep(1)
18+
19+
tasks = client.get_image_classification_tasks(
20+
project=PROJECT_SLUG,
21+
limit=1000,
22+
offset=offset
23+
)
24+
all_tasks.extend(tasks)
25+
26+
if len(tasks) > 0:
27+
offset = len(all_tasks) # Set the offset
28+
else:
29+
break
30+
31+
return all_tasks
32+
33+
def download_image(task: dict):
34+
urllib.request.urlretrieve(task["url"], os.path.join(IMAGE_DIR, task["name"]))
35+
36+
if __name__ == '__main__':
37+
38+
os.makedirs(IMAGE_DIR, exist_ok=True)
39+
40+
tasks = get_all_tasks()
41+
with Pool(cpu_count()) as p:
42+
p.map(download_image, tasks)

0 commit comments

Comments
 (0)