CoolFace
Apppublic

Satu3741/SignLanguage

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app_huggingface.py88 linesDownload Raw Back to root
1from fastapi import FastAPI, File, UploadFile
2from fastapi.middleware.cors import CORSMiddleware
3import tensorflow as tf
4import numpy as np
5import cv2
6import mediapipe as mp
7import io
8from PIL import Image
9
10app = FastAPI()
11
12# Configure CORS
13app.add_middleware(
14    CORSMiddleware,
15    allow_origins=["*"],
16    allow_credentials=True,
17    allow_methods=["*"],
18    allow_headers=["*"],
19)
20
21# Initialize mediapipe
22mp_hands = mp.solutions.hands
23hands = mp_hands.Hands(
24    static_image_mode=True,
25    max_num_hands=1,
26    min_detection_confidence=0.5,
27    min_tracking_confidence=0.5
28)
29mp_draw = mp.solutions.drawing_utils
30
31# Load the model
32model = tf.keras.models.load_model('cnn8grps_rad1_model.h5')
33
34def process_hand_landmarks(image):
35    try:
36        # Convert the image to RGB
37        image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
38        
39        # Get hand landmarks
40        results = hands.process(image_rgb)
41        
42        if results.multi_hand_landmarks:
43            # Create a white image with correct shape (400x400x3)
44            white = np.ones((400, 400, 3), np.uint8) * 255
45            
46            # Draw landmarks on white image
47            for hand_landmarks in results.multi_hand_landmarks:
48                mp_draw.draw_landmarks(white, hand_landmarks, mp_hands.HAND_CONNECTIONS)
49            
50            # Convert to RGB format
51            white = cv2.cvtColor(white, cv2.COLOR_BGR2RGB)
52            
53            # Normalize the image
54            white = white.astype('float32') / 255.0
55            
56            # Add batch dimension
57            white = np.expand_dims(white, axis=0)
58            
59            return white
60        return None
61    except Exception as e:
62        print(f"Error in process_hand_landmarks: {str(e)}")
63        return None
64
65@app.post("/predict")
66async def predict(file: UploadFile = File(...)):
67    try:
68        # Read the image file
69        contents = await file.read()
70        image = Image.open(io.BytesIO(contents))
71        image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
72        
73        # Process image
74        processed_image = process_hand_landmarks(image)
75        if processed_image is None:
76            return {"error": "No hand detected"}
77            
78        # Get prediction
79        prediction = model.predict(processed_image, verbose=0)
80        
81        return {"prediction": prediction.tolist()}
82        
83    except Exception as e:
84        return {"error": str(e)}
85
86@app.get("/")
87async def root():
88    return {"message": "Sign Language Recognition API is running"}