| import os |
| import sys |
| sys.path.append(os.getcwd()) |
|
|
| import cv2 |
| import gradio as gr |
| import numpy as np |
|
|
| from Lib.Consts import COLOR_MAP, LABELS |
| from Lib.Core import CablePoleSegmentation |
|
|
| demoImages = [ |
| "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", |
| "data/19_00350.png", |
| "data/25_00272.png", |
| "data/67_02661.png" |
| ] |
|
|
| MODEL = CablePoleSegmentation(retina_mask=False) |
|
|
| def Warning(): |
| gr.Info("DGH ARGE YAZILIM DANIŞMANLIK ENERJİ İNŞAAT SAN.TİC.LTD.ŞTİ", duration=0.5) |
|
|
| with 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(): |
| with gr.Column(): |
| inputImage = gr.Image(label="Fotoğraf") |
|
|
| with gr.Column(): |
| thresholdSlider = gr.Slider(0, 1, value=0.5, label="Model Eşik Değeri", info="0 ve 1 arası seçiniz.") |
| with gr.Accordion("Demo Görsellerden Seçebilirsiniz", open=False): |
| imageGallery = gr.Examples( |
| examples=[ |
| os.path.join("data", img_name) for img_name in sorted(os.listdir("data")) |
| ], |
| inputs=[inputImage], |
| label="Örnekler", |
| cache_examples=False, |
| examples_per_page=5 |
| ) |
| results = gr.Textbox(label="Log") |
| processButton = gr.Button("Tespit Et") |
|
|
|
|
| gr.HTML("</hr>") |
| processedImageGallery = gr.Gallery( |
| label="Sonuçlar", |
| rows=1, |
| columns=2, |
| object_fit="contain", |
| height="auto" |
| ) |
|
|
| annotatedImage = gr.AnnotatedImage(color_map=COLOR_MAP) |
|
|
| @processButton.click(outputs=[processedImageGallery, annotatedImage, results], inputs=[inputImage, 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] |
| |
| canvas, 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])] |
| |
| |
| image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
| return [image, canvas], (image, sections), "Görüntü İşlendi!" |
| |
|
|
|
|
| |
| block.load(Warning) |
|
|
|
|
| block.queue(max_size=10) |
| block.launch(server_name="0.0.0.0", server_port=1071) |
|
|