Danish15/Sign-Language
0
1import cv22import streamlit as st3import numpy as np4from cvzone.HandTrackingModule import HandDetector5from cvzone.ClassificationModule import Classifier6import math7from PIL import Image8st.set_page_config(page_title="Sign Language Detection", page_icon="🔥",layout="wide")9 10 11# Initialize the camera12cap = cv2.VideoCapture(0)13detector = HandDetector(maxHands=1)14classifier = Classifier("keras_model.h5", "labels.txt")15 16offset = 2017imgSize = 30018 19labels = ["hello", "how are you", "welcome"]20 21# Function to display video stream and detected text22def display_video_with_text():23 st.title("Sign Language Detection")24 st.write("This project employs computer vision and machine learning to recognize and interpret sign language gestures captured via webcam, providing real-time feedback and enabling communication for individuals who use sign language. Through accurate sign language detection, it promotes accessibility and inclusivity in digital interactions and communication platforms.")25 st.write("---")26 effects_images = {27 "hello.jpg": "Hello Sign",28 "how are you.jpg": "How Are You Sign",29 "welcome.jpg": "Welcome Sign"30 }31 st.markdown("### Images:")32 col1, col2, col3 = st.columns(3)33 for i, (image_path, caption) in enumerate(effects_images.items()):34 with col1 if i % 3 == 0 else col2 if i % 3 == 1 else col3:35 st.image(image_path, caption=caption, use_column_width=True, output_format="auto")36 st.write(37 "Place your hand in front of the camera to detect gestures. The detected text will appear on the right side."38 )39 st.write("---")40 41 # Create columns layout42 col1, col2 = st.columns([3, 1]) # Video stream will occupy 3/4 of the width, detected text will occupy 1/443 44 with col1:45 # Video stream placeholder46 st.header("Live Video Streaming:")47 48 video_placeholder = st.empty()49 50 with col2:51 # Detected text placeholder52 st.header("Detected Text:")53 text_placeholder = st.empty()54 55 56 57 while True:58 # Read frame from camera59 ret, img = cap.read()60 if not ret:61 st.error(62 "Failed to retrieve frame from camera. Please check your camera connection."63 )64 break65 66 imgOutput = img.copy()67 hands, img = detector.findHands(img)68 if hands:69 hand = hands[0]70 x, y, w, h = hand["bbox"]71 72 imgWhite = np.ones((imgSize, imgSize, 3), np.uint8) * 25573 imgCrop = img[y - offset : y + h + offset, x - offset : x + w + offset]74 75 imgCropShape = imgCrop.shape76 77 aspectRatio = h / w78 79 # If the cropped image is not empty, resize it80 if imgCropShape[0] != 0 and imgCropShape[1] != 0:81 if aspectRatio > 1:82 k = imgSize / h83 wCal = math.ceil(k * w)84 imgResize = cv2.resize(imgCrop, (wCal, imgSize))85 imgResizeShape = imgResize.shape86 wGap = math.ceil((imgSize - wCal) / 2)87 imgWhite[:, wGap : wCal + wGap] = imgResize88 prediction, index = classifier.getPrediction(imgWhite)89 else:90 k = imgSize / w91 hCal = math.ceil(k * h)92 imgResize = cv2.resize(imgCrop, (imgSize, hCal))93 imgResizeShape = imgResize.shape94 hGap = math.ceil((imgSize - hCal) / 2)95 imgWhite[hGap : hCal + hGap, :] = imgResize96 prediction, index = classifier.getPrediction(imgWhite)97 else:98 # If the cropped image is empty, set prediction and index to default values99 prediction, index = "", 0100 101 cv2.putText(102 imgOutput,103 labels[index],104 (x, y - 20),105 cv2.FONT_HERSHEY_COMPLEX,106 2,107 (255, 0, 255),108 2,109 )110 111 # Convert image to RGB for display112 imgOutput = cv2.cvtColor(imgOutput, cv2.COLOR_BGR2RGB)113 114 # Convert image array to PIL Image115 pil_img = Image.fromarray(imgOutput)116 117 # Display video stream and detected text118 video_placeholder.image(pil_img, channels="RGB")119 text_placeholder.write(labels[prediction.index(max(prediction))])120 121 122# Run the Streamlit app123if __name__ == "__main__":124 display_video_with_text()125 