Ask Your Question
0

Is it possible to get function and method documentation programmatically?

asked 2014-07-02 14:45:01 -0600

CVNovice gravatar image

updated 2014-07-02 18:17:31 -0600

Is it possible to have python return a list or dictionary of method arguments and their type?

For instance take cv2.threshold function. If I go to the docs webpage I can see all of the info I want, but is it possible to get this information in code? I would like to query the function cv2.threshold and have something tell me it takes the arguments [src,thresh,maxval,type[,dst]). It would be ideal if it also told me what type or class the values are:

e.g. the docs say: Python: cv2.threshold(src, thresh, maxval, type[, dst]) → retval, dst

so I want to be able to do something like:

foo.getMethods(cv2.threshold) -> {src:mat, thresh:int, maxval:int, type:built-in/[cv2. THRESH_BINARY_INV,cv2. THRESH_BINARY)]}

All of this info is in the documentation on the web page, I just want to access it to dynamically setup a codebase I am working on for a GUI.

Can I access this info without having to setup a web-scraper to pull it directly from the website?

Edit: I've uploaded a video to show exactly what I want to do.

Up to now for each function I have to manually enter its parameters which look like this (for cv2.threshold or Binary Threshold in the video):

    def guiParams(self):
    thresh = {'type':'int',
              'name':'threshold value',
              'min':0,
              'max' : 255,
              'default': 127}
    maxval = {'type':'int',
            'name':'max threshold value',
            'min':0,  
            'max' : 255,
          'default': 255}
    threshtype ={'type':'builtin',
            'name':'thresholding type',
            'options':{'THRESH_BINARY':cv2.THRESH_BINARY,
                       'THRESH_BINARY_INV':cv2.THRESH_BINARY_INV,
                       'THRESH_TRUNC':cv2.THRESH_TRUNC,
                       'THRESH_TOZERO':cv2.THRESH_TOZERO,
                       'THRESH_TOZERO_INV':cv2.THRESH_TOZERO_INV},
            'default':'THRESH_BINARY'
            }
    return [thresh,maxval,threshtype]

This question is trying to figure out how I can get the information in these dictionaries (primarily the attribute name and type, default/typical values would be a bonus) grammatically so I can ensure the gui will work with every image processing method in the opencv Library

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
2

answered 2014-07-02 15:00:14 -0600

berak gravatar image

updated 2014-07-02 15:10:43 -0600

easy from the repl:

>>> import cv2
>>> help(cv2.threshold)
Help on built-in function threshold in module cv2:

threshold(...)
    threshold(src, thresh, maxval, type[, dst]) -> retval, dst

for objects, you will have to create an instance first:

>>> s = cv2.SIFT()
>>> help(s)
Help on SIFT object:

class SIFT(Feature2D)
 |  Method resolution order:
 |      SIFT
 |      Feature2D
 |      FeatureDetector
 |      Algorithm
 |      __builtin__.object
 |
 |  Methods defined here:
 |
 |  __repr__(...)
 |      x.__repr__() <==> repr(x)
 |
 |  defaultNorm(...)
 |      defaultNorm() -> retval
 |
 |  descriptorSize(...)
 |      descriptorSize() -> retval
 |
 |  descriptorType(...)
 |      descriptorType() -> retval
 |
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from Feature2D:
 |
 |  compute(...)
 |      compute(image, keypoints[, descriptors]) -> keypoints, descriptors
 |
 |  detectAndCompute(...)
 |      detectAndCompute(image, mask[, descriptors[, useProvidedKeypoints]]) -
keypoints, descriptors
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from FeatureDetector:
 |
 |  detect(...)
 |      detect(image[, mask]) -> keypoints
 |

 (and so on)

another idea, start the local pydoc webserver:

d:\Programme\Python27\Lib\pydoc.py -p 1234

then access it on localhost:1234 from your browser

edit flag offensive delete link more

Comments

Thank you! Now what about getting the type of each argument in the method call?

Note: I'm using python 2.7, ubuntu14.04, and opencv2.4

CVNovice gravatar imageCVNovice ( 2014-07-02 15:11:03 -0600 )edit
1

yea, good one. no idea atm.

berak gravatar imageberak ( 2014-07-02 15:14:36 -0600 )edit

Ok a slightly bigger issue, I need to get this information into a class method call. But it seems help automatically prints to stdout. I need to figure out how to go about redirecting the output of help() so it can be called and parsed in a script. Any ideas?

CVNovice gravatar imageCVNovice ( 2014-07-02 15:32:44 -0600 )edit

oh, now (maybe) i get it: you're out to make some realtime gui documentation tool ?

berak gravatar imageberak ( 2014-07-02 16:03:14 -0600 )edit

Not quite. I'm making a GUI that takes a regular cv function and lets you change its attributes and see the results of different parameters in real time.

I have written wrappers for most of the common methods but I thought it would be really cool (if its possible) to have a script make the wrappers automatically for every cv method based on the documentation.

For instance, the threshold function gets passed its source image, then has a slider for changing the threshold value and max threshold value, and a combo box to select what type of threshold to use (binary, binary_inv).

cvtColor just has the combo box to select bgr2gray,bgr2hsv, etc.

Does that make sense? Give me a few mins and Ill post a screen shot.

CVNovice gravatar imageCVNovice ( 2014-07-02 17:29:05 -0600 )edit
1

sure makes sense ;)

have a look at the python scripts, that generate the wrappers, here and here . they go and collect the function information directly from the c++ headers. might be nice to abuse them to generate maybe some gui code even.

good luck, i think it's a nice idea !

berak gravatar imageberak ( 2014-07-03 00:34:30 -0600 )edit

Question Tools

Stats

Asked: 2014-07-02 14:45:01 -0600

Seen: 1,152 times

Last updated: Jul 02 '14