Files changed (1) hide show
  1. app.py +25 -40
app.py CHANGED
@@ -1,46 +1,31 @@
1
  import gradio as gr
2
- from transformers import ViTFeatureExtractor, ViTForImageClassification
3
  from PIL import Image
4
- import torch
5
- import numpy as np
6
 
7
- # *******************************************************************
8
- # ЕҢ ДӘЛ ЖӘНЕ ҚОҚЫС ЖІКТЕУГЕ АРНАЛҒАН МОДЕЛЬ ID-І (90%+ Accuracy)
9
- # *******************************************************************
10
- MODEL_ID = "keremberke/vit-base-patch16-224-full-empty-trash-bin"
11
- CLASS_NAMES = ['Empty', 'Full']
12
 
13
- try:
14
- feature_extractor = ViTFeatureExtractor.from_pretrained(MODEL_ID)
15
- model = ViTForImageClassification.from_pretrained(MODEL_ID)
16
- MODEL_LOADED = True
17
- if model.config.id2label:
18
- CLASS_NAMES = [model.config.id2label[i] for i in model.config.id2label]
 
 
 
 
 
 
 
19
 
20
- except Exception as e:
21
- print(f"ERROR: Model loading failed: {e}")
22
- MODEL_LOADED = False
23
-
24
- # [ҚАЛҒАН КӨПЕЙЛІК КОД ӨЗГЕРІССІЗ ҚАЛАДЫ]
25
-
26
- def classify_trash_bin(image):
27
- # ... (Алдыңғы жауаптағыдай қате өңдеу және болжау коды)
28
- if not MODEL_LOADED:
29
- return {"Error": 1.0, "Check Logs": 0.0}
30
- # ... (Қалған код)
31
-
32
- try:
33
- # ... (Болжам жасау)
34
- return results
35
-
36
- except Exception as e:
37
- return {"Error": 1.0, "Check Logs": 0.0}
38
-
39
- iface = gr.Interface(
40
- fn=classify_trash_bin,
41
- inputs=gr.Image(type="numpy", label="SmartTrachAI Input"),
42
- outputs=gr.Label(num_top_classes=2, label="Prediction"),
43
- title="SmartTrachAI",
44
- description="Automated Trash Bin Status Detector."
45
  )
46
- iface.launch()
 
 
1
  import gradio as gr
2
+ from ultralytics import YOLO
3
  from PIL import Image
 
 
4
 
5
+ # Pretrained модельді жүктеу
6
+ model = YOLO('yolov8n.pt')
 
 
 
7
 
8
+ # Суретті тексеру функциясы
9
+ def check_trash(img):
10
+ results = model(img)
11
+ boxes = results[0].boxes
12
+ output = []
13
+ for box in boxes:
14
+ label = box.cls.item()
15
+ conf = box.conf.item()
16
+ if conf > 0.5:
17
+ output.append(f"{results[0].names[int(label)]}: {conf*100:.1f}%")
18
+ if not output:
19
+ return "Қоқыс табылмады"
20
+ return "\n".join(output)
21
 
22
+ # Gradio интерфейсі
23
+ app = gr.Interface(
24
+ fn=check_trash,
25
+ inputs=gr.Image(type="pil"),
26
+ outputs=gr.Textbox(),
27
+ title="SmartCity Waste Detection",
28
+ description="Қоқыс жәшігін, сыртқа төгілген қоқысты және түрін анықтайды"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  )
30
+
31
+ app.launch()