Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapper class for allowing transforms to be in trackpipe and usable as image processors with defaults. #3

Open
LukasDauterman opened this issue Feb 6, 2020 · 4 comments

Comments

@LukasDauterman
Copy link

I have created a simple wrapper that allows transforms either to be usable by trackpipe, or to act like regular transforms but with default parameter values defined by the user.

Would this be worth integrating into trackpipe?

@geoff-va
Copy link
Owner

geoff-va commented Feb 6, 2020

Want to post an example or point me to a fork?

@LukasDauterman
Copy link
Author

import numpy as np
import cv2
from matplotlib import pyplot as plt
import os

from trackpipe import (
    pipeline,
    transforms,
)

class averageBlur(pipeline.Transform):
    
    kernel_size= pipeline.Param(label = "kernel_size", _max=15,adjust = transforms.make_odd)

    def draw(self,img):
        return self.apply(img,self.kernel_size.value)
    
    def apply(self,img,kernel_size):
        if kernel_size == 0:
            return img
        img_tmp = img.copy()
        kernel = np.ones((kernel_size,kernel_size),np.float32)/kernel_size**2
        img_tmp = cv2.filter2D(img_tmp,-1,kernel)
        return img_tmp

class transform_instance:
    
    def __init__(self,transform,value_list):
        self.transform = transform
        self.arg_dict = self.gen_arg_dict(value_list)
        
    def gen_arg_dict(self,value_list):
        return_dict = {}
        for arg,value in zip(self.transform.apply.__code__.co_varnames[2:len(value_list)+2],value_list):
            return_dict[arg] = value
        return return_dict
    
    def apply(self,img):
        return self.transform.apply(img,**self.arg_dict)

def apply_transforms(img,pipe_line):
    img_tmp = img.copy()
    
    for func in pipe_line:
        img_tmp = func.apply(img_tmp)
        
    return img_tmp

img_in = cv2.imread("input.jpg")
my_blur = transform_instance(averageBlur(),[6])
my_pipeline = [my_blur]
transforms_list = [func.transform for func in my_pipeline]
pipeline.run_pipe(transforms_list, img_in) 

img_processed = apply_transforms(img_in,my_pipeline )

@geoff-va
Copy link
Owner

geoff-va commented Feb 6, 2020

Is the intent to be able to run a pipeline with specific values for each transform?

For example, you've got the values picked out for each transform after using the trackbars, now you want to run it and get the final output of the entire pipeline with those values specified?

@LukasDauterman
Copy link
Author

Yes, I wanted to be able to get values using the pipeline.run functionality, and be able to set them and then get the final output independently of the experimentation, thought of it as an iterative process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants