Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,10 @@ from torchvision import models, transforms
|
|
3 |
from PIL import Image
|
4 |
import streamlit as st
|
5 |
from huggingface_hub import hf_hub_download
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Fungsi untuk mengunduh model
|
8 |
def load_model():
|
@@ -33,6 +37,87 @@ def classify_image(model, image):
|
|
33 |
_, predicted = torch.max(outputs, 1)
|
34 |
return predicted.item()
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
# Aplikasi Streamlit
|
37 |
st.title("Fruit and Vegetable Classifier")
|
38 |
st.write("Upload an image of a fruit or vegetable for classification.")
|
@@ -47,4 +132,19 @@ if uploaded_file is not None:
|
|
47 |
model = load_model()
|
48 |
label = classify_image(model, image)
|
49 |
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from PIL import Image
|
4 |
import streamlit as st
|
5 |
from huggingface_hub import hf_hub_download
|
6 |
+
import requests
|
7 |
+
|
8 |
+
# API Key yang kamu miliki
|
9 |
+
api_key = "3pm2NGZzYongVN1gRjnroVLUpsHC8rKWJFyx5moq"
|
10 |
|
11 |
# Fungsi untuk mengunduh model
|
12 |
def load_model():
|
|
|
37 |
_, predicted = torch.max(outputs, 1)
|
38 |
return predicted.item()
|
39 |
|
40 |
+
# Fungsi untuk mengambil informasi nutrisi dari API
|
41 |
+
def get_nutrition_info(food_name):
|
42 |
+
# URL endpoint untuk pencarian makanan
|
43 |
+
url = "https://api.nal.usda.gov/fdc/v1/foods/search"
|
44 |
+
|
45 |
+
# Parameter pencarian
|
46 |
+
params = {
|
47 |
+
"query": food_name, # Kata kunci makanan
|
48 |
+
"pageSize": 1, # Hanya ambil satu hasil
|
49 |
+
"api_key": api_key # Sertakan API Key
|
50 |
+
}
|
51 |
+
|
52 |
+
# Mengirim request GET ke API
|
53 |
+
response = requests.get(url, params=params)
|
54 |
+
|
55 |
+
# Mengubah hasil respons menjadi format JSON
|
56 |
+
data = response.json()
|
57 |
+
|
58 |
+
# Memeriksa apakah ada hasil dan mengambil nilai nutrisi
|
59 |
+
if "foods" in data and len(data["foods"]) > 0:
|
60 |
+
food = data["foods"][0]
|
61 |
+
nutrients_totals = {
|
62 |
+
"Energy": 0,
|
63 |
+
"Carbohydrate, by difference": 0,
|
64 |
+
"Fiber, total dietary": 0,
|
65 |
+
"Vitamin C, total ascorbic acid": 0
|
66 |
+
}
|
67 |
+
|
68 |
+
# Ambil nilai nutrisi
|
69 |
+
for nutrient in food['foodNutrients']:
|
70 |
+
nutrient_name = nutrient['nutrientName']
|
71 |
+
nutrient_value = nutrient['value']
|
72 |
+
|
73 |
+
# Cek apakah nutrisi termasuk yang diinginkan
|
74 |
+
if nutrient_name in nutrients_totals:
|
75 |
+
nutrients_totals[nutrient_name] += nutrient_value
|
76 |
+
|
77 |
+
return nutrients_totals
|
78 |
+
else:
|
79 |
+
return None
|
80 |
+
|
81 |
+
# Dictionary untuk label dan nama makanan
|
82 |
+
label_to_food = {
|
83 |
+
0: "apple",
|
84 |
+
1: "banana",
|
85 |
+
2: "beetroot",
|
86 |
+
3: "bell pepper",
|
87 |
+
4: "cabbage",
|
88 |
+
5: "capsicum",
|
89 |
+
6: "carrot",
|
90 |
+
7: "cauliflower",
|
91 |
+
8: "chilli pepper",
|
92 |
+
9: "corn",
|
93 |
+
10: "cucumber",
|
94 |
+
11: "eggplant",
|
95 |
+
12: "garlic",
|
96 |
+
13: "ginger",
|
97 |
+
14: "grapes",
|
98 |
+
15: "jalapeno",
|
99 |
+
16: "kiwi",
|
100 |
+
17: "lemon",
|
101 |
+
18: "lettuce",
|
102 |
+
19: "mango",
|
103 |
+
20: "onion",
|
104 |
+
21: "orange",
|
105 |
+
22: "paprika",
|
106 |
+
23: "pear",
|
107 |
+
24: "peas",
|
108 |
+
25: "pineapple",
|
109 |
+
26: "pomegranate",
|
110 |
+
27: "potato",
|
111 |
+
28: "radish",
|
112 |
+
29: "soy beans",
|
113 |
+
30: "spinach",
|
114 |
+
31: "sweetcorn",
|
115 |
+
32: "sweet potato",
|
116 |
+
33: "tomato",
|
117 |
+
34: "turnip",
|
118 |
+
35: "watermelon",
|
119 |
+
}
|
120 |
+
|
121 |
# Aplikasi Streamlit
|
122 |
st.title("Fruit and Vegetable Classifier")
|
123 |
st.write("Upload an image of a fruit or vegetable for classification.")
|
|
|
132 |
model = load_model()
|
133 |
label = classify_image(model, image)
|
134 |
|
135 |
+
# Ambil nama makanan berdasarkan label
|
136 |
+
food_name = label_to_food.get(label, "Unknown Food")
|
137 |
+
|
138 |
+
if food_name != "Unknown Food":
|
139 |
+
# Ambil informasi nutrisi dari API
|
140 |
+
nutrition_info = get_nutrition_info(food_name)
|
141 |
+
|
142 |
+
if nutrition_info:
|
143 |
+
st.write(f"Predicted class label: {food_name.capitalize()}")
|
144 |
+
st.write("Nutritional information:")
|
145 |
+
for nutrient_name, value in nutrition_info.items():
|
146 |
+
st.write(f"{nutrient_name}: {value:.2f}")
|
147 |
+
else:
|
148 |
+
st.write("Nutritional information not found.")
|
149 |
+
else:
|
150 |
+
st.write("Label not recognized.")
|