-
-
Notifications
You must be signed in to change notification settings - Fork 114
[Demo] Add smart cropping with Hugging Face to demo #851
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,33 @@ | ||
| import { Controller } from '@hotwired/stimulus'; | ||
|
|
||
| export default class extends Controller { | ||
| connect() { | ||
| this.element.addEventListener('dropzone:change', this._onChange.bind(this)); | ||
| this.element.addEventListener('dropzone:clear', this._onClear); | ||
| } | ||
|
|
||
| disconnect() { | ||
| this.element.removeEventListener('dropzone:change', this._onChange.bind(this)); | ||
| this.element.removeEventListener('dropzone:clear', this._onClear); | ||
| } | ||
|
|
||
| async _onChange(event) { | ||
| const cropComponent = document.getElementById('crop-component').__component; | ||
|
|
||
| cropComponent.set('imageData', await this.blobToBase64(event.detail)); | ||
| } | ||
|
|
||
| _onClear(event) { | ||
| const cropComponent = document.getElementById('crop-component').__component; | ||
|
|
||
| cropComponent.set('imageData', null); | ||
| } | ||
|
|
||
| blobToBase64(blob) { | ||
| return new Promise((resolve) => { | ||
| const reader = new FileReader(); | ||
| reader.readAsDataURL(blob); | ||
| reader.onloadend = () => resolve(reader.result); | ||
| }); | ||
| } | ||
| } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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,18 @@ | ||
| .crop { | ||
| body&, .card-img-top { | ||
| background: rgb(34,34,34); | ||
| background: linear-gradient(0deg, rgb(209, 14, 205) 0%, rgb(120, 21, 135) 100%); | ||
| } | ||
|
|
||
| .card-img-top { | ||
| color: #ffffff; | ||
| } | ||
|
|
||
| & footer { | ||
| color: #ffffff; | ||
|
|
||
| a { | ||
| color: #ffffff; | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,11 @@ | ||
| # Enable stateless CSRF protection for forms and logins/logouts | ||
| framework: | ||
| form: | ||
| csrf_protection: | ||
| token_id: submit | ||
|
|
||
| csrf_protection: | ||
| stateless_token_ids: | ||
| - submit | ||
| - authenticate | ||
| - logout |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| twig: | ||
| file_name_pattern: '*.twig' | ||
| form_themes: [ 'bootstrap_5_layout.html.twig' ] | ||
|
|
||
| when@test: | ||
| twig: | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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,56 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace App\Crop; | ||
|
|
||
| use Symfony\Component\Form\AbstractType; | ||
| use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | ||
| use Symfony\Component\Form\FormBuilderInterface; | ||
| use Symfony\UX\Dropzone\Form\DropzoneType; | ||
|
|
||
| final class CropForm extends AbstractType | ||
| { | ||
| public function buildForm(FormBuilderInterface $builder, array $options): void | ||
| { | ||
| $builder | ||
| ->add('originalImage', DropzoneType::class, [ | ||
| 'label' => 'Image to crop', | ||
| 'attr' => [ | ||
| 'data-controller' => 'dropzone', | ||
| 'placeholder' => 'Drag and drop an image or click to browse', | ||
| ], | ||
| ]) | ||
| ->add('format', ChoiceType::class, [ | ||
| 'choices' => [ | ||
| 'Square (1:1)' => '1:1', | ||
| 'Landscape (16:9)' => '16:9', | ||
| 'Portrait (9:16)' => '9:16', | ||
| ], | ||
| 'expanded' => true, | ||
| 'multiple' => false, | ||
| ]) | ||
| ->add('width', ChoiceType::class, [ | ||
| 'choices' => [ | ||
| 'Small (400px)' => 400, | ||
| 'Medium (800px)' => 800, | ||
| 'Large (1200px)' => 1200, | ||
| ], | ||
| 'expanded' => true, | ||
| 'multiple' => false, | ||
| ]) | ||
| ; | ||
| } | ||
|
|
||
| public function getBlockPrefix(): string | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| return ''; | ||
| } | ||
| } | ||
This file contains hidden or 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,63 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace App\Crop\Image; | ||
|
|
||
| use Symfony\AI\Platform\Bridge\HuggingFace\Output\ObjectDetectionResult; | ||
| use Symfony\AI\Platform\Bridge\HuggingFace\Task; | ||
| use Symfony\AI\Platform\Message\Content\Image; | ||
| use Symfony\AI\Platform\PlatformInterface; | ||
| use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
|
|
||
| final readonly class Analyzer | ||
| { | ||
| public function __construct( | ||
| #[Autowire(service: 'ai.platform.huggingface')] | ||
| private PlatformInterface $platform, | ||
| ) { | ||
| } | ||
|
|
||
| public function getRelevantArea(string $imageData): RelevantArea | ||
| { | ||
| $result = $this->platform->invoke('facebook/detr-resnet-50', Image::fromDataUrl($imageData), [ | ||
| 'task' => Task::OBJECT_DETECTION, | ||
| ])->asObject(); | ||
|
|
||
| \assert($result instanceof ObjectDetectionResult); | ||
|
|
||
| if ([] === $result->objects) { | ||
| throw new \RuntimeException('No objects detected.'); | ||
| } | ||
|
|
||
| $init = $result->objects[0]; | ||
| $xMin = $init->xmin; | ||
| $yMin = $init->ymin; | ||
| $xMax = $init->xmax; | ||
| $yMax = $init->ymax; | ||
|
|
||
| foreach ($result->objects as $object) { | ||
| if ($object->xmin < $xMin) { | ||
| $xMin = $object->xmin; | ||
| } | ||
| if ($object->ymin < $yMin) { | ||
| $yMin = $object->ymin; | ||
| } | ||
| if ($object->xmax > $xMax) { | ||
| $xMax = $object->xmax; | ||
| } | ||
| if ($object->ymax > $yMax) { | ||
| $yMax = $object->ymax; | ||
| } | ||
| } | ||
|
|
||
| return new RelevantArea((int) $xMin, (int)$yMin, (int)$xMax, (int)$yMax); | ||
| } | ||
| } |
This file contains hidden or 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,51 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace App\Crop\Image; | ||
|
|
||
| final readonly class RelevantArea | ||
| { | ||
| public function __construct( | ||
| public int $xMin, | ||
| public int $yMin, | ||
| public int $xMax, | ||
| public int $yMax, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @return int<1, max> | ||
| */ | ||
| public function getWidth(): int | ||
| { | ||
| $width = $this->xMax - $this->xMin; | ||
|
|
||
| if ($width < 1) { | ||
| throw new \InvalidArgumentException('Width must be at least 1 pixel.'); | ||
| } | ||
|
|
||
| return $width; | ||
| } | ||
|
|
||
| /** | ||
| * @return int<1, max> | ||
| */ | ||
| public function getHeight(): int | ||
| { | ||
| $height = $this->yMax - $this->yMin; | ||
|
|
||
| if ($height < 1) { | ||
| throw new \InvalidArgumentException('Height must be at least 1 pixel.'); | ||
| } | ||
|
|
||
| return $height; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.