Skip to content

Add warning for mismatched mimetype and filename extension #284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ exclude = ["^build/"]
[[tool.mypy.overrides]]
module = [
"_datetime.*",
"filetype.*",
# IPython is an optional dependency
"IPython.display.*",
# ipywidgets is an optional dependency
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ urllib3>=1.26.6
tqdm>=4.41.0
PyYAML>=5.3.1
requests_toolbelt
python-magic
filetype
29 changes: 19 additions & 10 deletions roboflow/core/project.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import datetime
import json
import mimetypes
import os
import sys
import time
import warnings
from typing import Dict, List, Optional, Union

import filetype
import requests
from PIL import Image, UnidentifiedImageError

from roboflow.adapters import rfapi
from roboflow.config import API_URL, DEMO_KEYS
from roboflow.core.version import Version
from roboflow.util.general import Retry
from roboflow.util.image_utils import load_labelmap

ACCEPTED_IMAGE_FORMATS = ["PNG", "JPEG"]
ACCEPTED_IMAGE_FORMATS = {
"image/bmp",
"image/jpeg",
"image/png",
"image/webp",
}


def custom_formatwarning(msg, *args, **kwargs):
Expand Down Expand Up @@ -337,7 +343,7 @@ def version(self, version_number: int, local: Optional[str] = None):

raise RuntimeError(f"Version number {version_number} is not found.")

def check_valid_image(self, image_path: str):
def check_valid_image(self, image_path: str) -> bool:
"""
Check if an image is valid. Useful before attempting to upload an image to Roboflow.

Expand All @@ -346,15 +352,18 @@ def check_valid_image(self, image_path: str):

Returns:
bool: whether the image is valid or not
""" # noqa: E501 // docs
try:
img = Image.open(image_path)
valid = img.format in ACCEPTED_IMAGE_FORMATS
img.close()
except UnidentifiedImageError:
"""
kind = filetype.guess(image_path)

if kind is None:
return False

return valid
extension_mimetype, _ = mimetypes.guess_type(image_path)

if extension_mimetype and extension_mimetype != kind.mime:
print(f"[{image_path}] file type ({kind.mime}) does not match filename extension.")

return kind.mime in ACCEPTED_IMAGE_FORMATS

def upload(
self,
Expand Down
17 changes: 13 additions & 4 deletions roboflow/models/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Optional, Tuple
from urllib.parse import urljoin

import magic
import filetype
import requests

from roboflow.config import API_URL
Expand All @@ -24,11 +24,20 @@
},
}

ACCEPTED_VIDEO_FORMATS = {
"video/mp4",
"video/x-msvideo", # AVI
"video/webm",
}


def is_valid_mime(filename):
mime = magic.Magic(mime=True)
file_type = mime.from_file(filename)
return file_type in ["video/mp4", "video/avi", "video/webm"]
kind = filetype.guess(filename)

if kind is None:
return False

return kind.mime in ACCEPTED_VIDEO_FORMATS


def is_valid_video(filename):
Expand Down