codewithkyrian/transformers
A Laravel-friendly transformers package for turning models, arrays, and API responses into consistent, reusable output. Define transformer classes, map fields, nest relations, and format data cleanly for JSON APIs, with minimal boilerplate and flexible customization.
Image classification is a computer vision task that involves assigning a label or class to an image. An image is expected to have only one label in this task. The labels to be selected from are predefined by the model. This task accepts image inputs and returns the classification label and the confidence score.
image-classificationXenova/vit-base-patch16-224.Image classification models find application in various scenarios, including:
Here's how to perform image classification using the pipeline:
use function Codewithkyrian\Transformers\Pipelines\pipeline;
$classifier = pipeline('image-classification');
$result = $classifier('path/to/image.jpg');
::: details Click to view output
['label' => 'tiger, Panthera tigris', 'score' => 0.63534494664876]
:::
When running the image-classification pipeline, you can the following options:
inputs (string)The image(s) to classify. It can be a local file path, a file resource, a URL to an image (local or remote), or an array of these inputs. It's the first argument so there's no need to pass it as a named argument.
$result = $classifier('https://example.com/image.jpg');
topK (int)The number of top labels to return. The default is 1.
$result = $classifier('https://example.com/image.jpg', topK: 3);
::: details Click to view output
[
['label' => 'tiger, Panthera tigris', 'score' => 0.63534494664876],
['label' => 'zebra', 'score' => 0.123456789],
['label' => 'lion, Panthera leo', 'score' => 0.098765432]
]
:::
The output of the pipeline is an array containing the classification label and the confidence score. The confidence score is a value between 0 and 1, with 1 being the highest confidence.
Since the actual labels depend on the model, it's crucial to consult the model's documentation for the specific labels it uses. Here are examples demonstrating how outputs might differ:
For a single image:
['label' => 'tiger, Panthera tigris', 'score' => 0.63534494664876]
For multiple images:
[
['label' => 'tiger, Panthera tigris', 'score' => 0.63534494664876],
['label' => 'cat', 'score' => 0.987654321],
['label' => 'dog', 'score' => 0.87654321]
]
How can I help you explore Laravel packages today?