actions.py 2.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
  Nome: actions.py
  Autor: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )

  Descricão: TODO
"""

import sys
import traceback

from interface import *
from segmentation import *
from util import *

DEBUG = True

IException = Interface.InterfaceException
f = File.File
segmenter = Slic.Slic()

image = None
const_image = None
segments = None


def open_image(tk):
    global image, const_image
    
    def onclick(event):
        if event.xdata != None and event.ydata != None and int(event.ydata) != 0:
            x = int(event.xdata)
            y = int(event.ydata)
            tk.write_logger("Coordinates: x = %d y = %d ", x, y)
            if segments is not None:
                tk.append_logger("Segment = %d", segments[y, x])

    imagename = tk.utils.ask_image_name()

    if imagename:
        image = f.open_image(imagename)
        name = f.get_filename(imagename)
        
        tk.write_logger("Opening %s...", name)
        tk.add_image(image, name, onclick)
        const_image = image
            

def rotate_image(tk):
    global image, const_image
    
    if const_image is None:
        raise IException("Image not found")
        
    image = f.rotate_image(const_image)
    
    tk.write_logger("Rotating image 90 degrees")
    tk.refresh_image(image)
    const_image = image


def close_image(tk):
    global const_image
    
    if const_image is None:
        raise IException("Image not found")
    
    if tk.close_image():
        tk.write_logger("Closing image...")
        const_image = None
        segments = None

    
def run_segmentation(tk):
    global image, const_image, segments
    
    if const_image is None:
        raise IException("Image not found")
    
    tk.write_logger("Running %s,,,", segmenter.get_name())

    tk.append_logger("\nConfig: %s", str(segmenter.get_summary_config()))
    segments, image, run_time = segmenter.run(const_image)
    tk.append_logger("\nTime elapsed: %0.3f seconds", run_time)
    
    tk.refresh_image(image)

def config_segmentation(tk):
    global segmenter
    
    title = "Configuring %s" % segmenter.get_name()
    tk.write_logger(title)

    current_config = segmenter.get_config()
    
    def process_config():
        new_config = tk.get_config_and_destroy()

        segmenter.set_config(new_config)
        tk.append_logger("\nConfig updated: %s", str(segmenter.get_summary_config()))

    tk.dialogue_config(title, current_config, process_config)


def func_not_available(tk):
    tk.write_logger("This functionality is not available right now.")