ValueError: too many values to unpack python 2.7 -
so trying compile following code it's showing me error on cv2.findcontours. though, using python 2.7 version. reason why error: many values unpack python 2.7 coming?
import cv2 import numpy np import time #open camera object cap = cv2.videocapture(0) #decrease frame size cap.set(cv2.cap_prop_frame_width, 1000) cap.set(cv2.cap_prop_frame_height, 600) def nothing(x): pass # function find angle between 2 vectors def angle(v1,v2): dot = np.dot(v1,v2) x_modulus = np.sqrt((v1*v1).sum()) y_modulus = np.sqrt((v2*v2).sum()) cos_angle = dot / x_modulus / y_modulus angle = np.degrees(np.arccos(cos_angle)) return angle # function find distance between 2 points in list of lists def finddistance(a,b): return np.sqrt(np.power((a[0][0]-b[0][0]),2) + np.power((a[0][1]-b[0][1]),2)) # creating window hsv track bars cv2.namedwindow('hsv_trackbar') # starting 100's prevent error while masking h,s,v = 100,100,100 # creating track bar cv2.createtrackbar('h', 'hsv_trackbar',0,179,nothing) cv2.createtrackbar('s', 'hsv_trackbar',0,255,nothing) cv2.createtrackbar('v', 'hsv_trackbar',0,255,nothing) while(1): #measure execution time start_time = time.time() #capture frames camera ret, frame = cap.read() #blur image blur = cv2.blur(frame,(3,3)) #convert hsv color space hsv = cv2.cvtcolor(blur,cv2.color_bgr2hsv) #create binary image white skin colors , rest black mask2 = cv2.inrange(hsv,np.array([2,50,50]),np.array([15,255,255])) #kernel matrices morphological transformation kernel_square = np.ones((11,11),np.uint8) kernel_ellipse= cv2.getstructuringelement(cv2.morph_ellipse,(5,5)) #perform morphological transformations filter out background noise #dilation increase skin color area #erosion increase skin color area dilation = cv2.dilate(mask2,kernel_ellipse,iterations = 1) erosion = cv2.erode(dilation,kernel_square,iterations = 1) dilation2 = cv2.dilate(erosion,kernel_ellipse,iterations = 1) filtered = cv2.medianblur(dilation2,5) kernel_ellipse= cv2.getstructuringelement(cv2.morph_ellipse,(8,8)) dilation2 = cv2.dilate(filtered,kernel_ellipse,iterations = 1) kernel_ellipse= cv2.getstructuringelement(cv2.morph_ellipse,(5,5)) dilation3 = cv2.dilate(filtered,kernel_ellipse,iterations = 1) median = cv2.medianblur(dilation2,5) ret,thresh = cv2.threshold(median,127,255,0) #find contours of filtered frame contours, hierarchy = cv2.findcontours(thresh,cv2.retr_tree,cv2.chain_approx_simple) //error! #draw contours #cv2.drawcontours(frame, cnt, -1, (122,122,0), 3) #cv2.imshow('dilation',median) #find max contour area (assume hand in frame) max_area=100 ci=0 in range(len(contours)): cnt=contours[i] area = cv2.contourarea(cnt) if(area>max_area): max_area=area ci=i #largest area contour cnts = contours[ci] #find convex hull hull = cv2.convexhull(cnts) #find convex defects hull2 = cv2.convexhull(cnts,returnpoints = false) defects = cv2.convexitydefects(cnts,hull2) #get defect points , draw them in original image fardefect = [] in range(defects.shape[0]): s,e,f,d = defects[i,0] start = tuple(cnts[s][0]) end = tuple(cnts[e][0]) far = tuple(cnts[f][0]) fardefect.append(far) cv2.line(frame,start,end,[0,255,0],1) cv2.circle(frame,far,10,[100,255,255],3) #find moments of largest contour moments = cv2.moments(cnts) #central mass of first order moments if moments['m00']!=0: cx = int(moments['m10']/moments['m00']) # cx = m10/m00 cy = int(moments['m01']/moments['m00']) # cy = m01/m00 centermass=(cx,cy) #draw center mass cv2.circle(frame,centermass,7,[100,0,255],2) font = cv2.font_hershey_simplex cv2.puttext(frame,'center',tuple(centermass),font,2,(255,255,255),2) #distance each finger defect(finger webbing) center mass distancebetweendefectstocenter = [] in range(0,len(fardefect)): x = np.array(fardefect[i]) centermass = np.array(centermass) distance = np.sqrt(np.power(x[0]-centermass[0],2)+np.power(x[1]-centermass[1],2)) distancebetweendefectstocenter.append(distance) #get average of 3 shortest distances finger webbing center mass sorteddefectsdistances = sorted(distancebetweendefectstocenter) averagedefectdistance = np.mean(sorteddefectsdistances[0:2]) #get fingertip points contour hull #if points in proximity of 80 pixels, consider single point in group finger = [] in range(0,len(hull)-1): if (np.absolute(hull[i][0][0] - hull[i+1][0][0]) > 80) or ( np.absolute(hull[i][0][1] - hull[i+1][0][1]) > 80): if hull[i][0][1] <500 : finger.append(hull[i][0]) #the fingertip points 5 hull points largest y coordinates finger = sorted(finger,key=lambda x: x[1]) fingers = finger[0:5] #calculate distance of each finger tip center mass fingerdistance = [] in range(0,len(fingers)): distance = np.sqrt(np.power(fingers[i][0]-centermass[0],2)+np.power(fingers[i][1]-centermass[0],2)) fingerdistance.append(distance) #finger pointed/raised if distance of between fingertip center mass larger #than distance of average finger webbing center mass 130 pixels result = 0 in range(0,len(fingers)): if fingerdistance[i] > averagedefectdistance+130: result = result +1 #print number of pointed fingers cv2.puttext(frame,str(result),(100,100),font,2,(255,255,255),2) #show height raised fingers #cv2.puttext(frame,'finger1',tuple(finger[0]),font,2,(255,255,255),2) #cv2.puttext(frame,'finger2',tuple(finger[1]),font,2,(255,255,255),2) #cv2.puttext(frame,'finger3',tuple(finger[2]),font,2,(255,255,255),2) #cv2.puttext(frame,'finger4',tuple(finger[3]),font,2,(255,255,255),2) #cv2.puttext(frame,'finger5',tuple(finger[4]),font,2,(255,255,255),2) #cv2.puttext(frame,'finger6',tuple(finger[5]),font,2,(255,255,255),2) #cv2.puttext(frame,'finger7',tuple(finger[6]),font,2,(255,255,255),2) #cv2.puttext(frame,'finger8',tuple(finger[7]),font,2,(255,255,255),2) #print bounding rectangle x,y,w,h = cv2.boundingrect(cnts) img = cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2) cv2.drawcontours(frame,[hull],-1,(255,255,255),2) ##### show final image ######## cv2.imshow('dilation',frame) ############################### #print execution time #print time.time()-start_time #close output video pressing 'esc' k = cv2.waitkey(5) & 0xff if k == 27: break cap.release() cv2.destroyallwindows()
the issue in line -
contours, hierarchy = cv2.findcontours(thresh,cv2.retr_tree,cv2.chain_approx_simple)
cv2.findcontours()
returns 3
values , not 2 , hence too many values unpack
error , -
image, contours, hierarchy = cv2.findcontours(thresh,cv2.retr_tree,cv2.chain_approx_simple)
cv2.findcontours()
returns image , contours , hierarchy , in order.
Comments
Post a Comment