Skip to content

Commit

Permalink
optimize image loading
Browse files Browse the repository at this point in the history
  • Loading branch information
david8862 committed Nov 11, 2021
1 parent 5bab428 commit 14f318b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
6 changes: 3 additions & 3 deletions common/backbones/imagenet_training/train_imagenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def verify_with_image(model, input_shape):
while True:
img_file = input('Input image filename:')
try:
img = Image.open(img_file)
img = Image.open(img_file).convert('RGB')
resized_img = img.resize(input_shape, Image.BICUBIC)
except:
print('Open Error! Try again!')
Expand All @@ -215,10 +215,10 @@ def verify_with_image(model, input_shape):
# show predict result on origin image
img_array = np.asarray(img)
cv2.putText(img_array, '{name}:{conf:.3f}'.format(name=result[0][0][1], conf=float(result[0][0][2])),
(10,30),
(10, 30),
cv2.FONT_HERSHEY_SIMPLEX,
fontScale=1,
color=(255,0,0),
color=(255, 0, 0),
thickness=2,
lineType=cv2.LINE_AA)
Image.fromarray(img_array).show()
Expand Down
11 changes: 8 additions & 3 deletions tools/dataset_converter/dataset_visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
import os, sys, argparse
import numpy as np
from PIL import Image
import cv2
from tqdm import tqdm

Expand All @@ -19,7 +20,8 @@ def dataset_visualize(annotation_file, classes_path):
for i, annotation_line in enumerate(annotation_lines):
pbar.update(1)
line = annotation_line.split()
image = cv2.imread(line[0])
image = Image.open(line[0]).convert('RGB')
image = np.array(image, dtype='uint8')
boxes = np.array([np.array(list(map(int, box.split(',')))) for box in line[1:]])

classes = boxes[:, -1]
Expand All @@ -31,13 +33,16 @@ def dataset_visualize(annotation_file, classes_path):
# show image file info
image_file_name = os.path.basename(line[0])
cv2.putText(image, image_file_name+'({}/{})'.format(i+1, len(annotation_lines)),
(3,15),
(3, 15),
cv2.FONT_HERSHEY_PLAIN,
fontScale=1,
color=(0,0,255),
color=(255, 0, 0),
thickness=1,
lineType=cv2.LINE_AA)

# convert to BGR for cv2.imshow
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

cv2.namedWindow("Image", 0)
cv2.imshow("Image", image)
keycode = cv2.waitKey(0) & 0xFF
Expand Down
10 changes: 5 additions & 5 deletions tools/evaluation/validate_yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def validate_yolo_model(model_path, image_file, anchors, class_names, model_inpu
custom_object_dict = get_custom_objects()
model = load_model(model_path, compile=False, custom_objects=custom_object_dict)

img = Image.open(image_file)
img = Image.open(image_file).convert('RGB')
image = np.array(img, dtype='uint8')
image_data = preprocess_image(img, model_input_shape)
#origin image shape, in (height, width) format
Expand Down Expand Up @@ -63,7 +63,7 @@ def validate_yolo_model_tflite(model_path, image_file, anchors, class_names, eli
if input_details[0]['dtype'] == np.float32:
floating_model = True

img = Image.open(image_file)
img = Image.open(image_file).convert('RGB')
image = np.array(img, dtype='uint8')

height = input_details[0]['shape'][1]
Expand Down Expand Up @@ -114,7 +114,7 @@ def validate_yolo_model_mnn(model_path, image_file, anchors, class_names, elim_g
model_input_shape = (height, width)

# prepare input image
img = Image.open(image_file)
img = Image.open(image_file).convert('RGB')
image = np.array(img, dtype='uint8')
image_data = preprocess_image(img, model_input_shape)
#origin image shape, in (height, width) format
Expand Down Expand Up @@ -280,7 +280,7 @@ def load_pb_graph(model_path):
batch, height, width, channel = image_input.shape
model_input_shape = (int(height), int(width))

img = Image.open(image_file)
img = Image.open(image_file).convert('RGB')
image = np.array(img, dtype='uint8')
image_data = preprocess_image(img, model_input_shape)
#origin image shape, in (height, width) format
Expand Down Expand Up @@ -326,7 +326,7 @@ def validate_yolo_model_onnx(model_path, image_file, anchors, class_names, elim_
model_input_shape = (height, width)

# prepare input image
img = Image.open(image_file)
img = Image.open(image_file).convert('RGB')
image = np.array(img, dtype='uint8')
image_data = preprocess_image(img, model_input_shape)
#origin image shape, in (height, width) format
Expand Down
6 changes: 3 additions & 3 deletions tools/misc/rotate_gridmask_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

def main():
# load some VOC2007 images for test
#image = Image.open("000001.jpg")
#image = Image.open("000001.jpg").convert('RGB')
#boxes = np.array([[48,240,195,371,11],[8,12,352,498,14]])

image = Image.open("000004.jpg")
image = Image.open("000004.jpg").convert('RGB')
boxes = np.array([[13,311,84,362,6],[362,330,500,389,6],[235,328,334,375,6],[175,327,252,364,6],[139,320,189,359,6],[108,325,150,353,6],[84,323,121,350,6]])

#image = Image.open("000010.jpg")
#image = Image.open("000010.jpg").convert('RGB')
#boxes = np.array([[87,97,258,427,12],[133,72,245,284,14]])


Expand Down
2 changes: 1 addition & 1 deletion yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def detect_img(yolo):
while True:
img = input('Input image filename:')
try:
image = Image.open(img)
image = Image.open(img).convert('RGB')
except:
print('Open Error! Try again!')
continue
Expand Down
2 changes: 1 addition & 1 deletion yolo3/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def get_ground_truth_data(annotation_line, input_shape, augment=True, max_boxes=100):
'''random preprocessing for real-time data augmentation'''
line = annotation_line.split()
image = Image.open(line[0])
image = Image.open(line[0]).convert('RGB')
image_size = image.size
model_input_size = input_shape[::-1]
boxes = np.array([np.array(list(map(int, box.split(',')))) for box in line[1:]])
Expand Down

0 comments on commit 14f318b

Please sign in to comment.