How can I save an image captured by my robot's camera in webots?

211 Views Asked by At

I have the below function to get the camera image and return the RGB values for color detection as part of my project. However, I see that the image is also obtained. I would like to save this image so that I can feed it to a CNN model for image recognition.

def get_camera_image(self, interval):
    width = self.camera.getWidth()
    height = self.camera.getHeight()
    image = self.camera.getImage()
    self.camera.saveImage("image.jpg", 100)
    
    # Capture new image after interval (no. of steps) has passed
    if self.camera_interval >= interval:
        for x in range(width):
            for y in range(height):
                self.red += self.camera.imageGetRed(image, self.camera.getWidth(), x, y)
                self.green += self.camera.imageGetGreen(image, self.camera.getWidth(), x, y)
                self.blue += self.camera.imageGetBlue(image, self.camera.getWidth(), x, y)
        self.red = int(self.red / (width * height))  # normalise wrt image size
        self.green = int(self.green / (width * height))  # normalise wrt image size
        self.blue = int(self.blue / (width * height))  # normalise wrt image size
        self.camera_interval = 0
    else:
        self.red = 0
        self.green = 0
        self.blue = 0
        self.camera_interval += 1
    
    #print("Camera: R =", self.red, ", G =", self.green, ", B =", self.blue)  # DEBUG
    return self.red, self.green, self.blue, image

This is how my main controller code looks like, I am not sure where to add the code to save the image obtained by the camera.

import robot

def main():
    red = 0
    green = 0
    blue = 0
    range = 0.0
    ground = 0.0
    robot1 = robot.ARAP()
    robot1.init_devices()
    flag1 = True
    flag2 = True
    flag3 = True
    flag4 = True
    flag5 = True
    summary = []
    
    while True:
        robot1.reset_actuator_values()
        range = robot1.get_sensor_input()
        robot1.blink_leds()
        ground = robot1.get_ground_sensor_values()
        red, green, blue, img = robot1.get_camera_image(5)
        #id = robot1.get_recognised_object()
        #print(red, green, blue) #debug
        #print(ground) #debug
        #if red >= 200 and green >= 200 and blue >= 200:
            #pass
        if flag1 == True:
            if  ground >= 700 and red > 150:
                #print(red, green, blue) #debug
                if red > blue and red > green:
                    print("I see red")
                    flag1 = False
                    summary.append("Red")  
                    print(summary)
                          
        if flag2 == True:
            if blue > 180 and ground >= 690:
                #print(red, green, blue) #debug
                if blue > red and blue > green:
                    print("I see blue")
                    flag2 = False
                    summary.append("blue")
                    print(summary)          
        
        if flag3 == True:
            if ground >= 700 and green > 180:
                #print(red, green, blue) #debug
                if green > red and green > blue:
                    print("I see green")
                    flag3 = False
                    summary.append("green")
                    print(summary)
        
        if flag4 == True:
            if ground >= 300 and ground <= 310 and blue >= 150:
                #print(ground, blue) #debug
                print("I found water")
                flag4 = False
                summary.append("water")
                print(summary)
        
        if flag5 == True:
            if ground >= 300 and ground <= 700 and green >= 150:
                #print(ground, green) #debug
                print("I found food")
                flag5 = False
                summary.append("food")
                print(summary)
           
                   
        if robot1.front_obstacles_detected():
            robot1.move_backward()
            robot1.turn_left()
        
        if robot1.right_obstacles_detected():
            robot1.move_backward()
            robot1.turn_left()
        
        if robot1.left_obstacles_detected():
            robot1.move_backward()
            robot1.turn_right()
        
        else:
            robot1.run_braitenberg()
        robot1.set_actuators()
        robot1.step()

if __name__ == "__main__":
    main()
0

There are 0 best solutions below