def debug_draw_plants_in_images_subset(debug_geo_images, possible_plants, actual_plants, image_out_directory): debug_images = [] DebugImage = namedtuple('DebugImage', 'image existed') for geo_image in debug_geo_images: if hasattr(geo_image, 'debug_filepath'): path = geo_image.debug_filepath already_existed = True else: path = geo_image.file_path already_existed = False img = cv2.imread(path, cv2.CV_LOAD_IMAGE_COLOR) if img is None: print "Could not open image {}".format(path) continue debug_images.append(DebugImage(img, already_existed)) for item in possible_plants: from random import randint item_color = (randint(50, 255), randint(50, 100), randint(50, 255)) for k, geo_image in enumerate(debug_geo_images): for ext_item in [item] + item.get('items',[]): image_rect = rect_to_image(ext_item['rect'], geo_image) center, size, theta = image_rect x, y = center if x >= 0 and x < geo_image.width and y >= 0 and y < geo_image.height: draw_rect(debug_images[k].image, image_rect, item_color, thickness=2) for plant in actual_plants: for ref in plant.all_refs: if not ref.parent_image_filename: continue geo_image_filenames = [geo_image.file_name for geo_image in debug_geo_images] try: debug_img_index = geo_image_filenames.index(ref.parent_image_filename) except ValueError: continue debug_img = debug_images[debug_img_index].image if ref.type == 'CreatedPlant': draw_rect(debug_img, ref.bounding_rect, (255, 255, 255), thickness=6) else: draw_rect(debug_img, ref.bounding_rect, (0, 255, 0), thickness=5) debug_filepaths = [os.path.join(image_out_directory, postfix_filename(geo_image.file_name, 'marked')) for geo_image in debug_geo_images] for k, (debug_image, filepath) in enumerate(zip(debug_images, debug_filepaths)): if not debug_image.existed: output_debug_image = debug_image.image # KLM - don't make image smaller or bounding rect coordinates won't be right next time try to draw on image. #output_debug_image = cv2.resize(debug_image.image, (0,0), fx=0.25, fy=0.25) else: output_debug_image = debug_image.image cv2.imwrite(filepath, output_debug_image) debug_geo_images[k].debug_filepath = filepath
def extract_global_plants_from_images(plants, geo_images, out_directory): for plant in plants: global_bounding_rect = plant.bounding_rect if global_bounding_rect is None: continue found_in_image = False for k, geo_image in enumerate(geo_images): from src.util.clustering import rect_to_image image_rect = rect_to_image(global_bounding_rect, geo_image) x, y = image_rect[0] if x > 0 and x < geo_image.width and y > 0 and y < geo_image.height: found_in_image = True if not plant.parent_image_filename.strip(): plant.bounding_rect = image_rect plant.parent_image_filename = geo_image.file_name else: plant_copy = plant.__class__('plant', position=plant.position, zone=plant.zone) plant_copy.bounding_rect = image_rect plant_copy.parent_image_filename = geo_image.file_name plant.add_other_item(plant_copy) if out_directory is None: continue image_out_directory = os.path.join(out_directory, os.path.splitext(geo_image.file_name)[0]) ImageWriter.output_directory = image_out_directory img = cv2.imread(geo_image.file_path, cv2.CV_LOAD_IMAGE_COLOR) if img is not None: plant_img = extract_square_image(img, image_rect, 200) plant_image_fname = postfix_filename(geo_image.file_name, "_{}".format(plant.type)) plant.image_path = ImageWriter.save_normal(plant_image_fname, plant_img) if not found_in_image: # Can't convert global rect to a rotated image rect so remove it to be consistent. plant.bounding_rect = None