【发布时间】:2020-10-21 16:23:16
【问题描述】:
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
print(tf.__version__)
DATADIR ="D:\Datasetes\legen"
CATEGORIES = ['light']
for category in CATEGORIES:
path = os.path.join(DATADIR,category) #to create a path to the image in our drive
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
plt.figure()
plt.imshow(img_array)
plt.colorbar()
plt.grid(False)
plt.show()
break
break
class_names = ['light', 'power', 'capture']
train_images.shape
len(train_labels)
train_labels
train_images = train_images / 255.0
train_labels = train_labels / 255.0
test_images = test_images / 255.0
test_labels = test_labels / 255.0
def class_names():
for category in CATEGORIES:
path = os.path.join(DATADIR,category)
class_names = CATEGORIES.index(category)
for img in (os.listdir(path)):
try:
img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE) # convert to array
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) # resize to normalize data size
training_data.append([new_array, class_num])
except Exception as e:
pass
import random
random.shuffle(training_data)
img_array = cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
plt.figure(figsize=(10,10))
for i in range(10):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(img_array, cmap='gray')
plt.xlabel(class_names(train_labels))
plt.show()
break
TypeError Traceback (most recent call last)
<ipython-input-147-400073625620> in <module>
29 plt.grid(False)
30 plt.imshow(img_array, cmap='gray')
---> 31 plt.xlabel(class_names(train_labels))
32 plt.show()
33 break
TypeError: class_names() 接受 0 个位置参数,但给出了 1 个
这是我正在使用的代码,它不断给出错误,因为“函数”对象不可下标。我已将整个代码添加到其中。请帮忙,因为这是一个学校项目,它包含在我该死的 GPA 中,错误发生在代码的最后一部分
【问题讨论】:
-
人们需要更多信息才能提供帮助。错误发生在哪一行?请包含错误消息。
-
嘿,伙计,欢迎来到 StackOverFlow,我是新手,所以发布有关您的标题和内容的有用信息会有所帮助,如果每个人都使用您的标题,那不会让生活变得更糟它?尽量让你的标题概括问题,感谢
-
请阅读以下文章,了解如何提出一个好问题:stackoverflow.com/help/how-to-ask
-
为什么这个标签是 tensorflow2.0?
-
难怪。该行使用
train_labels,它没有在任何地方定义。您的代码中还有其他未定义的内容。请包含您的其余代码,或生成一个较小的示例来演示该问题。
标签: python