Update UI/Main.py
Browse files1 unmodified line2345678910111213141 unmodified line16171819202122232 unmodified lines262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970711 unmodified lineimport syssys.path.append(os.getcwd())import numpy as npimport gradio as grimport cv2from Lib.Core import CablePoleSegmentationdemoImages = ["data/16_3450.png","data/16_3735.png","data/16_3900.png",1 unmodified line"data/25_00272.png","data/67_02661.png"]labels = {0: "Boş", 1: "Çelik Direkler", 2: "Kafes Kule", 3: "Kablo", 4: "Ahşap Kule"}color_map = {"Boş":"#ffffff", "Çelik Direkler":"#0000ff", "Kablo":"#00ff00", "Kafes Kule":"#ff0000", "Kablo":"#00ff00", "Ahşap Kule":"#ff0000"}MODEL = CablePoleSegmentation(retina_mask=False)def Warning():2 unmodified lineswith gr.Blocks(css="footer{display:none !important}") as block:gr.Markdown("## Yüksek Gerilim Hattı Kablo ve Direk Tespit ve Segmentasyon Uygulaması - Demo")with gr.Row():image = gr.Image(label="Fotoğraf")processedImage = gr.Image(label="Sonuçlar")with gr.Row() as imageGalleryRow:imageGallery = gr.Gallery(demoImages,label="Demo Görsellerden Seçebilirsiniz",rows=2,columns=4,object_fit="contain",height="auto")results = gr.Textbox(label="Log")processButton = gr.Button("Tespit Et")annotatedImage = gr.AnnotatedImage(color_map=color_map)@processButton.click(outputs=[processedImage, annotatedImage, results], inputs=image)def Process(image):MODEL = CablePoleSegmentation()masks, boxes, classes, result_plot = MODEL.Process(image)sections = []for m, b, c in zip(masks, boxes, classes):sections+=[(m, labels[c])]# sections+=[([*b], labels[c])]return result_plot, (image, sections), "Görüntü İşlendi!"@imageGallery.select(inputs=imageGallery, outputs=[processedImage, annotatedImage, results])def GalleryProcess(image, evt_data: gr.EventData):selectedIdx = evt_data._data["index"]imagePath = demoImages[selectedIdx]image = cv2.imread(imagePath)return Process(image)block.load(Warning)1 unmodified line2345678910111213141516171819201 unmodified line222324252627282 unmodified lines313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293941 unmodified lineimport syssys.path.append(os.getcwd())import cv2import gradio as grimport numpy as npfrom Lib.Consts import COLOR_MAP, LABELSfrom Lib.Core import CablePoleSegmentationdemoImages = ["data/DJI_20240905095004_0007_W.JPG","data/DJI_20240905091530_0003_W.JPG","data/DJI_20240905094647_0003_W.JPG","data/DJI_20240905094647_0003_Z.JPG","data/DJI_20240905101846_0005_W.JPG","data/16_3450.png","data/16_3735.png","data/16_3900.png",1 unmodified line"data/25_00272.png","data/67_02661.png"]MODEL = CablePoleSegmentation(retina_mask=False)def Warning():2 unmodified lineswith gr.Blocks(css="footer{display:none !important}") as block:gr.Markdown("## Yüksek Gerilim Hattı Kablo ve Direk Tespit ve Segmentasyon Uygulaması - Demo")with gr.Row():image = gr.Image(label="Fotoğraf")processedImageGallery = gr.Gallery(label="Sonuçlar",rows=1,columns=2,object_fit="contain",height="auto")with gr.Row() as imageGalleryRow:with gr.Accordion("Demo Görsellerden Seçebilirsiniz", open=False):imageGallery = gr.Gallery(demoImages,label="Demo Görseller",rows=3,columns=4,object_fit="contain",height="auto")results = gr.Textbox(label="Log")thresholdSlider = gr.Slider(0, 1, value=0.5, label="Model Eşik Değeri", info="0 ve 1 arası seçiniz.")processButton = gr.Button("Tespit Et")annotatedImage = gr.AnnotatedImage(color_map=COLOR_MAP)@processButton.click(outputs=[processedImageGallery, annotatedImage, results], inputs=[image, thresholdSlider])def Process(image, model_threshold):if image is None:raise gr.Warning("Lütfen görüntü yükleyiniz veya hazır seçiniz!", duration=3)batches = MODEL.Process(image, model_threshold)if len(batches) == 0:raise gr.Error("Bir Hata ile Karşılaşıldı: Görüntüde Tespit Yapılamadı 💥!", duration=5)scores, classes, masks, boxes = batches[0] # Firstcanvas, mask = MODEL.DrawResults(image, scores, classes, masks, boxes, class_filter=None)sections = []for m, b, c in zip(masks.cpu().numpy(), boxes.cpu().numpy(),classes.cpu().numpy()):sections+=[(m, LABELS[c])]# sections+=[(list(b.astype(np.int32)), LABELS[c])]image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)return [image, canvas], (image, sections), "Görüntü İşlendi!"@imageGallery.select(inputs=imageGallery, outputs=[processedImageGallery, annotatedImage, results])def GalleryProcess(image, evt_data: gr.EventData):selectedIdx = evt_data._data["index"]imagePath = demoImages[selectedIdx]image = cv2.imread(imagePath)return Process(image, thresholdSlider.value)block.load(Warning)
UI/Main.py+49-26