from fastapi import FastAPI, File, UploadFile import numpy as np from PIL import Image import io import tensorflow as tf import cv2 from tensorflow import keras import pickle import mediapipe as mp app = FastAPI() # @app.post("/classify") # async def classify(image: UploadFile = File(...)): # if image is not None: # img = Image.open(io.BytesIO(await image.read())) # img = img.resize((128, 128)) # img_array = np.array(img) / 255.0 # img_array = np.expand_dims(img_array, axis=0) # predictions = model.predict(img_array) # predicted_class_idx = np.argmax(predictions) # predicted_class_idx = int(predicted_class_idx) # return {"prediction": predicted_class_idx} # else: # return {"error": "No image provided"} # model = tf.keras.models.load_model('_9217') with open('label_encoder.pkl', 'rb') as f: label_encoder = pickle.load(f) model = keras.models.load_model('sign_language_model.keras') def js_to_image1(js_reply): image_bytes = base64.b64decode(js_reply.split(',')[1]) jpg_as_np = np.frombuffer(image_bytes, dtype=np.uint8) img = cv2.imdecode(jpg_as_np, flags=1) return img def js_to_image(image_data): imgdata = base64.b64decode(image_data) if not imgdata: raise ValueError("Received empty image data") jpg_as_np = np.frombuffer(imgdata, dtype=np.uint8) img = cv2.imdecode(jpg_as_np, flags=1) if img is None: raise ValueError("Failed to decode image") return img mp_hands = mp.solutions.hands hands_detector = mp_hands.Hands(min_detection_confidence=0.7) def detect_hands(image): image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) results = hands_detector.process(image_rgb) subImgs = [] if results.multi_hand_landmarks: for hand_landmarks in results.multi_hand_landmarks: x_coords = [landmark.x for landmark in hand_landmarks.landmark] y_coords = [landmark.y for landmark in hand_landmarks.landmark] x_min = int(min(x_coords) * image.shape[1]) y_min = int(min(y_coords) * image.shape[0]) x_max = int(max(x_coords) * image.shape[1]) y_max = int(max(y_coords) * image.shape[0]) side = max(x_max - x_min, y_max - y_min) center_x = (x_min + x_max) // 2 center_y = (y_min + y_max) // 2 new_x1 = max(0, center_x - side // 2) new_y1 = max(0, center_y - side // 2) new_x2 = min(image.shape[1], center_x + side // 2) new_y2 = min(image.shape[0], center_y + side // 2) hand_img = image[new_y1:new_y2, new_x1:new_x2] hand_img = cv2.resize(hand_img, (64, 64)) subImgs.append((new_x1, new_y1, new_x2, new_y2, hand_img)) return subImgs # def predict(image): # img = image.astype("float32") / 255.0 # img = np.expand_dims(img, axis=0) # pred = model.predict(img, verbose=0) # label = label_encoder.inverse_transform([np.argmax(pred)])[0] # return label # Prediction function def predict(image): img = cv2.resize(image, (64, 64)) img = np.expand_dims(img, axis=0) # Add batch dimension prediction = model.predict(img, verbose=0) predicted_class = np.argmax(prediction) predicted_label = label_encoder.classes_[predicted_class] return predicted_label @app.post("/ipredict") async def ipredict(image: UploadFile = File(...)): if image is not None: img = Image.open(io.BytesIO(await image.read())) img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) # img = img.resize((128, 128)) # img_array = np.array(img) / 255.0 # Get the prediction label = predict(img) # Return the prediction return {"prediction": label} else: return {"error": "No image provided"} @app.post("/vpredict") async def vpredict(image: UploadFile = File(...)): if image is not None: # img = Image.open(io.BytesIO(await image.read())) # # img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) # img = cv2.imdecode(np.array(img), cv2.IMREAD_COLOR) # print("IAM HERE **********************************************************************************") image_bytes = await image.read() # Convert bytes to a NumPy uint8 array nparr = np.frombuffer(image_bytes, np.uint8) # Decode into OpenCV image (BGR format) img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) # img = img.resize((128, 128)) # img_array = np.array(img) / 255.0 # Get the prediction predictions = [] for x1, y1, x2, y2, hand_img in detect_hands(img): label = predict(hand_img) predictions.append({ "x": x1, "y": y1, "w": x2, "h": y2, "label": label }) return {"predictions": predictions} else: return {"error": "No image provided"}