Upload 17 files
Browse files- .gitattributes +8 -0
- Lib/Core.py +87 -0
- Lib/__init__.py +0 -0
- Predict.py +57 -0
- UI/Main.py +77 -0
- UI/__init__.py +0 -0
- cable_mask.png +0 -0
- data/16_3450.png +3 -0
- data/16_3675.png +3 -0
- data/16_3735.png +3 -0
- data/16_3900.png +3 -0
- data/19_00350.png +3 -0
- data/23_00961.png +3 -0
- data/25_00272.png +3 -0
- data/67_02661.png +3 -0
- readme.txt +4 -0
- requirements.txt +5 -0
- weight/yolov8l-seg-pre100.onnx +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,11 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
data/16_3450.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
data/16_3675.png filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
data/16_3735.png filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
data/16_3900.png filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
data/19_00350.png filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
data/23_00961.png filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
data/25_00272.png filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
data/67_02661.png filter=lfs diff=lfs merge=lfs -text
|
Lib/Core.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torch
|
| 4 |
+
from matplotlib import pyplot as plt
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
+
from ultralytics.engine.results import Masks
|
| 7 |
+
|
| 8 |
+
class CablePoleSegmentation():
|
| 9 |
+
def __init__(self, MODEL_PATH=None, retina_mask=False):
|
| 10 |
+
if not MODEL_PATH:
|
| 11 |
+
MODEL_PATH = "./weight/yolov8l-seg-pre100.onnx"
|
| 12 |
+
self._RetinaMask=retina_mask
|
| 13 |
+
self.Model = YOLO(MODEL_PATH) # load a custom model
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def RescaleTheMask(self, orijinal_image, masks):
|
| 17 |
+
_masks = []
|
| 18 |
+
for contour in masks:
|
| 19 |
+
b_mask = np.zeros(orijinal_image.shape[:2], np.uint8)
|
| 20 |
+
contour = contour.astype(np.int32)
|
| 21 |
+
contour = contour.reshape(-1, 1, 2)
|
| 22 |
+
mask = cv2.drawContours(b_mask, [contour], -1, (1, 1, 1), cv2.FILLED)
|
| 23 |
+
_masks += [mask]
|
| 24 |
+
return _masks
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def Process(self, image):
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
results = self.Model(
|
| 30 |
+
image,
|
| 31 |
+
save=False,
|
| 32 |
+
show_boxes=False,
|
| 33 |
+
project="./result/",
|
| 34 |
+
conf=0.5,
|
| 35 |
+
retina_masks=self._RetinaMask,
|
| 36 |
+
stream=True
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
for result in results:
|
| 41 |
+
maskCountours = result.masks.xy
|
| 42 |
+
boxes = result.boxes.xyxy.int().cpu().numpy()
|
| 43 |
+
classes = result.boxes.cls.cpu().numpy()
|
| 44 |
+
|
| 45 |
+
rescaledMasks = self.RescaleTheMask(image, maskCountours)
|
| 46 |
+
return rescaledMasks, boxes, classes, result.plot()
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def PlotResults(self, masks, boxes, classes, original_image, result_image, mask, cable_mask):
|
| 51 |
+
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(27,15))
|
| 52 |
+
axs[0][0].imshow(original_image)
|
| 53 |
+
axs[0][0].set_title("Orijinal Görüntü")
|
| 54 |
+
|
| 55 |
+
axs[0][1].imshow(mask)
|
| 56 |
+
axs[0][1].set_title("Segmentasyon Maskesi")
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
cv2.imwrite("cable_mask.png", cable_mask)
|
| 60 |
+
axs[1][0].imshow(cable_mask)
|
| 61 |
+
axs[1][0].set_title("Seçilen")
|
| 62 |
+
|
| 63 |
+
axs[1][1].imshow(result_image)
|
| 64 |
+
axs[1][1].set_title("Sonuç")
|
| 65 |
+
plt.show()
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
if "__main__" == __name__:
|
| 70 |
+
test = "data/16_3450.png"
|
| 71 |
+
image = cv2.imread(test)
|
| 72 |
+
model = CablePoleSegmentation(retina_mask=True)
|
| 73 |
+
masks, boxes, classes, result_plot = model.Process(image)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(27,15))
|
| 77 |
+
axs[0][0].imshow(image)
|
| 78 |
+
axs[0][0].set_title("Orijinal Görüntü")
|
| 79 |
+
axs[1][1].imshow(np.any(masks, axis=0))
|
| 80 |
+
axs[1][1].set_title("Sonuç")
|
| 81 |
+
plt.show()
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# model.PlotResults(*model.Process(image))
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
Lib/__init__.py
ADDED
|
File without changes
|
Predict.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
from matplotlib import pyplot as plt
|
| 3 |
+
import numpy as np
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# Data
|
| 8 |
+
test01 = "data/16_3450.png"
|
| 9 |
+
test_image = test01
|
| 10 |
+
|
| 11 |
+
# Load a model
|
| 12 |
+
model = YOLO("weight/yolov8l-seg-pre100.onnx") # load a custom model
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
with torch.no_grad():
|
| 16 |
+
results = model(
|
| 17 |
+
test_image,
|
| 18 |
+
save=True,
|
| 19 |
+
show_boxes=False,
|
| 20 |
+
project="./result/",
|
| 21 |
+
conf=0.5,
|
| 22 |
+
retina_masks=False
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
with torch.no_grad():
|
| 27 |
+
for result in results:
|
| 28 |
+
masks = result.masks.data
|
| 29 |
+
boxes = result.boxes.data
|
| 30 |
+
|
| 31 |
+
#ALL
|
| 32 |
+
canvas = torch.any(masks, dim=0).int() * 255
|
| 33 |
+
|
| 34 |
+
clss = boxes[:, 5]
|
| 35 |
+
obj_indices = torch.where(clss == 4)
|
| 36 |
+
|
| 37 |
+
# Cable
|
| 38 |
+
obj_masks = masks[obj_indices]
|
| 39 |
+
obj_mask = torch.any(obj_masks, dim=0).int() * 255
|
| 40 |
+
# cropped_image = result.orig_img[obj_mask.cpu().numpy()]
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(27,15))
|
| 44 |
+
axs[0][0].imshow(result.orig_img)
|
| 45 |
+
axs[0][0].set_title("Orijinal Görüntü")
|
| 46 |
+
|
| 47 |
+
axs[0][1].imshow(canvas.cpu().numpy())
|
| 48 |
+
axs[0][1].set_title("Segmentasyon Maskesi")
|
| 49 |
+
|
| 50 |
+
mask = np.array(obj_mask.cpu().numpy())*255
|
| 51 |
+
cv2.imwrite("cable_mask.png", mask)
|
| 52 |
+
axs[1][0].imshow(obj_mask.cpu().numpy())
|
| 53 |
+
axs[1][0].set_title("Seçilen")
|
| 54 |
+
|
| 55 |
+
axs[1][1].imshow(result.plot())
|
| 56 |
+
axs[1][1].set_title("Sonuç")
|
| 57 |
+
plt.show()
|
UI/Main.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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",
|
| 15 |
+
"data/19_00350.png",
|
| 16 |
+
"data/25_00272.png",
|
| 17 |
+
"data/67_02661.png"
|
| 18 |
+
]
|
| 19 |
+
labels = {0: "Boş", 1: "Çelik Direkler", 2: "Kablo", 3: "Kafes Kule", 4: "Kablo", 5: "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():
|
| 24 |
+
gr.Info("DGH ARGE YAZILIM DANIŞMANLIK ENERJİ İNŞAAT SAN.TİC.LTD.ŞTİ", duration=0.5)
|
| 25 |
+
|
| 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)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
block.queue(max_size=10)
|
| 75 |
+
block.launch(server_port=1071)
|
| 76 |
+
|
| 77 |
+
|
UI/__init__.py
ADDED
|
File without changes
|
cable_mask.png
ADDED
|
data/16_3450.png
ADDED
|
Git LFS Details
|
data/16_3675.png
ADDED
|
Git LFS Details
|
data/16_3735.png
ADDED
|
Git LFS Details
|
data/16_3900.png
ADDED
|
Git LFS Details
|
data/19_00350.png
ADDED
|
Git LFS Details
|
data/23_00961.png
ADDED
|
Git LFS Details
|
data/25_00272.png
ADDED
|
Git LFS Details
|
data/67_02661.png
ADDED
|
Git LFS Details
|
readme.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
1-Anaconda ortamı kurulur.
|
| 2 |
+
|
| 3 |
+
2-Requirements içerisindeki kütüphaneler yüklenir.
|
| 4 |
+
$ conda install --yes --file requirements.txt -c pytorch -c nvidia
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics
|
| 2 |
+
pytorch
|
| 3 |
+
torchvision
|
| 4 |
+
torchaudio
|
| 5 |
+
pytorch-cuda=12.4
|
weight/yolov8l-seg-pre100.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bc705770eb7a2ac69bbc18fa811e48865c56426d9a7e413f7c3024eda3cabe25
|
| 3 |
+
size 183915900
|