From bd042d9cc355eb23afcc303085117077c94bfa8f Mon Sep 17 00:00:00 2001 From: Moshe Kaplan Date: Wed, 6 Mar 2013 01:41:02 -0500 Subject: [PATCH] Fixed shebangs, added error checking --- samples/python2/_coverage.py | 2 +- samples/python2/_doc.py | 2 +- samples/python2/asift.py | 26 +++++++++++++++++++++----- samples/python2/browse.py | 9 ++++++++- samples/python2/calibrate.py | 23 ++++++++++++++++++----- samples/python2/camshift.py | 16 +++++++++++----- samples/python2/coherence.py | 8 +++++--- samples/python2/color_histogram.py | 13 +++++++++---- samples/python2/common.py | 6 +++--- samples/python2/contours.py | 2 +- samples/python2/deconvolution.py | 14 +++++++++++--- samples/python2/demo.py | 18 ++++++++++++------ samples/python2/digits.py | 15 +++++++++++---- samples/python2/digits_adjust.py | 2 +- samples/python2/digits_video.py | 18 +++++++++++++----- samples/python2/distrans.py | 13 ++++++++++--- samples/python2/edge.py | 12 +++++++++--- samples/python2/facedetect.py | 10 +++++++--- samples/python2/feature_homography.py | 10 +++++++--- samples/python2/find_obj.py | 2 +- samples/python2/fitline.py | 6 +++++- samples/python2/floodfill.py | 12 +++++++++--- samples/python2/gabor_threads.py | 12 +++++++++--- samples/python2/gaussian_mix.py | 2 +- samples/python2/hist.py | 11 ++++++++--- samples/python2/inpaint.py | 13 ++++++++++--- samples/python2/kmeans.py | 2 +- samples/python2/lappyr.py | 8 +++++--- samples/python2/letter_recog.py | 2 +- samples/python2/lk_homography.py | 8 +++++--- samples/python2/lk_track.py | 8 +++++--- samples/python2/morphology.py | 14 +++++++++++--- samples/python2/mosse.py | 8 +++++--- samples/python2/motempl.py | 8 +++++--- samples/python2/mouse_and_match.py | 11 ++++++----- samples/python2/mser.py | 8 +++++--- samples/python2/opt_flow.py | 8 +++++--- samples/python2/peopledetect.py | 5 ++++- samples/python2/plane_ar.py | 8 +++++--- samples/python2/plane_tracker.py | 12 +++++++++--- samples/python2/squares.py | 2 +- samples/python2/stereo_match.py | 2 +- samples/python2/texture_flow.py | 12 +++++++++--- samples/python2/turing.py | 2 +- samples/python2/video.py | 9 +++++++-- samples/python2/video_dmtx.py | 4 +++- samples/python2/video_threaded.py | 8 +++++--- samples/python2/watershed.py | 9 +++++++-- 48 files changed, 308 insertions(+), 127 deletions(-) diff --git a/samples/python2/_coverage.py b/samples/python2/_coverage.py index 1d0f041..80edffb 100755 --- a/samples/python2/_coverage.py +++ b/samples/python2/_coverage.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' Utility for measuring python opencv API coverage by samples. diff --git a/samples/python2/_doc.py b/samples/python2/_doc.py index 71c9faa..fe2b6f3 100755 --- a/samples/python2/_doc.py +++ b/samples/python2/_doc.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' Scans current directory for *.py files and reports diff --git a/samples/python2/asift.py b/samples/python2/asift.py index 09894dd..bdcd3c9 100755 --- a/samples/python2/asift.py +++ b/samples/python2/asift.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' Affine invariant feature-based image matching sample. @@ -21,9 +21,12 @@ USAGE import numpy as np import cv2 + +# built-in modules import itertools as it from multiprocessing.pool import ThreadPool +# local modules from common import Timer from find_obj import init_feature, filter_matches, explore_match @@ -85,15 +88,18 @@ def affine_detect(detector, img, mask=None, pool=None): if descrs is None: descrs = [] return keypoints, descrs + keypoints, descrs = [], [] if pool is None: ires = it.imap(f, params) else: ires = pool.imap(f, params) + for i, (k, d) in enumerate(ires): print 'affine sampling: %d / %d\r' % (i+1, len(params)), keypoints.extend(k) descrs.extend(d) + print return keypoints, np.array(descrs) @@ -104,7 +110,8 @@ if __name__ == '__main__': opts, args = getopt.getopt(sys.argv[1:], '', ['feature=']) opts = dict(opts) feature_name = opts.get('--feature', 'sift-flann') - try: fn1, fn2 = args + try: + fn1, fn2 = args except: fn1 = 'data/aero1.jpg' fn2 = 'data/aero3.jpg' @@ -112,11 +119,20 @@ if __name__ == '__main__': img1 = cv2.imread(fn1, 0) img2 = cv2.imread(fn2, 0) detector, matcher = init_feature(feature_name) - if detector != None: - print 'using', feature_name - else: + + if img1 is None: + print 'Failed to load fn1:', fn1 + sys.exit(1) + + if img2 is None: + print 'Failed to load fn2:', fn2 + sys.exit(1) + + if detector is None: print 'unknown feature:', feature_name sys.exit(1) + + print 'using', feature_name pool=ThreadPool(processes = cv2.getNumberOfCPUs()) kp1, desc1 = affine_detect(detector, img1, pool=pool) diff --git a/samples/python2/browse.py b/samples/python2/browse.py index da2c98d..81ef33b 100755 --- a/samples/python2/browse.py +++ b/samples/python2/browse.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' browse.py @@ -14,6 +14,8 @@ browse.py [image filename] import numpy as np import cv2 + +# built-in modules import sys if __name__ == '__main__': @@ -25,6 +27,10 @@ if __name__ == '__main__': fn = sys.argv[1] print 'loading %s ...' % fn img = cv2.imread(fn) + if img is None: + print 'Failed to load fn:', fn + sys.exit(1) + else: sz = 4096 print 'generating %dx%d procedural image ...' % (sz, sz) @@ -33,6 +39,7 @@ if __name__ == '__main__': track = np.int32(track*10 + (sz/2, sz/2)) cv2.polylines(img, [track], 0, 255, 1, cv2.CV_AA) + small = img for i in xrange(3): small = cv2.pyrDown(small) diff --git a/samples/python2/calibrate.py b/samples/python2/calibrate.py index 54aba8a..37d3de2 100755 --- a/samples/python2/calibrate.py +++ b/samples/python2/calibrate.py @@ -1,10 +1,15 @@ -#/usr/bin/env python +#!/usr/bin/env python import numpy as np import cv2 -import os + +# local modules from common import splitfn +# built-in modules +import os + + USAGE = ''' USAGE: calib.py [--save ] [--debug ] [--square_size] [] ''' @@ -12,13 +17,17 @@ USAGE: calib.py [--save ] [--debug ] [--square_size] [1: - im = cv2.imread(sys.argv[1]) + fname = sys.argv[1] else : - im = cv2.imread('../cpp/lena.jpg') + fname = '../cpp/lena.jpg' print "usage : python hist.py " + im = cv2.imread(fname) + + if im is None: + print 'Failed to load image file:', fname + sys.exit(1) gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) diff --git a/samples/python2/inpaint.py b/samples/python2/inpaint.py index d2b8c84..15497d4 100755 --- a/samples/python2/inpaint.py +++ b/samples/python2/inpaint.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' Inpainting sample. @@ -21,11 +21,18 @@ from common import Sketcher if __name__ == '__main__': import sys - try: fn = sys.argv[1] - except: fn = '../cpp/fruits.jpg' + try: + fn = sys.argv[1] + except: + fn = '../cpp/fruits.jpg' + print __doc__ img = cv2.imread(fn) + if img is None: + print 'Failed to load image file:', fn + sys.exit(1) + img_mark = img.copy() mark = np.zeros(img.shape[:2], np.uint8) sketch = Sketcher('img', [img_mark, mark], lambda : ((255, 255, 255), 255)) diff --git a/samples/python2/kmeans.py b/samples/python2/kmeans.py index 4418a46..0656fa7 100755 --- a/samples/python2/kmeans.py +++ b/samples/python2/kmeans.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' K-means clusterization sample. diff --git a/samples/python2/lappyr.py b/samples/python2/lappyr.py index 232ec54..929d513 100755 --- a/samples/python2/lappyr.py +++ b/samples/python2/lappyr.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' An example of Laplacian Pyramid construction and merging. @@ -40,8 +40,10 @@ if __name__ == '__main__': import sys print __doc__ - try: fn = sys.argv[1] - except: fn = 0 + try: + fn = sys.argv[1] + except: + fn = 0 cap = video.create_capture(fn) leveln = 6 diff --git a/samples/python2/letter_recog.py b/samples/python2/letter_recog.py index 73eeff3..ef8c722 100755 --- a/samples/python2/letter_recog.py +++ b/samples/python2/letter_recog.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' The sample demonstrates how to train Random Trees classifier diff --git a/samples/python2/lk_homography.py b/samples/python2/lk_homography.py index 9996764..ae8b2d4 100755 --- a/samples/python2/lk_homography.py +++ b/samples/python2/lk_homography.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' Lucas-Kanade homography tracker @@ -103,8 +103,10 @@ class App: def main(): import sys - try: video_src = sys.argv[1] - except: video_src = 0 + try: + video_src = sys.argv[1] + except: + video_src = 0 print __doc__ App(video_src).run() diff --git a/samples/python2/lk_track.py b/samples/python2/lk_track.py index f0d0439..1ff90ff 100755 --- a/samples/python2/lk_track.py +++ b/samples/python2/lk_track.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' Lucas-Kanade tracker @@ -88,8 +88,10 @@ class App: def main(): import sys - try: video_src = sys.argv[1] - except: video_src = 0 + try: + video_src = sys.argv[1] + except: + video_src = 0 print __doc__ App(video_src).run() diff --git a/samples/python2/morphology.py b/samples/python2/morphology.py index 0980981..cfed9b8 100755 --- a/samples/python2/morphology.py +++ b/samples/python2/morphology.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' Morphology operations. @@ -23,9 +23,17 @@ if __name__ == '__main__': from itertools import cycle from common import draw_str - try: fn = sys.argv[1] - except: fn = '../cpp/baboon.jpg' + try: + fn = sys.argv[1] + except: + fn = '../cpp/baboon.jpg' + img = cv2.imread(fn) + + if img is None: + print 'Failed to load image file:', fn + sys.exit(1) + cv2.imshow('original', img) modes = cycle(['erode/dilate', 'open/close', 'blackhat/tophat', 'gradient']) diff --git a/samples/python2/mosse.py b/samples/python2/mosse.py index aecb423..0e2e7ee 100755 --- a/samples/python2/mosse.py +++ b/samples/python2/mosse.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' MOSSE tracking sample @@ -182,7 +182,9 @@ if __name__ == '__main__': import sys, getopt opts, args = getopt.getopt(sys.argv[1:], '', ['pause']) opts = dict(opts) - try: video_src = args[0] - except: video_src = '0' + try: + video_src = args[0] + except: + video_src = '0' App(video_src, paused = '--pause' in opts).run() diff --git a/samples/python2/motempl.py b/samples/python2/motempl.py index 4f78eba..ce9b666 100755 --- a/samples/python2/motempl.py +++ b/samples/python2/motempl.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python import numpy as np import cv2 @@ -20,8 +20,10 @@ def draw_motion_comp(vis, (x, y, w, h), angle, color): if __name__ == '__main__': import sys - try: video_src = sys.argv[1] - except: video_src = 0 + try: + video_src = sys.argv[1] + except: + video_src = 0 cv2.namedWindow('motempl') visuals = ['input', 'frame_diff', 'motion_hist', 'grad_orient'] diff --git a/samples/python2/mouse_and_match.py b/samples/python2/mouse_and_match.py index a9fa882..66c3b1a 100755 --- a/samples/python2/mouse_and_match.py +++ b/samples/python2/mouse_and_match.py @@ -1,5 +1,3 @@ -#/usr/bin/env python - #!/usr/bin/env python ''' mouse_and_match.py [-i path | --input path: default ./] @@ -11,12 +9,15 @@ Demonstrate using a mouse to interact with an image: ESC to exit ''' import numpy as np -from math import * -import sys +import cv2 as cv + +# built-in modules import os +import sys import glob import argparse -import cv2 as cv +from math import * + drag_start = None sel = (0,0,0,0) diff --git a/samples/python2/mser.py b/samples/python2/mser.py index 73e0a4f..beaa6e7 100755 --- a/samples/python2/mser.py +++ b/samples/python2/mser.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' MSER detector demo @@ -20,8 +20,10 @@ import video if __name__ == '__main__': import sys - try: video_src = sys.argv[1] - except: video_src = 0 + try: + video_src = sys.argv[1] + except: + video_src = 0 cam = video.create_capture(video_src) mser = cv2.MSER() diff --git a/samples/python2/opt_flow.py b/samples/python2/opt_flow.py index d0bc5c5..0dba85f 100755 --- a/samples/python2/opt_flow.py +++ b/samples/python2/opt_flow.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python import numpy as np import cv2 @@ -48,8 +48,10 @@ def warp_flow(img, flow): if __name__ == '__main__': import sys print help_message - try: fn = sys.argv[1] - except: fn = 0 + try: + fn = sys.argv[1] + except: + fn = 0 cam = video.create_capture(fn) ret, prev = cam.read() diff --git a/samples/python2/peopledetect.py b/samples/python2/peopledetect.py index f4bd467..9d945db 100755 --- a/samples/python2/peopledetect.py +++ b/samples/python2/peopledetect.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python import numpy as np import cv2 @@ -36,6 +36,9 @@ if __name__ == '__main__': print fn, ' - ', try: img = cv2.imread(fn) + if img is None: + print 'Failed to load image file:', fn + continue except: print 'loading error' continue diff --git a/samples/python2/plane_ar.py b/samples/python2/plane_ar.py index dc2d5d5..dcb5559 100755 --- a/samples/python2/plane_ar.py +++ b/samples/python2/plane_ar.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' Planar augmented reality @@ -100,6 +100,8 @@ if __name__ == '__main__': print __doc__ import sys - try: video_src = sys.argv[1] - except: video_src = 0 + try: + video_src = sys.argv[1] + except: + video_src = 0 App(video_src).run() diff --git a/samples/python2/plane_tracker.py b/samples/python2/plane_tracker.py index 189f50b..de5d7a0 100755 --- a/samples/python2/plane_tracker.py +++ b/samples/python2/plane_tracker.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' Multitarget planar tracking @@ -23,7 +23,11 @@ Select a textured planar object to track by drawing a box with a mouse. import numpy as np import cv2 + +# built-in modules from collections import namedtuple + +# local modules import video import common @@ -168,6 +172,8 @@ if __name__ == '__main__': print __doc__ import sys - try: video_src = sys.argv[1] - except: video_src = 0 + try: + video_src = sys.argv[1] + except: + video_src = 0 App(video_src).run() diff --git a/samples/python2/squares.py b/samples/python2/squares.py index a247c35..36676b4 100755 --- a/samples/python2/squares.py +++ b/samples/python2/squares.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' Simple "Square Detector" program. diff --git a/samples/python2/stereo_match.py b/samples/python2/stereo_match.py index 0e46c26..0803a3d 100755 --- a/samples/python2/stereo_match.py +++ b/samples/python2/stereo_match.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' Simple example of stereo image matching and point cloud generation. diff --git a/samples/python2/texture_flow.py b/samples/python2/texture_flow.py index 66eb58f..38767fa 100755 --- a/samples/python2/texture_flow.py +++ b/samples/python2/texture_flow.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' Texture flow direction estimation. @@ -15,10 +15,16 @@ import cv2 if __name__ == '__main__': import sys - try: fn = sys.argv[1] - except: fn = 'data/starry_night.jpg' + try: + fn = sys.argv[1] + except: + fn = 'data/starry_night.jpg' img = cv2.imread(fn) + if img is None: + print 'Failed to load image file:', fn + sys.exit(1) + gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) h, w = img.shape[:2] diff --git a/samples/python2/turing.py b/samples/python2/turing.py index 101f22c..e847f01 100755 --- a/samples/python2/turing.py +++ b/samples/python2/turing.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' Multiscale Turing Patterns generator diff --git a/samples/python2/video.py b/samples/python2/video.py index 2cdcb2a..8cbc6f0 100755 --- a/samples/python2/video.py +++ b/samples/python2/video.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' Video capture sample. @@ -30,9 +30,14 @@ Keys: ''' import numpy as np +from numpy import pi, sin, cos + import cv2 + +# built-in modules from time import clock -from numpy import pi, sin, cos + +# local modules import common class VideoSynthBase(object): diff --git a/samples/python2/video_dmtx.py b/samples/python2/video_dmtx.py index 1ed06c4..d523252 100755 --- a/samples/python2/video_dmtx.py +++ b/samples/python2/video_dmtx.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' Data matrix detector sample. @@ -18,6 +18,8 @@ Keyboard shortcuts: import cv2 import numpy as np + +# built-in modules import sys def data_matrix_demo(cap): diff --git a/samples/python2/video_threaded.py b/samples/python2/video_threaded.py index 6fce334..a638791 100755 --- a/samples/python2/video_threaded.py +++ b/samples/python2/video_threaded.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python ''' Multithreaded video processing sample. @@ -39,8 +39,10 @@ if __name__ == '__main__': print __doc__ - try: fn = sys.argv[1] - except: fn = 0 + try: + fn = sys.argv[1] + except: + fn = 0 cap = video.create_capture(fn) diff --git a/samples/python2/watershed.py b/samples/python2/watershed.py index d4ab22a..037af1e 100755 --- a/samples/python2/watershed.py +++ b/samples/python2/watershed.py @@ -31,6 +31,9 @@ from common import Sketcher class App: def __init__(self, fn): self.img = cv2.imread(fn) + if self.img is None: + raise Exception('Failed to load image file: %s' % fn) + h, w = self.img.shape[:2] self.markers = np.zeros((h, w), np.int32) self.markers_vis = self.img.copy() @@ -73,7 +76,9 @@ class App: if __name__ == '__main__': import sys - try: fn = sys.argv[1] - except: fn = '../cpp/fruits.jpg' + try: + fn = sys.argv[1] + except: + fn = '../cpp/fruits.jpg' print __doc__ App(fn).run() -- 2.7.4