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

Save only detected images #11719

Open
Syed05 opened this issue May 15, 2024 · 0 comments
Open

Save only detected images #11719

Syed05 opened this issue May 15, 2024 · 0 comments
Assignees

Comments

@Syed05
Copy link

Syed05 commented May 15, 2024

I run the following script to perform inference on multiple images using a trained model (trained on custom data). I want to save only those images in which objects are detected.

Below is my inference code:

import os
from mmdet.apis import init_detector, inference_detector
import mmcv
import cv2
from mmdet.registry import VISUALIZERS

config_file = 'configs/_base_/custom2.py'
checkpoint_file = 'work_dirs/custom2/epoch_300.pth'

model = init_detector(config_file, checkpoint_file)

image_folder = 'images'
output_folder = 'outputs'
image_paths = [os.path.join(image_folder, img_name) for img_name in os.listdir(image_folder)]

visualizer = VISUALIZERS.build(model.cfg.visualizer)
visualizer.dataset_meta = model.dataset_meta

for img_path in image_paths:
        image = mmcv.imread(img_path)
        result = inference_detector(model, image)
        os.makedirs(output_folder, exist_ok=True)
        output_file = os.path.join(output_folder, os.path.basename(img_path))
    
    visualizer.add_datasample(
        'result',
        cv2.cvtColor(image, cv2.COLOR_BGR2RGB),
        data_sample=result,
        draw_gt=False,
        wait_time=0,
        pred_score_thr=0.3,
        out_file=output_file
    )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants