|
1
|
|
|
<?php |
|
2
|
|
|
namespace OCA\FaceRecognition\Controller; |
|
3
|
|
|
|
|
4
|
|
|
use OCP\Image as OCP_Image; |
|
5
|
|
|
|
|
6
|
|
|
use OCP\IRequest; |
|
7
|
|
|
use OCP\Files\IRootFolder; |
|
8
|
|
|
use OCP\AppFramework\Http; |
|
9
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
10
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
11
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse; |
|
12
|
|
|
use OCP\AppFramework\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use OCA\FaceRecognition\Db\Face; |
|
15
|
|
|
use OCA\FaceRecognition\Db\FaceMapper; |
|
16
|
|
|
|
|
17
|
|
|
use OCA\FaceRecognition\Db\Image; |
|
18
|
|
|
use OCA\FaceRecognition\Db\ImageMapper; |
|
19
|
|
|
|
|
20
|
|
|
class FaceController extends Controller { |
|
21
|
|
|
|
|
22
|
|
|
private $rootFolder; |
|
23
|
|
|
private $faceMapper; |
|
24
|
|
|
private $imageMapper; |
|
25
|
|
|
private $userId; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct($AppName, |
|
28
|
|
|
IRequest $request, |
|
29
|
|
|
IRootFolder $rootFolder, |
|
30
|
|
|
FaceMapper $faceMapper, |
|
31
|
|
|
ImageMapper $imageMapper, |
|
32
|
|
|
$UserId) |
|
33
|
|
|
{ |
|
34
|
|
|
parent::__construct($AppName, $request); |
|
35
|
|
|
$this->rootFolder = $rootFolder; |
|
36
|
|
|
$this->faceMapper = $faceMapper; |
|
37
|
|
|
$this->imageMapper = $imageMapper; |
|
38
|
|
|
$this->userId = $UserId; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @NoAdminRequired |
|
43
|
|
|
* @NoCSRFRequired |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getThumb ($id, $size) { |
|
46
|
|
|
$face = $this->faceMapper->find($id); |
|
47
|
|
|
$image = $this->imageMapper->find($this->userId, $face->getImage()); |
|
|
|
|
|
|
48
|
|
|
$fileId = $image->getFile(); |
|
49
|
|
|
|
|
50
|
|
|
$userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
51
|
|
|
$nodes = $userFolder->getById($fileId); |
|
52
|
|
|
$file = $nodes[0]; |
|
53
|
|
|
|
|
54
|
|
|
$ownerView = new \OC\Files\View('/'. $this->userId . '/files'); |
|
|
|
|
|
|
55
|
|
|
$path = $userFolder->getRelativePath($file->getPath()); |
|
56
|
|
|
|
|
57
|
|
|
$img = new OCP_Image(); |
|
58
|
|
|
$fileName = $ownerView->getLocalFile($path); |
|
59
|
|
|
$img->loadFromFile($fileName); |
|
60
|
|
|
$img->fixOrientation(); |
|
61
|
|
|
|
|
62
|
|
|
$x = $face->getLeft (); |
|
63
|
|
|
$y = $face->getTop (); |
|
64
|
|
|
$w = $face->getRight () - $x; |
|
65
|
|
|
$h = $face->getBottom () - $y; |
|
66
|
|
|
|
|
67
|
|
|
$padding = $h*0.25; |
|
68
|
|
|
$x -= $padding; |
|
69
|
|
|
$y -= $padding; |
|
70
|
|
|
$w += $padding*2; |
|
71
|
|
|
$h += $padding*2; |
|
72
|
|
|
|
|
73
|
|
|
if (true) { |
|
74
|
|
|
$this->hipsterize($img, $face); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$img->crop($x, $y, $w, $h); |
|
78
|
|
|
$img->scaleDownToFit($size, $size); |
|
79
|
|
|
|
|
80
|
|
|
$resp = new DataDisplayResponse($img->data(), Http::STATUS_OK, ['Content-Type' => $img->mimeType()]); |
|
81
|
|
|
$resp->setETag((string)crc32($img->data())); |
|
82
|
|
|
$resp->cacheFor(7 * 24 * 60 * 60); |
|
83
|
|
|
$resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT'))); |
|
84
|
|
|
|
|
85
|
|
|
return $resp; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** @scrutinizer ignore-unused */ |
|
89
|
|
|
private function hipsterize(&$image, &$face) { |
|
90
|
|
|
$imgResource = $image->resource(); |
|
91
|
|
|
|
|
92
|
|
|
$landmarks = json_decode($face->getLandmarks(), true); |
|
93
|
|
|
if (count($landmarks) === 5) { |
|
94
|
|
|
$eyesX1 = $landmarks[2]['x']; |
|
95
|
|
|
$eyesY1 = $landmarks[2]['y']; |
|
96
|
|
|
|
|
97
|
|
|
$eyesX2 = $landmarks[0]['x']; |
|
98
|
|
|
$eyesY2 = $landmarks[0]['y']; |
|
99
|
|
|
|
|
100
|
|
|
$eyesXC = ($eyesX2 + $eyesX1)/2; |
|
101
|
|
|
$eyesYC = ($eyesY2 + $eyesY1)/2; |
|
102
|
|
|
|
|
103
|
|
|
$mustacheXC = $landmarks[4]['x']; |
|
104
|
|
|
$mustacheYC = $landmarks[4]['y']; |
|
105
|
|
|
} |
|
106
|
|
|
else if (count($landmarks) === 68) { |
|
107
|
|
|
$eyesX1 = $landmarks[36]['x']; |
|
108
|
|
|
$eyesY1 = $landmarks[36]['y']; |
|
109
|
|
|
$eyesX2 = $landmarks[45]['x']; |
|
110
|
|
|
$eyesY2 = $landmarks[45]['y']; |
|
111
|
|
|
|
|
112
|
|
|
$eyesXC = ($eyesX2 + $eyesX1)/2; |
|
113
|
|
|
$eyesYC = ($eyesY2 + $eyesY1)/2; |
|
114
|
|
|
|
|
115
|
|
|
$mustacheXC = $landmarks[52]['x']; |
|
116
|
|
|
$mustacheYC = $landmarks[52]['y']; |
|
117
|
|
|
} |
|
118
|
|
|
else { |
|
119
|
|
|
return; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$eyesW = $eyesX2 - $eyesX1; |
|
123
|
|
|
$eyesH = $eyesY2 - $eyesY1; |
|
124
|
|
|
|
|
125
|
|
|
$eyesL = sqrt(pow($eyesW, 2) + pow($eyesH, 2)); |
|
126
|
|
|
$angle = rad2deg(atan(-$eyesH/$eyesW)); |
|
127
|
|
|
|
|
128
|
|
|
$glassesGd = imagecreatefrompng(\OC_App::getAppPath('facerecognition') . '/img/glasses.png'); |
|
|
|
|
|
|
129
|
|
|
if ($glassesGd === false) |
|
130
|
|
|
return; |
|
131
|
|
|
$fillColor = imagecolorallocatealpha($glassesGd, 0, 0, 0, 127); |
|
132
|
|
|
$glassesGd = imagerotate($glassesGd, $angle, $fillColor); |
|
133
|
|
|
|
|
134
|
|
|
$glassesW = imagesx($glassesGd); |
|
|
|
|
|
|
135
|
|
|
$glassesH = imagesy($glassesGd); |
|
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
$glassesRatio = $eyesL/$glassesW*1.5; |
|
138
|
|
|
|
|
139
|
|
|
$glassesDestX = intval($eyesXC - $glassesW * $glassesRatio / 2); |
|
140
|
|
|
$glassesDestY = intval($eyesYC - $glassesH * $glassesRatio / 2); |
|
141
|
|
|
$glassesDestW = intval($glassesW * $glassesRatio); |
|
142
|
|
|
$glassesDestH = intval($glassesH * $glassesRatio); |
|
143
|
|
|
|
|
144
|
|
|
imagecopyresized($imgResource, $glassesGd, $glassesDestX, $glassesDestY, 0, 0, $glassesDestW, $glassesDestH, $glassesW, $glassesH); |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
$mustacheGd = imagecreatefrompng(\OC_App::getAppPath('facerecognition') . '/img/mustache.png'); |
|
147
|
|
|
if ($mustacheGd === false) |
|
148
|
|
|
return; |
|
149
|
|
|
$fillColor = imagecolorallocatealpha($mustacheGd, 0, 0, 0, 127); |
|
150
|
|
|
$mustacheGd = imagerotate($mustacheGd, $angle, $fillColor); |
|
151
|
|
|
$mustacheW = imagesx($mustacheGd); |
|
152
|
|
|
$mustacheH = imagesy($mustacheGd); |
|
153
|
|
|
|
|
154
|
|
|
$mustacheRatio = $eyesL/$glassesW*1.1; |
|
155
|
|
|
|
|
156
|
|
|
$mustacheDestX = intval($mustacheXC - $mustacheW * $mustacheRatio / 2); |
|
157
|
|
|
$mustacheDestY = intval($mustacheYC - $mustacheH * $mustacheRatio / 2); |
|
158
|
|
|
$mustacheDestW = intval($mustacheW * $mustacheRatio); |
|
159
|
|
|
$mustacheDestH = intval($mustacheH * $mustacheRatio); |
|
160
|
|
|
|
|
161
|
|
|
imagecopyresized($imgResource, $mustacheGd, $mustacheDestX, $mustacheDestY, 0, 0, $mustacheDestW, $mustacheDestH, $mustacheW, $mustacheH); |
|
162
|
|
|
|
|
163
|
|
|
$image->setResource($imgResource); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|