AkashKhatri commited on
Commit
bcbc229
·
1 Parent(s): 2087b2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -165
app.py CHANGED
@@ -1,169 +1,14 @@
1
- import streamlit as st
2
- import numpy as np
3
- from keras.models import load_model
4
- import cv2
5
- from io import BytesIO
6
- import mediapipe as mp
7
- import tensorflow as tf
8
-
9
- # Load the model
10
- import os
11
-
12
- # Set environment variables
13
- os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow warnings
14
- os.environ['CUDA_VISIBLE_DEVICES'] = '0' # Specify GPU device index
15
-
16
- # Specify GPU configuration
17
- config = tf.compat.v1.ConfigProto()
18
- config.gpu_options.allow_growth = True
19
- session = tf.compat.v1.Session(config=config)
20
-
21
- model_path = os.path.abspath('sign_asl_cnn_30_epochs.h5')
22
- if os.path.exists(model_path):
23
- # Load the model
24
- model = load_model(model_path)
25
- else:
26
- print(f"File not found: {model_path}")
27
- class_labels = {i: str(i) if i < 10 else chr(65 + i - 10) for i in range(36)}
28
-
29
- # Function to preprocess the image
30
- def preprocess_image(image):
31
- image = cv2.resize(image, (200, 200))
32
- image = image / 255.0
33
- image = image.reshape(1, 200, 200, 3)
34
- return image
35
-
36
- # Function to predict the sign language letter
37
- def predict_letter(image):
38
- processed_image = preprocess_image(image)
39
- predictions = model.predict(processed_image)
40
- predicted_class = np.argmax(predictions, axis=1)[0]
41
- sign_letter = class_labels[predicted_class]
42
- return sign_letter
43
-
44
- # Function to detect hands in the image
45
- def detect_hands(image):
46
- mp_hands = mp.solutions.hands
47
- hands = mp_hands.Hands()
48
- margin = 15
49
-
50
- # Convert the image to RGB
51
- image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
52
-
53
- # Process the image and get the hand landmarks
54
- results = hands.process(image_rgb)
55
 
56
- if results.multi_hand_landmarks:
57
- for landmarks in results.multi_hand_landmarks:
58
- # Get bounding box coordinates of the hand
59
- landmarks_xy = [(int(landmark.x * image.shape[1]), int(landmark.y * image.shape[0]))
60
- for landmark in landmarks.landmark]
61
-
62
- # Define the bounding box for the hand
63
- x_min = max(0, min(landmarks_xy, key=lambda x: x[0])[0] - margin)
64
- y_min = max(0, min(landmarks_xy, key=lambda x: x[1])[1] - margin)
65
- x_max = min(image.shape[1], max(landmarks_xy, key=lambda x: x[0])[0] + margin)
66
- y_max = min(image.shape[0], max(landmarks_xy, key=lambda x: x[1])[1] + margin)
67
-
68
- # Extract the hand region
69
- roi = image[y_min:y_max, x_min:x_max]
70
-
71
- # Check if the ROI is empty
72
- if roi.size == 0:
73
- continue
74
-
75
- # Resize the ROI to match your model's input shape
76
- roi = cv2.resize(roi, (200, 200), interpolation=cv2.INTER_AREA)
77
- hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)
78
-
79
- lower_yellow = np.array([93, 72, 51])
80
- upper_yellow = np.array([224, 194, 183])
81
- mask = cv2.inRange(hsv, lower_yellow, upper_yellow)
82
- roi = cv2.bitwise_and(roi, roi, mask=mask)
83
- roi = roi.reshape(1, 200, 200, 3) # Ensure it matches your model's input shape
84
-
85
- # Make predictions using your classifier
86
- predictions = model.predict(roi)
87
- predicted_class = int(np.argmax(predictions, axis=1)[0])
88
- result = class_labels[predicted_class]
89
-
90
- # Draw result on the image
91
- cv2.putText(image, str(result), (x_min, y_min - 10),
92
- cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
93
-
94
- # Draw bounding box on the image
95
- cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (255, 0, 0), 2)
96
-
97
- return image
98
 
99
- # Streamlit app
100
  st.title('Sign Language Recognition')
101
 
102
- # Sidebar with radio button for Upload/Webcam
103
- selected_option = st.sidebar.radio("Select Option", ["Upload", "Webcam"], index=0)
104
-
105
- if selected_option == "Upload":
106
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png"])
107
-
108
- if uploaded_file is not None:
109
- if st.button('Predict'):
110
- contents = uploaded_file.read()
111
- nparr = np.frombuffer(contents, np.uint8)
112
- image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
113
-
114
- # Make the prediction
115
- predicted_letter = predict_letter(image)
116
-
117
- # Display the predicted letter
118
- st.write('Predicted Letter:', predicted_letter)
119
-
120
- elif selected_option == "Webcam":
121
- # Placeholder for webcam frame
122
- webcam_frame = st.empty()
123
-
124
- # Placeholder for predicted letter in webcam mode
125
- predicted_letter_webcam = st.empty()
126
-
127
- # Placeholder for webcam capture status
128
- webcam_capture_status = st.empty()
129
-
130
- # Placeholder for webcam stop button
131
- webcam_stop_button = st.empty()
132
-
133
- # Placeholder for webcam status
134
- webcam_status = st.empty()
135
-
136
- # Placeholder for webcam button
137
- webcam_button = st.button("Start Webcam")
138
-
139
- if webcam_button:
140
- webcam_status.text("Webcam is on.")
141
- webcam_stop_button = st.button("Stop Webcam")
142
-
143
- # OpenCV video capture
144
- cap = cv2.VideoCapture(0)
145
-
146
- while True:
147
- # Read the frame from the webcam
148
- ret, frame = cap.read()
149
-
150
- # Display the frame in Streamlit
151
- webcam_frame.image(frame, channels="BGR")
152
-
153
- # Detect hands in the current frame
154
- frame = detect_hands(frame)
155
-
156
- # Convert the frame to JPEG format
157
- _, jpeg = cv2.imencode(".jpg", frame)
158
-
159
- # Display the predicted letter
160
- predicted_letter = predict_letter(frame)
161
- predicted_letter_webcam.text(f"Predicted Letter: {predicted_letter}")
162
-
163
- # Check if the "Stop Webcam" button is clicked
164
- if webcam_stop_button:
165
- webcam_status.text("Webcam is off.")
166
- break
167
-
168
- # Release the webcam when done
169
- cap.release()
 
1
+ # app.py
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ import streamlit as st
4
+ import requests
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
 
6
  st.title('Sign Language Recognition')
7
 
8
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png"])
9
+ if uploaded_file is not None:
10
+ if st.button('Predict'):
11
+ files = {'file': uploaded_file.getvalue()}
12
+ response = requests.post('http://74.12.105.219:8090/predict', files=files)
13
+ result = response.json()
14
+ st.write('Predicted Letter: ', result['predicted_letter'])