Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,84 +4,124 @@ import numpy as np
|
|
| 4 |
import os
|
| 5 |
import warnings
|
| 6 |
|
| 7 |
-
# --- 1. SİNSİ HATALARI
|
| 8 |
-
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
| 9 |
-
warnings.filterwarnings('ignore')
|
| 10 |
|
| 11 |
-
# --- 2. SAYFA AYARLARI
|
| 12 |
-
st.set_page_config(page_title="Car Logo AI 2026", layout="wide")
|
| 13 |
|
| 14 |
-
# --- 3.
|
| 15 |
-
# Modelleri ve veri setini RAM'e kilitle (Cache), her seferinde dosyadan okuma!
|
| 16 |
@st.cache_resource
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
#
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
# --- 4. RESİM İŞLEME
|
| 25 |
@st.cache_data
|
| 26 |
-
def
|
| 27 |
nparr = np.frombuffer(file_bytes, np.uint8)
|
| 28 |
img = cv2.imdecode(nparr, 1)
|
| 29 |
-
if img is None: return None, None
|
| 30 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 31 |
edges = cv2.Canny(gray, 100, 200)
|
| 32 |
-
return img, edges
|
| 33 |
|
| 34 |
-
# --- 5. ARAYÜZ
|
| 35 |
-
st.title("🚗
|
|
|
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
|
| 40 |
-
with
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
if uploaded_file:
|
| 47 |
-
# getvalue() kullanımı 'buffer empty' hatasını ve titremeyi önler
|
| 48 |
-
raw_img, edge_img = process_img(uploaded_file.getvalue())
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
st.markdown("### 🖼️ Görünüm / View")
|
| 54 |
-
# DİKKAT: use_container_width yerine width=700 kullanarak titremeyi kestik
|
| 55 |
-
st.image(raw_img, channels="BGR", width=500, caption="Orijinal / Original")
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
st.balloons()
|
| 61 |
-
st.success("✅ Eşleşme Başarılı / Match Successful")
|
| 62 |
-
st.metric(label="Güven / Confidence", value="%95")
|
| 63 |
|
| 64 |
-
|
| 65 |
-
st.
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
| 80 |
-
#
|
| 81 |
st.markdown("""
|
| 82 |
<style>
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
.block-container {padding-top: 2rem;}
|
| 86 |
</style>
|
| 87 |
""", unsafe_allow_html=True)
|
|
|
|
| 4 |
import os
|
| 5 |
import warnings
|
| 6 |
|
| 7 |
+
# --- 1. SİNSİ HATALARI SUSTUR (2026 OPTİMİZASYONU) ---
|
| 8 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
| 9 |
+
warnings.filterwarnings('ignore')
|
| 10 |
|
| 11 |
+
# --- 2. SAYFA AYARLARI / PAGE CONFIG ---
|
| 12 |
+
st.set_page_config(page_title="Car Logo AI Pro 2026", layout="wide", page_icon="🚗")
|
| 13 |
|
| 14 |
+
# --- 3. VERİ SETİNİ BELLEĞE YÜKLE (TİTREMEYİ ÖNLEYEN ANA YER) ---
|
|
|
|
| 15 |
@st.cache_resource
|
| 16 |
+
def load_car_dataset(base_path):
|
| 17 |
+
dataset = []
|
| 18 |
+
# Eğer klasör yoksa hata verme, ana dizine bak
|
| 19 |
+
if not os.path.exists(base_path):
|
| 20 |
+
image_files = [f for f in os.listdir('.') if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
| 21 |
+
for f in image_files:
|
| 22 |
+
img = cv2.imread(f, 0)
|
| 23 |
+
if img is not None:
|
| 24 |
+
dataset.append((f, cv2.resize(img, (100, 100))))
|
| 25 |
+
else:
|
| 26 |
+
for root, dirs, files in os.walk(base_path):
|
| 27 |
+
for file in files:
|
| 28 |
+
if file.lower().endswith(('.png', '.jpg', '.jpeg')):
|
| 29 |
+
full_path = os.path.join(root, file)
|
| 30 |
+
img = cv2.imread(full_path, 0)
|
| 31 |
+
if img is not None:
|
| 32 |
+
dataset.append((full_path, cv2.resize(img, (100, 100))))
|
| 33 |
+
return dataset
|
| 34 |
|
| 35 |
+
# Veri setini yükle (32 markalık klasörü tara)
|
| 36 |
+
dataset = load_car_dataset('Car_Logo_Dataset')
|
| 37 |
|
| 38 |
+
# --- 4. RESİM İŞLEME FONKSİYONU ---
|
| 39 |
@st.cache_data
|
| 40 |
+
def process_uploaded_image(file_bytes):
|
| 41 |
nparr = np.frombuffer(file_bytes, np.uint8)
|
| 42 |
img = cv2.imdecode(nparr, 1)
|
| 43 |
+
if img is None: return None, None, None
|
| 44 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 45 |
edges = cv2.Canny(gray, 100, 200)
|
| 46 |
+
return img, gray, edges
|
| 47 |
|
| 48 |
+
# --- 5. ARAYÜZ TASARIMI / UI DESIGN ---
|
| 49 |
+
st.title("🚗 Araba Logosu Tanıma Sistemi / Car Logo Recognition")
|
| 50 |
+
st.markdown("---")
|
| 51 |
|
| 52 |
+
# Sol ve Sağ Kolon Ayarı
|
| 53 |
+
col_ui, col_res = st.columns([1, 1], gap="large")
|
| 54 |
|
| 55 |
+
with col_ui:
|
| 56 |
+
st.subheader("📤 Yükleme Paneli / Upload Panel")
|
| 57 |
+
uploaded_file = st.file_uploader("Bir logo seçin / Select a logo", type=['jpg', 'png', 'jpeg'])
|
| 58 |
|
| 59 |
+
# Hassasiyet Sliderı
|
| 60 |
+
threshold = st.sidebar.slider("Hassasiyet / Sensitivity (Threshold)", 0.0, 1.0, 0.20)
|
| 61 |
+
st.sidebar.info(f"Sistemde yüklü referans sayısı: {len(dataset)}")
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
if uploaded_file:
|
| 64 |
+
# Resmi işle (Önbellekten gelir, titreme yapmaz)
|
| 65 |
+
raw_img, gray_img, edge_img = process_uploaded_image(uploaded_file.getvalue())
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
with col_ui:
|
| 68 |
+
# 2026 Standartlarında sabit genişlik (Titremeyi önler)
|
| 69 |
+
st.image(raw_img, channels="BGR", width=450, caption="Yüklenen / Uploaded")
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
with col_res:
|
| 72 |
+
st.subheader("🎯 Analiz Sonucu / Analysis Result")
|
| 73 |
+
|
| 74 |
+
if st.button("ŞİMDİ TANI / PREDICT NOW", type="primary"):
|
| 75 |
+
with st.spinner("32 Marka Taranıyor... / Scanning..."):
|
| 76 |
+
# Karşılaştırma döngüsü
|
| 77 |
+
query = cv2.resize(gray_img, (100, 100))
|
| 78 |
+
best_score = -1
|
| 79 |
+
best_path = None
|
| 80 |
+
|
| 81 |
+
for path, temp_img in dataset:
|
| 82 |
+
res = cv2.matchTemplate(query, temp_img, cv2.TM_CCOEFF_NORMED)
|
| 83 |
+
_, max_val, _, _ = cv2.minMaxLoc(res)
|
| 84 |
+
if max_val > best_score:
|
| 85 |
+
best_score = max_val
|
| 86 |
+
best_path = path
|
| 87 |
+
|
| 88 |
+
# MARKAYI YAZDIRMA BÖLÜMÜ
|
| 89 |
+
if best_path and best_score >= threshold:
|
| 90 |
+
st.balloons()
|
| 91 |
+
|
| 92 |
+
# Dosya yolundan 32 markadan hangisi olduğunu bul
|
| 93 |
+
folder_name = os.path.basename(os.path.dirname(best_path))
|
| 94 |
+
if not folder_name or folder_name == '.':
|
| 95 |
+
folder_name = os.path.basename(best_path).split('.')[0]
|
| 96 |
+
|
| 97 |
+
# İsmi temizle ve büyük harf yap
|
| 98 |
+
brand_name = ''.join([i for i in folder_name if not i.isdigit() and i not in ['-', '_']]).strip().upper()
|
| 99 |
+
|
| 100 |
+
st.success(f"### TAHMİN / PREDICTION: **{brand_name}**")
|
| 101 |
+
st.metric("Benzerlik / Similarity", f"%{int(best_score*100)}")
|
| 102 |
+
st.image(best_path, width=150, caption=f"Eşleşen: {brand_name}")
|
| 103 |
+
else:
|
| 104 |
+
st.error("❌ Eşleşme Bulunamadı / Match Not Found")
|
| 105 |
+
|
| 106 |
+
# --- TEKNİK ANALİZ (TİTREMEYEN ALT BÖLÜM) ---
|
| 107 |
+
st.markdown("---")
|
| 108 |
+
with st.expander("🔍 Teknik Görüntü Analizi / Technical Image Analysis"):
|
| 109 |
+
tr_col, en_col = st.columns(2)
|
| 110 |
+
|
| 111 |
+
with tr_col:
|
| 112 |
+
st.write("### 🇹🇷 Türkçe Analiz")
|
| 113 |
+
st.image(edge_img, width=300, caption="Canny Kenar Tespiti")
|
| 114 |
+
st.write(f"Logonun dış hatları çıkartıldı ve sistemdeki {len(dataset)} resimle karşılaştırıldı.")
|
| 115 |
|
| 116 |
+
with en_col:
|
| 117 |
+
st.write("### 🇺🇸 English Analysis")
|
| 118 |
+
st.image(edge_img, width=300, caption="Canny Edge Detection")
|
| 119 |
+
st.write(f"Outlines were extracted and compared with {len(dataset)} reference images.")
|
| 120 |
|
| 121 |
+
# Stil Ayarları (2026 Görünümü)
|
| 122 |
st.markdown("""
|
| 123 |
<style>
|
| 124 |
+
.stButton>button { height: 3em; font-size: 20px; font-weight: bold; }
|
| 125 |
+
.stMetric { background: #f0f2f6; padding: 10px; border-radius: 10px; }
|
|
|
|
| 126 |
</style>
|
| 127 |
""", unsafe_allow_html=True)
|