Skip to content

Commit d4e3bc9

Browse files
committed
Add warning for mismatched mimetype and filename extension
1 parent 9f59f71 commit d4e3bc9

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ urllib3>=1.26.6
1515
tqdm>=4.41.0
1616
PyYAML>=5.3.1
1717
requests_toolbelt
18-
python-magic
18+
filetype

roboflow/core/project.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
import datetime
22
import json
3+
import mimetypes
34
import os
45
import sys
56
import time
67
import warnings
78
from typing import Dict, List, Optional, Union
89

10+
import filetype
911
import requests
10-
from PIL import Image, UnidentifiedImageError
1112

1213
from roboflow.adapters import rfapi
1314
from roboflow.config import API_URL, DEMO_KEYS
1415
from roboflow.core.version import Version
1516
from roboflow.util.general import Retry
1617
from roboflow.util.image_utils import load_labelmap
1718

18-
ACCEPTED_IMAGE_FORMATS = ["PNG", "JPEG"]
19+
ACCEPTED_IMAGE_FORMATS = {
20+
"image/bmp",
21+
"image/jpeg",
22+
"image/png",
23+
"image/webp",
24+
}
1925

2026

2127
def custom_formatwarning(msg, *args, **kwargs):
@@ -346,15 +352,18 @@ def check_valid_image(self, image_path: str):
346352
347353
Returns:
348354
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:
355359
return False
356360

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
358367

359368
def upload(
360369
self,

roboflow/models/video.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Optional, Tuple
44
from urllib.parse import urljoin
55

6-
import magic
6+
import filetype
77
import requests
88

99
from roboflow.config import API_URL
@@ -24,11 +24,20 @@
2424
},
2525
}
2626

27+
ACCEPTED_VIDEO_FORMATS = {
28+
"video/mp4",
29+
"video/x-msvideo", # AVI
30+
"video/webm",
31+
}
32+
2733

2834
def is_valid_mime(filename):
29-
mime = magic.Magic(mime=True)
30-
file_type = mime.from_file(filename)
31-
return file_type in ["video/mp4", "video/avi", "video/webm"]
35+
kind = filetype.guess(filename)
36+
37+
if kind is None:
38+
return False
39+
40+
return kind.mime in ACCEPTED_VIDEO_FORMATS
3241

3342

3443
def is_valid_video(filename):

0 commit comments

Comments
 (0)