FaceRect   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 36
ccs 20
cts 21
cp 0.9524
rs 10
c 2
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A overlapPercent() 0 34 5
1
<?php
2
/**
3
 * @copyright Copyright (c) 2020, Matias De lellis <[email protected]>
4
 *
5
 * @author Matias De lellis <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\FaceRecognition\Helper;
25
26
class FaceRect {
27
28 1
	public static function overlapPercent(array $rectA, array $rectB): float {
29
		// Firts face rect
30 1
		$leftA = $rectA['left'];
31 1
		$rightA = $rectA['right'];
32 1
		$topA = $rectA['top'];
33 1
		$bottomA = $rectA['bottom'];
34
35
		// Face rect to compare
36 1
		$leftB = $rectB['left'];
37 1
		$rightB = $rectB['right'];
38 1
		$topB = $rectB['top'];
39 1
		$bottomB = $rectB['bottom'];
40
41
		// If one rectangle is on left side of other
42 1
		if ($leftA >= $rightB || $leftB >= $rightA)
43 1
			return 0.0;
44
45
		// If one rectangle is above other
46 1
		if ($topA >= $bottomB || $topB >= $bottomA)
47
			return 0.0;
48
49
		// Overlap area.
50 1
		$leftO = max($leftA, $leftB);
51 1
		$rightO = min($rightA, $rightB);
52 1
		$topO = max($topA, $topB);
53 1
		$bottomO = min($bottomA, $bottomB);
54
55
		// Calculate the areas of all the rectangles
56 1
		$areaA = ($rightA - $leftA) * ($bottomA - $topA);
57 1
		$areaB = ($rightB - $leftB) * ($bottomB - $topB);
58 1
		$overlapArea = ($rightO - $leftO) * ($bottomO - $topO);
59
60
		// Calculate and return the overlay percent.
61 1
		return floatval($overlapArea / ($areaA + $areaB - $overlapArea));
62
	}
63
64
}
65