# ---
# 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/survivingwithandroid/EdeImpulse-fruit-recognition-ESP32-CAM/blob/main/EdgeImpulse_Fruit_Recogition.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="AlUIBNeC353j"
# # Download the Fruit dataset from Kaggle

# + id="TZ9RhF955QdM"
# !pip install -q kaggle
import os
os.environ['KAGGLE_USERNAME']='username'
os.environ['KAGGLE_KEY']='kaggle_key'

# Flower dataset
# !kaggle datasets download -d alxmamaev/flowers-recognition
# !unzip flowers-recognition.zip

# Fruit dataset
# !kaggle datasets download -d moltean/fruits
# !unzip fruits.zip



# + [markdown] id="tkXeHow_8hEv"
# # Define labels and number of samples

# + id="Y089GTy_8gjY"
# labels
labels=['sunflower', 'dandelion', 'tulip']
samples=700

labels_unknown=['Walnut', 'Blueberry', 'Watermelon']

# path
base_path = './flowers'

# path unknown
base_unknown_path = './fruits-360/Training'

# output dir
out_path = './edge_dataset'

# + [markdown] id="6LwNAiJs_CfR"
# # Generate the dataset

# + colab={"base_uri": "https://localhost:8080/"} id="3J0qiTAi_IYI" outputId="03905809-0641-4651-cf11-bc9b77160018"
import random
import shutil

# Generate directory structure
if not os.path.exists(out_path):
  print ("Create dir " + out_path)
  os.mkdir(out_path)

for label in labels:
  dest = out_path + '/' + label.replace(" ", "_")
  if not os.path.exists(dest):
    print("Create dest dir ", dest)
    os.mkdir(dest)

# random
random.seed();
for label in labels:
  print("Selected word ["+label+"]")
  file_list = []
  for filename in os.listdir(base_path + '/' + label):
   # print("Filename: ", filename)
   file_list.append(filename)
  
  random.shuffle(file_list)

  for i in range(samples):
    src = base_path + '/' + label + '/' + file_list[i]
    dest = out_path + '/' + label.replace(" ", "_") + '/' + label + str(i) + '.jpg'
    # print ("Copy from " + src + " to " + dest)
    shutil.copyfile(src, dest)
  # count files
  print("Image numbers ", len(os.listdir(out_path + '/' + label.replace(" ", "_"))))

# + [markdown] id="VVQs-wJWzgPx"
# # Generate unknow label dataset

# + colab={"base_uri": "https://localhost:8080/"} id="dsp2pCR-zq6s" outputId="838a6d7d-aec2-463b-ad65-496402e17ca0"
samples_per_ul = samples // len(labels_unknown)
print("Sample per unknown image per label ", samples_per_ul)
dest_unknown_path = out_path + '/unknown/';

# Generate directory structure
if not os.path.exists(dest_unknown_path):
  print ("Create dir " + dest_unknown_path)
  os.mkdir(dest_unknown_path)

for ul in labels_unknown:
  print("Selected word ["+ul+"]")
  file_list = []
  for filename in os.listdir(base_unknown_path + '/' + ul):
   # print("Filename: ", filename)
   file_list.append(filename)
  
  print("File list size per " + ul + ", ", len(file_list))
  random.shuffle(file_list)
  
  for i in range(0, samples_per_ul):
    src = base_unknown_path + '/' + ul + '/' + file_list[i]
    dest = dest_unknown_path + '/' + ul + str(i) + '.jpg'
    shutil.copyfile(src, dest)

print("Image numbers ", len(os.listdir(out_path + '/unknown')))


# + [markdown] id="mVfHzl4KFIjT"
# # Edge Impluse Dataset Upload

# + [markdown] id="dAjc4gKlGXON"
# ## Install Edge Impulse CLI

# + id="UHDkRAAuFO4-"
# !npm install -g --unsafe-perm edge-impulse-cli

# + [markdown] id="8ngR91wIGi5M"
# ## Upload dataset

# + id="3xIgp3G8Goq4"
# API Key
api_key = '<KEY>'
labels.append('unknown')
for label in labels:
  label = label.replace(" ", "_")
  sample_dir = out_path + '/' + label + '/*.jpg'
  file_list = []
  for filename in os.listdir( out_path + '/' + label + '/'):
    _, ext = os.path.splitext(filename)
    if (ext.lower() == '.jpg'):
     file_list.append(filename)
 
  print("Uploading files from ", sample_dir)
  print("N. Samples ", len(file_list))

  cmd = 'edge-impulse-uploader --api-key ' + api_key + ' --label ' + label + ' ' + sample_dir
  os.system(cmd)

print("Done!");

# + [markdown] id="CC_Yvdp2Egl3"
# # Delete all dataset

# + id="qEtpfC7AEl8Z"
# !rm -r ./edge_dataset

# + id="R3BLzbHkErrh"
# !rm -rf ./fruits-360/

StarcoderdataJupyterScriptsDedupFiltered