Spaces:
Sleeping
Sleeping
Commit ·
f36ab4c
1
Parent(s): b35ee7b
- app.py +310 -27
- models/model0.pth +3 -0
app.py
CHANGED
|
@@ -1,25 +1,14 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
from flask import Flask, jsonify, request
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
app = Flask(__name__)
|
| 6 |
|
| 7 |
-
|
| 8 |
-
def database_connection():
|
| 9 |
-
try:
|
| 10 |
-
connection = mysql.connector.connect(
|
| 11 |
-
host = "147.93.17.134",
|
| 12 |
-
user = "u531297160_api",
|
| 13 |
-
password = "Myapi@123",
|
| 14 |
-
database = "u531297160_api",
|
| 15 |
-
port = "3306"
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
return connection
|
| 19 |
|
| 20 |
-
except mysql.connector.Error as e:
|
| 21 |
-
print(f"MySQL connection error: {e}")
|
| 22 |
-
return None
|
| 23 |
|
| 24 |
# Home route
|
| 25 |
@app.route("/")
|
|
@@ -32,16 +21,7 @@ def home():
|
|
| 32 |
# Health check route
|
| 33 |
@app.route("/health", methods=["GET", "POST"])
|
| 34 |
def health():
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
if connection:
|
| 38 |
-
connection.close()
|
| 39 |
-
|
| 40 |
-
return jsonify({
|
| 41 |
-
"status": "healthy",
|
| 42 |
-
"database": "connected",
|
| 43 |
-
"method": request.method
|
| 44 |
-
}), 200
|
| 45 |
|
| 46 |
return jsonify({
|
| 47 |
"status": "unhealthy",
|
|
@@ -49,6 +29,309 @@ def health():
|
|
| 49 |
"method": request.method
|
| 50 |
}), 503
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
if __name__ == "__main__":
|
| 53 |
port = int(os.environ.get("PORT", 7860))
|
| 54 |
app.run(host="0.0.0.0", port=port, debug=False)
|
|
|
|
| 1 |
import os
|
| 2 |
+
|
| 3 |
from flask import Flask, jsonify, request
|
| 4 |
+
import cv2
|
| 5 |
+
import torch
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
from torchvision import models, transforms
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
+
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Home route
|
| 14 |
@app.route("/")
|
|
|
|
| 21 |
# Health check route
|
| 22 |
@app.route("/health", methods=["GET", "POST"])
|
| 23 |
def health():
|
| 24 |
+
return jsonify({ "status": "healthy", }), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
return jsonify({
|
| 27 |
"status": "unhealthy",
|
|
|
|
| 29 |
"method": request.method
|
| 30 |
}), 503
|
| 31 |
|
| 32 |
+
|
| 33 |
+
@app.route("/model/0", methods=["GET", "POST"])
|
| 34 |
+
def model_0():
|
| 35 |
+
|
| 36 |
+
import cv2
|
| 37 |
+
import torch
|
| 38 |
+
import torch.nn as nn
|
| 39 |
+
import numpy as np
|
| 40 |
+
from torchvision import models, transforms
|
| 41 |
+
|
| 42 |
+
# ========================================================
|
| 43 |
+
# CONFIG
|
| 44 |
+
# ========================================================
|
| 45 |
+
|
| 46 |
+
MODEL_PATH = "model0.pth"
|
| 47 |
+
SAJAN_THRESHOLD = 0.90
|
| 48 |
+
|
| 49 |
+
device = torch.device("cpu")
|
| 50 |
+
|
| 51 |
+
# ========================================================
|
| 52 |
+
# LOAD MODEL
|
| 53 |
+
# ========================================================
|
| 54 |
+
|
| 55 |
+
checkpoint = torch.load(
|
| 56 |
+
MODEL_PATH,
|
| 57 |
+
map_location=device,
|
| 58 |
+
weights_only=False
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
classes = checkpoint["classes"]
|
| 62 |
+
|
| 63 |
+
model = models.mobilenet_v3_small(
|
| 64 |
+
weights=None
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
model.classifier[3] = nn.Linear(
|
| 68 |
+
model.classifier[3].in_features,
|
| 69 |
+
len(classes)
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
model.load_state_dict(
|
| 73 |
+
checkpoint["model_state_dict"]
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
model.to(device)
|
| 77 |
+
model.eval()
|
| 78 |
+
|
| 79 |
+
# ========================================================
|
| 80 |
+
# PREPROCESSING
|
| 81 |
+
# ========================================================
|
| 82 |
+
|
| 83 |
+
transform = transforms.Compose([
|
| 84 |
+
transforms.ToPILImage(),
|
| 85 |
+
transforms.Resize((224, 224)),
|
| 86 |
+
transforms.ToTensor(),
|
| 87 |
+
transforms.Normalize(
|
| 88 |
+
mean=[0.485, 0.456, 0.406],
|
| 89 |
+
std=[0.229, 0.224, 0.225]
|
| 90 |
+
)
|
| 91 |
+
])
|
| 92 |
+
|
| 93 |
+
# ========================================================
|
| 94 |
+
# FACE DETECTOR
|
| 95 |
+
# ========================================================
|
| 96 |
+
|
| 97 |
+
face_detector = cv2.CascadeClassifier(
|
| 98 |
+
cv2.data.haarcascades +
|
| 99 |
+
"haarcascade_frontalface_default.xml"
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# ========================================================
|
| 103 |
+
# GET REQUEST
|
| 104 |
+
# ========================================================
|
| 105 |
+
|
| 106 |
+
if request.method == "GET":
|
| 107 |
+
|
| 108 |
+
return jsonify({
|
| 109 |
+
"status": "model is running",
|
| 110 |
+
"model": "MobileNetV3-Small",
|
| 111 |
+
"classes": classes
|
| 112 |
+
}), 200
|
| 113 |
+
|
| 114 |
+
# ========================================================
|
| 115 |
+
# RECEIVE IMAGE
|
| 116 |
+
# ========================================================
|
| 117 |
+
|
| 118 |
+
if "image" not in request.files:
|
| 119 |
+
|
| 120 |
+
return jsonify({
|
| 121 |
+
"status": "error",
|
| 122 |
+
"message": "image is required"
|
| 123 |
+
}), 400
|
| 124 |
+
|
| 125 |
+
try:
|
| 126 |
+
|
| 127 |
+
file = request.files["image"]
|
| 128 |
+
|
| 129 |
+
image_bytes = file.read()
|
| 130 |
+
|
| 131 |
+
image_array = np.frombuffer(
|
| 132 |
+
image_bytes,
|
| 133 |
+
dtype=np.uint8
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
frame = cv2.imdecode(
|
| 137 |
+
image_array,
|
| 138 |
+
cv2.IMREAD_COLOR
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
if frame is None:
|
| 142 |
+
|
| 143 |
+
return jsonify({
|
| 144 |
+
"status": "error",
|
| 145 |
+
"message": "invalid image"
|
| 146 |
+
}), 400
|
| 147 |
+
|
| 148 |
+
# ====================================================
|
| 149 |
+
# FACE DETECTION
|
| 150 |
+
# ====================================================
|
| 151 |
+
|
| 152 |
+
gray = cv2.cvtColor(
|
| 153 |
+
frame,
|
| 154 |
+
cv2.COLOR_BGR2GRAY
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
faces = face_detector.detectMultiScale(
|
| 158 |
+
gray,
|
| 159 |
+
scaleFactor=1.1,
|
| 160 |
+
minNeighbors=6,
|
| 161 |
+
minSize=(50, 50)
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
# ====================================================
|
| 165 |
+
# NO FACE = UNKNOWN
|
| 166 |
+
# ====================================================
|
| 167 |
+
|
| 168 |
+
if len(faces) == 0:
|
| 169 |
+
|
| 170 |
+
return jsonify({
|
| 171 |
+
"status": "success",
|
| 172 |
+
"prediction": "UNKNOWN",
|
| 173 |
+
"confidence": 0,
|
| 174 |
+
"face_detected": False,
|
| 175 |
+
"bounding_box": None
|
| 176 |
+
}), 200
|
| 177 |
+
|
| 178 |
+
# ====================================================
|
| 179 |
+
# TAKE LARGEST FACE
|
| 180 |
+
# ====================================================
|
| 181 |
+
|
| 182 |
+
x, y, w, h = max(
|
| 183 |
+
faces,
|
| 184 |
+
key=lambda face: face[2] * face[3]
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
# ====================================================
|
| 188 |
+
# FACE MARGIN
|
| 189 |
+
# ====================================================
|
| 190 |
+
|
| 191 |
+
margin_x = int(w * 0.15)
|
| 192 |
+
margin_y = int(h * 0.15)
|
| 193 |
+
|
| 194 |
+
x1 = max(
|
| 195 |
+
0,
|
| 196 |
+
x - margin_x
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
y1 = max(
|
| 200 |
+
0,
|
| 201 |
+
y - margin_y
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
x2 = min(
|
| 205 |
+
frame.shape[1],
|
| 206 |
+
x + w + margin_x
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
y2 = min(
|
| 210 |
+
frame.shape[0],
|
| 211 |
+
y + h + margin_y
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
+
# ====================================================
|
| 215 |
+
# CROP FACE
|
| 216 |
+
# ====================================================
|
| 217 |
+
|
| 218 |
+
face = frame[
|
| 219 |
+
y1:y2,
|
| 220 |
+
x1:x2
|
| 221 |
+
]
|
| 222 |
+
|
| 223 |
+
if face.size == 0:
|
| 224 |
+
|
| 225 |
+
return jsonify({
|
| 226 |
+
"status": "success",
|
| 227 |
+
"prediction": "UNKNOWN",
|
| 228 |
+
"confidence": 0,
|
| 229 |
+
"face_detected": False,
|
| 230 |
+
"bounding_box": None
|
| 231 |
+
}), 200
|
| 232 |
+
|
| 233 |
+
# ====================================================
|
| 234 |
+
# PREPROCESS
|
| 235 |
+
# ====================================================
|
| 236 |
+
|
| 237 |
+
tensor = transform(
|
| 238 |
+
face
|
| 239 |
+
).unsqueeze(0).to(device)
|
| 240 |
+
|
| 241 |
+
# ====================================================
|
| 242 |
+
# PREDICTION
|
| 243 |
+
# ====================================================
|
| 244 |
+
|
| 245 |
+
with torch.inference_mode():
|
| 246 |
+
|
| 247 |
+
output = model(tensor)
|
| 248 |
+
|
| 249 |
+
probabilities = torch.softmax(
|
| 250 |
+
output,
|
| 251 |
+
dim=1
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
# ====================================================
|
| 255 |
+
# FIND SAJAN CLASS
|
| 256 |
+
# ====================================================
|
| 257 |
+
|
| 258 |
+
sajan_index = None
|
| 259 |
+
|
| 260 |
+
for i, class_name in enumerate(classes):
|
| 261 |
+
|
| 262 |
+
if class_name.lower() == "sajan":
|
| 263 |
+
|
| 264 |
+
sajan_index = i
|
| 265 |
+
break
|
| 266 |
+
|
| 267 |
+
# ====================================================
|
| 268 |
+
# SAJAN CONFIDENCE
|
| 269 |
+
# ====================================================
|
| 270 |
+
|
| 271 |
+
if sajan_index is not None:
|
| 272 |
+
|
| 273 |
+
sajan_confidence = float(
|
| 274 |
+
probabilities[
|
| 275 |
+
0,
|
| 276 |
+
sajan_index
|
| 277 |
+
].item()
|
| 278 |
+
)
|
| 279 |
+
|
| 280 |
+
else:
|
| 281 |
+
|
| 282 |
+
sajan_confidence = 0.0
|
| 283 |
+
|
| 284 |
+
# ====================================================
|
| 285 |
+
# FINAL DECISION
|
| 286 |
+
# ====================================================
|
| 287 |
+
|
| 288 |
+
if sajan_confidence >= SAJAN_THRESHOLD:
|
| 289 |
+
|
| 290 |
+
prediction = "Sajan"
|
| 291 |
+
|
| 292 |
+
else:
|
| 293 |
+
|
| 294 |
+
prediction = "UNKNOWN"
|
| 295 |
+
|
| 296 |
+
# ====================================================
|
| 297 |
+
# RESPONSE
|
| 298 |
+
# ====================================================
|
| 299 |
+
|
| 300 |
+
return jsonify({
|
| 301 |
+
|
| 302 |
+
"status": "success",
|
| 303 |
+
|
| 304 |
+
"prediction": prediction,
|
| 305 |
+
|
| 306 |
+
"confidence": round(
|
| 307 |
+
sajan_confidence,
|
| 308 |
+
4
|
| 309 |
+
),
|
| 310 |
+
|
| 311 |
+
"confidence_percent": round(
|
| 312 |
+
sajan_confidence * 100,
|
| 313 |
+
2
|
| 314 |
+
),
|
| 315 |
+
|
| 316 |
+
"face_detected": True,
|
| 317 |
+
|
| 318 |
+
"bounding_box": {
|
| 319 |
+
"x": int(x1),
|
| 320 |
+
"y": int(y1),
|
| 321 |
+
"width": int(x2 - x1),
|
| 322 |
+
"height": int(y2 - y1)
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
}), 200
|
| 326 |
+
|
| 327 |
+
except Exception as e:
|
| 328 |
+
|
| 329 |
+
return jsonify({
|
| 330 |
+
"status": "error",
|
| 331 |
+
"message": str(e)
|
| 332 |
+
}), 500
|
| 333 |
+
|
| 334 |
+
|
| 335 |
if __name__ == "__main__":
|
| 336 |
port = int(os.environ.get("PORT", 7860))
|
| 337 |
app.run(host="0.0.0.0", port=port, debug=False)
|
models/model0.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:82ffc6f47dd92c6458e709bd9aea9033c3f432294f5188fd3954e856fdd947f6
|
| 3 |
+
size 6219469
|