mbar0075 commited on
Commit
fd552ac
·
1 Parent(s): 899f1a4

Minor changes to output

Browse files
Files changed (3) hide show
  1. app.py +16 -27
  2. categories.json +4 -4
  3. parishes.json +47 -0
app.py CHANGED
@@ -20,6 +20,13 @@ with open('categories.json', 'r', encoding='utf-8') as f1:
20
  with open('categories_synopsis.json', 'r', encoding='utf-8') as f2:
21
  categories_synopsis = json.load(f2)
22
 
 
 
 
 
 
 
 
23
  # Model URLs
24
  models = {
25
  "Model v1": YOLO("https://huggingface.co/mbar0075/Maltese-Christian-Statue-Classification/resolve/main/MCS-Classify.pt").to(device),
@@ -28,32 +35,15 @@ models = {
28
  "Model v3 (Accurate)": YOLO("https://huggingface.co/mbar0075/Maltese-Christian-Statue-Classification/resolve/main/MCS-Classifyv3-Accurate.pt").to(device)
29
  }
30
 
31
- # For parishes
32
- parishes = {
33
- 0: "Birgu - Vittoriosa",
34
- 1: "Bormla - Cospicua",
35
- 2: "Isla - Senglea",
36
- 3: "Mosta",
37
- 4: "Nadur Għawdex",
38
- 5: "Naxxar",
39
- 6: "Rabat",
40
- 7: "Rabat - Victoria Għawdex",
41
- 8: "Raħal Ġdid - Paola",
42
- 9: "Valletta",
43
- 10: "Xagħra Għawdex",
44
- 11: "Xewkija Għawdex",
45
- 12: "Ħal Għaxaq",
46
- 13: "Ħal-Luqa",
47
- 14: "Ħal-Qormi",
48
- 15: "Ħaż-Żabbar",
49
- 16: "Ħaż-Żebbuġ",
50
- 17: "Żebbuġ Għawdex",
51
- 18: "Żejtun"
52
  }
53
 
54
- parishes_model = YOLO("https://huggingface.co/mbar0075/Maltese-Christian-Statue-Classification/resolve/main/MCS-Classify-Parishv1.pt").to(device)
55
-
56
- default_model = "Model v2"
 
57
 
58
  def predict_image(image, model_name: str, size=(244, 244)) -> List[Tuple[str, str, float]]:
59
  """Predict the class of a given image and return sorted probabilities with categories."""
@@ -105,7 +95,7 @@ def predict_parish(image, size=(244, 244)) -> List[Tuple[str, float]]:
105
  sorted_indices = np.argsort(pred_probs)[::-1] # Descending order
106
  sorted_predictions = [
107
  (
108
- parishes[i],
109
  round(pred_probs[i] * 100, 2) # Convert to percentage
110
  )
111
  for i in sorted_indices
@@ -140,9 +130,8 @@ def classify_image(input_image, model_name):
140
 
141
  # Modify the first formatted prediction to include "From the Parish of ..."
142
  first_label, first_confidence = parish_predictions[0]
143
- formatted_parish_predictions[f"From the Parish of {first_label} / Mill-Parroċċa ta' {first_label}"] = formatted_parish_predictions.pop(first_label)
144
 
145
-
146
  # Get the label with the highest confidence
147
  highest_confidence_label = predictions[0][0] # Assuming predictions are sorted by confidence
148
  highest_confidence_synopsis = categories_synopsis.get(highest_confidence_label, "No synopsis available.")
 
20
  with open('categories_synopsis.json', 'r', encoding='utf-8') as f2:
21
  categories_synopsis = json.load(f2)
22
 
23
+ # Loading the Parishes
24
+ with open('parishes.json', 'r', encoding='utf-8') as f3:
25
+ parishes = json.load(f3)
26
+
27
+ # Default model
28
+ default_model = "Model v2"
29
+
30
  # Model URLs
31
  models = {
32
  "Model v1": YOLO("https://huggingface.co/mbar0075/Maltese-Christian-Statue-Classification/resolve/main/MCS-Classify.pt").to(device),
 
35
  "Model v3 (Accurate)": YOLO("https://huggingface.co/mbar0075/Maltese-Christian-Statue-Classification/resolve/main/MCS-Classifyv3-Accurate.pt").to(device)
36
  }
37
 
38
+ parish_model_paths = {
39
+ "Model v1": "https://huggingface.co/mbar0075/Maltese-Christian-Statue-Classification/resolve/main/MCS-Classify-Parishv1.pt",
40
+ "Model v2": "https://huggingface.co/mbar0075/Maltese-Christian-Statue-Classification/resolve/main/MCS-Classify-Parishv2.pt"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  }
42
 
43
+ # Loading the respective Parishes Model and Categories
44
+ parishes_model_path = "Model v1"
45
+ parishes_model = YOLO(parish_model_paths[parishes_model_path]).to(device)
46
+ parishes_categories = parishes[parishes_model_path]
47
 
48
  def predict_image(image, model_name: str, size=(244, 244)) -> List[Tuple[str, str, float]]:
49
  """Predict the class of a given image and return sorted probabilities with categories."""
 
95
  sorted_indices = np.argsort(pred_probs)[::-1] # Descending order
96
  sorted_predictions = [
97
  (
98
+ parishes_categories[str(i)],
99
  round(pred_probs[i] * 100, 2) # Convert to percentage
100
  )
101
  for i in sorted_indices
 
130
 
131
  # Modify the first formatted prediction to include "From the Parish of ..."
132
  first_label, first_confidence = parish_predictions[0]
133
+ formatted_parish_predictions[f"From the Parish of / Mill-Parroċċa ta' {first_label}"] = formatted_parish_predictions.pop(first_label)
134
 
 
135
  # Get the label with the highest confidence
136
  highest_confidence_label = predictions[0][0] # Assuming predictions are sorted by confidence
137
  highest_confidence_synopsis = categories_synopsis.get(highest_confidence_label, "No synopsis available.")
categories.json CHANGED
@@ -30,7 +30,7 @@
30
  "10": "L-Aħħar Ċena",
31
  "11": "Il-Monument",
32
  "12": "Ir-Redentur",
33
- "13": "Il-Veronica"
34
  }
35
  },
36
  "Model v2": {
@@ -70,7 +70,7 @@
70
  "13": "L-Aħħar Ċena",
71
  "14": "Il-Monument",
72
  "15": "Ir-Redentur",
73
- "16": "Il-Veronica"
74
  }
75
  },
76
  "Model v3 (Fast)": {
@@ -110,7 +110,7 @@
110
  "13": "L-Aħħar Ċena",
111
  "14": "Il-Monument",
112
  "15": "Ir-Redentur",
113
- "16": "Il-Veronica"
114
  }
115
  },
116
  "Model v3 (Accurate)": {
@@ -150,7 +150,7 @@
150
  "13": "L-Aħħar Ċena",
151
  "14": "Il-Monument",
152
  "15": "Ir-Redentur",
153
- "16": "Il-Veronica"
154
  }
155
  }
156
  }
 
30
  "10": "L-Aħħar Ċena",
31
  "11": "Il-Monument",
32
  "12": "Ir-Redentur",
33
+ "13": "Il-Veronika"
34
  }
35
  },
36
  "Model v2": {
 
70
  "13": "L-Aħħar Ċena",
71
  "14": "Il-Monument",
72
  "15": "Ir-Redentur",
73
+ "16": "Il-Veronika"
74
  }
75
  },
76
  "Model v3 (Fast)": {
 
110
  "13": "L-Aħħar Ċena",
111
  "14": "Il-Monument",
112
  "15": "Ir-Redentur",
113
+ "16": "Il-Veronika"
114
  }
115
  },
116
  "Model v3 (Accurate)": {
 
150
  "13": "L-Aħħar Ċena",
151
  "14": "Il-Monument",
152
  "15": "Ir-Redentur",
153
+ "16": "Il-Veronika"
154
  }
155
  }
156
  }
parishes.json ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "Model v1": {
3
+ "0": "Birgu - Vittoriosa (Malta)",
4
+ "1": "Bormla - Cospicua (Malta)",
5
+ "2": "Isla - Senglea (Malta)",
6
+ "3": "Mosta (Malta)",
7
+ "4": "Nadur (Gozo / Għawdex)",
8
+ "5": "Naxxar (Malta)",
9
+ "6": "Rabat (Malta)",
10
+ "7": "Rabat - Victoria (Gozo / Għawdex)",
11
+ "8": "Raħal Ġdid - Paola (Malta)",
12
+ "9": "Valletta (Malta)",
13
+ "10": "Xagħra (Gozo / Għawdex)",
14
+ "11": "Xewkija (Gozo / Għawdex)",
15
+ "12": "Ħal Għaxaq (Malta)",
16
+ "13": "Ħal-Luqa (Malta)",
17
+ "14": "Ħal-Qormi (Malta)",
18
+ "15": "Ħaż-Żabbar (Malta)",
19
+ "16": "Ħaż-Żebbuġ (Malta)",
20
+ "17": "Żebbuġ (Gozo / Għawdex)",
21
+ "18": "Żejtun (Malta)"
22
+ },
23
+ "Model v2": {
24
+ "0": "Birgu - Vittoriosa (Malta)",
25
+ "1": "Bormla - Cospicua (Malta)",
26
+ "2": "Għargħur (Malta)",
27
+ "3": "Isla - Senglea (Malta)",
28
+ "4": "Mosta (Malta)",
29
+ "5": "Nadur (Gozo / Għawdex)",
30
+ "6": "Naxxar (Malta)",
31
+ "7": "Qala (Gozo / Għawdex)",
32
+ "8": "Rabat (Malta)",
33
+ "9": "Rabat - Victoria (Gozo / Għawdex)",
34
+ "10": "Raħal Ġdid - Paola (Malta)",
35
+ "11": "Valletta (Malta)",
36
+ "12": "Xagħra (Gozo / Għawdex)",
37
+ "13": "Xewkija (Gozo / Għawdex)",
38
+ "14": "Ħal Għaxaq (Malta)",
39
+ "15": "Ħal-Luqa (Malta)",
40
+ "16": "Ħal-Qormi (Malta)",
41
+ "17": "Ħaż-Żabbar (Malta)",
42
+ "18": "Ħaż-Żebbuġ (Malta)",
43
+ "19": "Żebbuġ (Gozo / Għawdex)",
44
+ "20": "Żejtun (Malta)"
45
+ }
46
+ }
47
+