medtty commited on
Commit
c15ccda
·
1 Parent(s): 062f615
Files changed (1) hide show
  1. app.py +26 -56
app.py CHANGED
@@ -4,16 +4,12 @@ import numpy as np
4
  import json
5
  from PIL import Image
6
  from fastapi import FastAPI, UploadFile, File, WebSocket, Request, Response
7
- from fastapi.responses import StreamingResponse
8
  import uvicorn
9
  import cv2
10
  import mediapipe as mp
11
  import io
12
- import base64
13
- import asyncio
14
  import time
15
- from typing import List, Dict, Any
16
- from pydantic import BaseModel
17
 
18
  # Initialize MediaPipe Hands
19
  mp_hands = mp.solutions.hands
@@ -43,19 +39,6 @@ MODEL_INPUT_SIZE = (224, 224)
43
  DETECTION_FREQUENCY = 5 # Process every Nth frame for performance
44
  CONFIDENCE_THRESHOLD = 0.5 # Minimum confidence to report a gesture
45
 
46
- # Data models for API
47
- class GestureResponse(BaseModel):
48
- class_name: str
49
- confidence: float
50
- timestamp: float
51
- all_predictions: Dict[str, float] = None
52
-
53
- class StreamRequest(BaseModel):
54
- stream_id: str = None
55
- width: int = 640
56
- height: int = 480
57
- fps: int = 15
58
-
59
  # Cache to store most recent detection results
60
  detection_cache = {}
61
 
@@ -196,45 +179,33 @@ def predict(image_pil):
196
  traceback.print_exc()
197
  return {"error": str(e)}
198
 
199
- # Define the Gradio interface
200
  with gradio_app:
201
  gr.Markdown("# Hand Gesture Recognition")
202
- with gr.Tabs():
203
- with gr.TabItem("Image Upload"):
204
- with gr.Row():
205
- input_image = gr.Image(type="pil", label="Upload Image")
206
- output_json = gr.JSON(label="Prediction Results")
207
- submit = gr.Button("Predict")
208
- submit.click(
209
- fn=predict,
210
- inputs=input_image,
211
- outputs=output_json
212
- )
213
- gr.Examples(
214
- examples=[["examples/two_up.jpg"], ["examples/stop.jpg"]],
215
- inputs=input_image
216
- )
217
-
218
- with gr.TabItem("Live Demo"):
219
- gr.Markdown("""
220
- ## Live Demo
221
- Try the live demo using your webcam!
222
- - Please allow camera access when prompted
223
- - Hold your hand gesture in front of the camera
224
- """)
225
- camera_input = gr.Image(source="webcam", streaming=True, label="Camera Input")
226
- live_output = gr.JSON(label="Live Detection Results")
227
-
228
- def process_camera_input(img):
229
- if img is None:
230
- return {"message": "No image received"}
231
- return predict(img)
232
-
233
- camera_input.change(
234
- fn=process_camera_input,
235
- inputs=camera_input,
236
- outputs=live_output
237
- )
238
 
239
  # Mount Gradio app to FastAPI
240
  fastapi_app = gr.mount_gradio_app(fastapi_app, gradio_app, path="/")
@@ -352,7 +323,6 @@ def health_check():
352
  """Simple health check endpoint"""
353
  return {"status": "healthy", "timestamp": time.time()}
354
 
355
- # Documentation for Android integration
356
  @fastapi_app.get("/")
357
  async def root():
358
  return {
 
4
  import json
5
  from PIL import Image
6
  from fastapi import FastAPI, UploadFile, File, WebSocket, Request, Response
 
7
  import uvicorn
8
  import cv2
9
  import mediapipe as mp
10
  import io
 
 
11
  import time
12
+ from typing import Dict
 
13
 
14
  # Initialize MediaPipe Hands
15
  mp_hands = mp.solutions.hands
 
39
  DETECTION_FREQUENCY = 5 # Process every Nth frame for performance
40
  CONFIDENCE_THRESHOLD = 0.5 # Minimum confidence to report a gesture
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  # Cache to store most recent detection results
43
  detection_cache = {}
44
 
 
179
  traceback.print_exc()
180
  return {"error": str(e)}
181
 
182
+ # Define the Gradio interface - simplified without webcam
183
  with gradio_app:
184
  gr.Markdown("# Hand Gesture Recognition")
185
+ with gr.Row():
186
+ input_image = gr.Image(type="pil", label="Upload Image")
187
+ output_json = gr.JSON(label="Prediction Results")
188
+ submit = gr.Button("Predict")
189
+ submit.click(
190
+ fn=predict,
191
+ inputs=input_image,
192
+ outputs=output_json
193
+ )
194
+ gr.Examples(
195
+ examples=[["examples/two_up.jpg"], ["examples/call.jpg"], ["examples/stop.jpg"]],
196
+ inputs=input_image
197
+ )
198
+
199
+ # Add information about API endpoints for Android integration
200
+ gr.Markdown("""
201
+ ## API Endpoints for Android Integration
202
+
203
+ - **Image Upload**: `POST /api/predict` with image file
204
+ - **Video Frame**: `POST /api/video/frame` with frame data and X-Stream-ID header
205
+ - **WebSocket Stream**: Connect to `/api/stream` for real-time processing
206
+ - **Available Gestures**: `GET /api/gestures` returns all gesture classes
207
+ - **Health Check**: `GET /health` checks server status
208
+ """)
 
 
 
 
 
 
 
 
 
 
 
 
209
 
210
  # Mount Gradio app to FastAPI
211
  fastapi_app = gr.mount_gradio_app(fastapi_app, gradio_app, path="/")
 
323
  """Simple health check endpoint"""
324
  return {"status": "healthy", "timestamp": time.time()}
325
 
 
326
  @fastapi_app.get("/")
327
  async def root():
328
  return {