Skip to content

Commit

Permalink
Fixing calib test path for ci + deprecation update
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurelien Besnier committed Jan 22, 2024
1 parent bc2abd9 commit f29d543
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
57 changes: 34 additions & 23 deletions src/openalea/phenomenal/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,30 @@
# ==============================================================================
from __future__ import division, print_function, absolute_import

import importlib
import json
import numpy
import cv2
import glob
import os
import collections
import pkg_resources
import pathlib


from openalea.phenomenal.mesh import read_ply_to_vertices_faces
from openalea.phenomenal.calibration import (Chessboard, Chessboards, Calibration, CalibrationSetup,
from openalea.phenomenal.calibration import (Chessboard, Chessboards,
Calibration, CalibrationSetup,
OldCalibrationCamera)
from openalea.phenomenal.object import VoxelGrid

# ==============================================================================

datadir = os.path.dirname(__file__).split('src')[0]


def data_dir(name_dir, dtype='bin'):
return os.path.join(datadir, 'examples', name_dir, '{}/'.format(dtype))


def _path_images(name_dir, dtype="bin"):
""" According to the plant number return a dict[id_camera][angle] containing
filename of file.
Expand All @@ -44,11 +47,12 @@ def _path_images(name_dir, dtype="bin"):
d : dict of dict of string
dict[id_camera][angle] = filename
"""
data_directory = os.path.join(datadir, 'examples', name_dir, '{}/'.format(dtype))
data_directory = os.path.join(datadir, 'examples', name_dir,
'{}/'.format(dtype))

d = collections.defaultdict(dict)
for id_camera in ["side", "top"]:
filenames = glob.glob(os.path.join(data_directory, id_camera, '*'))
filenames = glob.glob(os.path.join(str(data_directory), id_camera, '*'))
for filename in filenames:
angle = int(pathlib.Path(filename).stem)
d[id_camera][angle] = filename
Expand Down Expand Up @@ -99,8 +103,8 @@ def raw_images(name_dir):
d = path_raw_images(name_dir)
for id_camera in d:
for angle in d[id_camera]:
img = cv2.imread(d[id_camera][angle], cv2.IMREAD_COLOR)
d[id_camera][angle] = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.imread(d[id_camera][angle], cv2.IMREAD_COLOR)
d[id_camera][angle] = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
return d


Expand All @@ -121,7 +125,6 @@ def bin_images(name_dir):
return d



def chessboard_images(name_dir):
"""
According to the plant number return a dict[id_camera][angle] of
Expand All @@ -138,6 +141,7 @@ def chessboard_images(name_dir):
d[id_camera][angle] = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
return d,


# ==============================================================================


Expand All @@ -148,12 +152,13 @@ def chessboards(name_dir):
:return: dict[id_camera] of camera calibration object
"""
data_directory = os.path.join(datadir, 'examples', name_dir, 'chessboard/points/')
data_directory = os.path.join(datadir, 'examples', name_dir,
'chessboard/points/')

chessboards = list()
for id_chessboard in [1, 2]:
chessboards.append(Chessboard.load(
os.path.join(data_directory,
os.path.join(str(data_directory),
"chessboard_{}.json".format(id_chessboard))))

return chessboards
Expand All @@ -166,16 +171,17 @@ def image_points(name_dir):
:return: dict[id_camera] of camera calibration object
"""
data_directory = os.path.join(datadir, 'examples', name_dir, 'chessboard/points/')
data_directory = os.path.join(datadir, 'examples', name_dir,
'chessboard/points/')

chessboards = {}
keep = [42] + list(range(0,360,30))
keep = [42] + list(range(0, 360, 30))
for id_chessboard in ['target_1', 'target_2']:
chessboard = Chessboard.load(
os.path.join(data_directory,
os.path.join(str(data_directory),
"image_points_{}.json".format(id_chessboard)))
for rotation in list(chessboard.image_points['side']):
if not rotation in keep:
if rotation not in keep:
chessboard.image_points['side'].pop(rotation)
chessboards[id_chessboard] = chessboard

Expand Down Expand Up @@ -207,17 +213,19 @@ def do_calibration(name_dir):
# distance and inclination of objects
cameras_guess = {'side': (5500, 90), 'top': (2500, 0)}
targets_guess = {'target_1': (100, 45), 'target_2': (100, 45)}
setup = CalibrationSetup(cameras_guess, targets_guess, image_resolutions, image_sizes, facings,
setup = CalibrationSetup(cameras_guess, targets_guess, image_resolutions,
image_sizes, facings,
clockwise_rotation=True)
cameras, targets = setup.setup_calibration(reference_camera='side', reference_target='target_1')
cameras, targets = setup.setup_calibration(reference_camera='side',
reference_target='target_1')
calibration = Calibration(targets=targets, cameras=cameras,
target_points=target_points, image_points=image_points,
target_points=target_points,
image_points=image_points,
reference_camera='side', clockwise_rotation=True)
calibration.calibrate()
calibration.dump(os.path.join(data_directory, 'calibration_cameras.json'))



def calibrations(name_dir):
"""
According to name_dir return a dict[id_camera] of camera
Expand All @@ -236,18 +244,19 @@ def calibrations(name_dir):

return calibration


def new_calibrations(name_dir):
"""
According to name_dir return a camera
calibration object
"""

file_name = os.path.join(name_dir, 'calibration', 'calibration_cameras.json')
file_name = os.path.join(name_dir, 'calibration',
'calibration_cameras.json')
return Calibration.load(file_name)



def voxel_grid(name_dir, plant_number=1, voxels_size=4):
"""
According to the plant number and the voxel size desired return the
Expand All @@ -264,6 +273,7 @@ def voxel_grid(name_dir, plant_number=1, voxels_size=4):

return vg


# ==============================================================================


Expand All @@ -276,18 +286,18 @@ def tutorial_data_binarization_mask():
:return: list of image
"""

data_directory = pkg_resources.resource_filename(
data_directory = importlib.resources(
'openalea.phenomenal', 'data/plant_6/mask/')

masks = list()
for filename in ["mask_hsv.png",
"mask_mean_shift.png"]:

masks.append(cv2.imread(os.path.join(data_directory, filename),
flags=cv2.IMREAD_GRAYSCALE))

return masks


# ==============================================================================


Expand Down Expand Up @@ -317,6 +327,7 @@ def synthetic_plant(name_dir, registration_point=(0, 0, 750)):

return vertices, faces, meta_data


# ==============================================================================


Expand All @@ -328,4 +339,4 @@ def mesh_mccormik_plant(name_dir):

vertices, faces, colors = read_ply_to_vertices_faces(filename)

return vertices, faces, colors
return vertices, faces, colors
4 changes: 2 additions & 2 deletions src/openalea/phenomenal/segmentation/maize_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,14 @@ def organ_analysis(organ, polyline, closest_nodes, stem_vector_mean=None):
organ.info['pm_width_max'] = max(width)
organ.info['pm_width_mean'] = sum(width) / float(len(width))
organ.info['pm_surface'] = scipy.integrate.simpson(
width, curvilinear_abscissa)
y= width, x=curvilinear_abscissa)

fitted_width = compute_fitted_width(width, normalized_curvilinear_abscissa)
organ.info['pm_fitted_width_max'] = max(fitted_width)
organ.info['pm_fitted_width_mean'] = (sum(fitted_width) / float(len(
fitted_width)))
organ.info['pm_fitted_surface'] = scipy.integrate.simpson(
fitted_width, curvilinear_abscissa)
y=fitted_width, x=curvilinear_abscissa)
# Compute azimuth
azimuth_angle, vector_mean = compute_azimuth_angle(polyline)
organ.info['pm_vector_mean'] = vector_mean
Expand Down
9 changes: 7 additions & 2 deletions test/test_calibration/test_calibration_tutorial.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""Test computation used in calibration tutorial (examples/calibration)"""
import os

import openalea.phenomenal.data as phm_data
import openalea.phenomenal.calibration as phm_calib


def test_detect_corners():
chessboard_images = phm_data.chessboard_images("data/plant_1")[0]
path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
"../data/plant_1")
chessboard_images = phm_data.chessboard_images(path)[0]
square_size_of_chessboard = 47 # In mm
square_shape_of_chessboard = (8, 6) # (8 square x 6 square on chessboard)
chessboard = phm_calib.Chessboard(square_size_of_chessboard,
Expand All @@ -21,7 +24,9 @@ def test_detect_corners():


def test_calibrate():
image_points = phm_data.image_points("data/plant_1")
path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
"../data/plant_1")
image_points = phm_data.image_points(path)
calibrator = phm_calib.Calibrator(south_camera=('side', 90, 5500),
targets={'target_1': (45, 48),
'target_2': (45, 48 + 180)},
Expand Down

0 comments on commit f29d543

Please sign in to comment.