Update app.py
Browse files
app.py
CHANGED
|
@@ -5,12 +5,13 @@ from huggingface_hub import hf_hub_download
|
|
| 5 |
import tensorflow as tf
|
| 6 |
from tensorflow.keras.applications.efficientnet import preprocess_input
|
| 7 |
|
| 8 |
-
# ββ Config βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 9 |
-
MODEL_REPO
|
| 10 |
-
MODEL_FILE
|
| 11 |
-
LABELS_FILE
|
| 12 |
-
IMG_SIZE
|
| 13 |
CONFIDENCE_THRESHOLD = 0.60
|
|
|
|
| 14 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 15 |
|
| 16 |
print("Loading model...")
|
|
@@ -25,26 +26,41 @@ print("Classes:", class_names)
|
|
| 25 |
print("Model ready!")
|
| 26 |
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def predict(image):
|
| 29 |
if image is None:
|
| 30 |
return "Please upload an image.", {}
|
| 31 |
|
| 32 |
img = image.convert("RGB").resize(IMG_SIZE)
|
| 33 |
-
|
| 34 |
-
x = preprocess_input(x)
|
| 35 |
-
x = np.expand_dims(x, 0)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
preds = model.predict(x, verbose=0)[0]
|
| 38 |
|
| 39 |
top_idx = int(np.argmax(preds))
|
| 40 |
top_score = float(preds[top_idx])
|
| 41 |
-
|
| 42 |
-
scores = {class_names[i]: float(preds[i]) for i in range(len(preds))}
|
| 43 |
|
| 44 |
if top_score < CONFIDENCE_THRESHOLD:
|
| 45 |
-
label = f"Not an artifact
|
| 46 |
else:
|
| 47 |
-
label = f"{class_names[top_idx]}
|
| 48 |
|
| 49 |
return label, scores
|
| 50 |
|
|
|
|
| 5 |
import tensorflow as tf
|
| 6 |
from tensorflow.keras.applications.efficientnet import preprocess_input
|
| 7 |
|
| 8 |
+
# ββ Config βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 9 |
+
MODEL_REPO = "Unearthed/artifact-classifier"
|
| 10 |
+
MODEL_FILE = "artifact_classifier.keras"
|
| 11 |
+
LABELS_FILE = "labels.txt"
|
| 12 |
+
IMG_SIZE = (224, 224)
|
| 13 |
CONFIDENCE_THRESHOLD = 0.60
|
| 14 |
+
TEXTURE_THRESHOLD = 0.15
|
| 15 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 16 |
|
| 17 |
print("Loading model...")
|
|
|
|
| 26 |
print("Model ready!")
|
| 27 |
|
| 28 |
|
| 29 |
+
def texture_score(img_array):
|
| 30 |
+
gray = np.mean(img_array, axis=2)
|
| 31 |
+
patch_size = 8
|
| 32 |
+
h, w = gray.shape
|
| 33 |
+
variances = []
|
| 34 |
+
for i in range(0, h - patch_size, patch_size):
|
| 35 |
+
for j in range(0, w - patch_size, patch_size):
|
| 36 |
+
patch = gray[i:i+patch_size, j:j+patch_size]
|
| 37 |
+
variances.append(np.var(patch))
|
| 38 |
+
return min(np.mean(variances) / 300.0, 1.0)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
def predict(image):
|
| 42 |
if image is None:
|
| 43 |
return "Please upload an image.", {}
|
| 44 |
|
| 45 |
img = image.convert("RGB").resize(IMG_SIZE)
|
| 46 |
+
img_array = np.array(img, dtype=np.float32)
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
tex = texture_score(img_array)
|
| 49 |
+
if tex < TEXTURE_THRESHOLD:
|
| 50 |
+
return f"Not an artifact β no stone texture detected (texture score: {tex:.2f})", {"notartifact": 1.0}
|
| 51 |
+
|
| 52 |
+
x = preprocess_input(img_array.copy())
|
| 53 |
+
x = np.expand_dims(x, 0)
|
| 54 |
preds = model.predict(x, verbose=0)[0]
|
| 55 |
|
| 56 |
top_idx = int(np.argmax(preds))
|
| 57 |
top_score = float(preds[top_idx])
|
| 58 |
+
scores = {class_names[i]: float(preds[i]) for i in range(len(preds))}
|
|
|
|
| 59 |
|
| 60 |
if top_score < CONFIDENCE_THRESHOLD:
|
| 61 |
+
label = f"Not an artifact ({top_score*100:.1f}% confidence β below {CONFIDENCE_THRESHOLD*100:.0f}% threshold)"
|
| 62 |
else:
|
| 63 |
+
label = f"{class_names[top_idx]} ({top_score*100:.1f}% confidence)"
|
| 64 |
|
| 65 |
return label, scores
|
| 66 |
|