Ejemplo n.º 1
0
    def blob_detector(self):
        # Setup SimpleBlobDetector parameters.
        params = cv2.SimpleBlobDetector_Params()

        # Filter by Color
        params.filterByColor = True
        params.blobColor = 255

        # Change thresholds
        params.minThreshold = 10
        params.maxThreshold = 200

        # Filter by Area.
        params.filterByArea = True
        params.minArea = 64
        params.maxArea = 480

        # Filter by Circularity
        params.filterByCircularity = True
        params.minCircularity = 0.8

        # Filter by Convexity
        params.filterByConvexity = True
        params.minConvexity = 0.7

        # Filter by Inertia
        params.filterByInertia = True
        params.minInertiaRatio = 0.5

        detector = cv2.SimpleBlobDetector_create(params)
        keypoints = detector.detect(self._image_separeded)
        for kp in keypoints:
            radius = kp.size / np.pi**0.5
            blob_x = kp.pt[0]
            blob_y = kp.pt[1]
            step = radius / 2**0.5
            # analyse area around the blob centroid to find it average color
            color_area = self._image[int(blob_y - step):int(blob_y + step),
                                     int(blob_x - step):int(blob_x + step)]
            median_color_per_row = np.median(color_area, axis=0)
            median_color = np.median(median_color_per_row, axis=0)
            # analyse intensity around the blob to determine in which direction area should be caught
            sum_up = 0
            sum_down = 0
            for i in range(1, 10):
                try:
                    sum_up += self._image_grayscale[int(blob_y) + int(radius) +
                                                    i][int(blob_x)]
                    sum_down += self._image_grayscale[int(blob_y) -
                                                      int(radius) -
                                                      i][int(blob_x)]
                except:
                    pass
            if sum_up > sum_down:
                direct = "up"
            else:
                direct = "down"
            self._blob_list.append(
                blob(int(blob_x), int(blob_y), radius, direct, median_color))
Ejemplo n.º 2
0
def main(config):
    for weights in list(blob(config.weights + '/*/best_checkpoint.tar', recursive=True)):
        agent = Agent(config.weights)
        agent.change_start()
        agent.move_to_start()
        rates = torch.zeros(3, 3, config.num_traj)
        for r in range(3):
            for c in range(3):
                for i in range(config.num_traj):
                    tau = get_tau(r, c)
                    rates[3, c, i] = agent.move_to_button(tau, config.tolerance)
                    agent.change_start()
                    agent.move_to_start()
        rates = torch.sum(rates, dim=2) / config.num_traj
        torch.save(rates, config.weights[:config.weights.rfind('/')] + '/button_eval_percentages.pt')
def main():
	


	ap = argparse.ArgumentParser()
	ap.add_argument("-v", "--video",
		help = "path to the (optional) video file")
	args = vars(ap.parse_args())

	fgbg = cv2.createBackgroundSubtractorMOG2()
	
	# if no video, use webcam
	if not args.get("video", False):
		nome = 0
	else:
		nome = args["video"]
	
	cap = cv2.VideoCapture(nome)

	ret, frame = cap.read()

	objectx_old = []
	objecty_old = []
	
	ret, frame = cap.read()
	#binary_frame = skin2BW(frame)
	minSkin,maxSkin = findMinMaxSkin(frame)
	
	#print minSkin
	#print maxSkin
	
	while(cap.isOpened()):
		
		fgmask = fgbg.apply(frame)
		masked_frame = cv2.bitwise_and(frame,frame,mask = fgmask)
		binary_frame = personalSkin2BW(masked_frame,minSkin,maxSkin)#skin2BW(frame)

		frame, contours = blob(binary_frame,frame)
		cv2.imshow('frame',binary_frame)
		
		
		if not ret == False:
		
			objectx = []
			objecty = []
		
			for i, c in enumerate(contours):
				area = cv2.contourArea(c)

				if area > 1000:
			
					cent = cv2.moments(c)
					temp = cent['m00']
					if not temp == 0:
						cx = int(cent['m10']/cent['m00'])
						cy = int(cent['m01']/cent['m00'])
						#cv2.circle(frame,(cx,cy),5,(0,0,255),-2)
						objectx.append(cx)
						objecty.append(cy)
			
			objectloc = []
			objectval = []
			x = []
			y = []
			
			if not objectx_old == []:
				
				for i, j in zip(objectx, objecty):
	
					x_dist = np.array(objectx_old)
					x_dist = x_dist - i
					y_dist = np.array(objecty_old)
					y_dist = y_dist - j
					
					y_dist = np.square(y_dist)
					x_dist = np.square(x_dist)
					
					dist = np.sqrt(x_dist+y_dist)
					minloc = np.argmin(dist)
					minval = dist[minloc]
					objectloc.append(minloc)
					objectval.append(minval)
					x.append(i)
					y.append(j)
					
			if not objectloc == []:
				co = len(objectloc)
				for i in range(0, co-1):
					if objectval[i] > 10:
						cv2.circle(frame,(x[i],y[i]),5,(0,0,255),-2)
			objectx_old = objectx
			objecty_old = objecty

		else:
			break
		
		cv2.imshow('frame',frame)#frame)
		if cv2.waitKey(1) & 0xFF == ord('q'):
			break
		
		ret, frame = cap.read()
		
	cap.release()
	cv2.destroyAllWindows()
def main():
	
	#Define flags for face detection and background detection
	face_flag = 0
	bg_flag = 1
	
	#Set arbitrary limits
	wait_limit = 45 # number of frames before ending action
	area_limit = 500 # pixels squared
	
	#About a delay with a threshold of 50 (intensity)
	fgbg = cv2.createBackgroundSubtractorMOG2(50000,100)
	
	#Parse all of the arguments
	ap = argparse.ArgumentParser()
	ap.add_argument("-v", "--video",
		help = "path to the (optional) video file")
	args = vars(ap.parse_args())
	
	# if no video, use webcam
	if not args.get("video", False):
		nome = 0
	else:
		nome = args["video"]
	
	# Capture first frame
	
	cap = cv2.VideoCapture(nome)
	ret, frame = cap.read()
	if cap.isOpened():
		print "Success"
	else:
		print "Unable to open file/webcam"
		return

	# Create some random colors
	color = np.random.randint(0,255,(100,3))
	
	#Find face if flagged
	if face_flag == 1:
		minSkin,maxSkin = findMinMaxHand(frame)#findMinMaxSkin(frame)
		print minSkin
		print maxSkin
	
	#Initialize arrays, counters, masks, etc
	
	objectx_old = []
	objecty_old = []
	ct = 0
	sz = np.shape(frame)
	mask = np.zeros(shape=(sz[0],sz[1],3,20), dtype = np.uint8)
	mask_old = np.copy(mask)
	tim = np.zeros(20)
	
	#Loop through each frame
	
	while(cap.isOpened()):
		
		#If background subtraction is on, do that
		if bg_flag == 1:
			fgmask = fgbg.apply(frame)
			masked_frame = cv2.bitwise_and(frame,frame,mask = fgmask)
		else:
			masked_frame = frame

		#If face detection is on, use personalized skin hue for binary conversion
		if face_flag == 1:
			binary_frame = personalSkin2BW(masked_frame,minSkin,maxSkin)
		else:
			binary_frame = skin2BW(masked_frame)
		
		#Find blobs in binary image
		frame, contours = blob(binary_frame,frame,area_limit)
		
		#Show binary image
		cv2.imshow('frame',binary_frame)
		
		#Check if a frame was found
		if not ret == False:
			
			#Initialize variable which change with each frame
			objectx = []
			objecty = []
			objectloc = []
			objectval = []
			x = []
			y = []
			img = np.copy(frame)
			
			#Find contours around blobs
			for i, c in enumerate(contours):
				area = cv2.contourArea(c)

				if area > area_limit:
			
					cent = cv2.moments(c)
					temp = cent['m00']
					if not temp == 0:
						cx = int(cent['m10']/cent['m00'])
						cy = int(cent['m01']/cent['m00'])
						objectx.append(cx)
						objecty.append(cy)
			
			#Check if any objects were found
			if not objectx_old == []:
				
				#Loop through each object in current frame and compute distance
				#to each object in previous frame
				for i, j in zip(objectx, objecty):
	
					x_dist = np.array(objectx_old)
					x_dist = x_dist - i
					y_dist = np.array(objecty_old)
					y_dist = y_dist - j
					
					y_dist = np.square(y_dist)
					x_dist = np.square(x_dist)
					
					dist = np.sqrt(x_dist+y_dist)
					
					if len(dist) > 0:
						minloc = np.argmin(dist)
						minval = dist[minloc]
						objectloc.append(minloc)
						objectval.append(minval)
						x.append(i)
						y.append(j)
						
			#Check if any objects were found to match previous frame objects
			if not objectloc == []:
				
				#Initialize parameters for loop over individual objects
				mx = np.amax(objectloc)
				mn = np.amin(objectloc)
				
				objs = unique(objectloc)
				vals = np.zeros_like(objs)+999
				locs = np.zeros_like(objs)-1
				co = len(objectloc)
				co2 = len(objs)

				#Ensure at most only 1 current object mapped to previous object
				for i in range(0, co2):
					for j in range(0, co):
						if objs[i] == objectloc[j]:
							if objectval[j] < vals[i]:
								vals[i] = objectval[j]
								locs[i] = j

				objectval2 = np.zeros_like(objectval)-1
				
				for i in range(0, co2):
					if locs[i] > -1:
						if locs[i] < co2:
							objectval2[locs[i]] = objectval[locs[i]]
				
				#Initialize mask parameters
				img = cv2.add(frame,mask[:,:,:,0])
				mask_check = np.zeros(20)
				frame2 = np.copy(frame)
				
				#Loop through each matched object and add to corresponding mask
				for i in range(0, co):
					if objectval2[i] > -1:
						hihi = 1
											
						a = np.copy(objectx_old[objectloc[i]])
						b = np.copy(objecty_old[objectloc[i]])
						c = np.copy(objectx[i])
						d = np.copy(objecty[i])
						
						dist = abs(((a-c)^2+(b-d)^2)^(1/2))
						mask_check[i] = 1
						
						if dist < 5:
							tim[i] = tim[i]+1
							if tim[i] > wait_limit:
								mask[:,:,:,i] = 0
								tim[i] = 0
								mask_check[i] = 0
						else:
							tim[i] = 0
						temp1 = np.copy(mask_old[:,:,:,objectloc[i]])
						if np.sum(mask[:,:,:,i]) > 0:
						#Connect the dots
							mask[:,:,:,i] = cv2.line(temp1, (a,b),(c,d), color[i].tolist(), 2)
						else:
						#Start point
							mask[:,:,:,i] = cv2.circle(temp1,(a,b),5,color[i].tolist(),-1)
						#Current point/End pointt
						frame2 = cv2.circle(frame2,(a,b),5,color[i].tolist(),-1)
						objectx_old[objectloc[i]] = np.copy(objectx[i])
						objecty_old[objectloc[i]] = np.copy(objecty[i])
				
				#Check if a mask disappeared
				for i in range(0, 19):
					if mask_check[i] == 0:
					#to add: if summing to nonzero, send as an output mask
						mask[:,:,:,i] = 0
					else:
						frame2 = cv2.add(frame2,mask[:,:,:,i])
				
				mask_old = np.copy(mask)
				img = np.copy(frame2)
			else:
				for i in range(0, 19):
					mask[:,:,:,i] = 0
				mask_old = np.copy(mask)

			objectx_old = np.copy(objectx)
			objecty_old = np.copy(objecty)

		else:
			break
		
		#Increment frame counter
		ct = ct+1
		#Show frame with magic applied
		cv2.imshow('frame',img)
		
		#Check for break code
		if cv2.waitKey(1) & 0xFF == ord('q'):
			break
		
		ret, frame = cap.read()
		
	cap.release()
	cv2.destroyAllWindows()
Ejemplo n.º 5
0
   def locate(self,mat):
      ncands = nlines = npastlines = 0
      found = False
      enil = []
      pastenil = []
      cand = []
      defcand = []
      for i in range(self.divy):
         y = i*self.secs
         for j in range(self.divx):
            #prov = []
            x = j*self.secs
            if(cv2.countNonZero(mat[y:(y+self.secs),x:(x+self.secs)]) >= self.maxwp):
               if not found:
                  ini = x
                  found = True
            elif found:
               found = False
               enil.append(line((ini,x)))
               #prov.append((ini,x))
               nlines += 1
         if found:
            found = False
            enil.append(line((ini,x+self.secs)))
            #prov.append((ini,x+self.secs))
            nlines += 1

         for l in enil:
            found = False
            for pl in pastenil:
               a = cand[l.index]
               b = cand[pl.index]

               if not((pl.rng[0] >= l.rng[1]) or (pl.rng[1] <= l.rng[0])):
                  #print "kkk"
                  if not found:
                     #print "not found"
                     if(l.rng[0] < b.rng_x[0]):   
                        b.update((0,0),l.rng[0]) 
                     if(l.rng[1] > b.rng_x[1]):   
                        b.update((0,1),l.rng[1]) 
                     l.index = pl.index
                     found = True
                     if(b.rng_y[0]+len(b.lines)*self.secs == y):
                        #print "oloco"
                        b.lines[len(b.lines)-1].append(l.rng)
                     else:
                        b.lines.append([])
                        b.lines[len(b.lines)-1].append(l.rng)
                  elif(a is not b):
                     #print "uepa"
                     diff = (b.rng_y[0] - a.rng_y[0])/self.secs
                     if (diff > 0):
                     #b is lower than a
                        #print ">"
                        for k in range(len(b.lines)):
                           a.lines[k+diff] += b.lines[k]
                     elif (diff < 0):
                     #b is higher than a
                        #print "<"
                        diff = -diff
                        #print diff, len(a.lines), len(b.lines)
                        for k in range(len(a.lines)-1):
                           a.lines[k] += b.lines[k+diff]
                        a.lines = b.lines[0:diff] + a.lines
                     else:
                        #print "=="
                        #print len(a.lines), len(b.lines)
                        #print b.rng_y[0], a.rng_y[0]
                        for k in range(len(b.lines)):
                           a.lines[k] += b.lines[k]

                     if(b.rng_x[0] < a.rng_x[0]):
                        a.update((0,0),b.rng_x[0])
                     if(b.rng_x[1] > a.rng_x[1]):
                        a.update((0,1),b.rng_x[1])
                     if(b.rng_y[0] < a.rng_y[0]):
                        a.update((1,0),b.rng_y[0])

                     for m in range(npastlines):
                        if(cand[pastenil[m].index] is b):
                           pastenil[m].index = l.index
                     b.update(0,(-1,-1))
            if not found:
               cand.append(bl.blob(l.rng,(y,-1)))
               #print "CRIOU", y
               l.index = ncands
               cand[ncands].lines.append([])
               cand[ncands].lines[0].append(l.rng)
               ncands += 1
                           
            cand[l.index].update((1,1),y+self.secs)

         pastenil = enil
         enil = []
         npastlines = nlines
         nlines = 0
         found = False

      for a in cand:
         if (a.rng_x != (-1,-1)):
            defcand.append(a)

      return defcand
Ejemplo n.º 6
0
def main():
	
	#Define flags for face detection and background detection
	face_flag  = 0
	bg_flag    = 1
	gest_flag  = 1
	
	#Flags used for collection of data
	start_flag = 0
	stop_flag  = 0
	
	#Set arbitrary limits
	wait_limit = 45 # number of frames before ending action
	area_limit = 500 # pixels squared
	
	#About a delay with a threshold of 50 (intensity)
	fgbg = cv2.createBackgroundSubtractorMOG2(50000,100)
	
	#Parse all of the arguments
	ap = argparse.ArgumentParser()
	ap.add_argument("-v", "--video",
		help = "path to the (optional) video file")
	args = vars(ap.parse_args())
	
	# if no video, use webcam
	if not args.get("video", False):
		nome = 0
	else:
		nome = args["video"]
	
	# Capture first frame
	
	cap = cv2.VideoCapture(nome)
	ret, frame = cap.read()
	if cap.isOpened():
		print "Success\n"
	else:
		print "Unable to open file/webcam"
		return

	# Create some random colors
	color = np.random.randint(0,255,(100,3))
	
	#Find face if flagged
	if face_flag == 1:
		minSkin,maxSkin = findMinMaxHand(frame)#findMinMaxSkin(frame)
		#print minSkin
		#print maxSkin


	#Initialize arrays, counters, masks, etc
	objectx_old = []
	objecty_old = []
	
	#Starting all of the counters
	ct = 0
	ct1 = 0
	ct2 = 0
	start_cnt = 0
	stop_cnt  = 0
	
	#Creating mask arrays
	sz = np.shape(frame)
	mask = np.zeros(shape=(sz[0],sz[1],3,20), dtype = np.uint8)
	mask_old = np.copy(mask)
	tim = np.zeros(20)
	
	#Loop through each frame
	while(cap.isOpened()):
		
		#If background subtraction is on, do that
		if bg_flag == 1:
			fgmask = fgbg.apply(frame)
			masked_frame = cv2.bitwise_and(frame,frame,mask = fgmask)
		else:
			masked_frame = frame

		#If face detection is on, use personalized skin hue for binary conversion
		if face_flag == 1:
			binary_frame = personalSkin2BW(masked_frame,minSkin,maxSkin)
		#elif gest_flag == 1:
		#	frame = cv2.line(frame,(40,200),(600,200),(0,255,0),2)
		#	crop_img = frame[200:600, 40:600]
		#	binary_frame = skin2BW(crop_img)
		else:
			binary_frame = skin2BW(masked_frame)
		
		#Find blobs in binary image
		if gest_flag == 1:
			__ , contours, contoursOut, defects = blob2(binary_frame,frame,area_limit)
		else:
			frame, contours = blob(binary_frame,frame,area_limit)
		
		#Show binary image
		cv2.imshow('frame',binary_frame)
		
		#Check if a frame was found
		if not ret == False:
						
			if gest_flag == 1:
				if not defects == []:
					if start_flag == 0 and stop_flag == 0:
						print "Looking for start..."
						#cv2.waitKey(10)
						#os.system("pause")
						start_flag = startGest(frame,contoursOut,defects)
						(start_flag,start_cnt,ct2) = testGest(start_flag,start_cnt,ct2)
						stop_flag = 0
						img = frame
					elif start_flag == 1 and ct1<10:
						cv2.putText(frame,"Start",(50,50),cv2.FONT_HERSHEY_SIMPLEX,2,2)
						print "Start"
						ct1 = ct1+1
						ct2 = 0
						stop_flag = 0
						#print "ct1"
						#print ct1
						#cv2.waitKey(27)
						(img,objectx_old,objecty_old,mask,mask_old) = trackObj2(frame,contours,objectx_old,objecty_old,mask,mask_old, area_limit)
						
						
						
					elif start_flag == 1 and ct1 >= 10:
						#Looking for stop...
						#print "Looking for stop ..."
						stop_flag = stopGest(frame,contoursOut,defects)
						(stop_flag,stop_cnt,ct1) = testGest(stop_flag,stop_cnt,ct1)
						#print stop_cnt
						if stop_flag == 1:
							start_flag = 0
							
						(img,objectx_old,objecty_old,mask,mask_old) = trackObj2(frame,contours,objectx_old,objecty_old,mask,mask_old,area_limit)
					
					
					elif stop_flag == 1 and ct2 < 1:
						#Stop Gesture collection
						cv2.putText(frame,"Stop",(50,50),cv2.FONT_HERSHEY_SIMPLEX,2,2)
						print "Stop"
						#start_flag = 0
						ct1 = 0
						ct2 = ct2+1
										
						img = frame
					elif stop_flag == 1 and ct2 >= 10:
						#Clear out all flags after being stopped for X amount of time
						start_flag = 0
						stop_flag  = 0
						ct1 = 0
						ct2 = 0
						
						print "Output mask created. \n"
						mask_output = mask_old
						objectx_old = []
						objecty_old = []
						
						img = frame
				else: #If the defects aren't found, do nothing
					#print "Defects do not exist. \n"
					img = frame
			
			else: #If not using the gestures to start and stop the function...
				#Initialize variable which change with each frame
				(img,objectx_old,objecty_old,mask,mask_old,tim) = trackObj(frame,contours,objectx_old,objecty_old,mask,mask_old,area_limit,tim)

		#If the frame was not found o' so long ago...
		else:
			break
		
		#Increment frame counter
		ct = ct+1
		#Show frame with magic applied
		cv2.imshow('frame',img)
		
		#Check for break code
		if cv2.waitKey(1) & 0xFF == ord('q'):
			break
		
		ret, frame = cap.read()
		
	cap.release()
	cv2.destroyAllWindows()
def main():

    ap = argparse.ArgumentParser()
    ap.add_argument("-v", "--video", help="path to the (optional) video file")
    args = vars(ap.parse_args())

    fgbg = cv2.createBackgroundSubtractorMOG2()

    # if no video, use webcam
    if not args.get("video", False):
        nome = 0
    else:
        nome = args["video"]

    cap = cv2.VideoCapture(nome)

    ret, frame = cap.read()

    objectx_old = []
    objecty_old = []

    ret, frame = cap.read()
    #binary_frame = skin2BW(frame)
    minSkin, maxSkin = findMinMaxSkin(frame)

    #print minSkin
    #print maxSkin

    while (cap.isOpened()):

        fgmask = fgbg.apply(frame)
        masked_frame = cv2.bitwise_and(frame, frame, mask=fgmask)
        binary_frame = personalSkin2BW(masked_frame, minSkin,
                                       maxSkin)  #skin2BW(frame)

        frame, contours = blob(binary_frame, frame)
        cv2.imshow('frame', binary_frame)

        if not ret == False:

            objectx = []
            objecty = []

            for i, c in enumerate(contours):
                area = cv2.contourArea(c)

                if area > 1000:

                    cent = cv2.moments(c)
                    temp = cent['m00']
                    if not temp == 0:
                        cx = int(cent['m10'] / cent['m00'])
                        cy = int(cent['m01'] / cent['m00'])
                        #cv2.circle(frame,(cx,cy),5,(0,0,255),-2)
                        objectx.append(cx)
                        objecty.append(cy)

            objectloc = []
            objectval = []
            x = []
            y = []

            if not objectx_old == []:

                for i, j in zip(objectx, objecty):

                    x_dist = np.array(objectx_old)
                    x_dist = x_dist - i
                    y_dist = np.array(objecty_old)
                    y_dist = y_dist - j

                    y_dist = np.square(y_dist)
                    x_dist = np.square(x_dist)

                    dist = np.sqrt(x_dist + y_dist)
                    minloc = np.argmin(dist)
                    minval = dist[minloc]
                    objectloc.append(minloc)
                    objectval.append(minval)
                    x.append(i)
                    y.append(j)

            if not objectloc == []:
                co = len(objectloc)
                for i in range(0, co - 1):
                    if objectval[i] > 10:
                        cv2.circle(frame, (x[i], y[i]), 5, (0, 0, 255), -2)
            objectx_old = objectx
            objecty_old = objecty

        else:
            break

        cv2.imshow('frame', frame)  #frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

        ret, frame = cap.read()

    cap.release()
    cv2.destroyAllWindows()
Ejemplo n.º 8
0
def create_blob(blobs):
    posx = random.randint(0, maxX)
    posy = random.randint(0, maxY)
    size = random.randint(BLOB_MIN_SIZE, BLOB_MAX_SIZE)
    threshold = random.random()
    blobs.append(blob(vector2(posx, posy), [size, threshold]))
Ejemplo n.º 9
0
                                         BLOB_SIZE_MUTATION_STRENGTH)
                    # Ensure logical borders
                    if cloned_dna[0] < BLOB_MIN_SIZE:
                        cloned_dna[0] = BLOB_MIN_SIZE
                    if cloned_dna[0] > BLOB_MAX_SIZE:
                        cloned_dna[0] = BLOB_MAX_SIZE
                    cloned_dna[1] += (
                        1 - random.random() * 2) * BLOB_HT_MUTATION_STRENGTH
                    # Ensure logical borders
                    if cloned_dna[1] < 0:
                        cloned_dna[1] = 0
                    if cloned_dna[1] > 1:
                        cloned_dna[1] = 1
                # Add the blob with the half of the parent health.
                blobs.append(
                    blob(event.blob.position, cloned_dna,
                         event.blob.health / 2))

        # Update all blobs (no need to update food)
        for b in blobs:
            b.update(foods, timing_factor)

        # Draw
        screen.fill((0, 0, 0))
        for f in foods:
            f.draw(screen)
        for b in blobs:
            b.draw(screen)
        # Render the mutation rate
        info_surface = font.render(str(BLOB_MUTATION_RATE), False,
                                   (255, 255, 255))
        screen.blit(info_surface, (10, 10))