Skip to content

Refine benchmark script logic #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions calibration_boards/objdetect_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,27 @@
def get_norm(gold_corners, corners, type_dist):
if type_dist is TypeNorm.l1:
return LA.norm((gold_corners - corners).flatten(), ord=1)
if type_dist is TypeNorm.l2 or type_dist is TypeNorm.intersection_over_union:
if type_dist is TypeNorm.l2:
return LA.norm((gold_corners - corners).flatten(), ord=2)
if type_dist is TypeNorm.intersection_over_union:
poly1 = gold_corners.astype(np.float32)
poly2 = corners.astype(np.float32)
ret, inter_poly = cv.intersectConvexConvex(poly1, poly2)
area_inter = ret if isinstance(ret, float) else cv.contourArea(inter_poly)
area1 = cv.contourArea(poly1)
area2 = cv.contourArea(poly2)
union = area1 + area2 - area_inter
return (area_inter / union) if union > 0 else 0.0
if type_dist is TypeNorm.l_inf:
return LA.norm((gold_corners - corners).flatten(), ord=np.inf)
raise TypeError("this TypeNorm isn't supported")


def get_max_error(accuracy_threshold, type_dist):
if type_dist is TypeNorm.l1 or TypeNorm.l2 or TypeNorm.l3 or TypeNorm.l_inf:
return accuracy_threshold
if type_dist in (TypeNorm.l1, TypeNorm.l2, TypeNorm.l_inf):
return accuracy_threshold
if type_dist is TypeNorm.intersection_over_union:
return 1
return 1.0
raise TypeError("this TypeNorm isn't supported")


Expand All @@ -74,18 +83,10 @@ def get_synthetic_rt(yaw, pitch, distance):
return rvec, tvec


def get_coord(num_rows, num_cols, start_x=0, start_y=0):
i, j = np.ogrid[:num_rows, :num_cols]
v = np.empty((num_rows, num_cols, 2), dtype=np.float32)
v[..., 0] = j + start_y
v[..., 1] = i + start_x
v.shape = (1, -1, 2)
return v


class TransformObject:
def __init__(self):
self.name = "none"
self.name = ""

def transform_image(self, image):
return image
Expand Down Expand Up @@ -124,6 +125,7 @@ def __init__(self, *, img_size, yaw, pitch, distance=1.0, name="perspective"):
obj_points[:, -1:] = 0.

corners, _ = cv.projectPoints(obj_points, rvec, tvec, camera_matrix, np.zeros((5, 1), dtype=np.float64))
corners = corners.reshape(-1, 2).astype(np.float32)
self.transformation = cv.getPerspectiveTransform(original_corners, corners)

def transform_image(self, image):
Expand Down Expand Up @@ -825,7 +827,7 @@ def main():
parser.add_argument("--board_y", help="input board y size", default="6", action="store", dest="board_y", type=int)
parser.add_argument("--rel_center_x", help="the relative x-axis location of the center of the board in the image",
default=".5", action="store", dest="rel_center_x", type=float)
parser.add_argument("--rel_center_y", help="the relative x-axis location of the center of the board in the image",
parser.add_argument("--rel_center_y", help="the relative y-axis location of the center of the board in the image",
default=".5", action="store", dest="rel_center_y", type=float)
parser.add_argument("--metric", help="Metric for distance between result and gold", default="l_inf", action="store",
dest="metric", choices=['l1', 'l2', 'l_inf', 'intersection_over_union'], type=str)
Expand Down Expand Up @@ -886,12 +888,13 @@ def main():

folder_name = "report_" + get_time()
configuration = args.configuration
if configuration == "generate" or configuration == "generate_run":
if configuration in ("generate", "generate_run"):
generate_dataset(args, synthetic_object)
if configuration == "generate":
return
elif configuration == "show":
distances1 = read_distances(dataset_path+'/'+args.d1)
file = args.d1 if args.d1.endswith('.json') else args.d1 + '.json'
distances1 = read_distances(os.path.join(dataset_path, file))
distances3 = distances1
if args.d2 != '':
distances2 = read_distances(dataset_path + '/' + args.d2)
Expand Down