-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #421 from chesslablab/issue/420-Implement-the-reco…
…gnizer-command Issue/420 implement the recognizer command
- Loading branch information
Showing
6 changed files
with
65 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace ChessServer\Command\Game\Async; | ||
|
||
use ChessServer\Command\AbstractAsyncCommand; | ||
use ChessServer\Socket\AbstractSocket; | ||
|
||
class RecognizerCommand extends AbstractAsyncCommand | ||
{ | ||
public function __construct() | ||
{ | ||
$this->name = '/recognizer'; | ||
$this->description = 'Returns the piece placement in FEN format of a Base64 encoded image.'; | ||
$this->params = [ | ||
'params' => '<string>', | ||
]; | ||
} | ||
|
||
public function validate(array $argv) | ||
{ | ||
return count($argv) - 1 === count($this->params); | ||
} | ||
|
||
public function run(AbstractSocket $socket, array $argv, int $id) | ||
{ | ||
$params = json_decode(stripslashes($argv[1]), true); | ||
|
||
$this->pool->add(new RecognizerTask($params)) | ||
->then(function ($result) use ($socket, $id) { | ||
return $socket->getClientStorage()->send([$id], [ | ||
$this->name => $result, | ||
]); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace ChessServer\Command\Game\Async; | ||
|
||
use Chess\Media\FEN\JpgToPiecePlacement; | ||
use ChessServer\Command\AbstractAsyncTask; | ||
|
||
class RecognizerTask extends AbstractAsyncTask | ||
{ | ||
public function run() | ||
{ | ||
$data = base64_decode($this->params['data']); | ||
$image = imagecreatefromstring($data); | ||
|
||
return (new JpgToPiecePlacement($image))->predict(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters