Tuesday 15 September 2015

Python OpenCV Error -



Python OpenCV Error -

simple code used identify playing cards through cam have been trying run code maintain getting error when attempting utilize of methods

typeerror: src not numpy array, neither scalar

import sys import numpy np sys.path.insert(0, "/usr/local/lib/python2.7/site-packages/") import cv2 ############################################################################### # utility code ############################################################################### def rectify(h): h = h.reshape((4,2)) hnew = np.zeros((4,2),dtype = np.float32) add together = h.sum(1) hnew[0] = h[np.argmin(add)] hnew[2] = h[np.argmax(add)] diff = np.diff(h,axis = 1) hnew[1] = h[np.argmin(diff)] hnew[3] = h[np.argmax(diff)] homecoming hnew ############################################################################### # image matching ############################################################################### def preprocess(img): grayness = cv2.cvtcolor(img,cv2.color_bgr2gray) blur = cv2.gaussianblur(gray,(5,5),2 ) thresh = cv2.adaptivethreshold(blur,255,1,1,11,1) homecoming thresh def imgdiff(img1,img2): img1 = cv2.gaussianblur(img1,(5,5),5) img2 = cv2.gaussianblur(img2,(5,5),5) diff = cv2.absdiff(img1,img2) diff = cv2.gaussianblur(diff,(5,5),5) flag, diff = cv2.threshold(diff, 200, 255, cv2.thresh_binary) homecoming np.sum(diff) def find_closest_card(training,img): features = preprocess(img) homecoming sorted(training.values(), key=lambda x:imgdiff(x[1],features))[0][0] ############################################################################### # card extraction ############################################################################### def getcards(im, numcards=4): grayness = cv2.cvtcolor(im,cv2.color_bgr2gray) blur = cv2.gaussianblur(gray,(1,1),1000) flag, thresh = cv2.threshold(blur, 120, 255, cv2.thresh_binary) contours, hierarchy = cv2.findcontours(thresh,cv2.retr_tree,cv2.chain_approx_simple) contours = sorted(contours, key=cv2.contourarea,reverse=true)[:numcards] card in contours: peri = cv2.arclength(card,true) approx = rectify(cv2.approxpolydp(card,0.02*peri,true)) # box = np.int0(approx) # cv2.drawcontours(im,[box],0,(255,255,0),6) # imx = cv2.resize(im,(1000,600)) # cv2.imshow('a',imx) h = np.array([ [0,0],[449,0],[449,449],[0,449] ],np.float32) transform = cv2.getperspectivetransform(approx,h) warp = cv2.warpperspective(im,transform,(450,450)) yield warp def get_training(training_labels_filename,training_image_filename,num_training_cards,avoid_cards=none): training = {} labels = {} line in file(training_labels_filename): key, num, suit = line.strip().split() labels[int(key)] = (num,suit) print "training" im = cv2.imread(training_image_filename) i,c in enumerate(getcards(im,num_training_cards)): if avoid_cards none or (labels[i][0] not in avoid_cards[0] , labels[i][1] not in avoid_cards[1]): training[i] = (labels[i], preprocess(c)) print "done training" homecoming training if __name__ == '__main__': if len(sys.argv) == 6: filename = sys.argv[1] num_cards = int(sys.argv[2]) training_image_filename = sys.argv[3] training_labels_filename = sys.argv[4] num_training_cards = int(sys.argv[5]) training = get_training(training_labels_filename,training_image_filename,num_training_cards) im = cv2.imread("test") width = im.shape[0] height = im.shape[1] if width < height: im = cv2.transpose(im) im = cv2.flip(im,1) # debug: uncomment see registered images #for i,c in enumerate(getcards(im,num_cards)): # card = find_closest_card(training,c,) # cv2.imshow(str(card),c) # cv2.waitkey(0) cards = [find_closest_card(training,c) c in getcards(im,num_cards)] print cards else: print __doc__

here entire error message

traceback (most recent phone call last): file "<pyshell#15>", line 1, in <module> preprocess("test.jpg") file "c:\users\don ellison\desktop\class programs\csc 490 project\cards\python\playing-card-recognition-master\card_img.py", line 43, in preprocess grayness = cv2.cvtcolor(img,cv2.color_bgr2gray) typeerror: src not numpy array, neither scalar

you passing in wrong argument preprocess function, guess calling python shell.

you not suppose pass in image file name, rather numpy array. seems called proprocess function , passed in "test.jpg" in python shell. if want test preprocess function, do

test_img = cv2.imread("test.jpg") preprocess(test_img)

python opencv numpy

No comments:

Post a Comment