1

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()
  • Have you looked at the documentation at all? There is a `self.camera.saveImgae(name,quality)` method. – Tim Roberts Jul 31 '23 at 21:42
  • hi yes, I saw this in the documentation and tried this but I still do not see the image getting saved I have edited the above function in such a way to implement the saveImage method, but I'm not sure how to use this in my main controller code – Rohin Prasanth Aug 01 '23 at 00:11
  • I'm fairly new to coding so I'm not exactly sure how to use the saveImage method – Rohin Prasanth Aug 01 '23 at 00:12
  • I'm sure it IS being saved, but you don't know where to find the file. It will be in the "current directory" of your process, which is not the folder containing the source. Perhaps you should use a complete path so you know where to find it. – Tim Roberts Aug 01 '23 at 03:09
  • hi, I managed to figure out the path, for some reason it shows up inside my controller folder and not the worlds folder. But huge thanks for your help! – Rohin Prasanth Aug 01 '23 at 07:24
  • It is normal that it doesn't show up in the worlds folder because your controller process is not running in the worlds folder. Any relative path expressed in the code of your controller is relative to the location where your controller lies. – Olivier Michel Aug 03 '23 at 08:19

0 Answers0