EV5_Beeldherk_Bomen/src/suite.py

368 lines
13 KiB
Python
Raw Normal View History

2023-09-14 14:24:12 +02:00
#!/usr/bin/python3
import pathlib
import pygubu
2023-09-22 13:45:34 +02:00
import glob
2023-09-14 14:24:12 +02:00
from tkinter import *
from PIL import ImageTk, Image
import numpy as np
import cv2
2023-09-22 13:45:34 +02:00
import time
2023-09-22 15:23:22 +02:00
import matplotlib.pyplot as plt
2023-09-23 21:02:30 +02:00
import json
2023-09-25 14:57:34 +02:00
from helpers.statistics import imgStats
import datetime
import os
2023-09-27 16:38:22 +02:00
import copy
2023-09-14 14:24:12 +02:00
2023-09-23 21:02:30 +02:00
## UI config load
2023-09-14 14:24:12 +02:00
PROJECT_PATH = pathlib.Path(__file__).parent
2023-09-23 18:35:51 +02:00
PROJECT_UI = "./src/helpers/gui/main.ui"
2023-09-23 21:02:30 +02:00
## Config file load
2023-09-23 18:35:51 +02:00
CONFIG_PATH = "./src/config/config.json"
2023-09-23 21:02:30 +02:00
config_file = open(CONFIG_PATH)
config_json = json.load(config_file)
2023-09-14 14:24:12 +02:00
2023-09-23 21:02:30 +02:00
## UI class setup
2023-09-14 14:24:12 +02:00
class MainApp:
def __init__(self, master=None):
self.builder = builder = pygubu.Builder()
builder.add_resource_path(PROJECT_PATH)
builder.add_from_file(PROJECT_UI)
2023-09-22 15:23:22 +02:00
2023-09-14 14:24:12 +02:00
# Main widget
self.mainwindow = builder.get_object("main", master)
2023-09-23 21:02:30 +02:00
# Canvas for output images
2023-09-14 14:24:12 +02:00
self.canvas = builder.get_object("output_canvas")
2023-09-23 21:02:30 +02:00
self.tk_imgs = [] # Required or python will forget
self.output = [[] for x in range(2)]
self.meta = builder.get_object("dataset")
# Keep track of images in dataset
2023-09-22 13:45:34 +02:00
self.img_current = 0
2023-09-28 16:07:18 +02:00
self.img_name = ""
self.img_old = -1 ## minus 1 to enforce full update on start
2023-09-22 13:45:34 +02:00
self.img_max = 0
2023-09-22 15:23:22 +02:00
# Plots
self.axs = self.createPlot(2, 2)
2023-09-23 21:02:30 +02:00
# UI Variables
self.canny_thr1 = None
self.canny_thr2 = None
self.img_path = None
2023-09-25 22:08:01 +02:00
self.contrast = None
2023-09-27 16:38:22 +02:00
2023-09-23 21:02:30 +02:00
self.img_size = None
2023-09-27 16:38:22 +02:00
self.img_size_old = 0 ## Check if the rendering size has changed, if it has the analysis has to be run
2023-09-14 14:24:12 +02:00
self.sobel_select = None
2023-09-22 13:45:34 +02:00
self.export_id = None
2023-09-25 22:08:01 +02:00
self.brightness = None
builder.import_variables(self,['canny_thr1','canny_thr2','img_path','contrast','img_size','sobel_select','export_id','brightness'])
2023-09-14 14:24:12 +02:00
builder.connect_callbacks(self)
2023-09-23 21:02:30 +02:00
# Load values from config after UI has been initialised
self.img_path.set(config_json["path"])
self.img_size.set(config_json["size"])
def on_quit(self, event=None):
'''
Close PLT windows on main app quit
'''
plt.close()
self.mainwindow.quit();
2023-09-14 14:24:12 +02:00
def run(self):
2023-09-23 21:02:30 +02:00
'''
Run loop
'''
2023-09-14 14:24:12 +02:00
self.mainwindow.mainloop()
2023-09-22 13:45:34 +02:00
def img_prev(self, event=None):
2023-09-23 21:02:30 +02:00
'''
Open previous image from path
'''
2023-09-22 13:45:34 +02:00
if self.img_current == 0:
self.img_current = self.img_max - 1
else:
self.img_current = self.img_current - 1
self.update(self)
def img_next(self, event=None):
2023-09-23 21:02:30 +02:00
'''
Open next image from path
'''
2023-09-22 13:45:34 +02:00
if self.img_current == (self.img_max - 1):
self.img_current = 0
else:
self.img_current = self.img_current + 1
self.update(self)
2023-09-27 16:38:22 +02:00
def apply(self, event=None, path=None):
2023-09-23 21:02:30 +02:00
'''
Export current dataset
'''
# Get export settings
2023-09-22 13:45:34 +02:00
img_arr = self.tk_imgs
img_id = self.export_id.get()
2023-09-27 16:38:22 +02:00
if path == None:
path = config_json["out"]
else:
print(F"Using path: {path}")
2023-09-22 13:45:34 +02:00
if (img_id >= 0 and img_id < len(img_arr)):
# Create file
now = datetime.datetime.now()
2023-09-27 16:38:22 +02:00
new_file_name = F"{self.img_current}-{self.output[1][img_id]}-{now.strftime('%Y-%m-%dT%H.%M.%S')}.png"
# Put data
file_path = pathlib.Path(path, new_file_name)
# print(file_path)
imgpil = ImageTk.getimage(self.tk_imgs[img_id])
imgpil.save(file_path, "PNG" )
imgpil.close()
print(f"Exported Image ID {img_id} to {os.path.join(path, new_file_name)}")
2023-09-22 13:45:34 +02:00
else:
print("Nothing to export!")
2023-09-27 16:38:22 +02:00
def apply_all(self, event=None):
2023-09-28 16:07:18 +02:00
'''
Export given preprocess id for every image in the dataset folder
'''
2023-09-27 16:38:22 +02:00
img_id = self.export_id.get()
img_current = copy.deepcopy(self.img_current)
now = datetime.datetime.now()
path = pathlib.Path(config_json["out"], F"{self.output[1][img_id]}-all-{now.strftime('%Y-%m-%dT%H.%M.%S')}/")
os.mkdir(path)
while True:
self.img_next()
self.update(part_update=True) # Enforce partial update since we don't need the histograms etc.
self.apply(path=path)
if (self.img_current == img_current):
break
## Ensure display is always correct with image
self.update()
2023-09-14 14:24:12 +02:00
2023-09-23 21:02:30 +02:00
def add_output(self, data, name: str):
'''
Add CV2 image to canvas output
'''
self.output[0].append(data)
self.output[1].append(name)
def draw_output(self, size):
# Check if size of canvas has updated
drawW = self.canvas.winfo_width()
# Reset drawing position
drawX = 0
drawY = 0
# Clear previously printed images
self.tk_imgs = []
self.meta.config(state=NORMAL)
self.meta.delete(1.0, END)
2023-09-28 16:07:18 +02:00
self.meta.insert(END, f"{self.img_name[1]}\n")
2023-09-23 21:02:30 +02:00
# Draw all output images
for idx, data in enumerate(self.output[0]):
# Create ui image
tk_img = cv2.cvtColor(data, cv2.COLOR_BGR2RGB)
tk_img = ImageTk.PhotoImage(image=Image.fromarray(tk_img))
self.tk_imgs.append(tk_img)
## Check if next item will be out of range
if (drawX + size >= drawW):
drawY += size
drawX = 0
self.canvas.configure(height=(drawY+size))
self.canvas.create_image(drawX,drawY,anchor=NW,image=self.tk_imgs[idx],tags="og")
drawX += size
# Add name to text box
self.meta.insert(END, F"{idx}: {self.output[1][idx]}\n")
# Clear output
self.meta.config(state=DISABLED)
# Draw canvas
# TODO IDK volgens mij moet je deze wel callen maar het programma doet het nog (geen vragen stellen)
# self.canvas.draw()
2023-09-22 15:23:22 +02:00
def createPlot(self, columns, rows):
fig, axs = plt.subplots(columns, rows)
return axs
2023-09-23 21:02:30 +02:00
2023-09-22 15:23:22 +02:00
def drawHist(self, image, labels, column, row):
self.axs[column, row].clear()
for i,lab in enumerate(labels):
hist = cv2.calcHist(
[image],
[i],
None,
[256],
[0, 256],
)
self.axs[column, row].plot(hist, label=lab)
self.axs[column, row].grid()
self.axs[column, row].legend()
2023-09-22 16:15:57 +02:00
def drawCannyHM(self, img, column, row):
self.axs[column, row].clear()
canny_max = 500
canny_step = 20
results = [[] for x in range((int)(canny_max / canny_step))]
2023-09-22 15:23:22 +02:00
2023-09-22 16:15:57 +02:00
for th1 in range(0, canny_max, canny_step):
for th2 in range(0, canny_max, canny_step):
# Canny Edge Detection
edges = cv2.Canny(image=img, threshold1=th1, threshold2=th2)
w_res = cv2.countNonZero(edges)
y_ind = (int)(th1 / canny_step)
x_ind = (int)(th2 / canny_step)
results[y_ind].append(w_res)
# print(f"Result at thres {th1}, {th2}; \tIndex {y_ind}, {x_ind} \t= {w_res}")
# print(results[y_ind])
2023-09-25 22:08:01 +02:00
2023-09-28 16:07:18 +02:00
func = np.diag(results)
self.axs[column, row-1].clear()
self.axs[column, row-1].title.set_text("Canny F U N C")
self.axs[column, row-1].plot(func)
self.axs[column, row-1].plot(np.diff(func))
self.axs[column, row].title.set_text(F"Mean: {np.matrix(results).mean()}\nStd: {np.matrix(results).std()}")
2023-09-22 16:15:57 +02:00
self.axs[column, row].imshow(results)
self.axs[column, row].xaxis.set_major_formatter(lambda x, pos: str(x*canny_step))
self.axs[column, row].yaxis.set_major_formatter(lambda x, pos: str(x*canny_step))
2023-09-25 14:57:34 +02:00
def writeStats(self, img, labels, column, row):
mean, std = imgStats(img)
self.axs[column, row].title.set_text(
2023-09-28 16:07:18 +02:00
"Mean: %c:%d %c:%d %c:%d \nStd: %c:%d %c:%d %c:%d"
2023-09-25 14:57:34 +02:00
%(labels[0], mean[0], labels[1], mean[1], labels[2], mean[2],
2023-09-25 20:37:11 +02:00
labels[0], std[0], labels[1], std[1], labels[2], std[2]))
2023-09-22 16:15:57 +02:00
2023-09-27 16:38:22 +02:00
def update(self, event=None, part_update=False):
2023-09-14 14:24:12 +02:00
path = self.img_path.get()
2023-09-27 16:38:22 +02:00
## Check if hist and canny hm have to be rerendered
if not part_update: ## If partial update has not been forced, check if full update is required
if (self.img_current != self.img_old or self.img_size != self.img_size_old):
part_update = False
self.img_old = self.img_current
self.img_size_old = self.img_size
else:
part_update = True
else:
print("Partial update forced!")
2023-09-14 14:24:12 +02:00
if path != None and path != "":
2023-09-22 13:45:34 +02:00
# Get all images at current path
images = []
2023-09-28 16:07:18 +02:00
for file in glob.glob(path + "/*.jpg"):
2023-09-22 13:45:34 +02:00
images.append(file)
self.img_max = len(images)
2023-09-28 16:07:18 +02:00
self.img_name = os.path.split(images[self.img_current])
2023-09-22 13:45:34 +02:00
2023-09-14 14:24:12 +02:00
# Get all user vars
ct1 = self.canny_thr1.get()
2023-09-22 16:15:57 +02:00
ct2 = self.canny_thr2.get()
2023-09-14 14:24:12 +02:00
sxy = self.sobel_select.get()
size = self.img_size.get()
2023-09-25 22:08:01 +02:00
contrast = self.contrast.get()
bright = self.brightness.get()
2023-09-14 14:24:12 +02:00
2023-09-27 16:38:22 +02:00
# Clear output
self.output = [[] for x in range(2)]
2023-09-14 14:24:12 +02:00
# Import and resize image
2023-09-22 13:45:34 +02:00
img = cv2.imread(images[self.img_current])
2023-09-14 14:24:12 +02:00
img = cv2.resize(img, (size, size), interpolation = cv2.INTER_AREA)
2023-09-23 21:02:30 +02:00
self.add_output(img, "Original")
2023-09-14 14:24:12 +02:00
# Set grayscale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
2023-09-23 21:02:30 +02:00
self.add_output(img_gray, "Grayscale")
2023-09-14 14:24:12 +02:00
2023-09-25 22:08:01 +02:00
# Contrast / brightness boost
contrast_val = contrast / 100
bright_val = bright / 100
img_contrast = np.clip(contrast_val * (img_gray + bright_val), 0, 255).astype(np.uint8)
2023-09-27 16:38:22 +02:00
# self.add_output(img_contrast, F"Contrast / Brightness\n c+{contrast_val} b+{bright_val}")
self.add_output(img_contrast, F"BCG")
2023-09-25 22:08:01 +02:00
2023-09-14 14:24:12 +02:00
# Blurred edition
2023-09-25 22:08:01 +02:00
img_blur = cv2.GaussianBlur(img_gray, (3, 3), 0)
2023-09-27 16:38:22 +02:00
self.add_output(img_blur, "Blurred_k3")
2023-09-14 14:24:12 +02:00
# Sobel edge
if sxy in ['x', 'y', 'both']:
if sxy == 'x':
dx = 1
dy = 0
elif sxy == 'y':
dx = 0
dy = 1
elif sxy == 'both':
dx = 1
dy = 1
img_sobel = cv2.Sobel(src=img_blur, ddepth=cv2.CV_8U, dx=dx, dy=dy, ksize=5)
else:
2023-09-23 21:02:30 +02:00
img_sobel = img_gray
2023-09-27 16:38:22 +02:00
self.add_output(img_sobel, "Sobel_edge")
# self.add_output(img_sobel, F"Sobel Edge\n nz={cv2.countNonZero(img_sobel)}")
2023-09-14 14:24:12 +02:00
# Canny edge
img_canny = cv2.Canny(image=img_blur,threshold1=ct1,threshold2=ct2)
2023-09-27 16:38:22 +02:00
self.add_output(img_canny, "Canny_edge")
2023-09-22 15:23:22 +02:00
# BGR
2023-09-27 16:38:22 +02:00
self.add_output(img[:, :, 0], "BGR_B")
self.add_output(img[:, :, 1], "BGR_G")
self.add_output(img[:, :, 2], "BGR_R")
2023-09-23 21:02:30 +02:00
2023-09-22 15:23:22 +02:00
if img is not None:
self.drawHist(img, ('B', 'G', 'R'), 0, 0)
2023-09-28 16:07:18 +02:00
self.writeStats(img, ('B', 'G', 'R'), 0, 0)
2023-09-22 15:23:22 +02:00
# HSV
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
2023-09-23 21:02:30 +02:00
self.add_output(img_hsv, "HSV")
2023-09-27 16:38:22 +02:00
self.add_output(img_hsv[:, :, 0], "HSV_H") # H
self.add_output(img_hsv[:, :, 1], "HSV_S") # S
self.add_output(img_hsv[:, :, 2], "HSV_V") # V
2023-09-23 21:02:30 +02:00
2023-09-27 16:38:22 +02:00
if not part_update:
if img_hsv is not None:
self.drawHist(img_hsv, ('H', 'S', 'V'), 0, 1)
self.writeStats(img_hsv, ('H', 'S', 'V'), 0, 1)
2023-09-22 16:15:57 +02:00
2023-09-23 21:02:30 +02:00
# Canny Heatmap
2023-09-27 16:38:22 +02:00
if not part_update:
self.drawCannyHM(img, 1, 1)
2023-09-14 14:24:12 +02:00
2023-09-23 21:02:30 +02:00
# Show all data
plt.show(block=False) ## Graphs
self.draw_output(size) ## Images
2023-09-14 14:24:12 +02:00
if __name__ == "__main__":
app = MainApp()
app.run()