So you want to use Hugging Face’s pretrained models for object detection in images using Python? Well, first things first you need to download the image with cute cats that you saw on their website. Here’s how:
1. Go to https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/coco_sample.png (or whatever URL they gave you) and copy the link address. 2. Open up your Python code or Jupyter Notebook, and paste this line at the top:
# Import the necessary libraries
import requests # Import the requests library to make HTTP requests
from PIL import Image # Import the Image module from the PIL library for image processing
# Define the URL of the image to be downloaded
image_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/coco_sample.png"
# Make a GET request to the URL and store the response in a variable
response = requests.get(image_url)
# Check if the request was successful
if response.status_code == 200:
# If successful, open the response content as an image using the Image module
image = Image.open(response.content)
# Display the image
image.show()
else:
# If unsuccessful, print an error message
print("Error: Unable to retrieve image from the provided URL.")
This will allow us to download images using `requests`, and open them with `PIL`. 3. Next, we’ll use `requests` to get the image data from that URL you copied earlier. Here’s how:
# Import the necessary libraries
import requests # Import the requests library to make HTTP requests
from PIL import Image # Import the Image module from the PIL library to open and manipulate images
# Define the URL of the image we want to download
image_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/coco_sample.png"
# Use the requests library to get the image data from the URL
response = requests.get(image_url, stream=True) # Use the get() function to make a GET request to the URL and set the stream parameter to True to allow for downloading large files in chunks
# Get the raw image data from the response
image_data = response.raw # Use the raw attribute to get the raw data from the response
# Open the image using the Image module from PIL
image = Image.open(image_data) # Use the open() function to open the image using the raw data obtained from the response
# Display the image
image.show() # Use the show() function to display the image
This will download the image data and store it in a variable called `image_data`. 4. Now we’ll use `PIL` to open that image data as an actual image:
# Importing the necessary libraries
import PIL.Image as Image # Importing the PIL library and aliasing it as "Image"
from io import BytesIO # Importing the BytesIO module from the io library
# Downloading the image data and storing it in a variable called "image_data"
image_data = download_image()
# Using the PIL library to open the image data as an actual image
img = Image.open(BytesIO(image_data)) # Using the BytesIO module to convert the image data into a format that can be opened by the PIL library, and then using the open() function from the PIL library to open the image data as an actual image and storing it in the variable "img"
This will convert the raw image data into a usable `PIL` object called `img`. 5. Finally, we’ll use Hugging Face’s pretrained models for object detection to analyze that cute cat image:
# Import the necessary library for object detection
from transformers import pipeline
# Create an object detector using Hugging Face's pretrained models
object_detector = pipeline('object-detection')
# Use the object detector to analyze the image and store the results in a variable
results = object_detector(img)
# Loop through each result in the results variable
for result in results:
# Print the label, x and y coordinates, and score for each object detected
print("Found {} at ({}, {}) with a score of {}. ".format(result['label'], result['box']['xmin'], result['box']['ymin'], result['score']))
# The above code uses Hugging Face's object detection pipeline to analyze an image and print the label, coordinates, and score for each object detected.
This will run the pretrained object detection model on our cute cat image, and output any objects it finds along with their location and confidence score. And that’s it! You now know how to use Hugging Face’s pretrained models for object detection in images using Python. It’s like having a team of expert pizza chefs working together to create the perfect slice only instead of cheese, sauce, and pepperoni, we’re talking about cutting-edge AI technology that can analyze your favorite cat photos with ease!