|
1 | 1 | import datetime
|
2 | 2 | import json
|
| 3 | +import mimetypes |
3 | 4 | import os
|
4 | 5 | import sys
|
5 | 6 | import time
|
6 | 7 | import warnings
|
7 | 8 | from typing import Dict, List, Optional, Union
|
8 | 9 |
|
| 10 | +import filetype |
9 | 11 | import requests
|
10 |
| -from PIL import Image, UnidentifiedImageError |
11 | 12 |
|
12 | 13 | from roboflow.adapters import rfapi
|
13 | 14 | from roboflow.config import API_URL, DEMO_KEYS
|
14 | 15 | from roboflow.core.version import Version
|
15 | 16 | from roboflow.util.general import Retry
|
16 | 17 | from roboflow.util.image_utils import load_labelmap
|
17 | 18 |
|
18 |
| -ACCEPTED_IMAGE_FORMATS = ["PNG", "JPEG"] |
| 19 | +ACCEPTED_IMAGE_FORMATS = { |
| 20 | + "image/bmp", |
| 21 | + "image/jpeg", |
| 22 | + "image/png", |
| 23 | + "image/webp", |
| 24 | +} |
19 | 25 |
|
20 | 26 |
|
21 | 27 | def custom_formatwarning(msg, *args, **kwargs):
|
@@ -346,15 +352,18 @@ def check_valid_image(self, image_path: str):
|
346 | 352 |
|
347 | 353 | Returns:
|
348 | 354 | bool: whether the image is valid or not
|
349 |
| - """ # noqa: E501 // docs |
350 |
| - try: |
351 |
| - img = Image.open(image_path) |
352 |
| - valid = img.format in ACCEPTED_IMAGE_FORMATS |
353 |
| - img.close() |
354 |
| - except UnidentifiedImageError: |
| 355 | + """ |
| 356 | + kind = filetype.guess(image_path) |
| 357 | + |
| 358 | + if kind is None: |
355 | 359 | return False
|
356 | 360 |
|
357 |
| - return valid |
| 361 | + extension_mimetype, _ = mimetypes.guess_type(image_path) |
| 362 | + |
| 363 | + if extension_mimetype and extension_mimetype != kind.mime: |
| 364 | + print(f"[{image_path}] file type ({kind.mime}) does not match filename extension.") |
| 365 | + |
| 366 | + return kind.mime in ACCEPTED_IMAGE_FORMATS |
358 | 367 |
|
359 | 368 | def upload(
|
360 | 369 | self,
|
|
0 commit comments