Skip to content

Commit

Permalink
finally fix some hard-code problem
Browse files Browse the repository at this point in the history
  • Loading branch information
honeyxu1108 committed Dec 29, 2024
1 parent c461e2d commit ceae60e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
42 changes: 27 additions & 15 deletions logo_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,29 @@ def cache_reference_list(model, targetlist_path: str, grayscale=False):
:return file_name_list: targetlist paths
'''

# Prediction for targetlists
# Prediction for targetlists
logo_feat_list = []
file_name_list = []

for target in tqdm(os.listdir(targetlist_path)):
target_list = os.listdir(targetlist_path)
for target in tqdm(target_list):
if target.startswith('.'): # skip hidden files
continue
for logo_path in os.listdir(os.path.join(targetlist_path, target)):
if logo_path.endswith('.png') or logo_path.endswith('.jpeg') or logo_path.endswith(
'.jpg') or logo_path.endswith('.PNG') \
or logo_path.endswith('.JPG') or logo_path.endswith('.JPEG'):
if logo_path.startswith('loginpage') or logo_path.startswith('homepage'): # skip homepage/loginpage
logo_list = os.listdir(os.path.join(targetlist_path, target))
for logo_path in logo_list:
# List of valid image extensions
valid_extensions = ['.png', '.jpeg', '.jpg', 'PNG','.JPG', '.JPEG']
if any(logo_path.endswith(ext) for ext in valid_extensions):
skip_prefixes = ['loginpage', 'homepage']
if any(logo_path.startswith(prefix) for prefix in skip_prefixes): # skip homepage/loginpage
continue
try:
logo_feat_list.append(get_embedding(img=os.path.join(targetlist_path, target, logo_path),
model=model, grayscale=grayscale))
file_name_list.append(str(os.path.join(targetlist_path, target, logo_path)))
except OSError:
print(f"Error opening image: {os.path.join(targetlist_path, target, logo_path)}")
continue
logo_feat_list.append(get_embedding(img=os.path.join(targetlist_path, target, logo_path),
model=model, grayscale=grayscale))
file_name_list.append(str(os.path.join(targetlist_path, target, logo_path)))

return np.asarray(logo_feat_list), np.asarray(file_name_list)


@torch.no_grad()
Expand Down Expand Up @@ -133,9 +138,16 @@ def get_embedding(img, model, grayscale=False):

## Resize the image while keeping the original aspect ratio
pad_color = 255 if grayscale else (255, 255, 255)
img = ImageOps.expand(img, (
(max(img.size) - img.size[0]) // 2, (max(img.size) - img.size[1]) // 2,
(max(img.size) - img.size[0]) // 2, (max(img.size) - img.size[1]) // 2), fill=pad_color)
img = ImageOps.expand(
img,
(
(max(img.size) - img.size[0]) // 2,
(max(img.size) - img.size[1]) // 2,
(max(img.size) - img.size[0]) // 2,
(max(img.size) - img.size[1]) // 2
),
fill=pad_color
)

img = img.resize((img_size, img_size))

Expand Down
15 changes: 11 additions & 4 deletions logo_recog.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def pred_rcnn(im, predictor):
outputs = predictor(im)

instances = outputs['instances']
pred_classes = instances.pred_classes # tensor
pred_boxes = instances.pred_boxes # Boxes object
pred_classes = instances.pred_classes # tensor
pred_boxes = instances.pred_boxes # Boxes object

logo_boxes = pred_boxes[pred_classes == 1].tensor

Expand All @@ -52,6 +52,13 @@ def config_rcnn(cfg_path, weights_path, conf_threshold):
predictor = DefaultPredictor(cfg)
return predictor

COLORS = {
0: (255, 255, 0), # logo
1: (36, 255, 12), # input
2: (0, 255, 255), # button
3: (0, 0, 255), # label
4: (255, 0, 0) # block
}

def vis(img_path, pred_boxes):
'''
Expand All @@ -71,8 +78,8 @@ def vis(img_path, pred_boxes):
# draw rectangle
for j, box in enumerate(pred_boxes):
if j == 0:
cv2.rectangle(check, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (255, 255, 0), 2)
cv2.rectangle(check, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), COLORS['0'], 2)
else:
cv2.rectangle(check, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (36, 255, 12), 2)
cv2.rectangle(check, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), COLORS['1'], 2)

return check
1 change: 1 addition & 0 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set -e
# Function to display error messages and exit
error_exit() {
echo "$1" >&2
echo "$(date): $1" >> error.log # Log error to a file for debugging
exit 1
}

Expand Down

0 comments on commit ceae60e

Please sign in to comment.