def broke(): #read the frames _,frame = cap.read() #convert to hsv and find range of colors hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV) thresh = cv2.inRange(hsv,np.array((66,64,233)), np.array((92,255,255))) thresh2 = thresh.copy() #erode and dilate thresh = cv2.erode(thresh, (2,2)) thresh = cv2.dilate(thresh, (2,2)) (cnts, _) = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10] goalCnt = None #filter by area min_area = 2000 best_cnt = None #max_area = 4000 for i, cnt in enumerate(cnts): if i > 100: break area = cv2.contourArea(cnt) if area > min_area: min_area = area goalCnt = cnt if goalCnt == None: cx = cy = -1 print "Nothing Found" #filter by number of vertices for c in cnts: perimeter = cv2.arcLength(c, True) #change 0.02 for precision value approx = cv2.approxPolyDP(c, 0.02 * perimeter, True) #check if shape has 8 vertices if len(approx) > 8: goalCnt == approx break #find the centroid moment = cv2.moments(goalCnt) hull_cnt = cv2.convexHull(goalCnt) #set x and y and draw on frame cv2.drawContours(thresh, hull_cnt, -1, (0,255,0), 3) cx,cy = int(M['m10']/M['m00']), int(M['m01']/M['m00']) cv2.circle(thresh,(cx,cy),5,255,-1) #show the final processed image cv2.imshow("contours", frame) #find the error, distance, and angle error = cx-320 #make sure there is an object before finding distance and angle if (cy > -1 and cx > -1): dist = distance.findDistance(cy) ang = angle.getAngle(dist, error) else: #return -1 if no object is found ang = dist = -1 #give values to server serialserver.putData(error, ang, distance) #print for testing #print cx print cy
import distance import angle y = 400 distance = distance.findDistance(y) # print distance error = 100 angle = angle.getAngle(distance, angle)
import distance import angle y = 400 distance = distance.findDistance(y) #print distance error = 100 angle = angle.getAngle(distance, angle)