Stack Exchange
Python, Keras,Tensorflow
I'm trying to load in data for a school project on Tensorflow/Keras.
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
import json

DATADIR = (r"C:\Users\ellio\Anaconda Lessons & Quizzes\Competition\Images")
with open('categories.json') as json_file:  
    categories = json.load(json_file)

for category in categories:
    path=os.path.join(DATADIR,category)
    for img in os.listdir(path):
        img_array=cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
        plt.imshow(img_array,cmap='gray')
        plt.show()
        break
break

training_data = []

def create_training_data(): 
    for category in categories:
        path=os.path.join(DATADIR,category)
        class_num=categories.index(category)
        for img in os.listdir(path):
            try:
                img_array=cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
                new_array=cv2.resize(img_array,(IMG_SIZE,IMG_SIZE))
                training_data.append([new_array,class_num])
            except Exception as e:
                pass

create_training_data()

When I enter the code, the error is 'dict' object has no attribute 'index' 
I reckon this has something to do with my json file on the categories. Below are are the categories from the json file. Is something to do with it being a dictionary instead of a list?
{'Mobile': {'Others Mobile & Tablet': 35, 'Smartfren': 53, 'Infinix': 40, 'Brandcode': 39, 'Icherry': 52, 'Advan': 45, 'Iphone': 31, 'Realme': 51, 'Motorola': 49, 'Maxtron': 56, 'Nokia': 38, 'Xiaomi': 34, 'Mito': 46, 'Sony': 33, 'SPC': 57, 'Lenovo': 37, 'Alcatel': 55, 'Samsung': 32, 'Vivo': 42, 'Evercoss': 44, 'Strawberry': 50, 'Blackberry': 36, 'Asus': 43, 'Honor': 54, 'Oppo': 41, 'Huawei': 47, 'Sharp': 48}, 'Fashion': {'Wedding Dress': 23, 'Shirt': 27, 'Casual Dress': 18, 'Maxi Dress': 20, 'Big Size Dress': 24, 'Bodycon Dress': 22, 'Party Dress': 19, 'Blouse\xa0': 26, 'Tshirt': 25, 'Crop Top ': 29, 'Tanktop': 28, 'Others': 17, 'A Line Dress': 21, 'Big Size Top': 30}, 'Beauty': {'Foundation': 1, 'Face Palette': 0, 'Concealer': 7, 'Lip Gloss': 14, 'Blush On': 2, 'Highlighter': 8, 'BB & CC Cream': 5, 'Other Face Cosmetics': 4, 'Lip Tint': 13, 'Bronzer': 11, 'Lip Liner': 15, 'Powder': 3, 'Setting Spray': 10, 'Primer': 9, 'Contour': 6, 'Other Lip Cosmetics': 16, 'Lipstick': 12}}


Have you tried changing "index" to "get" ?

In your case (nested json structure), without flattening your json you would not get what you want.
You should flatten dictionary at first by creating a function:
def flatten(d):
    if not isinstance(d, dict):
        return d
    flattened = dict()
    for k in d:
        if isinstance(d[k], dict):
            flattened = {**flattened, **flatten(d[k])}
        else:
            flattened[k] = d[k]
    return flattened

And add this line after your json.load line:
categories = flatten(categories)

Finally you will get your class_num in your for loop by:
class_num = categories[category]


Hey first of all I would avoid having spaces in directories.
DATADIR = (r"C:\Users\ellio\Anaconda Lessons & Quizzes\Competition\Images")

Also, in python, the value of a dictionary object is based off a key rather than an index therefore it should be:
class_num=categories.get(category)

or
class_num=categories[category]

check here for more: https://docs.python.org/2/tutorial/datastructures.html#dictionaries
