File size: 8,684 Bytes
08a6a13
 
 
 
 
 
 
 
 
 
41504db
 
08a6a13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import glob
import gradio as gr
import matplotlib
import numpy as np
from PIL import Image, ImageOps
import torch
import tempfile
import cv2  # นำเข้าไลบรารี OpenCV
from gradio_imageslider import ImageSlider
import webbrowser  # ใช้สำหรับเปิดเว็บเบราว์เซอร์
import matplotlib.pyplot as plt


from depth_anything_v2.dpt import DepthAnythingV2

# การตั้งค่าการล็อกเพื่อเอาข้อความ "xFormers not available" ออก
import logging
logging.getLogger("transformers").setLevel(logging.ERROR)

css = """
#img-display-container {
    max-height: 100vh;
}
#img-display-input {
    max-height: 80vh;
}
#img-display-output {
    max-height: 80vh;
}
#download {
    height: 62px;
    margin-top: 20px;  # เพิ่มระยะห่างด้านบนของปุ่มดาวน์โหลด
}
"""
DEVICE = 'cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu'
model_configs = {
    'vits': {'encoder': 'vits', 'features': 64, 'out_channels': [48, 96, 192, 384]},
    'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]},
    'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},
    'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]}
}
encoder = 'vitl'
checkpoint_path = 'D:/00224499/Depth-Anything-V2-main/checkpoints/depth_anything_v2_vitl.pth'
model = DepthAnythingV2(**model_configs[encoder])
state_dict = torch.load(checkpoint_path, map_location="cpu", weights_only=True)
model.load_state_dict(state_dict)
model = model.to(DEVICE).eval()

title = "# แปลงภาพเป็นดีฟแม๊ป เกรสเกล "
description = """### Depth Map picture"""

def predict_depth(image):
    return model.infer_image(image)

def enhance_image(img, scale_factor):
    height, width = img.shape[:2]
    new_width = int(width * scale_factor)
    new_height = int(height * scale_factor)
    resized = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_LINEAR)
    
    sharpen_kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
    sharpened = cv2.filter2D(resized, -1, sharpen_kernel)
    
    denoised = cv2.fastNlMeansDenoisingColored(sharpened, None, 10, 10, 7, 21)
    
    return denoised

def convert_to_grayscale_and_adjust(image):
    grayscale = ImageOps.grayscale(image)
    grayscale_np = np.array(grayscale)
    return grayscale_np

def convert_webp_to_png(image_path):
    with Image.open(image_path) as img:
        png_path = image_path.replace(".webp", ".png")
        img.save(png_path, "PNG")
    return png_path

def on_submit(image, smooth_factor):
    original_image = Image.fromarray(image)

    # แปลงภาพเป็นเกรสเกลและปรับความคมชัด
    grayscale = convert_to_grayscale_and_adjust(original_image)
    grayscale_image = Image.fromarray(grayscale)

    depth = predict_depth(image[:, :, ::-1])

    raw_depth = Image.fromarray(depth.astype('uint16'))
    tmp_raw_depth = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
    raw_depth.save(tmp_raw_depth.name)

    depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
    depth = depth.astype(np.uint8)
    depth_colored = cv2.cvtColor(depth, cv2.COLOR_GRAY2BGR)
    colored_depth = (cmap(depth)[:, :, :3] * 255).astype(np.uint8)

    gray_depth = Image.fromarray(depth)
    tmp_gray_depth = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
    gray_depth.save(tmp_gray_depth.name)

    # ตรวจสอบและปรับขนาดของ depth ให้ตรงกับ grayscale
    depth_resized = cv2.resize(depth_colored, (grayscale.shape[1], grayscale.shape[0]))

    # ตรวจสอบให้แน่ใจว่าขนาดของภาพตรงกัน
    if grayscale.shape != depth_resized.shape[:2]:
        depth_resized = cv2.resize(depth_resized, (grayscale.shape[1], grayscale.shape[0]))

    # ซ้อนทับภาพเกรสเกลกับ Grayscale depth map รอบแรก
    overlay = cv2.addWeighted(grayscale, 0.7, depth_resized[:, :, 0], 0.3, 0)
    
    # ซ้อนทับภาพดีฟแม๊ปเพิ่มเติมอีกสองรอบตามค่า smooth_factor
    for _ in range(2):
        overlay = cv2.addWeighted(overlay, smooth_factor, depth_resized[:, :, 0], 1 - smooth_factor, 0)
        overlay = cv2.addWeighted(overlay, 0.4, depth_resized[:, :, 0], 0.6, 0)
        overlay = cv2.addWeighted(overlay, 0.9, grayscale, 0.1, 0)
        overlay = cv2.addWeighted(overlay, 0.8, depth_resized[:, :, 0], 0.2, 0)
        overlay = cv2.addWeighted(overlay, 0.7, depth_resized[:, :, 0], 0.3, 0)
        overlay = cv2.addWeighted(overlay, 0.7, grayscale, 0.3, 0)
        overlay = cv2.addWeighted(depth_resized[:, :, 0], 0.9, grayscale, 0.1, 0)

    # บันทึกภาพเกรสเกลที่แปลงเป็น .png
    tmp_grayscale = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
    grayscale_image.save(tmp_grayscale.name)

    # บันทึกภาพ overlay เป็น .png
    overlay_img = Image.fromarray(overlay)
    tmp_overlay = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
    overlay_img.save(tmp_overlay.name)

    return colored_depth, depth, grayscale, overlay, tmp_gray_depth.name, tmp_raw_depth.name, tmp_grayscale.name, tmp_overlay.name, overlay

def on_enhance(image, enhance_factor):
    enhanced = enhance_image(image, enhance_factor)
    return enhanced

with gr.Blocks(css=css) as demo:
    gr.Markdown(title)
    gr.Markdown(description)

    with gr.Row():
        input_image = gr.Image(label="Input Image", type='numpy', elem_id='img-display-input')
        enhanced_image = gr.Image(label="Enhanced Image", type='numpy', elem_id='img-display-enhanced')  # แสดงภาพ Enhanced
        with gr.Column():
            enhance_factor = gr.Slider(label="Enhance Factor", minimum=1, maximum=4, value=1, step=0.1)  # ที่ป้อนค่าเฟคเตอร์เริ่มต้นเป็น 0.5
            enhance_btn = gr.Button(value="Enhance Image")  # ปุ่มสำหรับการปรับแสงและความคมชัด
            smooth_factor = gr.Slider(label="Smooth Factor", minimum=0, maximum=3, value=1, step=0.1)  # เพิ่ม slider สำหรับการปรับความสมูท
            depth_image_slider = gr.Image(label="Depth Map with Slider View", type='numpy', elem_id='img-display-output')
            gray_depth_image = gr.Image(label="Grayscale Depth Map", type='numpy', elem_id='img-display-gray')
            download_gray_depth_file = gr.File(label="Download Grayscale Depth Map", elem_id="download")
        with gr.Column():
            grayscale_image = gr.Image(label="Grayscale Converted Image", type='numpy', elem_id='img-display-grayscale')
            overlay_image = gr.Image(label="Overlay Grayscale and Depth Map", type='numpy', elem_id='img-display-overlay')
            download_overlay_file = gr.File(label="Download Overlay Image", elem_id="download")

    submit = gr.Button(value="Compute Depth")
    raw_file = gr.File(label="16-bit raw output (can be considered as disparity)", elem_id="download")

    cmap = matplotlib.colormaps.get_cmap('Spectral_r')

    # เรียกใช้ฟังก์ชัน on_enhance เมื่อกดปุ่ม Enhance Image
    enhance_btn.click(fn=on_enhance, inputs=[input_image, enhance_factor], outputs=enhanced_image)

    submit.click(on_submit, inputs=[enhanced_image, smooth_factor], outputs=[depth_image_slider, gray_depth_image, grayscale_image, overlay_image, download_gray_depth_file, raw_file, gr.File(label="Converted Grayscale Image", elem_id="download"), download_overlay_file, overlay_image])

    example_files = glob.glob('assets/examples/*.webp')
    examples = [convert_webp_to_png(f) for f in example_files]
    gr.Examples(examples=examples, inputs=[input_image], outputs=[depth_image_slider, gray_depth_image, grayscale_image, overlay_image, download_gray_depth_file, raw_file, gr.File(label="Converted Grayscale Image", elem_id="download"), download_overlay_file, overlay_image], fn=on_submit)

if __name__ == '__main__':
    demo.queue().launch()

    # เปิดเว็บเบราว์เซอร์โดยอัตโนมัติ
    webbrowser.open_new_tab("http://127.0.0.1:7860")