# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

from urllib.request import Request, urlretrieve
import requests
import cv2
import numpy as np
import os
import urllib
import sys
from random import randint
import pandas as pd
import socket
socket.setdefaulttimeout(30)


# +
# Define function to download a set number of images associated with given wnid
def download_wnid(wnid, no_images):
    url = 'http://www.image-net.org/api/text/imagenet.synset.geturls?wnid=' + wnid
    request = urllib.request.Request(url)
    response = urllib.request.urlopen(request)
    urls = response.read().decode('utf-8').split('\n')
    selections = [randint(0, len(urls)-1) for i in range(no_images)]

    if os.path.exists('images/'+wnid) == False:
        os.makedirs('images/'+wnid)

    pic_num = 1
    for s in selections:
        try:
            print(urls[s])
            urlretrieve(urls[s], "images/"+wnid+"/"+str(pic_num)+".jpg")
            pic_num += 1

        except Exception as e:
            print(str(e))  
            
## Define a function to create a randomly selected list of synsets
def syn_list(syn_no):
    syns = pd.read_csv('synset_wnid.csv', header=None)
    syns_list = [randint(0, len(syns)-1) for i in range(syn_no)]
    return syns.loc[syns_list, 0]

## Find name of 
def return_name(wnid):
    name = 'http://www.image-net.org/api/text/wordnet.synset.getwords?wnid='+wnid
    eng_name = requests.get(name).content.decode("utf-8").strip('\n')
    return eng_name


# +
id_nos = [ 'n03345487', 'n03032252', 'n02509815', 'n07747607']
## Pull a number of images of number of synsets (randomly selected) ##

image_no = 1000

for i in id_nos:
    print(return_name(i),": ", i)
    download_wnid(i, image_no)

# +
## Pull a number of images of number of synsets (randomly selected) ##

syn_no = 10
image_no = 1000

for i in syn_list(syn_no):
    print(return_name(i),": ", i)
    download_wnid(i, image_no)

StarcoderdataJupyterScriptsDedupFiltered