Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My project is a sign language project user interface using Tkinter to predict the gesture on Realtime.

Here is my code:

Python
# Load our Libraries

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns

# Get our Training and Test Data

train = pd.read_csv('train.csv')

test = pd.read_csv('test.csv')

# Inspect our training data

train.head()

# Get our Training labels

labels= train['label'].values

# plot the quantities in each class

plt.figure(figsize =(18,8))

sns.countplot(x=labels)

# Drop Training Labels from our Training data so we can seperate it

train.drop('label',axis=1,inplace=True)

# Extract the image data from each row in our csv

images = train.values

images= np.array([np.reshape(i, (28, 28))for i in images])

images= np.array([i.flatten() for i in images])

# Extract the image data from each rows in our csv

from sklearn.preprocessing import LabelBinarizer

label_binrizer = LabelBinarizer()

labels = label_binrizer.fit_transform(labels)

# view our labels

labels

# Inspect an image

index = 2

print(labels[index])

plt.imshow(images[index].reshape(28,28))

# Use OpenCv to view 10 random images from our training data

import cv2

import numpy as np

for i in range(0,10):

rand = np.random.randint(0, len(images))

input_im = images[rand]

sample = input_im.reshape(28,28).astype(np.uint8)

sample = cv2.resize(sample, None, fx=10, fy=10, interpolation = cv2.INTER_CUBIC)

cv2.imshow("sample image", sample)

cv2.waitKey(0)

cv2.destroyAllWindows()

# split our data into x_train, x_test, y_train and y_test

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(images, labels, test_size = 0.3, random_state = 101)

# start Loading our tensorflow modules and define our batch size etc

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout

batch_size= 128

num_classes= 24

epochs= 10

from tensorflow.keras.layers import Conv2D, MaxPooling2D

from tensorflow.keras import backend as K

from tensorflow.keras.optimizers import Adam

from keras.models import Sequential

from keras.layers import Dense, Activation, Flatten, Dropout

model = Sequential()

model.add(Conv2D(64, kernel_size=(3, 3), activation = 'relu', input_shape= (28, 28, 1) ))

model.add(MaxPooling2D(pool_size= (2, 2)))

model.add(Conv2D(64, kernel_size=(3, 3), activation= 'relu'))

model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, kernel_size=(3, 3), activation= 'relu'))

model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())

model.add(Dense(128, activation= 'relu'))

model.add(Dropout(0.20))

model.add(Dense(num_classes, activation= 'softmax'))

# Scale our images

x_train = x_train / 255

x_test = x_test / 255

#Reshape them into the size required by Tensorflow and Keras

x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)

x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)

plt.imshow(x_train[0].reshape(28,28))

# compile our Model

model.compile(loss = 'categorical_crossentropy',

optimizer = Adam(),

metrics=['accuracy'])

print(model.summary())

# Train our Model

history = model.fit(x_train, y_train, validation_data= (x_test, y_test), epochs=epochs, batch_size=batch_size)

# View our Training history graphically

plt.plot(history.history['accuracy'])

plt.plot(history.history['val_accuracy'])

plt.title("Accuracy")

plt.xlabel('epoch')

plt.ylabel('accuracy')

plt.legend(['train', 'test'])

plt.show()

# Save our Model

model.save("sign_mnist_cnn_50_epochs.hs")

print("Model Saved")

# Reshape our test data so that we can evaluvate it's performance on unseen data

test_labels= test['label']

test.drop('label', axis= 1, inplace= True)

test_images= test.values

test_images= np.array([np.reshape(i, (28, 28)) for i in test_images])

test_images= np.array([i.flatten()for i in test_images])

test_labels= label_binrizer.fit_transform(test_labels)

test_images= test_images.reshape(test_images.shape[0], 28, 28, 1)

test_images.shape

y_pred= model.predict(test_images)

# Get our accuracy score

from sklearn.metrics import accuracy_score

accuracy_score(test_labels, y_pred.round())

#Create function to match label to letter

def getLetter(result):

classLabels= {0: 'A',

1: 'B',

2: 'C',

3: 'D',

4: 'E',

5: 'F',

6: 'G',

7: 'H',

8: 'I',

9: 'K',

10: 'L',

11: 'M',

12: 'N',

13: 'O',

14: 'P',

15: 'Q',

16: 'R',

17: 'S',

18: 'T',

19: 'U',

20: 'V',

21: 'W',

22: 'X',

23: 'Y'}

try:

res= int(result)

return classLabels[res]

except:

return "Error"


What I have tried:

Yes I have tried a lot. I hope sincerely that code project will help me.
Posted
Updated 22-Feb-22 20:59pm
v2

1 solution

Quote:
Yes I have tried a lot. I hope sincerely that code project will help me.

With what?
That code will not work as presented: Python has significant indentation, which means that all code in a block must be indented the same:
Python
j = 0
for i in range(0,5):
j += i
print(i)
print(j)
Will give a very different result (if it worked, which it doesn't) to this:
Python
j = 0
for i in range(0,5):
   j += i
   print(i)
print(j)
And it is impossible to work out from your post what indentation is intended to be where, and thus what the heck you expected that code to actually do.

Let alone any clue as to what it might have done that you didn't expect, given that we have no access at all to your training data files or any ability to run your code!

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

There is nowhere near enough information here for us to begin to help you - we have no idea what you are trying to do, or where you are starting from.

We don't even have any idea where you are stuck, or what help you might need - and that's kinda important, don't you think?

Start here: Asking questions is a skill[^] and think about what you need to know, and what you need to tell us in order to get help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900