Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial version of an external Model #389

Merged
merged 7 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
202 changes: 202 additions & 0 deletions lib/Model/ExternalModel/ExternalModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php
/**
* @copyright Copyright (c) 2020, Matias De lellis <mati86dl@gmail.com>
*
* @author Matias De lellis <mati86dl@gmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\FaceRecognition\Model\ExternalModel;

use OCA\FaceRecognition\Service\SettingsService;

use OCA\FaceRecognition\Model\IModel;

class ExternalModel implements IModel {
/*
* Model description
*/
const FACE_MODEL_ID = 5;
const FACE_MODEL_NAME = 'ExternalModel';
const FACE_MODEL_DESC = 'External Model (EXPERIMENTAL)';
const FACE_MODEL_DOC = 'https://github.com/matiasdelellis/facerecognition-external-model#run-service';

/** This model practically does not consume memory. Directly set the limits. */
const MINIMUM_MEMORY_REQUIREMENTS = 128 * 1024 * 1024;

/** @var String model api endpoint */
private $modelUrl = null;

/** @var String model api key */
private $modelApiKey = null;

/** @var String preferred mimetype */
private $preferredMimetype = null;

/** @var int maximun image area */
private $maximumImageArea = -1;

/** @var SettingsService */
private $settingsService;

/**
* ExternalModel __construct.
*
* @param SettingsService $settingsService
*/
public function __construct(SettingsService $settingsService)
{
$this->settingsService = $settingsService;
}

public function getId(): int {
return static::FACE_MODEL_ID;
}

public function getName(): string {
return static::FACE_MODEL_NAME;
}

public function getDescription(): string {
return static::FACE_MODEL_DESC;
}

public function getDocumentation(): string {
return static::FACE_MODEL_DOC;
}

public function isInstalled(): bool {
$this->modelUrl = $this->settingsService->getExternalModelUrl();
$this->modelApiKey = $this->settingsService->getExternalModelApiKey();
return !is_null($this->modelUrl) && !is_null($this->modelApiKey);
}

public function meetDependencies(string &$error_message): bool {
return true;
}

public function getMaximumArea(): int {
if ($this->maximumImageArea < 0) {
$this->open();
}
return $this->maximumImageArea;
}

public function getPreferredMimeType(): string {
if (is_null($this->preferredMimetype)) {
$this->open();
}
return $this->preferredMimetype;
}

public function install() {
return;
}

public function open() {
$this->modelUrl = $this->settingsService->getExternalModelUrl();
$this->modelApiKey = $this->settingsService->getExternalModelApiKey();

$ch = curl_init();
if ($ch === false) {
throw new \Exception('Curl error: unable to initialize curl');
}

curl_setopt($ch, CURLOPT_URL, $this->modelUrl . '/open');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['x-api-key: ' . $this->modelApiKey]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
if ($response === false) {
throw new \Exception('Cannot connect to external model: ' . curl_error($ch));
}

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode !== 200) {
throw new \Exception('Can\'t connect with external model. HTTP status code: ' . $httpCode);
}

$jsonResponse = json_decode($response, true);

$this->maximumImageArea = intval($jsonResponse['maximum_area']);
$this->preferredMimetype = $jsonResponse['preferred_mimetype'];

curl_close($ch);
}

public function detectFaces(string $imagePath, bool $compute = true): array {
$ch = curl_init();
if ($ch === false) {
throw new \Exception('Curl error: unable to initialize curl');
}

$cFile = curl_file_create($imagePath);
$post = array('file'=> $cFile);

curl_setopt($ch, CURLOPT_URL, $this->modelUrl . '/detect');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['x-api-key: ' . $this->modelApiKey]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
if ($response === false) {
throw new \Exception('External model dont response: ' . curl_error($ch));
}

curl_close($ch);

$jsonResponse = json_decode($response, true);

if ($jsonResponse['faces-count'] == 0)
return [];

return $jsonResponse['faces'];
}

public function compute(string $imagePath, array $face): array {
$ch = curl_init();
if ($ch === false) {
throw new \Exception('Curl error: unable to initialize curl');
}

$cFile = curl_file_create($imagePath, $this->preferredMimetype, basename($imagePath));
$post = [
'file' => $cFile,
'face' => json_encode($face),
];

curl_setopt($ch, CURLOPT_URL, $this->modelUrl . '/compute');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['x-api-key:' . $this->modelApiKey]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
if ($response === false) {
throw new \Exception('External model dont response: ' . curl_error($ch));
}

curl_close($ch);

$jsonResponse = json_decode($response, true);

return $jsonResponse['face'];
}

}
24 changes: 18 additions & 6 deletions lib/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

use OCA\FaceRecognition\Model\DlibCnnHogModel\DlibCnnHogModel;

use OCA\FaceRecognition\Model\ExternalModel\ExternalModel;

class ModelManager {

/** There is no default model. This is used by tests */
Expand All @@ -60,20 +62,25 @@ class ModelManager {
/** @var DlibCnnHogModel */
private $dlibCnnHogModel;

/** @var ExternalModel */
private $externalModel;

/**
* @patam IUserManager $userManager
* @param SettingsService $settingsService
* @param DlibCnn5Model $dlibCnn5Model
* @param DlibCnn68Model $dlibCnn68Model
* @param DlibHogModel $dlibHogModel
* @param DlibCnnHogModel $dlibCnnHogModel
* @param ExternalModel $externalModel
*/
public function __construct(IUserManager $userManager,
SettingsService $settingsService,
DlibCnn5Model $dlibCnn5Model,
DlibCnn68Model $dlibCnn68Model,
DlibHogModel $dlibHogModel,
DlibCnnHogModel $dlibCnnHogModel)
DlibCnnHogModel $dlibCnnHogModel,
ExternalModel $externalModel)
{
$this->userManager = $userManager;
$this->settingsService = $settingsService;
Expand All @@ -82,6 +89,7 @@ public function __construct(IUserManager $userManager,
$this->dlibCnn68Model = $dlibCnn68Model;
$this->dlibHogModel = $dlibHogModel;
$this->dlibCnnHogModel = $dlibCnnHogModel;
$this->externalModel = $externalModel;
}

/**
Expand All @@ -90,18 +98,21 @@ public function __construct(IUserManager $userManager,
*/
public function getModel(int $version): ?IModel {
switch ($version) {
case 1:
case DlibCnn5Model::FACE_MODEL_ID:
$model = $this->dlibCnn5Model;
break;
case 2:
case DlibCnn68Model::FACE_MODEL_ID:
$model = $this->dlibCnn68Model;
break;
case 3:
case DlibHogModel::FACE_MODEL_ID:
$model = $this->dlibHogModel;
break;
case 4:
case DlibCnnHogModel::FACE_MODEL_ID:
$model = $this->dlibCnnHogModel;
break;
case ExternalModel::FACE_MODEL_ID:
$model = $this->externalModel;
break;
default:
$model = null;
break;
Expand All @@ -125,7 +136,8 @@ public function getAllModels(): array {
$this->dlibCnn5Model,
$this->dlibCnn68Model,
$this->dlibHogModel,
$this->dlibCnnHogModel
$this->dlibCnnHogModel,
$this->externalModel
];
}

Expand Down
19 changes: 19 additions & 0 deletions lib/Service/SettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,18 @@ class SettingsService {
const DEFAULT_OBFUSCATE_FACE_THUMBS = 'false';

/** System setting to enable mimetypes */

const SYSTEM_ENABLED_MIMETYPES = 'enabledFaceRecognitionMimetype';
private $allowedMimetypes = ['image/jpeg', 'image/png'];
private $cachedAllowedMimetypes = false;

/** System setting to use custom folder for models */
const SYSTEM_MODEL_PATH = 'facerecognition.model_path';

/** System setting to configure external model */
const SYSTEM_EXTERNAL_MODEL_URL = 'facerecognition.external_model_url';
const SYSTEM_EXTERNAL_MODEL_API_KEY = 'facerecognition.external_model_api_key';

/**
* SettingsService
*/
Expand Down Expand Up @@ -304,4 +309,18 @@ public function getSystemModelPath(): ?string {
return $this->config->getSystemValue(self::SYSTEM_MODEL_PATH, null);
}

/**
* External model url
*/
public function getExternalModelUrl(): ?string {
return $this->config->getSystemValue(self::SYSTEM_EXTERNAL_MODEL_URL, null);
}

/**
* External model Api Key
*/
public function getExternalModelApiKey(): ?string {
return $this->config->getSystemValue(self::SYSTEM_EXTERNAL_MODEL_API_KEY, null);
}

}