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

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/freefrit/pytorch-metric-learning/blob/master/SOP_training/create_json_new.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="TwOfeBUd1f2M"
# ## This code is to create the .json file which content is the dict in shape:
#
# {
# 'image': filenames,
#
# 'label': annotations(num1~12)
# }

# + colab={"base_uri": "https://localhost:8080/"} id="HzNCzrYJx1df" outputId="92b8925c-a6a6-426b-a9a5-7b0931bdd73a"
from google.colab import drive
import os
import json
drive.mount('/content/gdrive')
root = '/content/gdrive/My Drive/Stanford_Online_Products'

# + colab={"base_uri": "https://localhost:8080/"} id="8NIfjQJn9-2b" outputId="80f9c9c5-8cba-412e-98eb-c09f24abecde"
# %cd /content/gdrive/My Drive/Stanford_Online_Products
# !ls

# + id="IpXkQp68yK50"
anno_loc = './annotations/'
if os.path.exists(anno_loc) == False:
  os.mkdir(anno_loc)

train_filenames = []
train_annotations = []

test_filenames = []
test_annotations= []

# + colab={"base_uri": "https://localhost:8080/"} id="Ol229vxoygsG" outputId="5d7eaa15-c331-40a6-d563-bd9b7ac94232"
# super_class = [
#       "bicycle", "cabinet", "chair", "coffee_maker", "fan", "kettle", "lamp",
#       "mug", "sofa", "stapler", "table", "toaster"
#   ]

train_path = root + '/Info_Files/Ebay_train.txt'
test_path = root + '/Info_Files/Ebay_test.txt'

print('creat train_img annotations')
f1 = open(train_path, 'r')
f1.readline()
for i in range(0, 59551):
  line = f1.readline()
  s = line.split(' ')
  print(s)
  train_annotations.append(int(s[2]))
  train_filenames.append(s[3].strip('\n'))
f1.close()
print('for training set: ' + str(len(train_filenames)) + ' datas wrote.')

print('creat test_img annotations')
f2 = open(test_path, 'r')
f2.readline()
for i in range(59552, 120054):
  line = f2.readline()
  s = line.split(' ')
  print(s)
  test_annotations.append(int(s[2]))
  test_filenames.append(s[3].strip('\n'))
f2.close()
print('for testing set: ' + str(len(test_filenames)) + ' datas wrote.')  

# + colab={"base_uri": "https://localhost:8080/"} id="_fclXS1wSPg1" outputId="3c0b130a-6905-436a-b995-22fb1f1d65a5"
train_filenames

# + colab={"base_uri": "https://localhost:8080/"} id="wXulyjpK7Dh8" outputId="2598a6a3-95c9-45ac-db22-03286ddc2e82"
train_annot_dict = {
        'image': train_filenames,
        'label': train_annotations
    }
test_annot_dict = {
        'image': test_filenames,
        'label': test_annotations
    }

# %cd /content/gdrive/My Drive/Stanford_Online_Products
train_json = json.dumps(train_annot_dict)
f = open('./annotations/OnlineProducts_train_new.json', 'w')
f.write(train_json)
f.close()

test_json = json.dumps(test_annot_dict)
f = open('./annotations/OnlineProducts_test_new.json', 'w')
f.write(test_json)
f.close()

print('annotations have writen to json file')

StarcoderdataJupyterScriptsDedupFiltered