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
def left(bilde): bild = Imager(bilde) #bild = bild.resize(1000, 1000) width, height = bild.image.size #bild.display() left = 0 top = 0 right = width/2 bot = height bild.image = bild.image.crop((left, top, right, bot)) return bild
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 __init__(self): self.n = int(input('One or two img?\n>> ')) while self.n not in (1, 2): self.n = int(input('I said ONE or TWO img?!\n>> ')) fid1 = input('Paste img1 path:\n>> ') self.img1_er = Imager(fid1) self.img1 = Image.open(fid1) if self.n == 2: fid2 = input('Paste img2 file path:\n>> ') self.img2_er = Imager(fid2) self.img2 = Image.open(fid2)
def sense_and_act(self): if self.active_flag: print("BLUE AVOID") im = self.cam.get_value() #loads image from camera im = Imager(image=im) #creates an Imager object rgb = im.most_frequent_colour() # gets the most frequent pixel most_rgb = max(rgb[1][0], rgb[1][1], rgb[1][2]) #gets the edominant RGB colour print("MOST_RGB: ", rgb[0], " vs ", im.xmax * im.ymax) #if the dominant colour is not red, camera is useless and gives off a picture with dominant red colour if most_rgb != rgb[1][0]: self.match_degree = 1 self.active_flag = False
def process_sensor_data(self, sensor_data): """ process the data from the camera sensor. """ img = sensor_data[0] image = Imager(image=img) # excuse me image = image.map_color_wta() red_pixels = 0 for pixel in image.image.getdata(): if pixel[0] > 200 and pixel[1] == 0 and pixel[2] == 0: red_pixels += 1 print("The amount of red pixels are:", red_pixels) self.value = red_pixels / self.image_size
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
def update(self): print('Updating CamUltra') self.distance = self.ultrasensor.update() print('dist = ', self.distance) if self.distance < self.range: print('Taking a picture in CamUltra') s = 1 self.image = Imager(image=self.camera.update()) else: self.image = False
class TheArtist: def __init__(self): self.n = int(input('One or two img?\n>> ')) while self.n not in (1, 2): self.n = int(input('I said ONE or TWO img?!\n>> ')) fid1 = input('Paste img1 path:\n>> ') self.img1_er = Imager(fid1) self.img1 = Image.open(fid1) if self.n == 2: fid2 = input('Paste img2 file path:\n>> ') self.img2_er = Imager(fid2) self.img2 = Image.open(fid2) def resize(self, newxsize=250, newysize=250): self.img1_er = self.img1_er.resize(newxsize, newysize) if self.n == 2: self.img2_er = self.img2_er.resize(newxsize, newysize) def scale_colors(self): imer = self.img1_er.scale_colors() imer.display() def wta(self): imer = self.img1_er.map_color_wta() imer.display() def greyscale(self): imer = self.img1_er.gen_grayscale() imer.display() def tunnel(self): imer = self.img1_er.tunnel() imer.display() def morph_roll(self): self.resize() imer = self.img1_er.morphroll(self.img2_er) imer.display() def emboss(self): im = self.img1.filter(ImageFilter.EMBOSS) im.show() def blur(self): im = self.img1.filter(ImageFilter.BLUR) im.show() def contrast(self, factor=100): enh = ImageEnhance.Contrast(self.img1) enh.enhance(factor).show()
class FindRed(Behavior): """ Uses the camera to find out if there is a red object in front of the robot """ def __init__(self, sensob=None, debug=False): super().__init__() self.priority = MED self.debug = debug self.imager = Imager(width=IMG_WIDTH, height=IMG_HEIGHT) self.motor_recommendations = [Motors.forward] def __name__(self): return "FindRed" def sense_and_act(self): """ Take a picture, find the percentage of red pixels """ def red_only(p): """ Maps red pixels to white, everything else to black """ lower = (155, 25, 0) upper = (255, 100, 100) r, g, b = p if (lower[0] <= r <= upper[0] and lower[1] <= g <= upper[1] and lower[2] <= b <= upper[2]): return (255, 255, 255) return (0, 0, 0) image = self.bbcon.get_sensob_value(Camera) mapped = self.imager.map_image2(red_only, image=image) if self.debug: mapped.dump_image(fid='image_mapped', type='png') im_arr = np.array(mapped.image) white_pixels = np.sum(im_arr == (255, 255, 255)) ratio = white_pixels / (len(im_arr[0]) * len(im_arr)) if ratio > 0.5: self.match_degree = 0.8 self.motor_speed = 1 else: self.match_degree = 0 self.motor_speed = 0.3
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
class FindRed(Behavior): """ This behaviour advices the robot to accelerate whenever the camera spots something red. """ def __init__(self, sensob=None, debug=False): self.priority = 1 self.debug = debug self.imager = Imager(width=IMG_WIDTH, height=IMG_HEIGHT) super().__init__() def sense_and_act(self): """ Take a picture, find the percentage of red pixels """ def red_only(p): """ Maps red pixels to white, everything else to black """ lower = (155, 25, 0) upper = (255, 100, 100) # r, g, b = p # if (lower[0] <= r <= upper[0] and # lower[1] <= g <= upper[1] and # lower[2] <= b <= upper[2]): # return (255, 255, 255) if lower <= p <= upper: return (255, 255, 255) return (0, 0, 0) image = self.bbcon.sensobs.values['camera'] mapped = self.imager.map_image2(red_only, image=image) if self.debug: mapped.dump_image(fid='image_mapped', type='png') im_arr = np.array(mapped.image) white_pixels = np.sum(im_arr == (255, 255, 255)) ratio = white_pixels/(len(im_arr[0])*len(im_arr)) return 0.8 if ratio > 0.05 else 0, 5
def __init__(self, sensob=None, debug=False): super().__init__() self.priority = MED self.debug = debug self.imager = Imager(width=IMG_WIDTH, height=IMG_HEIGHT) self.motor_recommendations = [Motors.forward]
def update(self): image = self.sensor.update() if False: # True = save image img = Imager(image=image) img.dump_image("img.jpeg") self.value = self.analyze_picture(image)
def __init__(self, sensob=None, debug=False): self.priority = 1 self.debug = debug self.imager = Imager(width=IMG_WIDTH, height=IMG_HEIGHT) super().__init__()
def camTest(): ZumoButton().wait_for_press() sensor = Camera(img_width=128, img_height=96, img_rot=0) #endre disse? sensor2 = Camera(img_width=256, img_height=192, img_rot=0) sensor3 = Camera(img_width=512, img_height=384, img_rot=0) sensor4 = Camera(img_width=1024, img_height=768, img_rot=0) sensor.update() sensor2.update() sensor3.update() sensor4.update() pic = sensor.get_value() pic2 = sensor2.get_value() pic3 = sensor3.get_value() pic4 = sensor4.get_value() b = Imager() b.image = pic b.dump_image("test", type="JPEG") #dump as jpeg/jpg/gif? b.image = pic2 b.dump_image("test2", type="JPEG") b.image = pic3 b.dump_image("test3", type="JPEG") b.image = pic4 b.dump_image("test4", type="JPEG")
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.