hilalcan/Computer-Vision
0
1import streamlit as st2import torch3import torch.nn as nn4from torchvision import models, transforms5from PIL import Image6 7# ========================8# 1. Sınıf adlarını tanımla9# ========================10class_names = [11 "Salata", "Pizza", "Makarna", "Burger", "Çorba",12 "Tatlı", "Tavuk", "Balık", "Sebze Yemeği", "Diğer"13]14 15# ========================16# 2. Modeli yükle17# ========================18device = torch.device("cuda" if torch.cuda.is_available() else "cpu")19model = models.resnet18(pretrained=False)20num_features = model.fc.in_features21model.fc = nn.Linear(num_features, len(class_names))22model.load_state_dict(torch.load("src/food_model.pth", map_location=device))23model.eval()24 25# ========================26# 3. Transform27# ========================28transform = transforms.Compose([29 transforms.Resize((224, 224)),30 transforms.ToTensor(),31 transforms.Normalize([0.485, 0.456, 0.406],32 [0.229, 0.224, 0.225])33])34 35# ========================36# 4. Streamlit arayüzü37# ========================38st.set_page_config(page_title="Yemek Görüntü Sınıflandırma", page_icon="🍽️")39 40st.title("🍽️ Yemek Sınıflandırma Uygulaması")41st.markdown("**Bir yemek fotoğrafı yükleyin, model hangi kategori olduğunu tahmin etsin.**")42 43uploaded_file = st.file_uploader("📂 Lütfen bir resim yükleyin (.jpg veya .png)", type=["jpg", "jpeg", "png"])44 45if uploaded_file is not None:46 image = Image.open(uploaded_file).convert("RGB")47 48 # Kolonlara ayıralım49 col1, col2 = st.columns([2, 1])50 with col1:51 st.image(image, caption="Yüklenen Resim", use_column_width=True)52 with col2:53 st.write("🔄 **Tahmin ediliyor...**")54 55 # Preprocess56 input_tensor = transform(image).unsqueeze(0)57 with torch.no_grad():58 outputs = model(input_tensor.to(device))59 _, pred = torch.max(outputs, 1)60 predicted_class = class_names[pred.item()]61 62 # Sonucu daha estetik göster63 st.success(f"✅ **Tahmin edilen sınıf:** {predicted_class}")64else:65 st.info("👆 Yukarıdan bir resim yüklediğinizde burada tahmin sonucu görünecek.")66 