updated requirements
Browse files- src/app/app.py → app.py +0 -0
- requirements.txt +3 -11
- src/pipeline/prediction_pipeline.py +41 -33
src/app/app.py → app.py
RENAMED
|
File without changes
|
requirements.txt
CHANGED
|
@@ -1,13 +1,5 @@
|
|
| 1 |
-
numpy
|
| 2 |
-
pandas
|
| 3 |
-
matplotlib
|
| 4 |
-
seaborn
|
| 5 |
tensorflow
|
| 6 |
-
opencv-python
|
| 7 |
-
fastapi
|
| 8 |
-
uvicorn
|
| 9 |
-
python-dotenv
|
| 10 |
Pillow
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
tensorflow
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
Pillow
|
| 3 |
+
numpy
|
| 4 |
+
gradio
|
| 5 |
+
huggingface_hub
|
src/pipeline/prediction_pipeline.py
CHANGED
|
@@ -5,12 +5,30 @@ from PIL import Image, ImageDraw, ImageFont
|
|
| 5 |
import tensorflow as tf
|
| 6 |
import json
|
| 7 |
|
|
|
|
|
|
|
| 8 |
from src.config import Config
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
CATEGORY_LABELS_PATH = Path(Config.MODEL_DIR) / "category_labels.json"
|
| 14 |
with open(CATEGORY_LABELS_PATH, "r") as f:
|
| 15 |
CATEGORY_LABELS = json.load(f)
|
| 16 |
|
|
@@ -19,6 +37,7 @@ FRESHNESS_LABELS = ["Fresh", "Rotten"]
|
|
| 19 |
CATEGORY_IMG_SIZE = (224, 224)
|
| 20 |
FRESHNESS_IMG_SIZE = (224, 224)
|
| 21 |
|
|
|
|
| 22 |
class PredictionPipeline:
|
| 23 |
def __init__(self):
|
| 24 |
self.category_model = tf.keras.models.load_model(CATEGORY_MODEL_PATH)
|
|
@@ -28,33 +47,29 @@ class PredictionPipeline:
|
|
| 28 |
|
| 29 |
def _preprocess_image(self, img, target_size, normalize=True):
|
| 30 |
if isinstance(img, (str, Path)):
|
| 31 |
-
|
| 32 |
-
if not img_path.exists():
|
| 33 |
-
raise FileNotFoundError(f"Image not found: {img_path.resolve()}")
|
| 34 |
-
img = Image.open(str(img_path)).convert("RGB")
|
| 35 |
elif isinstance(img, np.ndarray):
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
else:
|
| 39 |
-
raise ValueError("NumPy input must be shape (H, W, 3)")
|
| 40 |
img_resized = img.resize(target_size)
|
| 41 |
img_array = np.array(img_resized).astype("float32")
|
|
|
|
| 42 |
if normalize:
|
| 43 |
img_array = img_array / 255.0
|
|
|
|
| 44 |
img_array = np.expand_dims(img_array, axis=0)
|
| 45 |
return img_array, img
|
| 46 |
|
| 47 |
-
|
| 48 |
def predict(self, img):
|
| 49 |
-
|
| 50 |
-
cat_img_array, pil_img = self._preprocess_image(img, CATEGORY_IMG_SIZE
|
| 51 |
cat_pred = self.category_model.predict(cat_img_array)
|
| 52 |
cat_idx = int(np.argmax(cat_pred))
|
| 53 |
cat_label = self.category_labels[cat_idx]
|
| 54 |
cat_score = float(np.max(cat_pred))
|
| 55 |
|
| 56 |
-
|
| 57 |
-
fresh_img_array, _ = self._preprocess_image(img, FRESHNESS_IMG_SIZE
|
| 58 |
fresh_pred = self.freshness_model.predict(fresh_img_array)
|
| 59 |
fresh_idx = int(np.argmax(fresh_pred))
|
| 60 |
fresh_label = self.freshness_labels[fresh_idx]
|
|
@@ -63,31 +78,24 @@ class PredictionPipeline:
|
|
| 63 |
return {
|
| 64 |
"category": {"label": cat_label, "idx": cat_idx, "score": cat_score},
|
| 65 |
"freshness": {"label": fresh_label, "idx": fresh_idx, "score": fresh_score},
|
| 66 |
-
"pil_img": pil_img
|
| 67 |
}
|
| 68 |
|
| 69 |
def annotate(self, img, result, font_size=28):
|
| 70 |
pil_img = result["pil_img"]
|
| 71 |
draw = ImageDraw.Draw(pil_img)
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
try:
|
| 75 |
font = ImageFont.truetype("arial.ttf", font_size)
|
| 76 |
except:
|
| 77 |
font = ImageFont.load_default()
|
| 78 |
-
draw.rectangle([0, 0, pil_img.width, font_size+8], fill=(0,0,0,160))
|
| 79 |
-
draw.text((5, 2), text, fill=(255, 255, 255), font=font)
|
| 80 |
-
return pil_img
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
img_path = r"artifacts\data\category\test\Apple\rottenApple (175).jpg"
|
| 86 |
-
|
| 87 |
-
result = pipeline.predict(img_path)
|
| 88 |
-
|
| 89 |
-
print("Prediction:", result["category"], "|", result["freshness"])
|
| 90 |
-
|
| 91 |
-
annotated_img = pipeline.annotate(img_path, result)
|
| 92 |
|
| 93 |
-
|
|
|
|
| 5 |
import tensorflow as tf
|
| 6 |
import json
|
| 7 |
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
+
|
| 10 |
from src.config import Config
|
| 11 |
|
| 12 |
+
# ------------------------
|
| 13 |
+
# Load models from Hugging Face Hub
|
| 14 |
+
# ------------------------
|
| 15 |
+
|
| 16 |
+
CATEGORY_MODEL_PATH = hf_hub_download(
|
| 17 |
+
repo_id="samithcs/Category_Classifier",
|
| 18 |
+
filename="category_classifier.keras"
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
FRESHNESS_MODEL_PATH = hf_hub_download(
|
| 22 |
+
repo_id="samithcs/Classifier_Fruits_Vegetables",
|
| 23 |
+
filename="mobilenetv2_baseline.keras"
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# ------------------------
|
| 27 |
+
# Load labels from LOCAL FILE
|
| 28 |
+
# ------------------------
|
| 29 |
+
|
| 30 |
+
CATEGORY_LABELS_PATH = Path("artifacts/models/category_labels.json")
|
| 31 |
|
|
|
|
| 32 |
with open(CATEGORY_LABELS_PATH, "r") as f:
|
| 33 |
CATEGORY_LABELS = json.load(f)
|
| 34 |
|
|
|
|
| 37 |
CATEGORY_IMG_SIZE = (224, 224)
|
| 38 |
FRESHNESS_IMG_SIZE = (224, 224)
|
| 39 |
|
| 40 |
+
|
| 41 |
class PredictionPipeline:
|
| 42 |
def __init__(self):
|
| 43 |
self.category_model = tf.keras.models.load_model(CATEGORY_MODEL_PATH)
|
|
|
|
| 47 |
|
| 48 |
def _preprocess_image(self, img, target_size, normalize=True):
|
| 49 |
if isinstance(img, (str, Path)):
|
| 50 |
+
img = Image.open(str(img)).convert("RGB")
|
|
|
|
|
|
|
|
|
|
| 51 |
elif isinstance(img, np.ndarray):
|
| 52 |
+
img = Image.fromarray(img)
|
| 53 |
+
|
|
|
|
|
|
|
| 54 |
img_resized = img.resize(target_size)
|
| 55 |
img_array = np.array(img_resized).astype("float32")
|
| 56 |
+
|
| 57 |
if normalize:
|
| 58 |
img_array = img_array / 255.0
|
| 59 |
+
|
| 60 |
img_array = np.expand_dims(img_array, axis=0)
|
| 61 |
return img_array, img
|
| 62 |
|
|
|
|
| 63 |
def predict(self, img):
|
| 64 |
+
# Category prediction
|
| 65 |
+
cat_img_array, pil_img = self._preprocess_image(img, CATEGORY_IMG_SIZE)
|
| 66 |
cat_pred = self.category_model.predict(cat_img_array)
|
| 67 |
cat_idx = int(np.argmax(cat_pred))
|
| 68 |
cat_label = self.category_labels[cat_idx]
|
| 69 |
cat_score = float(np.max(cat_pred))
|
| 70 |
|
| 71 |
+
# Freshness prediction
|
| 72 |
+
fresh_img_array, _ = self._preprocess_image(img, FRESHNESS_IMG_SIZE)
|
| 73 |
fresh_pred = self.freshness_model.predict(fresh_img_array)
|
| 74 |
fresh_idx = int(np.argmax(fresh_pred))
|
| 75 |
fresh_label = self.freshness_labels[fresh_idx]
|
|
|
|
| 78 |
return {
|
| 79 |
"category": {"label": cat_label, "idx": cat_idx, "score": cat_score},
|
| 80 |
"freshness": {"label": fresh_label, "idx": fresh_idx, "score": fresh_score},
|
| 81 |
+
"pil_img": pil_img,
|
| 82 |
}
|
| 83 |
|
| 84 |
def annotate(self, img, result, font_size=28):
|
| 85 |
pil_img = result["pil_img"]
|
| 86 |
draw = ImageDraw.Draw(pil_img)
|
| 87 |
+
|
| 88 |
+
text = (
|
| 89 |
+
f"{result['category']['label']} ({result['category']['score']:.2f}) | "
|
| 90 |
+
f"{result['freshness']['label']} ({result['freshness']['score']:.2f})"
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
try:
|
| 94 |
font = ImageFont.truetype("arial.ttf", font_size)
|
| 95 |
except:
|
| 96 |
font = ImageFont.load_default()
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
draw.rectangle([0, 0, pil_img.width, font_size + 8], fill=(0, 0, 0, 160))
|
| 99 |
+
draw.text((5, 2), text, fill=(255, 255, 255), font=font)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
return pil_img
|