Uploaded model and examples
Browse files- MCS-Classify.pt +3 -0
- README.md +2 -2
- app.py +100 -0
- examples/1.jpg +0 -0
- examples/10.jpg +0 -0
- examples/11.jpg +0 -0
- examples/12.jpg +0 -0
- examples/2.jpg +0 -0
- examples/3.jpg +0 -0
- examples/4.jpg +0 -0
- examples/5.jpg +0 -0
- examples/6.jpg +0 -0
- examples/7.jpg +0 -0
- examples/8.jpg +0 -0
- examples/9.jpg +0 -0
- requirements.txt +8 -0
MCS-Classify.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a03e2b79be60934dd222fe016590ae587ad619d3a28a70fa3b05d25ba4ed8ff3
|
3 |
+
size 20907727
|
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
title: Maltese Christian Statue Classification
|
3 |
-
emoji:
|
4 |
colorFrom: red
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.12.0
|
8 |
app_file: app.py
|
|
|
1 |
---
|
2 |
title: Maltese Christian Statue Classification
|
3 |
+
emoji: ✝
|
4 |
colorFrom: red
|
5 |
+
colorTo: purple
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.12.0
|
8 |
app_file: app.py
|
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List, Tuple
|
2 |
+
import gradio as gr
|
3 |
+
from ultralytics import YOLO
|
4 |
+
import cv2
|
5 |
+
import os
|
6 |
+
import torch
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
# Check device availability
|
10 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
11 |
+
|
12 |
+
# Categories in English and Maltese
|
13 |
+
categories_english = {
|
14 |
+
0: 'Jesus has Risen', 1: 'Jesus praying in Gethsemane', 2: 'Saint Philip of Agira',
|
15 |
+
3: 'Simon of Cyrene', 4: 'The Betrayal of Judas', 5: 'The Cross',
|
16 |
+
6: 'The Crucifixion', 7: 'The Ecce Homo', 8: 'The Flogged',
|
17 |
+
9: 'The Lady of Sorrows', 10: 'The Last Supper', 11: 'The Monument',
|
18 |
+
12: 'The Redeemer', 13: 'The Veronica'
|
19 |
+
}
|
20 |
+
|
21 |
+
categories_maltese = {
|
22 |
+
0: 'L-Irxoxt', 1: 'Ġesù fl-Ort tal-Ġetsemani', 2: 'San Filep ta’ Aġġira',
|
23 |
+
3: 'Xmun min Ċireni', 4: 'It-Tradiment ta’ Ġuda', 5: 'Is-Salib',
|
24 |
+
6: 'Il-Vara Il-Kbira', 7: 'L-Ecce Homo', 8: 'Il-Marbut',
|
25 |
+
9: 'Id-Duluri', 10: 'L-Aħħar Ċena', 11: 'Il-Monument',
|
26 |
+
12: 'Ir-Redentur', 13: 'Il-Veronica'
|
27 |
+
}
|
28 |
+
|
29 |
+
def predict_image(image, model, size=(244, 244)) -> List[Tuple[str, str, float]]:
|
30 |
+
"""Predict the class of a given image and return sorted probabilities with categories."""
|
31 |
+
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
32 |
+
resized_img = cv2.resize(image, size)
|
33 |
+
resized_img = resized_img / 255.0 # Normalize
|
34 |
+
resized_img = resized_img.transpose(2, 0, 1) # Convert to (C, H, W)
|
35 |
+
resized_img = resized_img[None, ...] # Add batch dimension
|
36 |
+
|
37 |
+
# Run prediction
|
38 |
+
results = model.predict(image)
|
39 |
+
pred_probs = results[0].probs.data.cpu().numpy()
|
40 |
+
|
41 |
+
# Sort predictions by probability
|
42 |
+
sorted_indices = np.argsort(pred_probs)[::-1] # Descending order
|
43 |
+
sorted_predictions = [
|
44 |
+
(
|
45 |
+
categories_english[i],
|
46 |
+
categories_maltese[i],
|
47 |
+
round(pred_probs[i] * 100, 2) # Convert to percentage
|
48 |
+
)
|
49 |
+
for i in sorted_indices
|
50 |
+
]
|
51 |
+
|
52 |
+
return sorted_predictions
|
53 |
+
|
54 |
+
# Load the model
|
55 |
+
model = YOLO("https://huggingface.co/mbar0075/Maltese-Christian-Statue-Classification/resolve/main/MCS-Classify.pt").to(device)
|
56 |
+
|
57 |
+
example_dir = "https://huggingface.co/spaces/mbar0075/Maltese-Christian-Statue-Classification/resolve/main/examples/"
|
58 |
+
|
59 |
+
# Example images
|
60 |
+
example_images = [
|
61 |
+
example_dir + "1.jpg",
|
62 |
+
example_dir + "2.jpg",
|
63 |
+
example_dir + "3.jpg",
|
64 |
+
example_dir + "4.jpg",
|
65 |
+
example_dir + "5.jpg",
|
66 |
+
example_dir + "6.jpg",
|
67 |
+
example_dir + "7.jpg",
|
68 |
+
example_dir + "8.jpg",
|
69 |
+
example_dir + "9.jpg",
|
70 |
+
example_dir + "10.jpg",
|
71 |
+
example_dir + "11.jpg",
|
72 |
+
example_dir + "12.jpg"
|
73 |
+
]
|
74 |
+
|
75 |
+
# Define the Gradio interface
|
76 |
+
def classify_image(input_image):
|
77 |
+
predictions = predict_image(input_image, model)
|
78 |
+
english_labels = "\n".join([f"{label}: {confidence}%" for label, _, confidence in predictions])
|
79 |
+
maltese_labels = "\n".join([f"{label}: {confidence}%" for _, label, confidence in predictions])
|
80 |
+
return english_labels, maltese_labels
|
81 |
+
|
82 |
+
# Gradio interface
|
83 |
+
gr.Interface(
|
84 |
+
fn=classify_image,
|
85 |
+
inputs=gr.Image(type="pil", label="Upload an image", tool="editor"),
|
86 |
+
outputs=[
|
87 |
+
gr.Textbox(label="Predictions (English)", lines=15),
|
88 |
+
gr.Textbox(label="Predictions (Maltese)", lines=15),
|
89 |
+
],
|
90 |
+
examples=example_images,
|
91 |
+
live=True,
|
92 |
+
title="Maltese Christian Statue Image Classification ✝",
|
93 |
+
description=(
|
94 |
+
"This project aims to classify Maltese Christian statues and religious figures depicted in images. The model is trained "
|
95 |
+
"to recognise 14 unique categories related to the Maltese Christian heritage, presented in both English and Maltese. "
|
96 |
+
"Upload an image to receive predictions of the statue's category, sorted by likelihood. This initiative promotes the "
|
97 |
+
"preservation of Maltese culture and traditions through AI technology."
|
98 |
+
),
|
99 |
+
article="The YOLO11s classificaiton model was trained on a dataset of Maltese Christian statues and religious figures during the period of Lent."
|
100 |
+
).launch()
|
examples/1.jpg
ADDED
![]() |
examples/10.jpg
ADDED
![]() |
examples/11.jpg
ADDED
![]() |
examples/12.jpg
ADDED
![]() |
examples/2.jpg
ADDED
![]() |
examples/3.jpg
ADDED
![]() |
examples/4.jpg
ADDED
![]() |
examples/5.jpg
ADDED
![]() |
examples/6.jpg
ADDED
![]() |
examples/7.jpg
ADDED
![]() |
examples/8.jpg
ADDED
![]() |
examples/9.jpg
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
setuptools<70.0.0
|
2 |
+
awscli==1.29.54
|
3 |
+
gradio
|
4 |
+
inference
|
5 |
+
supervision
|
6 |
+
ultralytics
|
7 |
+
dill
|
8 |
+
timm
|