{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import math\n", "def calculate_iou(box1, box2):\n", "\n", " # Calculate the (x1, y1, x2, y2) coordinates of the intersection of box1 and box2. Calculate its Area.\n", " xi1 = max(box1[0], box2[0])\n", " yi1 = max(box1[1], box2[1])\n", " xi2 = min(box1[2], box2[2])\n", " yi2 = min(box1[3], box2[3])\n", " inter_area = max((xi2 - xi1), 0) * max((yi2 - yi1), 0) # Max keeps into account not overlapping box\n", "\n", " # Calculate the Union area by using Formula: Union(A,B) = A + B - Inter(A,B)\n", " box1_area = (box1[2] - box1[0]) * (box1[3] - box1[1])\n", " box2_area = (box2[2] - box2[0]) * (box2[3] - box2[1])\n", " union_area = box1_area + box2_area - inter_area\n", "\n", " # compute the IoU\n", " iou = inter_area / union_area\n", "\n", " return iou" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15.0\n", "[8.450052369622647, 12.393410142113215, 88.45005236962265, 77.39341014211321]\n", "0.4850460596873889\n" ] } ], "source": [ "x1 = 75\n", "y1 = 60\n", "\n", "box1 = [0, 0, x1, y1]\n", "alpha = math.atan2(110,75) # good number\n", "diag = 15\n", "x_cateto = diag * math.cos(alpha)\n", "y_cateto = diag * math.sin(alpha)\n", "print(math.sqrt(x_cateto**2 + y_cateto**2))\n", "box2 = [x_cateto, y_cateto, x1 + x_cateto + 5, y1 + y_cateto+ 5]\n", "print(box2)\n", "print(calculate_iou(box1, box2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }