Spaces:
Paused
Paused
| import requests | |
| import base64 | |
| from pathlib import Path | |
| from PIL import Image | |
| import io | |
| def resize_image(image: Image.Image, max_size: int = 224) -> Image.Image: | |
| """ | |
| Resize an image while maintaining aspect ratio. | |
| Args: | |
| image: PIL Image object to resize | |
| max_size: Maximum dimension (width or height) of the output image | |
| Returns: | |
| PIL Image: Resized image with maintained aspect ratio | |
| """ | |
| # Get current dimensions | |
| width, height = image.size | |
| # Calculate scaling factor to fit within max_size | |
| scale = min(max_size / width, max_size / height) | |
| # Only resize if image is larger than max_size | |
| if scale < 1: | |
| new_width = int(width * scale) | |
| new_height = int(height * scale) | |
| image = image.resize( | |
| (new_width, new_height), | |
| Image.LANCZOS | |
| ) | |
| return image | |
| # Define your desired size | |
| TARGET_SIZE = 16 | |
| # Define the image paths | |
| image_paths = [ | |
| "images/AAA0119DNBSPD01.jpg", | |
| "images/AAA0119DNBSPD02.jpg" | |
| ] | |
| # Read and encode images | |
| images = [] | |
| for path in image_paths: | |
| # Open the image | |
| img = Image.open(path) | |
| # Resize the image (using LANCZOS for high-quality downsampling) | |
| img = resize_image(img, max_size=TARGET_SIZE) | |
| # Convert to bytes | |
| buffered = io.BytesIO() | |
| img.save(buffered, format="JPEG") # You can change format to PNG if needed | |
| # Encode to base64 | |
| base64_image = base64.b64encode(buffered.getvalue()).decode('utf-8') | |
| images.append(base64_image) | |
| # Make the request | |
| print(images[0]) | |
| url = "http://localhost:8000/classify" | |
| payload = {"images": [images[0]]} | |
| headers = {"Content-Type": "application/json"} | |
| response = requests.post(url, json=payload, headers=headers) | |
| print(f"Status Code: {response.status_code}") | |
| print("Response Text:") | |
| print(response.text) | |