def test_env_load(self): """Get user_env from redis""" self.r.set('BOT_STATUS.user_env.testkey', self.testvalue) self.assertEqual( ENV.redis_load('user_env', name='testkey', get_json=False, other_redis=self.r), self.testvalue)
def _get_raw_coordinate_values(self, redis=None): temp = [] legacy = int(os.getenv('FARMBOT_OS_VERSION', '0.0.0')[0]) < 6 if legacy: for axis in ['x', 'y', 'z']: temp.append( ENV.redis_load('location_data.position.' + axis, other_redis=redis)) else: state = self._get_bot_state() for axis in ['x', 'y', 'z']: try: value = state['location_data']['position'][str(axis)] except KeyError: value = None temp.append(value) return temp
def _get_raw_coordinate_values(self, redis=None): temp = [] if USE_FARMWARE_TOOLS and redis is None: coord = device.get_current_position() or {} return [coord.get('x'), coord.get('y'), coord.get('z')] legacy = int(os.getenv('FARMBOT_OS_VERSION', '0.0.0')[0]) < 6 if legacy: for axis in ['x', 'y', 'z']: temp.append( ENV.redis_load('location_data.position.' + axis, other_redis=redis)) else: state = self._get_bot_state() for axis in ['x', 'y', 'z']: try: value = state['location_data']['position'][str(axis)] except KeyError: value = None temp.append(value) return temp
def load_calibration_data_from_env(self): """Load calibration parameters from environment variable.""" self.calibration_params = ENV.load(self.env_var_name) if self.calibration_params is None: raise ValueError("ENV load failed.")
def save_to_env_var(self, widget): """Save input parameters to environment variable.""" if 'calibration' in widget: prefix = 'CAMERA_CALIBRATION_' else: prefix = 'WEED_DETECTOR_' for label, value in self.parameters.items(): if 'blur' in label: ENV.save(prefix + 'blur', value) elif 'morph' in label: ENV.save(prefix + 'morph', value) elif 'iteration' in label: ENV.save(prefix + 'iteration', value) elif 'H' in label: ENV.save(prefix + 'H_LO', value[0]) ENV.save(prefix + 'H_HI', value[1]) if value[0] > value[1]: ENV.save(prefix + 'invert_hue_selection', 'TRUE') else: ENV.save(prefix + 'invert_hue_selection', 'FALSE') elif 'S' in label: ENV.save(prefix + 'S_LO', value[0]) ENV.save(prefix + 'S_HI', value[1]) elif 'V' in label: ENV.save(prefix + 'V_LO', value[0]) ENV.save(prefix + 'V_HI', value[1]) elif 'total_rotation_angle' in label: ENV.save(prefix + 'total_rotation_angle', value) elif 'image_bot_origin_location' in label: if value == [0, 1]: ENV.save(prefix + 'image_bot_origin_location', 'bottom_left'.upper()) if value == [0, 0]: ENV.save(prefix + 'image_bot_origin_location', 'top_left'.upper()) if value == [1, 1]: ENV.save(prefix + 'image_bot_origin_location', 'bottom_right'.upper()) if value == [1, 0]: ENV.save(prefix + 'image_bot_origin_location', 'top_right'.upper()) elif 'coord_scale' in label: ENV.save(prefix + 'coord_scale', value) elif 'camera_offset' in label: ENV.save(prefix + 'camera_offset_x', value[0]) ENV.save(prefix + 'camera_offset_y', value[1]) elif 'calibration_circle_separation' in label: ENV.save(prefix + 'calibration_object_separation', value) elif 'calibration_circles_xaxis' in label: if value: ENV.save(prefix + 'calibration_along_axis', 'X') else: ENV.save(prefix + 'calibration_along_axis', 'Y') elif 'easy_calibration' in label: if value: ENV.save(prefix + 'easy_calibration', 'TRUE') else: ENV.save(prefix + 'easy_calibration', 'FALSE') elif 'camera_z' in label: ENV.save(prefix + 'camera_z', value) elif 'center_pixel_location' in label: ENV.save(prefix + 'center_pixel_location_x', value[0]) ENV.save(prefix + 'center_pixel_location_y', value[1])
def env_var_converter(self, widget): """Convert environment variable contents to dictionary.""" common_app_var_names = [ 'blur', 'morph', 'iteration', 'H_HI', 'H_LO', 'S_HI', 'S_LO', 'V_HI', 'V_LO'] calibration_names = [ 'total_rotation_angle', 'easy_calibration', 'invert_hue_selection', 'image_bot_origin_location', 'coord_scale', 'camera_offset_y', 'camera_offset_x', 'calibration_object_separation', 'calibration_along_axis', 'camera_z', 'center_pixel_location_x', 'center_pixel_location_y'] options_app_var_names = [ 'WEED_DETECTOR_' + n for n in common_app_var_names] calibration_app_var_names = [ 'CAMERA_CALIBRATION_' + n for n in ( common_app_var_names + calibration_names)] if 'calibration' in widget: app_var_names = calibration_app_var_names input_template = copy.deepcopy(self.cdefaults) else: app_var_names = options_app_var_names input_template = copy.deepcopy(self.defaults) invert_hue_selection = False try: if input_template['invert_hue_selection']: invert_hue_selection = True except KeyError: pass for name in app_var_names: loaded_value = ENV.load(name) if loaded_value is not None: if 'H_LO' in name: input_template['H'][0] = loaded_value elif 'H_HI' in name: input_template['H'][1] = loaded_value elif 'S_LO' in name: input_template['S'][0] = loaded_value elif 'S_HI' in name: input_template['S'][1] = loaded_value elif 'V_LO' in name: input_template['V'][0] = loaded_value elif 'V_HI' in name: input_template['V'][1] = loaded_value elif 'iteration' in name: input_template['iterations'] = loaded_value elif 'calibration_along_axis' in name: input_template['calibration_circles_xaxis'] = bool( 'x' in loaded_value.lower()) elif 'calibration_object_separation' in name: input_template[ 'calibration_circle_separation'] = loaded_value elif 'image_bot_origin_location' in name: if 'bottom_left' in loaded_value.lower(): input_template['image_bot_origin_location'] = [0, 1] if 'top_left' in loaded_value.lower(): input_template['image_bot_origin_location'] = [0, 0] if 'bottom_right' in loaded_value.lower(): input_template['image_bot_origin_location'] = [1, 1] if 'top_right' in loaded_value.lower(): input_template['image_bot_origin_location'] = [1, 0] elif 'camera_offset_x' in name: input_template[ 'camera_offset_coordinates'][0] = loaded_value elif 'camera_offset_y' in name: input_template[ 'camera_offset_coordinates'][1] = loaded_value elif 'center_pixel_location_x' in name: try: input_template['center_pixel_location'][0] except KeyError: input_template['center_pixel_location'] = [0, 0] input_template['center_pixel_location'][0] = loaded_value elif 'center_pixel_location_y' in name: try: input_template['center_pixel_location'][0] except KeyError: input_template['center_pixel_location'] = [0, 0] input_template['center_pixel_location'][1] = loaded_value elif 'invert_hue_selection' in name: invert_hue_selection = bool('true' in loaded_value.lower()) elif 'easy_calibration' in name: input_template['easy_calibration'] = bool( 'true' in loaded_value.lower()) else: for cname in calibration_names + common_app_var_names: if cname in name: input_template[cname] = loaded_value if invert_hue_selection: if input_template['H'][0] < input_template['H'][1]: input_template['H'] = input_template['H'][::-1] else: if input_template['H'][0] > input_template['H'][1]: input_template['H'] = input_template['H'][::-1] return input_template
"""Download an image from the Web App and use it to calibrate the camera. download the image corresponding to the ID provided and run calibration """ import os import sys sys.path.insert(1, os.path.join(sys.path[0], '..')) from plant_detection.PlantDetection import PlantDetection from plant_detection import ENV from plant_detection.CeleryPy import log if __name__ == "__main__": IMAGE_ID = ENV.load('CAMERA_CALIBRATION_selected_image', get_json=False) if IMAGE_ID is None: log('No image selected.', message_type='error', title='historical-camera-calibration') sys.exit(0) PD = PlantDetection(coordinates=True, app=True, app_image_id=IMAGE_ID) PD.calibrate()
#!/usr/bin/env python """Plant Detection Image Capture. For Plant Detection. """ import sys import os from time import time, sleep from subprocess import call import cv2 from plant_detection import ENV from plant_detection.Log import log CAMERA = (ENV.load('camera', get_json=False) or 'USB').upper() class Capture(object): """Capture image for Plant Detection.""" def __init__(self, directory=None): """Set initial attributes.""" self.image = None self.ret = None self.camera_port = None self.image_captured = False self.silent = False self.directory = directory def camera_check(self): """Check for camera at ports 0 and 1.""" if not os.path.exists('/dev/video' + str(self.camera_port)):
"""Download an image from the Web App and detect coordinates. download the image corresponding to the ID provided and run plant detection and coordinate conversion """ import os import sys sys.path.insert(1, os.path.join(sys.path[0], '..')) from plant_detection.PlantDetection import PlantDetection from plant_detection import ENV from plant_detection.CeleryPy import log if __name__ == "__main__": IMAGE_ID = ENV.load('PLANT_DETECTION_selected_image', get_json=False) if IMAGE_ID is None: log('No image selected.', message_type='error', title='historical-plant-detection') sys.exit(0) PD = PlantDetection(coordinates=True, app=True, app_image_id=IMAGE_ID) PD.detect_plants()
#!/usr/bin/env python """Plant Detection Image Capture. For Plant Detection. """ import sys import os from time import time, sleep from subprocess import call import cv2 from plant_detection import ENV from plant_detection.Log import log CAMERA = ENV.load('camera', get_json=False) if CAMERA is None: CAMERA = 'USB' # default camera else: if 'RPI' in CAMERA: CAMERA = 'RPI' # Raspberry Pi Camera else: CAMERA = 'USB' class Capture(object): """Capture image for Plant Detection.""" def __init__(self): """Set initial attributes.""" self.image = None self.ret = None self.camera_port = None self.image_captured = False
def test_bad_json_os_env_load(self): """Try to get bad json env from os""" os.environ['testbadjson'] = '{"label": "whoop' self.assertEqual(ENV.load_env('testbadjson'), None)
def test_none_os_env_load(self): """Try to get a non-existent env from os""" self.assertEqual(ENV.load_env('doesntexist'), None)
def test_os_env_load(self): """Try to get an env from os""" os.environ['oktestenv'] = 'test' self.assertEqual(ENV.load_env('oktestenv', get_json=False), 'test')
def test_none_user_env_load(self): """Try to get a non-existent user_env from redis""" self.assertEqual( ENV.redis_load('user_env', name='doesntexist', other_redis=self.r), None)
def test_bad_json_env_load(self): """Try to get bad json user_env from redis""" self.r.set('BOT_STATUS.user_env.testdata', self.badjson_string) self.assertEqual( ENV.redis_load('user_env', name='testdata', other_redis=self.r), None)
def test_json_env_load(self): """Get json user_env from redis""" self.r.set('BOT_STATUS.user_env.testdata', json.dumps(self.testjson)) self.assertEqual(ENV.redis_load( 'user_env', name='testdata', other_redis=self.r), self.testjson)