Skip to content

Commit

Permalink
Merge pull request meituan#780 from meituan/fix_preprocess_bug_in_hub…
Browse files Browse the repository at this point in the history
…conf

Fix preprocess bug in hubconf
  • Loading branch information
mtjhl authored Apr 14, 2023
2 parents 37bb5c2 + b18c85c commit c4d3b75
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions hubconf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import cv2
import math
import pathlib
import torch
Expand Down Expand Up @@ -70,9 +71,16 @@ def make_divisible(x, divisor):

def process_image(path, img_size, stride):
'''Preprocess image before inference.'''
img_src = np.asarray(Image.open(path).convert('RGB'))
try:
img_src = cv2.imread(path)
img_src = cv2.cvtColor(img_src, cv2.COLOR_RGB2BGR)
assert img_src is not None, f"opencv cannot read image correctly or {path} not exists"
except:
img_src = np.asarray(Image.open(path))
assert img_src is not None, f"Image Not Found {path}, workdir: {os.getcwd()}"

image = letterbox(img_src, img_size, stride=stride)[0]
image = image.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB
image = image.transpose((2, 0, 1)) # HWC to CHW
image = torch.from_numpy(np.ascontiguousarray(image))
image = image.float()
image /= 255
Expand Down

0 comments on commit c4d3b75

Please sign in to comment.