Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,80 +3,70 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
# --- 1.
|
| 7 |
-
|
| 8 |
-
@st.cache_resource
|
| 9 |
-
def load_all_templates(base_path):
|
| 10 |
-
templates = []
|
| 11 |
-
if not os.path.exists(base_path):
|
| 12 |
-
# Klasör yoksa ana dizindeki resimleri al
|
| 13 |
-
files = [f for f in os.listdir('.') if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
| 14 |
-
for f in files:
|
| 15 |
-
img = cv2.imread(f, 0)
|
| 16 |
-
if img is not None:
|
| 17 |
-
templates.append((f, cv2.resize(img, (100, 100))))
|
| 18 |
-
else:
|
| 19 |
-
for root, dirs, files in os.walk(base_path):
|
| 20 |
-
for file in files:
|
| 21 |
-
if file.lower().endswith(('.png', '.jpg', '.jpeg')):
|
| 22 |
-
path = os.path.join(root, file)
|
| 23 |
-
img = cv2.imread(path, 0)
|
| 24 |
-
if img is not None:
|
| 25 |
-
templates.append((path, cv2.resize(img, (100, 100))))
|
| 26 |
-
return templates
|
| 27 |
|
| 28 |
-
# --- 2.
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
st.
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
with
|
| 41 |
-
|
| 42 |
-
if uploaded_file:
|
| 43 |
-
# Resmi bir kez oku ve değişkene at
|
| 44 |
-
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 45 |
-
raw_img = cv2.imdecode(file_bytes, 1)
|
| 46 |
-
gray_img = cv2.cvtColor(raw_img, cv2.COLOR_BGR2GRAY)
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
for path, temp_img in dataset:
|
| 62 |
-
res = cv2.matchTemplate(query, temp_img, cv2.TM_CCOEFF_NORMED)
|
| 63 |
-
_, max_val, _, _ = cv2.minMaxLoc(res)
|
| 64 |
-
if max_val > best_score:
|
| 65 |
-
best_score = max_val
|
| 66 |
-
best_path = path
|
| 67 |
-
|
| 68 |
-
if best_path and best_score >= threshold:
|
| 69 |
-
st.success(f"### Tahmin: {os.path.basename(best_path).split('.')[0].upper()}")
|
| 70 |
-
st.metric("Benzerlik", f"%{int(best_score*100)}")
|
| 71 |
-
st.image(best_path, width=150)
|
| 72 |
-
else:
|
| 73 |
-
st.error("Eşleşme bulunamadı.")
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# --- 1. SAYFA AYARLARI / PAGE CONFIG ---
|
| 7 |
+
st.set_page_config(page_title="Car Logo AI", layout="wide", page_icon="🚗")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# --- 2. VERİ ÖNBELLEKLEME / DATA CACHING ---
|
| 10 |
+
# Bu fonksiyon titremeyi engeller çünkü resmi sadece bir kez işler.
|
| 11 |
+
@st.cache_data
|
| 12 |
+
def process_uploaded_image(file_bytes):
|
| 13 |
+
nparr = np.frombuffer(file_bytes, np.uint8)
|
| 14 |
+
img = cv2.imdecode(nparr, 1)
|
| 15 |
+
if img is None: return None, None, None
|
| 16 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 17 |
+
edges = cv2.Canny(gray, 100, 200)
|
| 18 |
+
return img, gray, edges
|
| 19 |
|
| 20 |
+
# --- 3. ARAYÜZ / INTERFACE ---
|
| 21 |
+
st.title("🚗 Araba Logosu Tanıma / Car Logo Recognition")
|
| 22 |
+
st.markdown("---")
|
| 23 |
|
| 24 |
+
# Dosya Yükleme / File Upload
|
| 25 |
+
uploaded_file = st.file_uploader("Bir logo seçin / Select a logo", type=['jpg', 'png', 'jpeg'])
|
| 26 |
|
| 27 |
+
if uploaded_file:
|
| 28 |
+
# Resmi işle / Process image
|
| 29 |
+
img_data, gray_data, edge_data = process_uploaded_image(uploaded_file.getvalue())
|
| 30 |
+
|
| 31 |
+
col_img, col_res = st.columns(2)
|
| 32 |
+
|
| 33 |
+
with col_img:
|
| 34 |
+
st.subheader("🖼️ Yüklenen Resim / Uploaded Image")
|
| 35 |
+
st.image(img_data, channels="BGR", use_container_width=True)
|
| 36 |
|
| 37 |
+
with col_res:
|
| 38 |
+
st.subheader("🎯 Analiz / Analysis")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Yan yana dil seçeneği ile buton
|
| 41 |
+
if st.button("Markayı Tahmin Et / Predict Brand", type="primary"):
|
| 42 |
+
with st.spinner("Aranıyor... / Searching..."):
|
| 43 |
+
# Örnek sonuç simülasyonu (Buraya kendi eşleştirme döngünü ekleyebilirsin)
|
| 44 |
+
st.balloons()
|
| 45 |
+
st.success("✅ İşlem Tamamlandı / Process Completed")
|
| 46 |
+
st.metric("Benzerlik / Similarity", "%92")
|
| 47 |
|
| 48 |
+
# --- TİTREMEYEN TEKNİK DETAYLAR / NON-FLICKERING DETAILS ---
|
| 49 |
+
st.markdown("---")
|
| 50 |
+
with st.expander("🔍 Görüntü İşleme Detayları / View Image Processing Details"):
|
| 51 |
+
tab1, tab2 = st.tabs(["🇹🇷 Türkçe Açıklama", "🇺🇸 English Description"])
|
| 52 |
|
| 53 |
+
with tab1:
|
| 54 |
+
st.write("### Görüntü Analiz Aşamaları")
|
| 55 |
+
c1, c2 = st.columns(2)
|
| 56 |
+
c1.image(edge_data, caption="Kenar Algılama (Canny)")
|
| 57 |
+
c2.image(cv2.resize(gray_data, (100, 100)), caption="Yapay Zeka Giriş Boyutu (100x100)")
|
| 58 |
+
st.info("Sistem, logonun dış hatlarını (kenarlarını) çıkararak veri setindeki örneklerle karşılaştırır.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
with tab2:
|
| 61 |
+
st.write("### Image Analysis Stages")
|
| 62 |
+
ce1, ce2 = st.columns(2)
|
| 63 |
+
ce1.image(edge_data, caption="Edge Detection (Canny)")
|
| 64 |
+
ce2.image(cv2.resize(gray_data, (100, 100)), caption="AI Input Size (100x100)")
|
| 65 |
+
st.info("The system extracts the outlines (edges) of the logo and compares them with the samples in the dataset.")
|
| 66 |
+
|
| 67 |
+
# Alt Bilgi / Footer
|
| 68 |
+
st.sidebar.markdown("""
|
| 69 |
+
### 🛠️ Sistem Durumu / System Status
|
| 70 |
+
- **Hız / Speed:** Optimize Edildi (Optimized)
|
| 71 |
+
- **Mod:** Karşılaştırmalı (Comparative)
|
| 72 |
+
""")
|