|
| 1 | +import cv2 |
| 2 | +from flask import Flask, render_template |
| 3 | +import cv2 |
| 4 | +import openai |
| 5 | +import mediapipe as mp |
| 6 | +import pyttsx3 |
| 7 | +from langchain import ChatOpenAI |
| 8 | + |
| 9 | +# Initialize OpenAI API |
| 10 | +openai.api_key = "YOUR_OPENAI_API_KEY" |
| 11 | + |
| 12 | +# Initialize text-to-speech |
| 13 | +engine = pyttsx3.init() |
| 14 | + |
| 15 | +# Function to send gesture to LLM for context |
| 16 | +def interpret_gesture(gesture_description): |
| 17 | + prompt = f"What does the gesture '{gesture_description}' signify?" |
| 18 | + response = openai.Completion.create( |
| 19 | + engine="text-davinci-003", |
| 20 | + prompt=prompt, |
| 21 | + max_tokens=60 |
| 22 | + ) |
| 23 | + return response.choices[0].text.strip() |
| 24 | + |
| 25 | +# Function for voice output |
| 26 | +def speak_text(text): |
| 27 | + engine.say(text) |
| 28 | + engine.runAndWait() |
| 29 | + |
| 30 | +# Initialize MediaPipe Hands for gesture tracking |
| 31 | +mp_hands = mp.solutions.hands |
| 32 | +hands = mp_hands.Hands() |
| 33 | +mp_draw = mp.solutions.drawing_utils |
| 34 | + |
| 35 | +# Webcam capture |
| 36 | +cap = cv2.VideoCapture(0) |
| 37 | + |
| 38 | +while cap.isOpened(): |
| 39 | + ret, frame = cap.read() |
| 40 | + if not ret: |
| 41 | + break |
| 42 | + |
| 43 | + # Convert the frame to RGB for MediaPipe |
| 44 | + rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) |
| 45 | + result = hands.process(rgb_frame) |
| 46 | + |
| 47 | + if result.multi_hand_landmarks: |
| 48 | + for hand_landmarks in result.multi_hand_landmarks: |
| 49 | + mp_draw.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS) |
| 50 | + |
| 51 | + # Here you would add logic to classify the hand gesture |
| 52 | + # For simplicity, let's assume we recognize a "thumbs up" |
| 53 | + recognized_gesture = "thumbs up" |
| 54 | + gesture_meaning = interpret_gesture(recognized_gesture) |
| 55 | + |
| 56 | + print(f"Recognized Gesture: {recognized_gesture}") |
| 57 | + print(f"Interpreted Meaning: {gesture_meaning}") |
| 58 | + |
| 59 | + # Voice output |
| 60 | + speak_text(f"Gesture: {recognized_gesture}. Meaning: {gesture_meaning}") |
| 61 | + |
| 62 | + # Display the frame |
| 63 | + cv2.imshow("Gesture Prediction", frame) |
| 64 | + |
| 65 | + # Exit with the 'q' key |
| 66 | + if cv2.waitKey(10) & 0xFF == ord('q'): |
| 67 | + break |
| 68 | + |
| 69 | +cap.release() |
| 70 | +cv2.destroyAllWindows() |
| 71 | + |
| 72 | + |
| 73 | +app = Flask(__name__) |
| 74 | + |
| 75 | +@app.route('/') |
| 76 | +def index(): |
| 77 | + return render_template('index.html') |
| 78 | + |
| 79 | +# Gesture recognition logic can be linked here to update the UI |
| 80 | + |
| 81 | +if __name__ == '__main__': |
| 82 | + app.run(debug=True) |
0 commit comments