Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- .gitattributes +1 -0
- accident_model.keras +3 -0
- app.py +50 -0
- metadata.json +19 -0
- requirements.txt +4 -0
.gitattributes
CHANGED
|
@@ -34,3 +34,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
backend/accident_model.keras filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
backend/accident_model.keras filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
accident_model.keras filter=lfs diff=lfs merge=lfs -text
|
accident_model.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a119e7725838b3813adb595c7d706c987a666b1f5c0ea36fb38bf1d367238d9f
|
| 3 |
+
size 31873058
|
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
import json
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Load metadata
|
| 9 |
+
CLASSES = ['Minor', 'Serious', 'Fatal']
|
| 10 |
+
IMG_SIZE = 224
|
| 11 |
+
|
| 12 |
+
if os.path.exists("metadata.json"):
|
| 13 |
+
with open("metadata.json") as f:
|
| 14 |
+
meta = json.load(f)
|
| 15 |
+
CLASSES = meta.get("classes", CLASSES)
|
| 16 |
+
IMG_SIZE = meta.get("img_size", IMG_SIZE)
|
| 17 |
+
|
| 18 |
+
# Load model
|
| 19 |
+
model = tf.keras.models.load_model("model.keras")
|
| 20 |
+
|
| 21 |
+
def preprocess(image):
|
| 22 |
+
img = image.resize((IMG_SIZE, IMG_SIZE))
|
| 23 |
+
img = np.array(img, dtype=np.float32) / 255.0
|
| 24 |
+
return np.expand_dims(img, 0)
|
| 25 |
+
|
| 26 |
+
def predict(image):
|
| 27 |
+
img_array = preprocess(image)
|
| 28 |
+
|
| 29 |
+
preds = model.predict(img_array)[0]
|
| 30 |
+
idx = int(np.argmax(preds))
|
| 31 |
+
|
| 32 |
+
probs = {
|
| 33 |
+
CLASSES[i]: float(preds[i] * 100)
|
| 34 |
+
for i in range(len(CLASSES))
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
return {
|
| 38 |
+
"severity": CLASSES[idx],
|
| 39 |
+
"confidence": f"{preds[idx]*100:.2f}%",
|
| 40 |
+
"probabilities": probs
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
iface = gr.Interface(
|
| 44 |
+
fn=predict,
|
| 45 |
+
inputs=gr.Image(type="pil"),
|
| 46 |
+
outputs="json",
|
| 47 |
+
title="Accident Severity Prediction"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
iface.launch()
|
metadata.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"classes": [
|
| 3 |
+
"Minor",
|
| 4 |
+
"Serious",
|
| 5 |
+
"Fatal"
|
| 6 |
+
],
|
| 7 |
+
"folder_classes": [
|
| 8 |
+
"1",
|
| 9 |
+
"2",
|
| 10 |
+
"3"
|
| 11 |
+
],
|
| 12 |
+
"label_map": {
|
| 13 |
+
"1": "Minor",
|
| 14 |
+
"2": "Serious",
|
| 15 |
+
"3": "Fatal"
|
| 16 |
+
},
|
| 17 |
+
"img_size": 224,
|
| 18 |
+
"model_type": "MobileNetV2"
|
| 19 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
tensorflow
|
| 3 |
+
numpy
|
| 4 |
+
pillow
|