Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +68 -0
- face_shape_model.h5 +3 -0
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image, UnidentifiedImageError
|
| 6 |
+
|
| 7 |
+
# Model yükleme
|
| 8 |
+
try:
|
| 9 |
+
model = tf.keras.models.load_model("face_shape_model.h5")
|
| 10 |
+
except Exception as e:
|
| 11 |
+
raise RuntimeError(f"Model loading failed: {str(e)}")
|
| 12 |
+
|
| 13 |
+
# Sınıf isimleri
|
| 14 |
+
class_names = ['Heart', 'Oblong', 'Oval', 'Round', 'Square']
|
| 15 |
+
|
| 16 |
+
# FastAPI uygulamasını başlat
|
| 17 |
+
app = FastAPI()
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# Resmi yükle ve ön işle
|
| 21 |
+
def load_and_preprocess_image(image):
|
| 22 |
+
try:
|
| 23 |
+
# Eğer resim RGBA veya diğer modda ise RGB'ye çevir
|
| 24 |
+
if image.mode != "RGB":
|
| 25 |
+
image = image.convert("RGB")
|
| 26 |
+
|
| 27 |
+
# Resmi yeniden boyutlandır
|
| 28 |
+
img = image.resize((224, 224))
|
| 29 |
+
# Array'e çevir ve normalize et
|
| 30 |
+
img_array = img_to_array(img) / 255.0
|
| 31 |
+
# Batch boyutunu ekle
|
| 32 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 33 |
+
return img_array
|
| 34 |
+
except Exception as e:
|
| 35 |
+
raise ValueError(f"Preprocessing error: {str(e)}")
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@app.post("/predict/")
|
| 39 |
+
async def predict(file: UploadFile = File(...)):
|
| 40 |
+
try:
|
| 41 |
+
# Yüklenen dosyayı aç
|
| 42 |
+
try:
|
| 43 |
+
image = Image.open(file.file)
|
| 44 |
+
except UnidentifiedImageError as e:
|
| 45 |
+
return {"error": f"Invalid image file: {str(e)}"}
|
| 46 |
+
|
| 47 |
+
# Resmi yükle ve ön işle
|
| 48 |
+
img_array = load_and_preprocess_image(image)
|
| 49 |
+
|
| 50 |
+
# Tahmin yap
|
| 51 |
+
predictions = model.predict(img_array, verbose=0)
|
| 52 |
+
predicted_class = class_names[np.argmax(predictions[0])]
|
| 53 |
+
confidence = np.max(predictions[0]) * 100
|
| 54 |
+
|
| 55 |
+
# Tüm sınıfların olasılıklarını hesapla
|
| 56 |
+
class_probabilities = {
|
| 57 |
+
class_names[i]: float(predictions[0][i] * 100)
|
| 58 |
+
for i in range(len(class_names))
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
return {
|
| 62 |
+
"predicted_class": predicted_class,
|
| 63 |
+
"confidence": f"{confidence:.2f}%",
|
| 64 |
+
"class_probabilities": class_probabilities
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
except Exception as e:
|
| 68 |
+
return {"error": f"Prediction failed: {str(e)}"}
|
face_shape_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:69b280ddee29554842a41ca899870e2bf81e7f3981a6f18860049c9c66371d1e
|
| 3 |
+
size 243366704
|