Passed
Pull Request — master (#313)
by Matias
07:10 queued 05:26
created

FaceRect   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A getOverlayPercent() 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
	public static function getOverlayPercent($rectA, $rectB): float {
29
		// Firts face rect
30
		$leftA = $rectA['left'];
31
		$rightA = $rectA['right'];
32
		$topA = $rectA['top'];
33
		$bottomA = $rectA['bottom'];
34
35
		// Face rect to compare
36
		$leftB = $rectB['left'];
37
		$rightB = $rectB['right'];
38
		$topB = $rectB['top'];
39
		$bottomB = $rectB['bottom'];
40
41
		// If one rectangle is on left side of other
42
		if ($leftA >= $rightB || $leftB >= $rightA)
43
			return 0.0;
44
45
		// If one rectangle is above other
46
		if ($topA >= $bottomB || $topB >= $bottomA)
47
			return 0.0;
48
49
		// Overlap area.
50
		$leftO = max($leftA, $leftB);
51
		$rightO = min($rightA, $rightB);
52
		$topO = max($topA, $topB);
53
		$bottomO = min($bottomA, $bottomB);
54
55
		// Calculate the areas of all the rectangles
56
		$areaA = ($rightA - $leftA) * ($bottomA - $topA);
57
		$areaB = ($rightB - $leftB) * ($bottomB - $topB);
58
		$overlapArea = ($rightO - $leftO) * ($bottomO - $topO);
59
60
		// Calculate and return the overlay percent.
61
		return floatval($overlapArea / ($areaA + $areaB - $overlapArea));
62
	}
63
64
}
65