102 lines
3.6 KiB
Python
102 lines
3.6 KiB
Python
import os
|
|
from django import forms
|
|
from .models import *
|
|
from keras.models import load_model
|
|
from keras import backend as keras_backend
|
|
import numpy as np
|
|
from keras.preprocessing import image
|
|
|
|
|
|
class SignForm(forms.ModelForm):
|
|
"""Form with one Image upload field,
|
|
acquiring the image and predicting the letter."""
|
|
|
|
class Meta:
|
|
# assign model to the form
|
|
model = Sign
|
|
fields = ['gesture']
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super(SignForm, self).__init__(*args, **kwargs)
|
|
# disable labels and validation errors for input fields.
|
|
self.fields['gesture'].label = ""
|
|
self.fields['gesture'].error_messages = {
|
|
'blank': 'INVALID!!11', 'null': 'NULL11!', 'required': ''}
|
|
|
|
def sav(self):
|
|
# get the image uploaded from the form.
|
|
uploaded_image = super(SignForm, self).save()
|
|
image_name = uploaded_image.gesture.name
|
|
keras_backend.clear_session()
|
|
|
|
classifier = load_model('Trained_model.h5')
|
|
|
|
# Prediction of single image
|
|
loaded_image = image.load_img(
|
|
f'./media/{image_name}', target_size=(64, 64))
|
|
img_array = image.img_to_array(loaded_image)
|
|
img_dims = np.expand_dims(img_array, axis=0)
|
|
classifier_result = classifier.predict(img_dims)
|
|
predicted_char = ''
|
|
|
|
#map to the character in the alphabet.
|
|
if classifier_result[0][0] == 1:
|
|
predicted_char = 'A'
|
|
elif classifier_result[0][1] == 1:
|
|
predicted_char = 'B'
|
|
elif classifier_result[0][2] == 1:
|
|
predicted_char = 'C'
|
|
elif classifier_result[0][3] == 1:
|
|
predicted_char = 'D'
|
|
elif classifier_result[0][4] == 1:
|
|
predicted_char = 'E'
|
|
elif classifier_result[0][5] == 1:
|
|
predicted_char = 'F'
|
|
elif classifier_result[0][6] == 1:
|
|
predicted_char = 'G'
|
|
elif classifier_result[0][7] == 1:
|
|
predicted_char = 'H'
|
|
elif classifier_result[0][8] == 1:
|
|
predicted_char = 'I'
|
|
elif classifier_result[0][9] == 1:
|
|
predicted_char = 'J'
|
|
elif classifier_result[0][10] == 1:
|
|
predicted_char = 'K'
|
|
elif classifier_result[0][11] == 1:
|
|
predicted_char = 'L'
|
|
elif classifier_result[0][12] == 1:
|
|
predicted_char = 'M'
|
|
elif classifier_result[0][13] == 1:
|
|
predicted_char = 'N'
|
|
elif classifier_result[0][14] == 1:
|
|
predicted_char = 'O'
|
|
elif classifier_result[0][15] == 1:
|
|
predicted_char = 'P'
|
|
elif classifier_result[0][16] == 1:
|
|
predicted_char = 'Q'
|
|
elif classifier_result[0][17] == 1:
|
|
predicted_char = 'R'
|
|
elif classifier_result[0][18] == 1:
|
|
predicted_char = 'S'
|
|
elif classifier_result[0][19] == 1:
|
|
predicted_char = 'T'
|
|
elif classifier_result[0][20] == 1:
|
|
predicted_char = 'U'
|
|
elif classifier_result[0][21] == 1:
|
|
predicted_char = 'V'
|
|
elif classifier_result[0][22] == 1:
|
|
predicted_char = 'W'
|
|
elif classifier_result[0][23] == 1:
|
|
predicted_char = 'X'
|
|
elif classifier_result[0][24] == 1:
|
|
predicted_char = 'Y'
|
|
elif classifier_result[0][25] == 1:
|
|
predicted_char = 'Z'
|
|
keras_backend.clear_session()
|
|
|
|
# remove image after prediction
|
|
try:
|
|
os.remove(f'./media/{image_name}')
|
|
except:
|
|
print("unable to remove image")
|
|
return predicted_char
|