File size: 2,900 Bytes
fe01ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe4e370
fe01ed6
 
 
 
 
1a08b3e
fe01ed6
 
fe4e370
 
fe01ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a08b3e
fe01ed6
 
 
 
 
 
 
 
 
1a08b3e
fe01ed6
 
 
1a08b3e
fe4e370
1a08b3e
fe01ed6
1a08b3e
fe01ed6
 
 
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
import torch
import cv2
import numpy as np
import gradio as gr
import segmentation_models_pytorch as smp
from albumentations import Compose, Resize, Normalize
from albumentations.pytorch import ToTensorV2

# Load the trained model
class UNetCamouflage(torch.nn.Module):
    def __init__(self, encoder_name="resnet50", encoder_weights=None):
        super().__init__()
        self.model = smp.Unet(
            encoder_name=encoder_name,
            encoder_weights=encoder_weights,
            in_channels=3,
            classes=1
        )

    def forward(self, x):
        return self.model(x)

# Load model weights
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = UNetCamouflage().to(device)
model.load_state_dict(torch.load("CamoVision_Final.pth", map_location=device))
model.eval()

# Image preprocessing
transform = Compose([
    Resize(256, 256),
    Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
    ToTensorV2()
])

def predict(image):
    """Process the image and detect camouflage objects."""
    img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    augmented = transform(image=img)
    img_tensor = augmented["image"].unsqueeze(0).to(device)

    with torch.no_grad():
        output = torch.sigmoid(model(img_tensor))

    # Convert model output to binary mask
    mask = (output.squeeze().cpu().numpy() > 0.5).astype(np.uint8) * 255

    # Resize mask to original image size
    mask_resized = cv2.resize(mask, (image.shape[1], image.shape[0]))

    # Overlay mask on image
    overlay = cv2.addWeighted(image, 0.7, cv2.cvtColor(mask_resized, cv2.COLOR_GRAY2BGR), 0.3, 0)

    return overlay, mask_resized

# Custom CSS for professional UI
custom_css = """
footer {visibility: hidden;}
#app-title {text-align: center; font-size: 32px; font-weight: bold; color: #FFFFFF; margin-top: 20px;}
#app-description {text-align: center; font-size: 18px; color: #CCCCCC;}
#footer-text {text-align: center; font-size: 16px; color: #BBBBBB; margin-top: 20px;}
"""

# Gradio Blocks UI
with gr.Blocks(css=custom_css, theme="soft") as app:
    gr.HTML('<div id="app-title">🕵️‍♂️ Camouflage Object Detection</div>')
    gr.HTML('<div id="app-description">Upload an image and detect hidden objects with AI-powered camouflage detection.</div>')
    
    with gr.Row():
        with gr.Column():
            image_input = gr.Image(type="numpy", label="Upload Image")
            submit_btn = gr.Button("🔍 Detect")

        with gr.Column():
            output_overlay = gr.Image(type="numpy", label="Detected Camouflage (Overlay)")
            output_mask = gr.Image(type="numpy", label="Segmentation Mask")

    submit_btn.click(predict, inputs=[image_input], outputs=[output_overlay, output_mask])

    gr.HTML('<div id="footer-text">Made with ❤️ by Jaskaranjeet Singh</div>')

# Launch the app
if __name__ == "__main__":
    app.launch()