Skip to content
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

Add support for WebP images #782

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 16 additions & 8 deletions demo/configs/webpack/common.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
// All rights reserved.

// This source code is licensed under the license found in the
// LICENSE file in the root directory of this source tree.

const { resolve } = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const FriendlyErrorsWebpackPlugin = require("friendly-errors-webpack-plugin");
Expand Down Expand Up @@ -42,10 +36,24 @@ module.exports = {
use: ["style-loader", "css-loader", "postcss-loader"],
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
test: /\.(jpe?g|png|gif|svg|webp)$/i,
use: [
"file-loader?hash=sha512&digest=hex&name=img/[contenthash].[ext]",
"image-webpack-loader?bypassOnDebug&optipng.optimizationLevel=7&gifsicle.interlaced=false",
{
loader: "image-webpack-loader",
options: {
bypassOnDebug: true,
optipng: {
optimizationLevel: 7,
},
gifsicle: {
interlaced: false,
},
webp: {
quality: 75,
},
},
},
],
},
{
Expand Down
11 changes: 5 additions & 6 deletions demo/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
// All rights reserved.

// This source code is licensed under the license found in the
// LICENSE file in the root directory of this source tree.

import { InferenceSession, Tensor } from "onnxruntime-web";
import React, { useContext, useEffect, useState } from "react";
import "./assets/scss/App.scss";
Expand Down Expand Up @@ -76,6 +70,11 @@ const App = () => {
img.height = height;
setImage(img);
};
img.onerror = () => {
if (url.href.endsWith(".webp")) {
console.log("Failed to load WebP image. Please ensure the image is in the correct format.");
}
};
} catch (error) {
console.log(error);
}
Expand Down
7 changes: 5 additions & 2 deletions scripts/amg.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,14 @@ def main(args: argparse.Namespace) -> None:

for t in targets:
print(f"Processing '{t}'...")
image = cv2.imread(t)
image = cv2.imread(t, cv2.IMREAD_UNCHANGED)
if image is None:
print(f"Could not load '{t}' as an image, skipping...")
continue
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
if t.lower().endswith('.webp'):
image = cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA)
else:
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

masks = generator.generate(image)

Expand Down
8 changes: 8 additions & 0 deletions scripts/export_onnx_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# LICENSE file in the root directory of this source tree.

import torch
import cv2 # type: ignore

from segment_anything import sam_model_registry
from segment_anything.utils.onnx import SamOnnxModel
Expand Down Expand Up @@ -173,6 +174,13 @@ def to_numpy(tensor):

if __name__ == "__main__":
args = parser.parse_args()
image = cv2.imread(args.input, cv2.IMREAD_UNCHANGED)
if image is None:
print(f"Could not load '{args.input}' as an image, skipping...")
if args.input.lower().endswith('.webp'):
image = cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA)
else:
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
run_export(
model_type=args.model_type,
checkpoint=args.checkpoint,
Expand Down