Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677from flask import Flask, render_template, Responseimport cv2import numpy as npimport tensorflow as tfimport mediapipe as mpapp = Flask(__name__)@app.route('/test')def test():return "Flask is working!"# Load the trained modelmodel = tf.keras.models.load_model("asl_model.h5")# Initialize MediaPipe for hand trackingmp_hands = mp.solutions.handsmp_drawing = mp.solutions.drawing_utilshands = mp_hands.Hands()cap = cv2.VideoCapture(0)# Define gesture labels (must match training labels)gesture_labels = ["A", "B", "C", "D", "E"] # Update based on your datasetdef predict_gesture(landmarks):""" Predicts gesture based on hand landmarks. """landmarks = np.array(landmarks).reshape(1, -1) # Reshape for modelprediction = model.predict(landmarks)return gesture_labels[np.argmax(prediction)] # Get the highest probability gesturedef generate_frames():""" Captures video frames and processes hand gestures in real-time. """while True:ret, frame = cap.read()if not ret:breakframe = cv2.flip(frame, 1)rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)results = hands.process(rgb_frame)detected_gesture = "None"if results.multi_hand_landmarks:for hand_landmarks in results.multi_hand_landmarks:mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)# Extract landmark positionslandmarks = []for lm in hand_landmarks.landmark:landmarks.extend([lm.x, lm.y, lm.z])detected_gesture = predict_gesture(landmarks)# Display detected gesturecv2.putText(frame, f"Gesture: {detected_gesture}", (50, 50),cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)_, buffer = cv2.imencode('.jpg', frame)frame = buffer.tobytes()yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')@app.route('/')def index():return render_template('index.html')@app.route('/video')def video():return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')if __name__ == '__main__':app.run(debug=True)123456789101112131415161718import gradio as grimport cv2import mediapipe as mpmp_hands = mp.solutions.handshands = mp_hands.Hands()mp_drawing = mp.solutions.drawing_utilsdef process_frame(image):image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)results = hands.process(image)if results.multi_hand_landmarks:for hand_landmarks in results.multi_hand_landmarks:mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)return imageiface = gr.Interface(fn=process_frame, inputs="image", outputs="image", live=True)iface.launch()
app.py+18-77