File size: 3,102 Bytes
9c82b9e
 
f483af5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr

import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
from skimage.transform import radon, iradon
from scipy.fft import fft, ifft
from io import BytesIO
import base64
from PIL import Image

def process_image(image):
    # Orijinal görüntüyü işleme (grayscale'e çevirme)
    image = Image.open(BytesIO(image)).convert("L")
    image = np.array(image)

    # Sinogram oluşturma (projeksiyon verileri)
    theta = np.linspace(0., 180., max(image.shape), endpoint=False)
    sinogram = radon(image, theta=theta, circle=True)

    # Projeksiyon verilerine Fourier dönüşümü uygulama
    fourier = fft(sinogram, axis=0)

    # Ramp filtre uygulama
    freq = np.fft.fftfreq(sinogram.shape[0]).reshape(-1, 1)
    ramp_filter = np.abs(freq)
    filtered_fourier = fourier * ramp_filter

    # Ters Fourier dönüşümü uygulama
    filtered_sinogram = np.real(ifft(filtered_fourier, axis=0))

    # Geri yansıtma (back projection) ile görüntü oluşturma
    reconstructed_image = iradon(filtered_sinogram, theta=theta, circle=True)

    # Görselleştirme için görüntüler
    fig, axes = plt.subplots(2, 2, figsize=(10, 10))
    axes[0, 0].set_title("Orijinal Görüntü")
    axes[0, 0].imshow(image, cmap="gray")
    axes[0, 0].axis("off")

    axes[0, 1].set_title("Sinogram")
    axes[0, 1].imshow(sinogram, cmap="gray", aspect="auto")
    axes[0, 1].axis("off")

    axes[1, 0].set_title("Filtrelenmiş Sinogram")
    axes[1, 0].imshow(filtered_sinogram, cmap="gray", aspect="auto")
    axes[1, 0].axis("off")

    axes[1, 1].set_title("Rekonstürülen Görüntü")
    axes[1, 1].imshow(reconstructed_image, cmap="gray")
    axes[1, 1].axis("off")

    plt.tight_layout()

    # Görüntüleri base64 formatında döndürme
    buf = BytesIO()
    plt.savefig(buf, format="png")
    buf.seek(0)
    encoded_image = base64.b64encode(buf.read()).decode("utf-8")
    buf.close()
    plt.close()

    return f"<img src='data:image/png;base64,{encoded_image}'/>"

# GPT-2 modelini yükleme
def gpt2_response(prompt):
    # GPT-2 modelinden yanıt alma
    model = gr.load("models/openai-community/gpt2")
    response = model([prompt])[0]
    return response

# Gradio arayüzü tanımlama
with gr.Blocks() as demo:
    gr.Markdown("# GPT-2 Destekli Radon Dönüşümü Görselleştirme")
    gr.Markdown("Bir görüntü yükleyin, GPT-2 modelinden işlem açıklamaları alın ve Radon dönüşümünü uygulayın.")

    with gr.Row():
        text_prompt = gr.Textbox(label="GPT-2 Prompt", placeholder="Radon dönüşümü için talimat yazın.")
        gpt2_output = gr.Textbox(label="GPT-2 Yanıtı")

    gpt2_button = gr.Button("GPT-2 Çalıştır")

    with gr.Row():
        image_input = gr.Image(type="bytes", label="Görüntü Yükle")
        output = gr.HTML(label="Sonuçlar")

    image_button = gr.Button("Görüntü İşle")

    gpt2_button.click(gpt2_response, inputs=[text_prompt], outputs=[gpt2_output])
    image_button.click(process_image, inputs=[image_input], outputs=[output])

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