File size: 5,023 Bytes
6eec5de
8439d88
 
 
8be214a
24c6d78
 
 
 
8439d88
54afe11
8439d88
b7b10e6
24c6d78
 
 
 
 
 
 
 
 
 
 
 
 
 
54afe11
 
 
 
 
24c6d78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89f7e1b
 
 
 
 
 
24c6d78
 
89f7e1b
 
 
 
 
 
 
24c6d78
 
 
 
 
eabc720
24c6d78
 
 
 
 
 
 
 
 
 
 
 
6eec5de
74dc566
 
 
 
 
 
 
 
 
 
 
24c6d78
 
 
 
 
 
 
 
 
 
 
 
 
 
8439d88
6eec5de
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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"}