alikayh commited on
Commit
fb5dc5b
·
verified ·
1 Parent(s): 5aeec13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -118
app.py CHANGED
@@ -1,121 +1,61 @@
 
 
1
  import streamlit as st
2
  import cv2
3
  import numpy as np
4
- import base64
5
-
6
- # Function to convert text to image
7
- def text_to_image(text, width=400, height=200, font_size=0.7, font_color=(0, 0, 0), bg_color=(255, 255, 255)):
8
- # Create a blank image with white background
9
- image = np.zeros((height, width, 3), dtype=np.uint8)
10
- image[:, :] = bg_color
11
-
12
- # Put text on the image using OpenCV
13
- font = cv2.FONT_HERSHEY_SIMPLEX
14
- text_size, _ = cv2.getTextSize(text, font, font_size, 2)
15
- text_x = (width - text_size[0]) // 2
16
- text_y = (height + text_size[1]) // 2
17
- cv2.putText(image, text, (text_x, text_y), font, font_size, font_color, 2, cv2.LINE_AA)
18
-
19
- return image
20
-
21
- # Function to detect open palm gesture
22
- def detect_gesture(frame):
23
- # Convert frame to grayscale
24
- gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
25
-
26
- # Use median blur for noise reduction
27
- gray = cv2.medianBlur(gray, 5)
28
-
29
- # Thresholding
30
- _, threshold = cv2.threshold(gray, 130, 255, cv2.THRESH_BINARY_INV)
31
-
32
- # Find contours in the thresholded image
33
- contours, _ = cv2.findContours(threshold.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
34
-
35
- # Find the largest contour (likely the hand)
36
- if len(contours) > 0:
37
- hand_contour = max(contours, key=cv2.contourArea)
38
- hull = cv2.convexHull(hand_contour)
39
- defects = cv2.convexityDefects(hand_contour, cv2.convexHull(hand_contour, returnPoints=False))
40
-
41
- finger_count = 0
42
-
43
- # Count fingers based on convex defects
44
- for i in range(defects.shape[0]):
45
- s, e, f, d = defects[i][0]
46
- start = tuple(hand_contour[s][0])
47
- end = tuple(hand_contour[e][0])
48
- far = tuple(hand_contour[f][0])
49
-
50
- a = np.sqrt((end[0] - start[0])**2 + (end[1] - start[1])**2)
51
- b = np.sqrt((far[0] - start[0])**2 + (far[1] - start[1])**2)
52
- c = np.sqrt((end[0] - far[0])**2 + (end[1] - far[1])**2)
53
-
54
- angle = np.arccos((b**2 + c**2 - a**2) / (2 * b * c)) * 57
55
-
56
- if angle <= 90:
57
- finger_count += 1
58
-
59
- finger_count += 1
60
-
61
- # Define gestures based on finger count
62
- if finger_count == 1:
63
- return "Thumbs Up"
64
- elif finger_count == 2:
65
- return "Two Fingers"
66
- elif finger_count == 5:
67
- return "Open Palm"
68
- else:
69
- return "Not Recognized"
70
-
71
- return "No Hand Detected"
72
-
73
- # Streamlit App
74
- st.title("Gesture Recognition and Sign Language Translator")
75
-
76
- # Checkbox to choose the functionality
77
- use_gesture = st.sidebar.checkbox("Do you know Sign Language?")
78
-
79
- if use_gesture:
80
- st.write("This section detects basic gestures using a webcam.")
81
- st.write("Please make sure your webcam is accessible.")
82
-
83
- # Open webcam
84
- cam = cv2.VideoCapture(0)
85
-
86
- if not cam.isOpened():
87
- st.write("Error: Could not open webcam.")
88
- else:
89
- frame_placeholder = st.empty()
90
-
91
- while True:
92
- ret, frame = cam.read()
93
- if not ret:
94
- st.write("Error: Could not read frame.")
95
- break
96
-
97
- gesture = detect_gesture(frame)
98
- cv2.putText(frame, f"Gesture: {gesture}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
99
-
100
- # Convert OpenCV image to format suitable for Streamlit
101
- _, buffer = cv2.imencode('.jpg', frame)
102
- frame_base64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
103
- img_str = f"data:image/jpeg;base64,{frame_base64}"
104
-
105
- # Display the frame
106
- frame_placeholder.markdown(f'<img src="{img_str}" style="width: 100%;">', unsafe_allow_html=True)
107
-
108
- if st.button("Stop Webcam", key="stop_webcam"):
109
- break
110
-
111
- # Release webcam
112
- cam.release()
113
- else:
114
- st.write("Enter the text that you want to show to people who don't know sign language.")
115
- text_input = st.text_input("Enter text:")
116
-
117
- if st.button("Convert Text to Image"):
118
- generated_image = text_to_image(text_input)
119
- st.image(generated_image, caption="Generated Image from Text", use_column_width=True)
120
-
121
- st.write("Thanks for using our Sign Language Assistant!")
 
1
+ # Sign Language Translator with Streamlit
2
+
3
  import streamlit as st
4
  import cv2
5
  import numpy as np
6
+ import mediapipe as mp
7
+
8
+ # Load Mediapipe Hands model
9
+ mp_hands = mp.solutions.hands
10
+ hands = mp_hands.Hands()
11
+
12
+ # Function to process webcam feed
13
+ def process_webcam():
14
+ cap = cv2.VideoCapture(0)
15
+ while cap.isOpened():
16
+ ret, frame = cap.read()
17
+ if not ret:
18
+ break
19
+
20
+ # Convert the frame to RGB
21
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
22
+ results = hands.process(frame_rgb)
23
+
24
+ # Draw hand landmarks
25
+ if results.multi_hand_landmarks:
26
+ for hand_landmarks in results.multi_hand_landmarks:
27
+ mp.solutions.drawing_utils.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
28
+
29
+ # Display the frame
30
+ cv2.imshow('Webcam', frame)
31
+
32
+ if cv2.waitKey(1) & 0xFF == ord('q'):
33
+ break
34
+
35
+ cap.release()
36
+ cv2.destroyAllWindows()
37
+
38
+ # Streamlit UI
39
+ st.title("Sign Language Translator")
40
+ st.write("This application translates sign language into Farsi and provides visual aids for understanding.")
41
+
42
+ if st.button("Open Webcam"):
43
+ st.write("Opening webcam...")
44
+ process_webcam()
45
+
46
+ # Placeholder for translation logic
47
+ def translate_sign_language(hand_gesture):
48
+ # Placeholder for actual translation logic
49
+ return "Translated Text in Farsi"
50
+
51
+ # Placeholder for visual aid
52
+ def show_visual_aid(hand_gesture):
53
+ # Placeholder for actual visual aid logic
54
+ st.image("path_to_hand_shape_image.png", caption="Hand Shape for Sign Language")
55
+
56
+ # User input for non-sign language users
57
+ user_input = st.text_input("Enter text if you do not know sign language:")
58
+ if user_input:
59
+ st.write("You entered:", user_input)
60
+ # Logic to show visual aid
61
+ show_visual_aid(user_input)