leafora commited on
Commit
0c22c0e
·
verified ·
1 Parent(s): 12af45d

update app.py with nutritional dataset

Browse files
Files changed (1) hide show
  1. app.py +130 -24
app.py CHANGED
@@ -3,12 +3,80 @@ import model_builder as mb
3
  from torchvision import transforms
4
  import torch
5
 
6
- device = torch.device("cpu")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
 
8
  normalize = transforms.Normalize(
9
  mean=[0.485, 0.456, 0.406],
10
  std=[0.229, 0.224, 0.225])
11
-
12
  manual_transform = transforms.Compose([
13
  transforms.ToPILImage(),
14
  transforms.Resize(size=(224, 224)),
@@ -16,23 +84,6 @@ manual_transform = transforms.Compose([
16
  normalize
17
  ])
18
 
19
- # class_names = ['Fresh Banana',
20
- # 'Fresh Lemon',
21
- # 'Fresh Lulo',
22
- # 'Fresh Mango',
23
- # 'Fresh Orange',
24
- # 'Fresh Strawberry',
25
- # 'Fresh Tamarillo',
26
- # 'Fresh Tomato',
27
- # 'Spoiled Banana',
28
- # 'Spoiled Lemon',
29
- # 'Spoiled Lulo',
30
- # 'Spoiled Mango',
31
- # 'Spoiled Orange',
32
- # 'Spoiled Strawberry',
33
- # 'Spoiled Tamarillo',
34
- # 'Spoiled Tomato']
35
-
36
  class_names = ['Fresh Apple',
37
  'Fresh Banana',
38
  'Fresh Orange',
@@ -43,13 +94,58 @@ class_names = ['Fresh Apple',
43
  model = mb.create_model_baseline_effnetb0(out_feats=len(class_names), device=device)
44
  model.load_state_dict(torch.load(f="models/effnetb0_freshvisionv0_10_epochs.pt", map_location="cpu"))
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  def pred(img):
47
  model.eval()
48
  transformed = manual_transform(img).to(device)
49
  with torch.inference_mode():
50
  logits = model(transformed.unsqueeze(dim=0))
51
  pred = torch.softmax(logits, dim=-1)
52
- return f"prediction: {class_names[pred.argmax(dim=-1).item()]} | confidence: {pred.max():.3f}"
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  demo = gr.Blocks()
55
 
@@ -61,11 +157,16 @@ with demo:
61
  This model has been trained on [kaggle datasets](https://www.kaggle.com/datasets/sriramr/fruits-fresh-and-rotten-for-classification) using NVIDIA T4 GPU._
62
 
63
  ## Model capabilities:
64
- - Classify freshness from fruits image (apple, orange, and banana) with two labels: _Fresh_ and _Rotten/spoiled_
 
 
 
 
 
65
  ## Model drawbacks:
66
  - Sometimes perform false prediction on some fruits condition, this is due to low variability on the image datasets
67
  - Can't perform accurate prediction on multiple objects/combined condition (e.g. two bananas with different freshness condition)
68
- - This models can't identify non-fruits objects , since it's only trained with fruits dataset.
69
 
70
  ## **How to get the best result with this model:**
71
  1. The image should only contain fruits that the model can recognize (apple, orange, and banana)
@@ -73,8 +174,13 @@ with demo:
73
  3. Ensure the object is captured with sufficient light so that the surface of the fruits is exposed properly
74
 
75
  get the [source code](https://github.com/devdezzies/freshvision) on my github
76
- """)
77
- gr.Interface(pred, gr.Image(), outputs="text")
 
 
 
 
 
78
 
79
  if __name__ == "__main__":
80
  demo.launch()
 
3
  from torchvision import transforms
4
  import torch
5
 
6
+ # Comprehensive nutrition data per 165g serving
7
+ NUTRITION_DATA = {
8
+ 'Fresh Apple': {
9
+ 'macronutrients': {
10
+ 'calories': 99.2,
11
+ 'protein': 0.8,
12
+ 'carbs': 23.3,
13
+ 'fats': 0.3,
14
+ 'water': 140.2,
15
+ 'fiber': 1.5
16
+ },
17
+ 'micronutrients': {
18
+ 'vitamin_c': 96.7,
19
+ 'thiamin': 0.1,
20
+ 'niacin': 0.4,
21
+ 'vitamin_b6': 0.2
22
+ },
23
+ 'macrominerals': {
24
+ 'magnesium': 22.1,
25
+ 'phosphorus': 8.9,
26
+ 'potassium': 226.0,
27
+ 'calcium': 20.6
28
+ }
29
+ },
30
+ 'Fresh Banana': {
31
+ 'macronutrients': {
32
+ 'calories': 147.0,
33
+ 'protein': 1.8,
34
+ 'carbs': 38.0,
35
+ 'fats': 0.5,
36
+ 'water': 132.0,
37
+ 'fiber': 3.5
38
+ },
39
+ 'micronutrients': {
40
+ 'vitamin_c': 14.7,
41
+ 'thiamin': 0.4,
42
+ 'niacin': 1.2,
43
+ 'vitamin_b6': 0.5
44
+ },
45
+ 'macrominerals': {
46
+ 'magnesium': 41.3,
47
+ 'phosphorus': 33.0,
48
+ 'potassium': 537.0,
49
+ 'calcium': 8.3
50
+ }
51
+ },
52
+ 'Fresh Orange': {
53
+ 'macronutrients': {
54
+ 'calories': 82.0,
55
+ 'protein': 1.6,
56
+ 'carbs': 21.0,
57
+ 'fats': 0.2,
58
+ 'water': 146.0,
59
+ 'fiber': 4.0
60
+ },
61
+ 'micronutrients': {
62
+ 'vitamin_c': 82.7,
63
+ 'thiamin': 0.2,
64
+ 'niacin': 0.5,
65
+ 'vitamin_b6': 0.1
66
+ },
67
+ 'macrominerals': {
68
+ 'magnesium': 18.2,
69
+ 'phosphorus': 28.1,
70
+ 'potassium': 237.6,
71
+ 'calcium': 74.3
72
+ }
73
+ }
74
+ }
75
 
76
+ device = torch.device("cpu")
77
  normalize = transforms.Normalize(
78
  mean=[0.485, 0.456, 0.406],
79
  std=[0.229, 0.224, 0.225])
 
80
  manual_transform = transforms.Compose([
81
  transforms.ToPILImage(),
82
  transforms.Resize(size=(224, 224)),
 
84
  normalize
85
  ])
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  class_names = ['Fresh Apple',
88
  'Fresh Banana',
89
  'Fresh Orange',
 
94
  model = mb.create_model_baseline_effnetb0(out_feats=len(class_names), device=device)
95
  model.load_state_dict(torch.load(f="models/effnetb0_freshvisionv0_10_epochs.pt", map_location="cpu"))
96
 
97
+ def format_nutrition(fruit_name):
98
+ """Format comprehensive nutrition information for display"""
99
+ if fruit_name not in NUTRITION_DATA:
100
+ return ""
101
+
102
+ nutrition = NUTRITION_DATA[fruit_name]
103
+ macro = nutrition['macronutrients']
104
+ micro = nutrition['micronutrients']
105
+ minerals = nutrition['macrominerals']
106
+
107
+ return f"""
108
+ Nutritional Information (per 165g serving):
109
+
110
+ Macronutrients:
111
+ • Calories: {macro['calories']} kcal
112
+ • Protein: {macro['protein']} g
113
+ • Carbs: {macro['carbs']} g
114
+ • Fats: {macro['fats']} g
115
+ • Water: {macro['water']} ml
116
+ • Fiber: {macro['fiber']} g
117
+
118
+ Micronutrients:
119
+ • Vitamin C: {micro['vitamin_c']} mg
120
+ • Thiamin: {micro['thiamin']} mg
121
+ • Niacin: {micro['niacin']} mg
122
+ • Vitamin B6: {micro['vitamin_b6']} mg
123
+
124
+ Macrominerals:
125
+ • Magnesium: {minerals['magnesium']} mg
126
+ • Phosphorus: {minerals['phosphorus']} mg
127
+ • Potassium: {minerals['potassium']} mg
128
+ • Calcium: {minerals['calcium']} mg
129
+ """
130
+
131
  def pred(img):
132
  model.eval()
133
  transformed = manual_transform(img).to(device)
134
  with torch.inference_mode():
135
  logits = model(transformed.unsqueeze(dim=0))
136
  pred = torch.softmax(logits, dim=-1)
137
+
138
+ predicted_class = class_names[pred.argmax(dim=-1).item()]
139
+ confidence = pred.max().item()
140
+
141
+ result = f"Prediction: {predicted_class} | Confidence: {confidence:.3f}"
142
+
143
+ # Add nutrition information if it's a fresh fruit
144
+ if predicted_class.startswith('Fresh'):
145
+ nutrition_info = format_nutrition(predicted_class)
146
+ result += f"\n{nutrition_info}"
147
+
148
+ return result
149
 
150
  demo = gr.Blocks()
151
 
 
157
  This model has been trained on [kaggle datasets](https://www.kaggle.com/datasets/sriramr/fruits-fresh-and-rotten-for-classification) using NVIDIA T4 GPU._
158
 
159
  ## Model capabilities:
160
+ - Classify freshness from fruits image (apple, orange, and banana) with two labels: *Fresh* and *Rotten/spoiled*
161
+ - Provides comprehensive nutritional information for fresh fruits including:
162
+ * Macronutrients (calories, protein, carbs, fats, water, fiber)
163
+ * Micronutrients (vitamins C, B6, thiamin, niacin)
164
+ * Macrominerals (magnesium, phosphorus, potassium, calcium)
165
+
166
  ## Model drawbacks:
167
  - Sometimes perform false prediction on some fruits condition, this is due to low variability on the image datasets
168
  - Can't perform accurate prediction on multiple objects/combined condition (e.g. two bananas with different freshness condition)
169
+ - This models can't identify non-fruits objects, since it's only trained with fruits dataset
170
 
171
  ## **How to get the best result with this model:**
172
  1. The image should only contain fruits that the model can recognize (apple, orange, and banana)
 
174
  3. Ensure the object is captured with sufficient light so that the surface of the fruits is exposed properly
175
 
176
  get the [source code](https://github.com/devdezzies/freshvision) on my github
177
+ """)
178
+ gr.Interface(
179
+ fn=pred,
180
+ inputs=gr.Image(),
181
+ outputs=gr.Textbox(label="Prediction Results", lines=15),
182
+ title="FreshVision Fruit Classifier"
183
+ )
184
 
185
  if __name__ == "__main__":
186
  demo.launch()