Spaces:
Sleeping
Sleeping
File size: 1,997 Bytes
a62fcb2 0323a42 a62fcb2 a71fa5b 0323a42 1758e8f a62fcb2 0323a42 bd3ff0c 1758e8f a71fa5b 5f5ea10 a62fcb2 0323a42 a71fa5b 0323a42 1758e8f 0323a42 3d908cb a71fa5b 0323a42 3d908cb a62fcb2 a71fa5b 3d908cb a71fa5b 0323a42 a62fcb2 a71fa5b a62fcb2 0323a42 3d908cb | 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 | import gradio as gr
import tensorflow as tf
import joblib
import numpy as np
from rule_based_quality import RuleBasedQualityEvaluator
from preprocess_cnn import preprocess_for_cnn
from preprocess_quality import GeometricFeatureExtractor
cnn_model = tf.keras.models.load_model("custom_cnn_shape.keras")
xgb_quality = joblib.load("xgb_quality.pkl")
scaler = joblib.load("scaler.pkl")
le_quality = joblib.load("le_quality.pkl")
le_shape = joblib.load("le_shape.pkl")
feature_extractor = GeometricFeatureExtractor()
rule_evaluator = RuleBasedQualityEvaluator()
SHAPES = ['Triangle', 'Square', 'Circle', 'Rectangle']
def predict(image):
response = {}
x = preprocess_for_cnn(image)
shape_probs = cnn_model.predict(x, verbose=0)[0]
shape_idx = int(np.argmax(shape_probs))
shape_label = le_shape.inverse_transform([shape_idx])[0]
response["shape"] = shape_label
response["shape_confidence"] = float(shape_probs[shape_idx])
features = feature_extractor.extract_features(image)
if features is None:
response["quality"] = "unknown"
response["quality_confidence"] = 0.0
response["rl_quality"] = "unknown"
response["rl_confidence"] = 0.0
return response
features_scaled = scaler.transform(features.reshape(1, -1))
quality_probs = xgb_quality.predict_proba(features_scaled)[0]
q_idx = int(np.argmax(quality_probs))
response["quality"] = le_quality.inverse_transform([q_idx])[0]
response["quality_confidence"] = float(quality_probs[q_idx])
rl_label, rl_conf = rule_evaluator.evaluate(
feature_extractor.last_feature_dict,
shape_label
)
response["rl_quality"] = rl_label
response["rl_confidence"] = float(rl_conf)
return response
gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs="json",
title="Shape & Quality Recognition System",
description="CNN (Keras) for shape recognition + XGBoost for drawing quality assessment"
).launch() |