JefferyJapheth commited on
Commit
9b0c68a
1 Parent(s): 927391d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -23
app.py CHANGED
@@ -12,8 +12,6 @@ SEED = 42
12
  NUM_CLASSES = 250
13
  INPUT_SIZE = 32
14
 
15
- cap = cv2.VideoCapture(1)
16
-
17
 
18
 
19
  # Tensorflow layer to process data in TFLite
@@ -198,6 +196,17 @@ def make_prediction(processed_landmarks):
198
  # ...
199
 
200
  with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
 
 
 
 
 
 
 
 
 
 
 
201
  def predict_with_webcam(frame):
202
  if frame is None:
203
  raise ValueError("Frame is None. Make sure your webcam is working properly.")
@@ -205,7 +214,9 @@ with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=
205
  # Make detections using mediapipe
206
  image, results = mediapipe_detection(frame, holistic)
207
  print(results)
208
- landmarks = extract_keypoints(results)
 
 
209
  if landmarks is not None:
210
  # Calculate the number of landmarks per frame
211
  landmarks_per_frame = len(landmarks) // (N_ROWS * N_DIMS)
@@ -221,29 +232,40 @@ with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=
221
  else:
222
  return "Could not detect landmarks. Make sure your webcam is working properly."
223
 
224
- # Set mediapipe model
225
- while cap.isOpened():
226
- # Read feed
227
- ret, frame = cap.read()
228
- if not ret:
229
- print("Failed to capture frame from the webcam.")
230
- break
231
 
232
- try:
233
- # Make predictions
234
- prediction = predict_with_webcam(frame)
 
 
 
 
 
 
 
 
 
235
 
236
- # Display the frame with the prediction
237
- cv2.putText(frame, prediction, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
238
- cv2.imshow('Webcam Landmark Prediction', frame)
239
 
240
- except Exception as e:
241
- print("Error occurred:", e)
242
 
243
- # Exit the loop when 'q' key is pressed
244
- if cv2.waitKey(1) & 0xFF == ord('q'):
245
- break
246
 
247
- cap.release()
248
- cv2.destroyAllWindows()
 
 
 
 
 
 
 
249
 
 
 
 
12
  NUM_CLASSES = 250
13
  INPUT_SIZE = 32
14
 
 
 
15
 
16
 
17
  # Tensorflow layer to process data in TFLite
 
196
  # ...
197
 
198
  with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
199
+ import cv2
200
+ import numpy as np
201
+ import gradio as gr
202
+ from tensorflow import lite as tflite
203
+ import tensorflow as tf
204
+ import mediapipe as mp
205
+
206
+
207
+ # ... (Previous code remains the same)
208
+
209
+ # Modify the predict_with_webcam function to take an image as input and return the prediction string
210
  def predict_with_webcam(frame):
211
  if frame is None:
212
  raise ValueError("Frame is None. Make sure your webcam is working properly.")
 
214
  # Make detections using mediapipe
215
  image, results = mediapipe_detection(frame, holistic)
216
  print(results)
217
+
218
+ if results is not None:
219
+ landmarks = extract_keypoints(results)
220
  if landmarks is not None:
221
  # Calculate the number of landmarks per frame
222
  landmarks_per_frame = len(landmarks) // (N_ROWS * N_DIMS)
 
232
  else:
233
  return "Could not detect landmarks. Make sure your webcam is working properly."
234
 
 
 
 
 
 
 
 
235
 
236
+ # Set mediapipe model
237
+ cap = cv2.VideoCapture(0)
238
+ while cap.isOpened():
239
+ # Read feed
240
+ ret, frame = cap.read()
241
+ if not ret:
242
+ print("Failed to capture frame from the webcam.")
243
+ break
244
+
245
+ try:
246
+ # Make predictions
247
+ prediction = predict_with_webcam(frame)
248
 
249
+ # Display the frame with the prediction
250
+ cv2.putText(frame, prediction, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
251
+ cv2.imshow('Webcam Landmark Prediction', frame)
252
 
253
+ except Exception as e:
254
+ print("Error occurred:", e)
255
 
256
+ # Exit the loop when 'q' key is pressed
257
+ if cv2.waitKey(1) & 0xFF == ord('q'):
258
+ break
259
 
260
+ cap.release()
261
+ cv2.destroyAllWindows()
262
+
263
+ # Define the Gradio interface
264
+ iface = gr.Interface(
265
+ fn=predict_with_webcam, # The function to use for prediction
266
+ inputs="webcam", # Use Gradio's "webcam" input to capture frames from the webcam
267
+ outputs=gr.outputs.Textbox() # Display the prediction as text
268
+ )
269
 
270
+ # Launch the interface
271
+ iface.launch()