簡體   English   中英

Python,Keras,Tensorflow

[英]Python, Keras,Tensorflow

我正在嘗試為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()

當我輸入代碼時,錯誤是'dict' object has no attribute 'index'

我認為這與我在類別上的json文件有關。 以下是json文件中的類別。 與它是字典而不是列表有關嗎?

{'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}}

嘿,首先我會避免在目錄中有空格。

DATADIR = (r"C:\Users\ellio\Anaconda Lessons & Quizzes\Competition\Images")

此外,在python中,字典對象的值基於鍵而不是索引,因此它應該是:

class_num=categories.get(category)

要么

class_num=categories[category]

點擊此處查看更多信息: https//docs.python.org/2/tutorial/datastructures.html#dictionaries

在你的情況下(嵌套的json結構),沒有展平你的json你就不會得到你想要的。

你應該首先通過創建一個函數來展平字典:

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

並在json.load行之后添加以下行:

categories = flatten(categories)

最后,您將通過以下方式獲取for循環中的class_num

class_num = categories[category]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM