Example #1
0
def handle_data(dt):
    end = (len(dt[3:]) / 2) + 3
    #for i in range(10):
    #    print dt[i]
    print 'len:', dt[0], '\nnode_ID', dt[1], "\ntimestamp:", dt[2]
    #print end, len(dt), dt.dtype
    if int(dt[0]) == len(dt[3:]):  # 0:length of data  1:nodeId 2:timestamp
        print "ch1 : %d  ch2 : %d" % (len(dt[3:end]), len(dt[end:]))
        if not str(int(dt[1])) in listdir('./eloc_data'):
            mkdir('./eloc_data/' + str(int(dt[1])) + '/' + date)
        path = '../eloc_data/' + str(int(dt[1])) + '/' + date + '/' + str(
            int(dt[2])) + '.wav'
        print "file path : ", path
        write(path, 600, np.array([dt[3:end], dt[end:]]).T)
        angle = getAngle(dt[3:end], dt[end:], 600)
        if insertData(db, int(dt[1]), int(dt[2]), path, angle):
            print ".....data base insert success...."
        else:
            print "data base insertion error"
        #plotSpec(dt[3:end],dt[end:],600)
    return 1
        #print "Nothing Found!"
    else:
        #finding centroids of best_cnt and draw a circle there
        M = cv2.moments(best_cnt)
        hull_cnt = cv2.convexHull(best_cnt)
        #print min_area/cv2.contourArea(hull_cnt)
        cv2.drawContours(frame, hull_cnt, -1, (0, 255, 0), 3)
        cx, cy = int(M['m10'] / M['m00']), int(M['m01'] / M['m00'])
        cv2.circle(frame, (cx, cy), 5, 255, -1)

    #find the error
    error = cx - 400

    #find the angle
    if error != -1000:
        ang = angle.getAngle(error)
    else:
        #no target found, do not calculate the angle
        ang = -1000

#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

#dist = 1

#give values to server
    	#finding centroids of best_cnt and draw a circle there
    	M = cv2.moments(best_cnt)
    	hull_cnt = cv2.convexHull(best_cnt)
    	#print min_area/cv2.contourArea(hull_cnt)
    	cv2.drawContours(frame, hull_cnt, -1, (0,255,0), 3)
    	cx,cy = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
    	cv2.circle(frame,(cx,cy),5,255,-1)



    #find the error
    error = cx-400

    #find the angle
    if error != -1000:
         ang = angle.getAngle(error)
    else:
        #no target found, do not calculate the angle
        ang = -1000



	#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

    #dist = 1
Example #4
0
 def test_0_deg(self):
     vec1 = (1, 0)
     vec2 = (1, 0)
     self.assertLessEqual(
         abs_diff(getAngle(vec1, vec2), to_radian(self.test_0_deg)), epsilon)
import distance
import angle

y = 400
distance = distance.findDistance(y)
# print distance

error = 100
angle = angle.getAngle(distance, angle)
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)