SachithRKA commited on
Commit
f853f39
·
verified ·
1 Parent(s): daf7ea0

Upload 2 files

Browse files
Files changed (2) hide show
  1. asl_landmark_mine_model_one.h5 +3 -0
  2. run.py +126 -0
asl_landmark_mine_model_one.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c52a2dc429dece33e7ce6e8758b97ea03ac975552323c49198ac3743cb6751d4
3
+ size 14892072
run.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import google.generativeai as genai
3
+ import os
4
+ import markdown
5
+ import cv2
6
+ import numpy as np
7
+ from tensorflow.keras.models import load_model
8
+ import mediapipe as mp
9
+ from dotenv import load_dotenv
10
+
11
+ load_dotenv()
12
+ genai.configure(api_key=os.environ.get("API_KEY"))
13
+
14
+ # Setup the model
15
+ generation_config = {
16
+ "temperature": 1,
17
+ "top_p": 0.95,
18
+ "top_k": 0,
19
+ "max_output_tokens": 8192,
20
+ }
21
+
22
+ safety_settings = [
23
+ {
24
+ "category": "HARM_CATEGORY_HARASSMENT",
25
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
26
+ },
27
+ {
28
+ "category": "HARM_CATEGORY_HATE_SPEECH",
29
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
30
+ },
31
+ {
32
+ "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
33
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
34
+ },
35
+ {
36
+ "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
37
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
38
+ },
39
+ ]
40
+
41
+ model = genai.GenerativeModel(model_name="gemini-1.5-pro-latest",
42
+ generation_config=generation_config,
43
+ safety_settings=safety_settings)
44
+
45
+ convo = model.start_chat(history=[])
46
+
47
+ model = load_model('asl_landmark_mine_model_one.h5')
48
+
49
+
50
+ def preprocess_image(img, target_size=(64, 64)):
51
+ img = cv2.cvtColor(cv2.flip(img, 1), cv2.COLOR_BGR2RGB)
52
+ img = cv2.resize(img, target_size)
53
+ img = np.expand_dims(img, axis=0)
54
+ img = img / 255.0
55
+ return img
56
+
57
+
58
+ def predict_asl_letter(image, model):
59
+ img = preprocess_image(image)
60
+ predictions = model.predict(img)
61
+ predicted_class = np.argmax(predictions)
62
+ asl_letter = chr(predicted_class + ord('A'))
63
+ return asl_letter
64
+
65
+
66
+ def asl_video():
67
+ cap = cv2.VideoCapture(0)
68
+ sentence = ""
69
+
70
+ mp_hands = mp.solutions.hands
71
+ mp_drawing = mp.solutions.drawing_utils
72
+ hands = mp_hands.Hands(min_detection_confidence=0.5, min_tracking_confidence=0.5)
73
+
74
+ while cap.isOpened():
75
+ ret, frame = cap.read()
76
+ if not ret:
77
+ print("Ignoring empty camera frame.")
78
+ continue
79
+
80
+ frame.flags.writeable = False
81
+ results = hands.process(frame)
82
+
83
+ frame.flags.writeable = True
84
+ frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
85
+ if results.multi_hand_landmarks:
86
+ for hand_landmarks in results.multi_hand_landmarks:
87
+ mp_drawing.draw_landmarks(
88
+ frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
89
+
90
+ asl_letter = predict_asl_letter(frame, model)
91
+ cv2.putText(frame, "Predicted Letter: " + asl_letter, (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0),
92
+ 2)
93
+ sentence += asl_letter
94
+
95
+ cv2.imshow('MediaPipe Hands', frame)
96
+ if cv2.waitKey(5) & 0xFF == ord('q'):
97
+ break
98
+
99
+ cap.release()
100
+ cv2.destroyAllWindows()
101
+ return sentence
102
+
103
+
104
+ def greet():
105
+ global convo
106
+
107
+ sentence = "Hello"
108
+ expected = "Hello"
109
+ questionsRight = 4
110
+ numberOfQuestions = 10
111
+ questionNumber = 5
112
+
113
+ sentence = asl_video()
114
+ prompt = f"Analyze the user's sign for \"{expected}\" and provide feedback. Did they sign it correctly? If not, explain what went wrong and how to improve. If they got it right, offer encouragement. The user's sign was \"{sentence}\". This is question number {questionNumber} out of {numberOfQuestions}, and the user has gotten {questionsRight} questions right so far."
115
+ convo.send_message(prompt)
116
+ result = markdown.markdown(convo.last.text)
117
+
118
+ return result
119
+
120
+
121
+ if __name__ == "__main__":
122
+ gr.Interface(
123
+ fn=greet,
124
+ inputs=None,
125
+ outputs="html"
126
+ ).launch()