def save_images(imagesPath, outPath=None, extension='jpg'): """ Saves batch of images the given out path after doing the preprocessing which could be 1- subtract mean 2- normalize 3- both of them """ #imagesNames = os.listdir(imagesPath) Image.set_path(dataPath) if outPath == None: image = Image(size=IMAGE_SIZE_MOBILENET, outputBasePath=out_path) else: if (not os.path.exists(outPath)): os.mkdir(outPath) print("folder created : " + outPath) else: #TODO #handle this properly if the images already exists #you may remove them pass image = Image(size=IMAGE_SIZE_MOBILENET) for imageName in imagesPath: if (is_valid_name(imageName.split('_'))): image.read_image(imageName) if outPath != None: image.save(outputPath=outPath, extension=extension, normalize_mean=False, print_info=False) else: image.save(extension=extension, normalize_mean=False, print_info=False)
def auvdeployment_import(auvdeployment, files): """Import an AUV deployment from disk. This uses the track file and hydro netcdf files (as per RELEASE_DATA). If the deployment comes in the CATAMI deployment format use that importer instead. Certain parameters of the deployment should be prefilled - namely short_name, campaign, license, descriptive_keywords and owner. The rest are obtained from the on disk information. Information obtained within the function includes start and end time stamps, start and end positions, min and max depths and mission aim. Additionally the region column, and other AUV specific fields are filled. """ print "Entered!" logger.debug("Entering auvdeployment import") netcdf = NetCDFParser(open(files['netcdf'], "r")) track_parser = TrackParser(open(files['track'], "r")) image_subfolder = files['image'] # now start going through and creating the data auvdeployment.mission_aim = "Generic Description." auvdeployment.min_depth = 14000 auvdeployment.max_depth = 0 auvdeployment.start_position = "POINT(0.0 0.0)" auvdeployment.end_position = "POINT(0.0 0.0)" auvdeployment.start_time_stamp = datetime.datetime.now() auvdeployment.end_time_stamp = datetime.datetime.now() auvdeployment.transect_shape = "POLYGON((0.0 0.0, 0.0 0.0, 0.0 0.0, 0.0 0.0, 0.0 0.0))" # we have to save the deployment so that everything else can link to it logger.debug("Initial save of auvdeployment.") auvdeployment.save() # create the left-colour camera object # we don't normally give out the right mono # images... leftcamera = Camera() leftcamera.deployment = auvdeployment leftcamera.name = "Left Colour" leftcamera.angle = Camera.DOWN_ANGLE leftcamera.save() # get the sm types that we are going to use logger.debug("Get handle on required ScientificMeasurementTypes") temperature = ScientificMeasurementType.objects.get( normalised_name='temperature') salinity = ScientificMeasurementType.objects.get( normalised_name='salinity') pitch = ScientificMeasurementType.objects.get(normalised_name='pitch') roll = ScientificMeasurementType.objects.get(normalised_name='roll') yaw = ScientificMeasurementType.objects.get(normalised_name='yaw') altitude = ScientificMeasurementType.objects.get( normalised_name='altitude') logger.debug("Got all required ScientificMeasurementTypes") first_pose = None last_pose = None lat_lim = LimitTracker('latitude') lon_lim = LimitTracker('longitude') logger.debug("First readings from netcdf file.") earlier_seabird = netcdf.next() later_seabird = netcdf.next() # now we get to the images... (and related data) logger.debug("Begin parsing images.") for row in track_parser: pose = Pose() image_name = os.path.splitext(row['leftimage'])[0] + ".tif" pose.deployment = auvdeployment pose_datetime = datetime.datetime.strptime( os.path.splitext(image_name)[0], "PR_%Y%m%d_%H%M%S_%f_LC16") pose.date_time = pose_datetime.replace(tzinfo=tzutc()) pose.position = "POINT ({0} {1})".format(row['longitude'], row['latitude']) pose.depth = float(row['depth']) # save now as it is complete and so image # can refer to it pose.save() # quickly calculate limit info if pose.depth > auvdeployment.max_depth: auvdeployment.max_depth = pose.depth if pose.depth < auvdeployment.min_depth: auvdeployment.min_depth = pose.depth lat_lim.check(row) lon_lim.check(row) # calculate image locations and create thumbnail campaign_name = auvdeployment.campaign.short_name deployment_name = auvdeployment.short_name image_path = os.path.join(image_subfolder, image_name) archive_path, webimage_path = image_import(campaign_name, deployment_name, image_name, image_path) image = Image() image.camera = leftcamera image.pose = pose image.archive_location = archive_path image.web_location = webimage_path image.save() # get the extra measurements from the seabird data while pose.date_time > later_seabird['date_time']: later_seabird, earlier_seabird = earlier_seabird, netcdf.next() # find which is closer - could use interpolation instead if (later_seabird['date_time'] - pose.date_time) > ( pose.date_time - earlier_seabird['date_time']): closer_seabird = earlier_seabird else: closer_seabird = later_seabird # add those extra scientific measurements temp_m = ScientificPoseMeasurement() temp_m.measurement_type = temperature temp_m.value = closer_seabird['temperature'] temp_m.pose = pose temp_m.save() sal_m = ScientificPoseMeasurement() sal_m.measurement_type = salinity sal_m.value = closer_seabird['salinity'] sal_m.pose = pose sal_m.save() roll_m = ScientificPoseMeasurement() roll_m.measurement_type = roll roll_m.value = row['roll'] roll_m.pose = pose roll_m.save() pitch_m = ScientificPoseMeasurement() pitch_m.measurement_type = pitch pitch_m.value = row['pitch'] pitch_m.pose = pose pitch_m.save() yaw_m = ScientificPoseMeasurement() yaw_m.measurement_type = yaw yaw_m.value = row['heading'] yaw_m.pose = pose yaw_m.save() alt_m = ScientificPoseMeasurement() alt_m.measurement_type = altitude alt_m.value = row['altitude'] alt_m.pose = pose alt_m.save() # we need first and last to get start/end points and times last_pose = pose if first_pose is None: first_pose = pose auvdeployment.start_time_stamp = first_pose.date_time auvdeployment.end_time_stamp = last_pose.date_time auvdeployment.start_position = first_pose.position auvdeployment.end_position = last_pose.position auvdeployment.transect_shape = "POLYGON(({0} {2}, {0} {3}, {1} {3}, {1} {2}, {0} {2} ))".format( lon_lim.minimum, lon_lim.maximum, lat_lim.minimum, lat_lim.maximum) # now save the actual min/max depth as well as start/end times and # start position and end position auvdeployment.save() return auvdeployment
class Robot: """ @function: constructor @brief: Intilize the robot class and make it prepared to explore """ def __init__(self): """ Intilize RobotMotor, SensorMotor, UltrasoundSensor, the TouchSensor and all the other necessary components for the robot then set its position to (0, 0) """ self.moteur = RobotMotor(5.5, 14.0, OUTPUT_A, OUTPUT_D) # Intilize both motors self.sensor = USensor() # Intilize Ultrasound sensor self.ts = TouchSensor() # Intilize TouchSensor self.sensor_motor = SensorMotor() # Initlize SensorMotor that is will be responsible to rotate the ultra sound sensor self.is_running = False # Is running boolean self.cool_down_time = 1.0 # the cooldown time for the program to sleep at each iteration self.posx = 0 # Set position X to 0 self.posy = 0 # Set position Y to 0 self.img = Image("explore.png", 1028, 1028) # Create the result image thats gonna be filled during exploring self.sensor_dir = 1 # Sensor state either (0:left, 1:middle, 2:right), iniltized to look front self.obstacles = [] # Array of arrays containing obstacles """ @function: start @brief: when called the robot will start exploring his surrounding to draw the map """ def start(self): self.moteur.go_forwards() # Start the motor and advance forward # print("* Going forward") dist = self.sensor.get_distance() # Get intial distance # print("TouchSensor: "+str(self.ts.value())) while not self.ts.value(): # While the press button isn't pressed self.loop(dist) # Run the loop function that will handle all the logic dist = self.sensor.get_distance() # Update distance at each iteration self.shutdown() """ @function: loop @brief: handle all the logic at each iteration according the current state """ def loop(self, dist): self.posy += 1 # Advance in the y direction # print("Distance of USensor: %0.2f"%dist) # Print distance for debugging if (dist < 30.0): # if distance is less than 30 cm away self.handleObstacle() # call the handle obstacle function else: self.img.point((self.posx, self.posy), (0, 255, 0)) # mark this point as visited in the picture by putting green pixel # Update and rotate the sensor direction: self.rotateSensor() # Print current position for debugging purposes # print("* My Position is : (%.2f, %.2f)" % (self.posx, self.posy)) sleep(self.cool_down_time) # Wait for the cooldown to expire to do another iteration """ @function: rotateSensor @brief: handle sensor rotation to cover the 180 degrees field of view in the front """ def rotateSensor(self): if (self.sensor_dir == 0): # if the sensor dir is left self.sensor_motor.stop() # stop the motor sleep(0.1) # sleep for a little bit self.sensor_motor.turn_right(None, 20) # turn it to the right to face the front # print("* Turning sensor to the middle") # debug output elif (self.sensor_dir == 1): # if the sensor dir is in the middle self.sensor_motor.stop() # stop the motor sleep(0.1) # sleep for little bit self.sensor_motor.turn_right(None, 20) # turn the motor to the right # print("* Turning sensor to the right") # Debug message elif (self.sensor_dir == 2): # if the sensor dir is the left self.sensor_motor.stop() # stop the motor sleep(0.1) # sleep for little bit self.sensor_motor.turn_left(None) # rotate the motor to the left # print("* Turning sensor to the left") # debug message # Increment and do modulo 3 to no go out of our 3 states (left, middle, right) self.sensor_dir = (self.sensor_dir + 1) % 3 """ @function: handleObstacle @brief: handle the logic when the robots sees an obstacle """ def handleObstacle(self): if (self.sensor_dir == 0): # if the sensor is to the left # print("* Handling obstacle to the left") # debug message self.addObstacle((self.posx, self.posy - 1)) # add obstacle to the table with the right coordinates elif (self.sensor_dir == 1): # print("* Handling obstacle to the middle") # debug message to the table self.addObstacle((self.posx+1, self.posy)) # add obstacle to the table with the right coordinates elif (self.sensor_dir == 2): # print("* Handling obstacle to the right") # debug message to the table self.addObstacle((self.posx, self.posy+1)) # add obstacle to the table with the right coordinates """ @function: addObstacle @brief: add obstacles coordinate """ def addObstacle(self, pos): if((self.posx, self.posy - 1) not in self.obstacles): # if the coordinates are not already in onstacles then self.obstacles.append((self.posx, self.posy - 1)) # add to obstacles else: # print("deja enregistré") # print debug messages pass """ @function: stop @brief: stop all motors """ def stop(self): self.moteur.stop() # stop motor self.sensor_motor.stop() # stop sensor motor """ @function: shutdown @brief: shutdown robot """ def shutdown(self): # print("* Shutting down") # debug output self.is_running = False # put is running state is false self.stop() # stop all motors self.img.save() # save the result image