File size: 4,565 Bytes
ec41162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa8b60d
ec41162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
676de75
ec41162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
import logging
import gradio as gr
from PIL import Image as PILImg
from iteach_toolkit.DHYOLO import DHYOLODetector

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def detect_objects(selected_model, input_image, conf, imu_threshold, detections):
    try:
        model_path = model_options[selected_model]
        dhyolo = DHYOLODetector(model_path)

        # Save the input image temporarily to a path for processing
        input_image_path = "dhyolo_temp_input_image.jpg"
        input_image.save(input_image_path)

        # Perform prediction on the image
        orig_image, detections = dhyolo.predict(input_image_path, conf, imu_threshold, detections)

        # Log the detections
        logger.info("Detections: %s", detections)

        # Plot the bounding boxes on the original image
        orig_image, image_with_bboxes = dhyolo.plot_bboxes(attach_watermark=True)

        # Convert the image (with bounding boxes) from a NumPy array to a PIL Image for display.
        pil_img_with_bboxes = PILImg.fromarray(image_with_bboxes)

        return input_image, pil_img_with_bboxes

    except FileNotFoundError as e:
        logger.error("File not found: %s", e)
        return None, None
    except Exception as e:
        logger.error("An error occurred: %s", e)
        return None, None

def load_test_images():
    """Load images from the test_imgs directory."""
    test_imgs_dir = os.path.join(os.getcwd(), "test_imgs")
    logger.info("Loading images from: %s", test_imgs_dir)  # Log the directory path
    return [f for f in os.listdir(test_imgs_dir) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]

# Gradio interface
def create_interface():
    with gr.Blocks() as demo:
        # Center the title using HTML
        gr.Markdown("<h1 style='text-align: center;'>πŸšͺπŸ” DHYOLO DoorHandle Object Detection</h1>")

        # Add paper description with correct hyperlink syntax
        gr.Markdown("<h2 style='text-align: center;'>πŸ“š iTeach: Interactive Teaching for Robot Perception using Mixed Reality</h2>")

        # Add the project link using HTML <a> tag
        gr.Markdown("<h2 style='text-align: center;'>🌐 Project Link: <a href='https://irvlutd.github.io/iTeach/' target='_blank'>iTeach Project</a></h2>")

        with gr.Row():
            with gr.Column():
                # Load a default image for input
                default_image_path = os.path.join(os.getcwd(), "test_imgs", "jpad-irvl-test.jpg")  # Update with the correct default image name
                
                # Attempt to open the default image and log if it fails
                try:
                    default_image = PILImg.open(default_image_path)
                except FileNotFoundError:
                    logger.error("Default image not found at: %s", default_image_path)
                    default_image = None  # Set to None or a placeholder image

                input_image = gr.Image(type="pil", label="Input Image", value=default_image)

                # Pretrained model paths
                cwd = os.getcwd()
                global model_options
                model_options = {
                    "dh-yolo-v1-pb-ddf-524": f'{cwd}/pretrained_ckpts/dh-yolo-v1-pb-ddf-524.pt',
                    "dh-yolo-exp27-pb-1008": f'{cwd}/pretrained_ckpts/dh-yolo-exp27-pb-1008.pt',
                    "dh-yolo-exp31-pb-1532": f'{cwd}/pretrained_ckpts/dh-yolo-exp-31-pb-1532.pt',
                    "dh-yolo-exp31-pl-1532": f'{cwd}/pretrained_ckpts/dh-yolo-exp-31-pl-1532.pt'
                }
                
                model_path = gr.Dropdown(choices=list(model_options.keys()), label="Select Pretrained Model", value="dh-yolo-v1-pb-ddf-524")

                conf = gr.Slider(label="Confidence Threshold", minimum=0.0, maximum=1.0, step=0.01, value=0.5)
                imu_threshold = gr.Slider(label="IoU Threshold", minimum=0.0, maximum=1.0, step=0.01, value=0.5)
                detections = gr.Slider(label="Max number of Detections", minimum=1, maximum=100, step=1, value=10)

            with gr.Column():
                output_image = gr.Image(label="Output Image with DH-YOLO Detections", type="pil")

        detect_button = gr.Button("Run")

        # Detect button functionality
        detect_button.click(detect_objects, inputs=[model_path, input_image, conf, imu_threshold, detections], 
                            outputs=[input_image, output_image])

    return demo

if __name__ == "__main__":
    demo = create_interface()
    demo.launch()