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