def bb_intersect_union(d, g):
    """Compute intersection and union separately.
    Inspired by
    <https://github.com/cocodataset/cocoapi/blob/636becdc73d54283b3aac6d4ec363cffbb6f9b20/common/maskApi.c#L109>

    >>> bb_intersect_union([0, 0, 20, 20], [0, 0, 20, 20], False)
    (400, 400)
    >>> bb_intersect_union([0, 0, 20, 20], [0, 0, 10, 10], False)
    (100, 400)
    >>> bb_intersect_union([0, 0, 20, 20], [0, 0, 10, 10], True)
    (100, 400)
    >>> bb_intersect_union([0, 0, 20, 20], [0, 0, 30, 30], True)
    (400, 400)
    >>> bb_intersect_union([10, 20, 10, 10], [10, 20, 5, 5], True)
    (25, 100)
    """
    dx, dy, dw, dh = d
    gx, gy, gw, gh = g

    detection_area = dw * dh
    groundtruth_area = gw * gh

    intersection_left = max(dx, gx)
    intersection_right = min(dx + dw, gx + gw)
    intersection_top = max(dy, gy)
    intersection_bottom = min(dy + dh, gy + gh)

    w = max(intersection_right - intersection_left, 0)
    h = max(intersection_bottom - intersection_top, 0)

    intersect = w * h
    union = detection_area + groundtruth_area - intersect
    return intersect, union