Пример #1
0
class PDTestJSONinput(unittest.TestCase):
    """Test ENV VAR inputs"""
    def setUp(self):
        os.environ.clear()
        self.pd = PlantDetection(image='soil_image.jpg',
                                 from_env_var=True,
                                 text_output=False,
                                 save=False)
        self.json_params = {
            "blur": 15,
            "morph": 6,
            "iterations": 4,
            "H": [30, 90],
            "S": [20, 255],
            "V": [20, 255]
        }
        os.environ["PLANT_DETECTION_options"] = json.dumps(self.json_params)
        self.pd.detect_plants()

    def test_json_parameters_input(self):
        """Load JSON input parameters from ENV VAR"""
        self.assertEqual(self.pd.params.parameters, self.json_params)

    def tearDown(self):
        os.environ.clear()
Пример #2
0
 def test_verbose_text_output_no_coordinates(self):
     """Test verbpse text output without coordinate conversion"""
     pd = PlantDetection(image="soil_image.jpg",
                         save=False,
                         print_all_json=True)
     pd.detect_plants()
     check_file_length(self, 71)
Пример #3
0
 def setUp(self):
     pd = PlantDetection(image="soil_image.jpg",
                         text_output=False,
                         save=False,
                         grey_out=True)
     pd.detect_plants()
     self.pixel_mean = get_average_pixel_value(pd.image.images['current'])
     self.expected_pixel_mean = 138.7
Пример #4
0
 def test_debug_no_coordinates(self):
     """Test debug mode without coordinate conversion"""
     pd = PlantDetection(image="soil_image.jpg",
                         text_output=False,
                         save=False,
                         debug=True)
     pd.detect_plants()
     self.assertTrue(os.path.exists('soil_image_masked_original.jpg'))
Пример #5
0
 def setUp(self):
     pd = PlantDetection(image="soil_image.jpg",
                         text_output=False,
                         save=False,
                         draw_contours=True,
                         circle_plants=False)
     pd.detect_plants()
     self.pixel_mean = get_average_pixel_value(pd.image.images['current'])
     self.expected_pixel_mean = 72.9
Пример #6
0
 def setUp(self):
     """Test clump buster"""
     self.pd = PlantDetection(image="soil_image.jpg",
                              morph=10,
                              text_output=False,
                              save=False,
                              clump_buster=True)
     self.pd.detect_plants()
     self.object_count = 39
Пример #7
0
 def test_set_inputs(self):
     """Set input environment variable"""
     os.environ["PLANT_DETECTION_options"] = json.dumps(self.input_params)
     os.environ["DB"] = json.dumps(self.input_plants)
     pd = PlantDetection(image="soil_image.jpg",
                         from_env_var=True,
                         text_output=False,
                         save=False)
     pd.detect_plants()
     self.assertEqual(pd.params.parameters, self.input_params)
Пример #8
0
class TestFromFile(unittest.TestCase):
    """Test file use"""
    def setUp(self):
        self.outfile = open('text_output_test.txt', 'w')
        sys.stdout = self.outfile
        # Generate and save calibration data
        self.pd = PlantDetection(calibration_img="PD/p2c_test_calibration.jpg",
                                 text_output=False,
                                 save=False)
        self.pd.calibrate()
        self.pd.p2c.save_calibration_parameters()
        # Expected number of objects detected
        self.object_count = 16

    def test_detect_coordinates(self):
        """Detect coordinates, getting calibration parameters from file"""
        # Set input parameters for detection
        pdx = PlantDetection()
        pdx.params.parameters = {
            'blur': 15,
            'morph': 6,
            'iterations': 4,
            'H': [30, 90],
            'S': [20, 255],
            'V': [20, 255]
        }
        pdx.params.save()
        # Load the set parameters
        pd = PlantDetection(image="soil_image.jpg",
                            from_file=True,
                            coordinates=True,
                            text_output=False,
                            save=False)
        pd.detect_plants()
        self.assertEqual(pd.plant_db.object_count, self.object_count)

    def test_calibration(self):
        """Load calibration input from file"""
        # Set input parameters for calibration
        pdx = PlantDetection()
        pdx.params.parameters['H'] = [160, 20]
        pdx.params.save()
        # Load the set parameters
        pd = PlantDetection(calibration_img="PD/p2c_test_calibration.jpg",
                            from_file=True,
                            text_output=False,
                            save=False,
                            debug=True)
        pd.calibrate()
        self.assertEqual(pd.plant_db.object_count, 2)

    def tearDown(self):
        self.outfile.close()
        sys.stdout = sys.__stdout__
        os.remove('text_output_test.txt')
Пример #9
0
 def setUp(self):
     self.outfile = open('text_output_test.txt', 'w')
     sys.stdout = self.outfile
     # Generate and save calibration data
     self.pd = PlantDetection(calibration_img="PD/p2c_test_calibration.jpg",
                              text_output=False,
                              save=False)
     self.pd.calibrate()
     self.pd.p2c.save_calibration_parameters()
     # Expected number of objects detected
     self.object_count = 16
Пример #10
0
 def setUp(self):
     self.parameters = {
         'blur': 5,
         'morph': 5,
         'iterations': 1,
         'H': [30, 90],
         'S': [20, 255],
         'V': [20, 255]
     }
     self.pd = PlantDetection(image='soil_image.jpg',
                              text_output=False,
                              save=False)
     self.pd.detect_plants()
Пример #11
0
 def setUp(self):
     self.pd = PlantDetection(image="soil_image.jpg",
                              calibration_img="PD/p2c_test_calibration.jpg",
                              text_output=False,
                              save=False)
     self.pd.calibrate()
     self.input_params = {
         "blur": 15,
         "morph": 6,
         "iterations": 4,
         "H": [30, 90],
         "S": [20, 255],
         "V": [20, 255]
     }
Пример #12
0
 def test_calibration(self):
     """Load calibration input from file"""
     # Set input parameters for calibration
     pdx = PlantDetection()
     pdx.params.parameters['H'] = [160, 20]
     pdx.params.save()
     # Load the set parameters
     pd = PlantDetection(calibration_img="PD/p2c_test_calibration.jpg",
                         from_file=True,
                         text_output=False,
                         save=False,
                         debug=True)
     pd.calibrate()
     self.assertEqual(pd.plant_db.object_count, 2)
Пример #13
0
class PDTestClumpBuster(unittest.TestCase):
    """Test other options"""
    def setUp(self):
        """Test clump buster"""
        self.pd = PlantDetection(image="soil_image.jpg",
                                 morph=10,
                                 text_output=False,
                                 save=False,
                                 clump_buster=True)
        self.pd.detect_plants()
        self.object_count = 39

    def test_clump_buster(self):
        """Test clump buster"""
        self.assertEqual(self.pd.plant_db.object_count, self.object_count)
Пример #14
0
 def test_input_defaults(self):
     """Use defaults"""
     pd = PlantDetection()
     self.assertEqual(pd.args, self.default_func_args)
     self.assertEqual(pd.plant_db.plants['known'], [])
     self.assertEqual(pd.params.parameters, self.default_input_params)
     self.assertEqual(pd.params.array, None)
Пример #15
0
 def setUp(self):
     os.environ.clear()
     self.pd = PlantDetection(image='soil_image.jpg',
                              from_env_var=True,
                              text_output=False,
                              save=False)
     self.json_params = {
         "blur": 15,
         "morph": 6,
         "iterations": 4,
         "H": [30, 90],
         "S": [20, 255],
         "V": [20, 255]
     }
     os.environ["PLANT_DETECTION_options"] = json.dumps(self.json_params)
     self.pd.detect_plants()
Пример #16
0
 def test_input_args(self):
     """Set all arguments"""
     self.maxDiff = None
     pd = PlantDetection(**self.func_args)
     self.assertEqual(pd.args, self.func_args)
     self.assertEqual(pd.plant_db.plants['known'],
                      self.func_args['known_plants'])
     self.assertEqual(pd.params.parameters, self.set_input_params)
     self.assertEqual(pd.params.array, self.func_args['array'])
Пример #17
0
 def test_array_detect_simple(self):
     """Detect plants using simple array input"""
     pd = PlantDetection(image="soil_image.jpg",
                         array=[{
                             "size": 5,
                             "kernel": 'ellipse',
                             "type": 'erode',
                             "iters": 2
                         }, {
                             "size": 3,
                             "kernel": 'ellipse',
                             "type": 'dilate',
                             "iters": 8
                         }],
                         text_output=False,
                         save=False)
     pd.detect_plants()
     object_count = 29
     self.assertEqual(pd.plant_db.object_count, object_count)
Пример #18
0
 def test_array_detect_debug(self):
     """Detect plants using array input and debug"""
     pd = PlantDetection(image="soil_image.jpg",
                         array=[{
                             "size": 5,
                             "kernel": 'ellipse',
                             "type": 'close',
                             "iters": 2
                         }, {
                             "size": 3,
                             "kernel": 'ellipse',
                             "type": 'open',
                             "iters": 8
                         }],
                         text_output=False,
                         save=False,
                         debug=True)
     pd.detect_plants()
     object_count = 25
     self.assertEqual(pd.plant_db.object_count, object_count)
Пример #19
0
class PDTestNoJSONinput(unittest.TestCase):
    """Test defaults"""
    def setUp(self):
        self.parameters = {
            'blur': 5,
            'morph': 5,
            'iterations': 1,
            'H': [30, 90],
            'S': [20, 255],
            'V': [20, 255]
        }
        self.pd = PlantDetection(image='soil_image.jpg',
                                 text_output=False,
                                 save=False)
        self.pd.detect_plants()

    def test_json_parameters_input(self):
        """Do not load JSON input parameters from ENV VAR"""
        self.assertEqual(self.pd.plant_db.plants['known'], [])
        self.assertEqual(self.pd.params.parameters, self.parameters)
Пример #20
0
 def test_detect_coordinates(self):
     """Detect coordinates, getting calibration parameters from file"""
     # Set input parameters for detection
     pdx = PlantDetection()
     pdx.params.parameters = {
         'blur': 15,
         'morph': 6,
         'iterations': 4,
         'H': [30, 90],
         'S': [20, 255],
         'V': [20, 255]
     }
     pdx.params.save()
     # Load the set parameters
     pd = PlantDetection(image="soil_image.jpg",
                         from_file=True,
                         coordinates=True,
                         text_output=False,
                         save=False)
     pd.detect_plants()
     self.assertEqual(pd.plant_db.object_count, self.object_count)
Пример #21
0
 def setUp(self):
     self.pd = PlantDetection(image="PD/p2c_test_objects.jpg",
                              calibration_img="PD/p2c_test_calibration.jpg",
                              HSV_min=[160, 100, 100],
                              HSV_max=[20, 255, 255],
                              morph=15,
                              blur=5,
                              text_output=False,
                              save=False)
     self.pd.calibrate()
     self.calibration_json = {
         "blur": 5,
         "morph": 15,
         "calibration_iters": 3,
         "H": [160, 20],
         "S": [100, 255],
         "V": [100, 255],
         "calibration_circles_xaxis": True,
         "camera_offset_coordinates": [200, 100],
         "image_bot_origin_location": [0, 1],
         "calibration_circle_separation": 1000,
         "total_rotation_angle": 0.0,
         "coord_scale": 1.7182,
         "center_pixel_location": [465, 290]
     }
     self.objects = [{
         'y': 300.69,
         'x': 300.0,
         'radius': 46.86
     }, {
         'y': 599.66,
         'x': 897.94,
         'radius': 46.86
     }, {
         'y': 800.68,
         'x': 98.97,
         'radius': 47.53
     }]
Пример #22
0
 def test_verbose_text_output(self):
     """Test verbpse text output"""
     pd = PlantDetection(image="soil_image.jpg",
                         calibration_img="PD/p2c_test_calibration.jpg",
                         save=False,
                         print_all_json=True)
     pd.calibrate()
     pd.detect_plants()
     check_file_length(self, 80)
Пример #23
0
 def test_debug_with_coordinates(self):
     """Test debug mode with coordinate conversion"""
     pd = PlantDetection(image="soil_image.jpg",
                         calibration_img="PD/p2c_test_calibration.jpg",
                         text_output=False,
                         save=False,
                         debug=True)
     pd.calibrate()
     pd.detect_plants()
     self.assertTrue(os.path.exists('soil_image_coordinates_found.jpg'))
     os.remove('soil_image_masked_original.jpg')
Пример #24
0
    def test_calibration_ENV_VAR(self):
        """Use calibration data environment variable"""
        os.environ["PLANT_DETECTION_options"] = json.dumps(
            self.calibration_input_params)
        os.environ["DB"] = json.dumps(self.input_plants)
        self.pd = PlantDetection(calibration_img="PD/p2c_test_calibration.jpg",
                                 from_env_var=True,
                                 text_output=False,
                                 save=False)
        self.pd.calibrate()
        compare_calibration_results(self)

        os.environ["PLANT_DETECTION_options"] = json.dumps(self.input_params)
        pd = PlantDetection(image="soil_image.jpg",
                            from_env_var=True,
                            coordinates=True,
                            text_output=False,
                            save=False)
        pd.detect_plants()
Пример #25
0
"""Capture and Calibrate commands to load as farmware.

take a photo and run calibration
"""
import os
import sys

sys.path.insert(1, os.path.join(sys.path[0], '..'))

from PlantDetection import PlantDetection

if __name__ == "__main__":
    PD = PlantDetection(coordinates=True, app=True)
    PD.calibrate()
Пример #26
0
mask = cv2.dilate(mask, kernel, iterations=2)
image3 = cv2.bitwise_and(new_image, new_image,
                         mask=mask)  ##aplicamos la máscara
image3 = cv2.medianBlur(image3, 5)
cv2.imwrite('/tmp/images/1549138027.jpg', image3)

PD = PlantDetection(image='/tmp/images/1549138027.jpg',
                    blur=0,
                    morph=2,
                    iterations=1,
                    from_env_var=True,
                    coordinates=True,
                    array=[{
                        "size": 3,
                        "kernel": 'ellipse',
                        "type": 'dilate',
                        "iters": 3
                    }, {
                        "size": 3,
                        "kernel": 'ellipse',
                        "type": 'erode',
                        "iters": 3
                    }],
                    HSV_min=[50, 110, 55],
                    HSV_max=[95, 255, 255])
PD.detect_plants()
radios = []
radios_counter = 0
for cl in PD.plant_db.coordinate_locations:
    if cl[2] > 12.0:
        radios.append(cl[2])
Пример #27
0
new_image = img2
cv2.imwrite('/tmp/images/1549138022.jpg', new_image)
mask = create_mask(new_image, np.array([HL, SL, VL]),
                   np.array([HH, SH, VH]))  ###Creamos la máscara
image3 = cv2.bitwise_and(new_image, new_image,
                         mask=mask)  ##aplicamos la máscara
image3 = cv2.medianBlur(image3, 5)
cv2.imwrite('/tmp/images/1549138027.jpg', image3)

PD = PlantDetection(image='/tmp/images/1549138027.jpg',
                    blur=2,
                    morph=2,
                    iterations=3,
                    from_env_var=True,
                    coordinates=True,
                    array=[{
                        "size": 5,
                        "kernel": 'ellipse',
                        "type": 'erode',
                        "iters": 1
                    }],
                    HSV_min=[49, 95, 50],
                    HSV_max=[110, 255, 255])
PD.detect_plants()  # detect coordinates and sizes of weeds and plants
if len(PD.plant_db.coordinate_locations) >= 1:
    holes = []
    for coordinate_location in PD.plant_db.coordinate_locations:
        if 19 > coordinate_location[2] > 7 and coordinate_location[
                0] < 950 and coordinate_location[1] > 200:
            holes.append([coordinate_location[0], coordinate_location[1]])
    rows, cols = array_shape(holes)
    matrix10 = np.zeros((rows, cols, 2))
Пример #28
0
"""Load (recent image) and Detect commands to load as farmware.

load newest photo in /tmp/images and run calibration
"""

import os
import sys
import glob

sys.path.insert(1, os.path.join(sys.path[0], '..'))

from PlantDetection import PlantDetection

if __name__ == "__main__":
    try:
        RECENT_IMAGE = max(glob.iglob('/tmp/images/*.[Jj][Pp][Gg]'),
                           key=os.path.getctime)
    except ValueError:
        print("No images in /tmp/images")
        sys.exit(0)

    PD = PlantDetection(calibration_img=RECENT_IMAGE, app=True)
    PD.calibrate()
Пример #29
0
"""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 PlantDetection import PlantDetection

if __name__ == "__main__":
    PD = PlantDetection(coordinates=True, app=True)
    PD.detect_plants()
Пример #30
0
"""Load (recent image) and Detect commands to load as farmware.

load newest photo in /tmp/images and run plant detection
and coordinate conversion
"""

import os
import sys
import glob

sys.path.insert(1, os.path.join(sys.path[0], '..'))

from PlantDetection import PlantDetection

if __name__ == "__main__":
    try:
        RECENT_IMAGE = max(glob.iglob('/tmp/images/*.[Jj][Pp][Gg]'),
                           key=os.path.getctime)
    except ValueError:
        print("No images in /tmp/images")
        sys.exit(0)

    PD = PlantDetection(image=RECENT_IMAGE, coordinates=True, app=True)
    PD.detect_plants()