def sense_and_act(self): if self.bbcon.can_take_photo: print("Taking photo!") image_obj = self.c_sensob.update() img = Imager(image=image_obj) img.dump_image('/') self.match_degree = 0.9 triple2 = [0] * 3 for x in range(img.xmax): for y in range(img.ymax): t = img.get_pixel(x, y) for i in range(len(triple2)): triple2[i] += t[i] print("RGB", triple2) print(triple2[0] > triple2[1] and triple2[0] > triple2[2]) if triple2[0] > triple2[1] and triple2[0] > triple2[2]: self.motor_recommendations = ['t'] else: self.motor_recommendations = ['f'] self.bbcon.photo_taken() self.priority = 0.9
class FindRedSensob(Sensob): def __init__(self): self.bilde = None self.value = [0,0,0,0,0] #Makes the list with red pixels Sensob.__init__(self,[Camera()]) #Super-constructor with the camera-sensor. def update(self): if super(FindRedSensob,self).update(): #Run the super-update, to update the sensor values. self.take_picture() #Takes the picture and stores it on the rpi. self.bilde = Imager('red_image.jpeg') #Accesses the picture. self.value = [0, 0, 0, 0, 0] #Resets the list. self.delta = self.bilde.xmax // 5 #Divides the picture into 5 equal columns. self.make_image_wta() #Performs a winner-take-all-function to make every pixel only red, green or blue. self.make_red_image() #Makes all green and blue pixels black, and maximizes the red in the red pixels. self.calculate_where_most_red() #Calculates wich fith has the most red, and returns the list as the self.value. return self.value def take_picture(self): im = Imager(image=self.sensors[0].update()).scale(1,1) #Takes the picture. im.dump_image('red_image.jpeg') #Stores the picture. def reset(self): Sensob.reset(self) self.value = [0,0,0,0,0] def get_value(self): return self.value def is_red_pixel(self,x,y): pixel = self.bilde.get_pixel(x,y) if pixel[0] > pixel[1] and pixel[0] > pixel[2] and pixel[0] > 50: return True def make_image_wta(self): #Performs a winner-take-all-function to make every pixel only red, green or blue. self.bilde = self.bilde.map_color_wta() def make_red_image(self): #Makes all green and blue pixels black, and maximizes the red in the red pixels. for i in range(self.bilde.xmax): for j in range(self.bilde.ymax): if self.is_red_pixel(i,j): self.bilde.set_pixel(i,j,(255,0,0)) else: self.bilde.set_pixel(i,j,(0,0,0)) def calculate_where_most_red(self): #Calculates wich fith has the most red, and returns the list as the self.value for i in range(self.bilde.xmax): for j in range(self.bilde.ymax): if self.is_red_pixel(i,j): which_fifth = i//self.delta #print("which fifth: ", which_fifth) self.value[which_fifth - 1] += 1
def process_data(self, data): print("Datatype: ", data) red_count = 0 image = Imager(image=data[0]) pixel_list = [] for x in range(image.xmax): for y in range(image.ymax): pixel_list.append(image.get_pixel(x, y)) for pixel in pixel_list: temp_count = 0 for i in range(3): if self.lower[i] < pixel[i] < self.upper[i]: temp_count += 1 if temp_count == 3: red_count += 1 self.value = red_count / self.camob.get_size()
def detect_red(image, percentage): """Checks if more than a percentage of the pixels are red""" imager = Imager(image=image) count = 0 for x in range(imager.xmax): for y in range(imager.ymax): pixel = imager.get_pixel(x, y) if pixel[0] - pixel[1] >= 150 and pixel[0] - pixel[2] >= 150: count += 1 elif pixel[1] < 25 and pixel[2] < 25 and pixel[0] - pixel[ 1] >= 80 and pixel[0] - pixel[2] >= 80: count += 1 elif pixel[1] > 150 and pixel[2] > 150 and pixel[0] - pixel[ 1] >= 80 and pixel[0] - pixel[2] >= 80: count += 1 return count / (imager.xmax * imager.ymax) > percentage
def update(self): """Check if you see red Takes a picture, runs wta(winner takes all) on it, then counts the number of red pixels If there's enough red, motor""" self.sensor.update() taken_image = self.sensor.get_value() wta_image = Imager(image=taken_image).map_color_wta(thresh=0.5) # colors each pixel the dominant color red_count = 0 for i in range(20, 100): for j in range(20, 80): if wta_image.get_pixel(i, j)[0] > 100: red_count += 1 print('Red count is:', red_count) if red_count > self.color_treshold: self.value = 1.0 else: self.value = 0.0