Par24 commited on
Commit
14f0a77
·
verified ·
1 Parent(s): da71168

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -9
app.py CHANGED
@@ -4,17 +4,22 @@ import gradio as gr
4
  import tensorflow as tf
5
  from huggingface_hub import hf_hub_download
6
 
7
- # Load the model from Hugging Face Model Hub
8
- model_path = hf_hub_download(repo_id="sign_language", filename="saved_model.pb", repo_type="model")
9
- model = tf.saved_model.load(model_path)
 
 
10
  infer = model.signatures["serving_default"]
11
 
12
- # Define class labels (modify as per your dataset)
13
  class_labels = ['Hello', 'Yes', 'No', 'Thank You', 'Please']
14
 
15
  def predict_sign(frame):
 
 
 
16
  # Preprocess the frame
17
- img = cv2.resize(frame, (224, 224)) # Resize to match model input
18
  img = img / 255.0 # Normalize
19
  img = np.expand_dims(img, axis=0) # Add batch dimension
20
  img = tf.convert_to_tensor(img, dtype=tf.float32)
@@ -23,6 +28,8 @@ def predict_sign(frame):
23
  predictions = infer(tf.constant(img))
24
  output_tensor_name = list(predictions.keys())[0] # Get the output tensor name
25
  predictions = predictions[output_tensor_name].numpy()
 
 
26
  predicted_class = class_labels[np.argmax(predictions)]
27
  confidence = np.max(predictions)
28
 
@@ -30,14 +37,23 @@ def predict_sign(frame):
30
 
31
  def process_frame(frame):
32
  pred, conf = predict_sign(frame)
 
 
 
 
 
33
  cv2.putText(frame, f"{pred} ({conf:.2f})", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
 
34
  return frame
35
 
36
  # Gradio Live Webcam Interface
37
- def webcam_feed():
38
- return gr.Video(source="webcam", streaming=True, mirror=True)
39
-
40
- gui = gr.Interface(fn=process_frame, inputs=webcam_feed(), outputs="image", live=True)
 
 
41
 
 
42
  if __name__ == "__main__":
43
  gui.launch(server_name="0.0.0.0", server_port=7860)
 
4
  import tensorflow as tf
5
  from huggingface_hub import hf_hub_download
6
 
7
+ # Download the entire model directory
8
+ model_dir = hf_hub_download(repo_id="Par24/sign_language", filename="saved_model", repo_type="model")
9
+
10
+ # Load the model correctly
11
+ model = tf.saved_model.load(model_dir)
12
  infer = model.signatures["serving_default"]
13
 
14
+ # Define class labels
15
  class_labels = ['Hello', 'Yes', 'No', 'Thank You', 'Please']
16
 
17
  def predict_sign(frame):
18
+ # Convert BGR (OpenCV) to RGB (TensorFlow format)
19
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
20
+
21
  # Preprocess the frame
22
+ img = cv2.resize(frame, (224, 224)) # Resize
23
  img = img / 255.0 # Normalize
24
  img = np.expand_dims(img, axis=0) # Add batch dimension
25
  img = tf.convert_to_tensor(img, dtype=tf.float32)
 
28
  predictions = infer(tf.constant(img))
29
  output_tensor_name = list(predictions.keys())[0] # Get the output tensor name
30
  predictions = predictions[output_tensor_name].numpy()
31
+
32
+ # Get predicted class and confidence
33
  predicted_class = class_labels[np.argmax(predictions)]
34
  confidence = np.max(predictions)
35
 
 
37
 
38
  def process_frame(frame):
39
  pred, conf = predict_sign(frame)
40
+
41
+ # Convert RGB back to BGR for OpenCV
42
+ frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
43
+
44
+ # Overlay prediction text
45
  cv2.putText(frame, f"{pred} ({conf:.2f})", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
46
+
47
  return frame
48
 
49
  # Gradio Live Webcam Interface
50
+ gui = gr.Interface(
51
+ fn=process_frame, # Function to process frames
52
+ inputs="webcam", # Use webcam as input
53
+ outputs="image", # Output is an image
54
+ live=True
55
+ )
56
 
57
+ # Launch Gradio App
58
  if __name__ == "__main__":
59
  gui.launch(server_name="0.0.0.0", server_port=7860)