prakasa1234 commited on
Commit
b9c1b8a
·
verified ·
1 Parent(s): d6c4c85

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -10
app.py CHANGED
@@ -1,13 +1,15 @@
1
  import os
 
2
  import cv2
3
  import numpy as np
4
- import requests
5
  import torch
 
 
6
  from ultralytics import YOLO
7
  import gradio as gr
8
- from mediapipe import Image as MPImage
9
  from mediapipe.tasks import python
10
  from mediapipe.tasks.python import vision
 
11
  import traceback
12
 
13
  # -----------------------------
@@ -49,14 +51,28 @@ def predict_asl(image):
49
  h, w, _ = img.shape
50
  print(f"🔹 Uploaded image shape: {img.shape}, dtype: {img.dtype}")
51
 
52
- # --- Annotate hand landmarks ---
53
- mp_image = MPImage.create_from_array(img)
54
- detection_result = detector.detect(mp_image)
55
- if detection_result.hand_landmarks:
56
- for hand_landmarks in detection_result.hand_landmarks:
57
- for landmark in hand_landmarks:
58
- x, y = int(landmark.x * w), int(landmark.y * h)
59
- cv2.circle(img, (x, y), 3, (0, 255, 0), -1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  # --- YOLO prediction directly on NumPy array ---
62
  results = yolo_model.predict(img, imgsz=300, verbose=False)[0]
 
1
  import os
2
+ import io
3
  import cv2
4
  import numpy as np
 
5
  import torch
6
+ import requests
7
+ from PIL import Image
8
  from ultralytics import YOLO
9
  import gradio as gr
 
10
  from mediapipe.tasks import python
11
  from mediapipe.tasks.python import vision
12
+ from mediapipe.tasks.python.vision import Image as MPImage
13
  import traceback
14
 
15
  # -----------------------------
 
51
  h, w, _ = img.shape
52
  print(f"🔹 Uploaded image shape: {img.shape}, dtype: {img.dtype}")
53
 
54
+ # --- MediaPipe annotation ---
55
+ try:
56
+ # Convert OpenCV BGR -> RGB
57
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
58
+
59
+ # PIL + BytesIO to create MediaPipe image
60
+ pil_img = Image.fromarray(img_rgb)
61
+ buf = io.BytesIO()
62
+ pil_img.save(buf, format="PNG")
63
+ buf.seek(0)
64
+
65
+ mp_img = MPImage.create_from_file(buf)
66
+
67
+ detection_result = detector.detect(mp_img)
68
+ if detection_result.hand_landmarks:
69
+ for hand_landmarks in detection_result.hand_landmarks:
70
+ for landmark in hand_landmarks:
71
+ x, y = int(landmark.x * w), int(landmark.y * h)
72
+ cv2.circle(img, (x, y), 3, (0, 255, 0), -1)
73
+ except Exception as e:
74
+ print("❌ MediaPipe annotation error:", e)
75
+ traceback.print_exc()
76
 
77
  # --- YOLO prediction directly on NumPy array ---
78
  results = yolo_model.predict(img, imgsz=300, verbose=False)[0]