blitzkrieg0000 commited on
Commit
ad81be2
·
verified ·
1 Parent(s): ec77a3e

Update UI/Main.py

Browse files
Files changed (1) hide show
  1. UI/Main.py +49 -26
UI/Main.py CHANGED
@@ -2,13 +2,19 @@ import os
2
  import sys
3
  sys.path.append(os.getcwd())
4
 
5
- import numpy as np
6
- import gradio as gr
7
  import cv2
8
- from Lib.Core import CablePoleSegmentation
 
9
 
 
 
10
 
11
  demoImages = [
 
 
 
 
 
12
  "data/16_3450.png",
13
  "data/16_3735.png",
14
  "data/16_3900.png",
@@ -16,8 +22,7 @@ demoImages = [
16
  "data/25_00272.png",
17
  "data/67_02661.png"
18
  ]
19
- labels = {0: "Boş", 1: "Çelik Direkler", 2: "Kafes Kule", 3: "Kablo", 4: "Ahşap Kule"}
20
- color_map = {"Boş":"#ffffff", "Çelik Direkler":"#0000ff", "Kablo":"#00ff00", "Kafes Kule":"#ff0000", "Kablo":"#00ff00", "Ahşap Kule":"#ff0000"}
21
  MODEL = CablePoleSegmentation(retina_mask=False)
22
 
23
  def Warning():
@@ -26,46 +31,64 @@ def Warning():
26
  with gr.Blocks(css="footer{display:none !important}") as block:
27
  gr.Markdown("## Yüksek Gerilim Hattı Kablo ve Direk Tespit ve Segmentasyon Uygulaması - Demo")
28
 
29
-
30
  with gr.Row():
31
  image = gr.Image(label="Fotoğraf")
32
- processedImage = gr.Image(label="Sonuçlar")
33
-
34
- with gr.Row() as imageGalleryRow:
35
- imageGallery = gr.Gallery(
36
- demoImages,
37
- label="Demo Görsellerden Seçebilirsiniz",
38
- rows=2,
39
- columns=4,
40
  object_fit="contain",
41
  height="auto"
42
  )
 
 
 
 
 
 
 
 
 
 
 
43
 
44
 
45
  results = gr.Textbox(label="Log")
 
 
 
46
  processButton = gr.Button("Tespit Et")
47
- annotatedImage = gr.AnnotatedImage(color_map=color_map)
48
 
49
- @processButton.click(outputs=[processedImage, annotatedImage, results], inputs=image)
50
- def Process(image):
51
- MODEL = CablePoleSegmentation()
52
- masks, boxes, classes, result_plot = MODEL.Process(image)
53
 
54
- sections = []
 
 
 
 
 
55
 
56
- for m, b, c in zip(masks, boxes, classes):
57
- sections+=[(m, labels[c])]
58
- # sections+=[([*b], labels[c])]
59
 
60
- return result_plot, (image, sections), "Görüntü İşlendi!"
 
 
 
 
 
 
61
 
62
 
63
- @imageGallery.select(inputs=imageGallery, outputs=[processedImage, annotatedImage, results])
64
  def GalleryProcess(image, evt_data: gr.EventData):
65
  selectedIdx = evt_data._data["index"]
66
  imagePath = demoImages[selectedIdx]
67
  image = cv2.imread(imagePath)
68
- return Process(image)
69
 
70
 
71
  block.load(Warning)
 
2
  import sys
3
  sys.path.append(os.getcwd())
4
 
 
 
5
  import cv2
6
+ import gradio as gr
7
+ import numpy as np
8
 
9
+ from Lib.Consts import COLOR_MAP, LABELS
10
+ from Lib.Core import CablePoleSegmentation
11
 
12
  demoImages = [
13
+ "data/DJI_20240905095004_0007_W.JPG",
14
+ "data/DJI_20240905091530_0003_W.JPG",
15
+ "data/DJI_20240905094647_0003_W.JPG",
16
+ "data/DJI_20240905094647_0003_Z.JPG",
17
+ "data/DJI_20240905101846_0005_W.JPG",
18
  "data/16_3450.png",
19
  "data/16_3735.png",
20
  "data/16_3900.png",
 
22
  "data/25_00272.png",
23
  "data/67_02661.png"
24
  ]
25
+
 
26
  MODEL = CablePoleSegmentation(retina_mask=False)
27
 
28
  def Warning():
 
31
  with gr.Blocks(css="footer{display:none !important}") as block:
32
  gr.Markdown("## Yüksek Gerilim Hattı Kablo ve Direk Tespit ve Segmentasyon Uygulaması - Demo")
33
 
 
34
  with gr.Row():
35
  image = gr.Image(label="Fotoğraf")
36
+ processedImageGallery = gr.Gallery(
37
+ label="Sonuçlar",
38
+ rows=1,
39
+ columns=2,
 
 
 
 
40
  object_fit="contain",
41
  height="auto"
42
  )
43
+
44
+ with gr.Row() as imageGalleryRow:
45
+ with gr.Accordion("Demo Görsellerden Seçebilirsiniz", open=False):
46
+ imageGallery = gr.Gallery(
47
+ demoImages,
48
+ label="Demo Görseller",
49
+ rows=3,
50
+ columns=4,
51
+ object_fit="contain",
52
+ height="auto"
53
+ )
54
 
55
 
56
  results = gr.Textbox(label="Log")
57
+
58
+ thresholdSlider = gr.Slider(0, 1, value=0.5, label="Model Eşik Değeri", info="0 ve 1 arası seçiniz.")
59
+
60
  processButton = gr.Button("Tespit Et")
61
+ annotatedImage = gr.AnnotatedImage(color_map=COLOR_MAP)
62
 
63
+ @processButton.click(outputs=[processedImageGallery, annotatedImage, results], inputs=[image, thresholdSlider])
64
+ def Process(image, model_threshold):
65
+ if image is None:
66
+ raise gr.Warning("Lütfen görüntü yükleyiniz veya hazır seçiniz!", duration=3)
67
 
68
+ batches = MODEL.Process(image, model_threshold)
69
+
70
+ if len(batches) == 0:
71
+ raise gr.Error("Bir Hata ile Karşılaşıldı: Görüntüde Tespit Yapılamadı 💥!", duration=5)
72
+
73
+ scores, classes, masks, boxes = batches[0] # First
74
 
75
+ canvas, mask = MODEL.DrawResults(image, scores, classes, masks, boxes, class_filter=None)
 
 
76
 
77
+ sections = []
78
+ for m, b, c in zip(masks.cpu().numpy(), boxes.cpu().numpy(),classes.cpu().numpy()):
79
+ sections+=[(m, LABELS[c])]
80
+ # sections+=[(list(b.astype(np.int32)), LABELS[c])]
81
+
82
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
83
+ return [image, canvas], (image, sections), "Görüntü İşlendi!"
84
 
85
 
86
+ @imageGallery.select(inputs=imageGallery, outputs=[processedImageGallery, annotatedImage, results])
87
  def GalleryProcess(image, evt_data: gr.EventData):
88
  selectedIdx = evt_data._data["index"]
89
  imagePath = demoImages[selectedIdx]
90
  image = cv2.imread(imagePath)
91
+ return Process(image, thresholdSlider.value)
92
 
93
 
94
  block.load(Warning)