Added csv export entire dataset

This commit is contained in:
Arne van Iterson 2023-10-01 20:16:43 +02:00
parent a41d57f451
commit 7c1e14e1b4
3 changed files with 78 additions and 62 deletions

View File

@ -34,7 +34,7 @@
<property name="variable">int:canny_thr1</property> <property name="variable">int:canny_thr1</property>
<bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" /> <bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" />
<layout manager="grid"> <layout manager="grid">
<property name="column">1</property> <property name="column">2</property>
<property name="padx">0</property> <property name="padx">0</property>
<property name="row">1</property> <property name="row">1</property>
</layout> </layout>
@ -44,7 +44,7 @@
<object class="ttk.Label" id="canny_thr1_label" named="True"> <object class="ttk.Label" id="canny_thr1_label" named="True">
<property name="text" translatable="yes">Canny thresh. 1</property> <property name="text" translatable="yes">Canny thresh. 1</property>
<layout manager="grid"> <layout manager="grid">
<property name="column">1</property> <property name="column">2</property>
<property name="row">0</property> <property name="row">0</property>
</layout> </layout>
</object> </object>
@ -57,7 +57,7 @@
<property name="variable">int:canny_thr2</property> <property name="variable">int:canny_thr2</property>
<bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" /> <bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" />
<layout manager="grid"> <layout manager="grid">
<property name="column">2</property> <property name="column">3</property>
<property name="columnspan">1</property> <property name="columnspan">1</property>
<property name="row">1</property> <property name="row">1</property>
</layout> </layout>
@ -67,7 +67,7 @@
<object class="ttk.Label" id="canny_thr2_label" named="True"> <object class="ttk.Label" id="canny_thr2_label" named="True">
<property name="text" translatable="yes">Canny thresh. 2</property> <property name="text" translatable="yes">Canny thresh. 2</property>
<layout manager="grid"> <layout manager="grid">
<property name="column">2</property> <property name="column">3</property>
<property name="columnspan">1</property> <property name="columnspan">1</property>
<property name="ipady">5</property> <property name="ipady">5</property>
<property name="row">0</property> <property name="row">0</property>
@ -84,9 +84,9 @@
<bind sequence="&lt;&lt;PathChooserPathChanged&gt;&gt;" handler="update" add="" /> <bind sequence="&lt;&lt;PathChooserPathChanged&gt;&gt;" handler="update" add="" />
<layout manager="grid"> <layout manager="grid">
<property name="column">0</property> <property name="column">0</property>
<property name="columnspan">1 </property> <property name="columnspan">2</property>
<property name="ipadx">30</property> <property name="ipadx">30</property>
<property name="row">7</property> <property name="row">6</property>
<property name="rowspan">1</property> <property name="rowspan">1</property>
</layout> </layout>
</object> </object>
@ -100,6 +100,7 @@
<bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" /> <bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" />
<layout manager="grid"> <layout manager="grid">
<property name="column">0</property> <property name="column">0</property>
<property name="columnspan">2</property>
<property name="ipadx">60</property> <property name="ipadx">60</property>
<property name="padx">0</property> <property name="padx">0</property>
<property name="row">3</property> <property name="row">3</property>
@ -111,6 +112,7 @@
<property name="text" translatable="yes">Contrast adjust</property> <property name="text" translatable="yes">Contrast adjust</property>
<layout manager="grid"> <layout manager="grid">
<property name="column">0</property> <property name="column">0</property>
<property name="columnspan">2</property>
<property name="row">2</property> <property name="row">2</property>
</layout> </layout>
</object> </object>
@ -123,7 +125,7 @@
<property name="variable">int:img_size</property> <property name="variable">int:img_size</property>
<bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" /> <bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" />
<layout manager="grid"> <layout manager="grid">
<property name="column">1</property> <property name="column">2</property>
<property name="columnspan">2</property> <property name="columnspan">2</property>
<property name="ipadx">60</property> <property name="ipadx">60</property>
<property name="row">3</property> <property name="row">3</property>
@ -135,7 +137,7 @@
<object class="ttk.Label" id="img_size_label" named="True"> <object class="ttk.Label" id="img_size_label" named="True">
<property name="text" translatable="yes">Display size adjust</property> <property name="text" translatable="yes">Display size adjust</property>
<layout manager="grid"> <layout manager="grid">
<property name="column">1</property> <property name="column">2</property>
<property name="columnspan">2</property> <property name="columnspan">2</property>
<property name="row">2</property> <property name="row">2</property>
<property name="rowspan">1</property> <property name="rowspan">1</property>
@ -148,10 +150,10 @@
<property name="values">x y both</property> <property name="values">x y both</property>
<bind sequence="&lt;Leave&gt;" handler="update" add="" /> <bind sequence="&lt;Leave&gt;" handler="update" add="" />
<layout manager="grid"> <layout manager="grid">
<property name="column">0</property> <property name="column">1</property>
<property name="columnspan">1</property> <property name="columnspan">1</property>
<property name="ipadx">42</property> <property name="row">4</property>
<property name="row">5</property> <property name="rowspan">1</property>
</layout> </layout>
</object> </object>
</child> </child>
@ -171,9 +173,9 @@
<property name="text" translatable="yes">&lt; Prev img</property> <property name="text" translatable="yes">&lt; Prev img</property>
<bind sequence="&lt;ButtonPress&gt;" handler="img_prev" add="" /> <bind sequence="&lt;ButtonPress&gt;" handler="img_prev" add="" />
<layout manager="grid"> <layout manager="grid">
<property name="column">1</property> <property name="column">2</property>
<property name="ipadx">10</property> <property name="ipadx">10</property>
<property name="row">5</property> <property name="row">4</property>
<property name="rowspan">1</property> <property name="rowspan">1</property>
</layout> </layout>
</object> </object>
@ -183,9 +185,9 @@
<property name="text" translatable="yes">Next img &gt;</property> <property name="text" translatable="yes">Next img &gt;</property>
<bind sequence="&lt;ButtonPress&gt;" handler="img_next" add="" /> <bind sequence="&lt;ButtonPress&gt;" handler="img_next" add="" />
<layout manager="grid"> <layout manager="grid">
<property name="column">2</property> <property name="column">3</property>
<property name="ipadx">10</property> <property name="ipadx">10</property>
<property name="row">5</property> <property name="row">4</property>
<property name="rowspan">1</property> <property name="rowspan">1</property>
</layout> </layout>
</object> </object>
@ -195,10 +197,10 @@
<property name="text" translatable="yes">Export PNG</property> <property name="text" translatable="yes">Export PNG</property>
<bind sequence="&lt;ButtonPress&gt;" handler="apply" add="" /> <bind sequence="&lt;ButtonPress&gt;" handler="apply" add="" />
<layout manager="grid"> <layout manager="grid">
<property name="column">2</property> <property name="column">3</property>
<property name="columnspan">1</property> <property name="columnspan">1</property>
<property name="ipadx">10</property> <property name="ipadx">10</property>
<property name="row">7</property> <property name="row">6</property>
<property name="rowspan">1</property> <property name="rowspan">1</property>
</layout> </layout>
</object> </object>
@ -208,8 +210,8 @@
<property name="textvariable">int:export_id</property> <property name="textvariable">int:export_id</property>
<property name="width">15</property> <property name="width">15</property>
<layout manager="grid"> <layout manager="grid">
<property name="column">1</property> <property name="column">2</property>
<property name="row">7</property> <property name="row">6</property>
</layout> </layout>
</object> </object>
</child> </child>
@ -217,9 +219,9 @@
<object class="ttk.Label" id="label1"> <object class="ttk.Label" id="label1">
<property name="text" translatable="yes">Export Image by ID</property> <property name="text" translatable="yes">Export Image by ID</property>
<layout manager="grid"> <layout manager="grid">
<property name="column">1</property> <property name="column">2</property>
<property name="columnspan">2</property> <property name="columnspan">2</property>
<property name="row">6</property> <property name="row">5</property>
</layout> </layout>
</object> </object>
</child> </child>
@ -235,7 +237,7 @@
<property name="width">25</property> <property name="width">25</property>
<property name="wrap">word</property> <property name="wrap">word</property>
<layout manager="grid"> <layout manager="grid">
<property name="column">3</property> <property name="column">4</property>
<property name="columnspan">1</property> <property name="columnspan">1</property>
<property name="row">0</property> <property name="row">0</property>
<property name="rowspan">8</property> <property name="rowspan">8</property>
@ -251,6 +253,7 @@
<bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" /> <bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" />
<layout manager="grid"> <layout manager="grid">
<property name="column">0</property> <property name="column">0</property>
<property name="columnspan">2</property>
<property name="ipadx">60</property> <property name="ipadx">60</property>
<property name="row">1</property> <property name="row">1</property>
</layout> </layout>
@ -261,6 +264,7 @@
<property name="text" translatable="yes">Brightness adjust</property> <property name="text" translatable="yes">Brightness adjust</property>
<layout manager="grid"> <layout manager="grid">
<property name="column">0</property> <property name="column">0</property>
<property name="columnspan">2</property>
<property name="row">0</property> <property name="row">0</property>
</layout> </layout>
</object> </object>
@ -270,17 +274,8 @@
<property name="text" translatable="yes">Dataset path</property> <property name="text" translatable="yes">Dataset path</property>
<layout manager="grid"> <layout manager="grid">
<property name="column">0</property> <property name="column">0</property>
<property name="row">6</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="label4">
<property name="text" translatable="yes">Image control</property>
<layout manager="grid">
<property name="column">1</property>
<property name="columnspan">2</property> <property name="columnspan">2</property>
<property name="row">4</property> <property name="row">5</property>
</layout> </layout>
</object> </object>
</child> </child>
@ -289,10 +284,10 @@
<property name="text" translatable="yes">Export ID for entire dataset</property> <property name="text" translatable="yes">Export ID for entire dataset</property>
<bind sequence="&lt;ButtonRelease&gt;" handler="apply_all" add="" /> <bind sequence="&lt;ButtonRelease&gt;" handler="apply_all" add="" />
<layout manager="grid"> <layout manager="grid">
<property name="column">1</property> <property name="column">2</property>
<property name="columnspan">2</property> <property name="columnspan">2</property>
<property name="ipadx">30</property> <property name="ipadx">30</property>
<property name="row">8</property> <property name="row">7</property>
</layout> </layout>
</object> </object>
</child> </child>
@ -301,7 +296,20 @@
<property name="font">TkIconFont</property> <property name="font">TkIconFont</property>
<property name="text" translatable="yes">Powered by ARNweb.nl &amp; TomSelier.com</property> <property name="text" translatable="yes">Powered by ARNweb.nl &amp; TomSelier.com</property>
<layout manager="grid"> <layout manager="grid">
<property name="column">0</property> <property name="column">4</property>
<property name="columnspan">2</property>
<property name="row">8</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Button" id="button1">
<property name="text" translatable="yes">Run analysis for entire dataset (!)</property>
<bind sequence="&lt;ButtonPress&gt;" handler="analyse_all" add="" />
<layout manager="grid">
<property name="column">2</property>
<property name="columnspan">2</property>
<property name="ipadx">15</property>
<property name="row">8</property> <property name="row">8</property>
</layout> </layout>
</object> </object>

View File

@ -37,7 +37,9 @@ class Logger:
self.file.write(self.csv(self.index)) self.file.write(self.csv(self.index))
self.file.write(self.csv(self.data)) self.file.write(self.csv(self.data))
self.clear()
def clear(self):
# Clear data # Clear data
self.index = [] self.index = []
self.data = [] self.data = []

View File

@ -2,7 +2,7 @@
import pathlib import pathlib
import pygubu import pygubu
import glob import glob
from tkinter import * import tkinter
from PIL import ImageTk, Image from PIL import ImageTk, Image
import numpy as np import numpy as np
import cv2 import cv2
@ -27,7 +27,6 @@ config_json = json.load(config_file)
log = Logger(config_json["out"]) log = Logger(config_json["out"])
## UI class setup ## UI class setup
class MainApp: class MainApp:
def __init__(self, master=None): def __init__(self, master=None):
@ -151,26 +150,26 @@ class MainApp:
else: else:
print("Nothing to export!") print("Nothing to export!")
def apply_all(self, event=None): def apply_all(self, event=None, export = True):
""" """
Export given preprocess id for every image in the dataset folder Export given preprocess id for every image in the dataset folder
""" """
img_id = self.export_id.get() img_id = self.export_id.get()
img_current = copy.deepcopy(self.img_current) img_current = copy.deepcopy(self.img_current)
now = datetime.datetime.now() if (export):
path = pathlib.Path( now = datetime.datetime.now()
config_json["out"], path = pathlib.Path(
f"{self.output[1][img_id]}-all-{now.strftime('%Y-%m-%dT%H.%M.%S')}/", config_json["out"],
) f"{self.output[1][img_id]}-all-{now.strftime('%Y-%m-%dT%H.%M.%S')}/",
os.mkdir(path) )
os.mkdir(path)
while True: while True:
self.img_next() self.img_next()
self.update(
part_update=True if (export):
) # Enforce partial update since we don't need the histograms etc. self.apply(path=path)
self.apply(path=path)
if self.img_current == img_current: if self.img_current == img_current:
break break
@ -178,6 +177,9 @@ class MainApp:
## Ensure display is always correct with image ## Ensure display is always correct with image
self.update() self.update()
def analyse_all(self, event=None):
self.apply_all(export=False)
def add_output(self, data, name: str): def add_output(self, data, name: str):
""" """
Add CV2 image to canvas output Add CV2 image to canvas output
@ -196,9 +198,9 @@ class MainApp:
# Clear previously printed images # Clear previously printed images
self.tk_imgs = [] self.tk_imgs = []
self.meta.config(state=NORMAL) self.meta.config(state=tkinter.NORMAL)
self.meta.delete(1.0, END) self.meta.delete(1.0, tkinter.END)
self.meta.insert(END, f"{self.img_name}\n") self.meta.insert(tkinter.END, f"{self.img_name}\n")
# Draw all output images # Draw all output images
for idx, data in enumerate(self.output[0]): for idx, data in enumerate(self.output[0]):
@ -214,15 +216,15 @@ class MainApp:
self.canvas.configure(height=(drawY + size)) self.canvas.configure(height=(drawY + size))
self.canvas.create_image( self.canvas.create_image(
drawX, drawY, anchor=NW, image=self.tk_imgs[idx], tags="og" drawX, drawY, anchor=tkinter.NW, image=self.tk_imgs[idx], tags="og"
) )
drawX += size drawX += size
# Add name to text box # Add name to text box
self.meta.insert(END, f"{idx}: {self.output[1][idx]}\n") self.meta.insert(tkinter.END, f"{idx}: {self.output[1][idx]}\n")
# Clear output # Clear output
self.meta.config(state=DISABLED) self.meta.config(state=tkinter.DISABLED)
def createPlot(self, columns, rows): def createPlot(self, columns, rows):
fig, axs = plt.subplots(columns, rows) fig, axs = plt.subplots(columns, rows)
@ -284,7 +286,8 @@ class MainApp:
log.add("Canny Std", np.matrix(results).std()) log.add("Canny Std", np.matrix(results).std())
log.add("Canny Min", np.matrix(results).min()) log.add("Canny Min", np.matrix(results).min())
log.add("Canny Max", np.matrix(results).max()) log.add("Canny Max", np.matrix(results).max())
# log.add("Canny Diff max", np.diff(func)) log.add("Canny Diff min", np.diff(func).min())
log.add("Canny Diff max", np.diff(func).max())
def writeStats(self, img, labels, column, row): def writeStats(self, img, labels, column, row):
mean, std = imgStats(img) mean, std = imgStats(img)
@ -310,13 +313,11 @@ class MainApp:
log.add(f"Mean {label}", mean[idx]) log.add(f"Mean {label}", mean[idx])
log.add(f"Std {label}", std[idx]) log.add(f"Std {label}", std[idx])
def update(self, event=None, part_update=False): def update(self, event=None, part_update=None):
path = self.img_path.get() path = self.img_path.get()
## Check if hist and canny hm have to be rerendered ## Check if hist and canny hm have to be rerendered
if ( if part_update == None: ## If partial update has not been forced, check if full update is required
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: if self.img_current != self.img_old or self.img_size != self.img_size_old:
part_update = False part_update = False
self.img_old = self.img_current self.img_old = self.img_current
@ -324,7 +325,10 @@ class MainApp:
else: else:
part_update = True part_update = True
else: else:
print("Partial update forced!") if part_update == True:
print("Partial update forced!")
else:
print("Full update forced!")
if path != None and path != "": if path != None and path != "":
# Get all images at current path # Get all images at current path
@ -358,7 +362,7 @@ class MainApp:
# Set grayscale # Set grayscale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
self.add_output(img_gray, "Grayscale") self.add_output(img_gray, "Grayscale")
# Contrast / brightness boost # Contrast / brightness boost
contrast_val = contrast / 100 contrast_val = contrast / 100
bright_val = bright / 100 bright_val = bright / 100
@ -425,6 +429,8 @@ class MainApp:
# Write results to CSV file # Write results to CSV file
if not part_update: if not part_update:
log.update() log.update()
else:
log.clear() # Prevent partial updates from breaking log
# Show all data # Show all data
plt.show(block=False) ## Graphs plt.show(block=False) ## Graphs