Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,93 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
import gradio as gr
|
4 |
+
import numpy as np
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
from skimage.transform import radon, iradon
|
7 |
+
from scipy.fft import fft, ifft
|
8 |
+
from io import BytesIO
|
9 |
+
import base64
|
10 |
+
from PIL import Image
|
11 |
+
|
12 |
+
def process_image(image):
|
13 |
+
# Orijinal görüntüyü işleme (grayscale'e çevirme)
|
14 |
+
image = Image.open(BytesIO(image)).convert("L")
|
15 |
+
image = np.array(image)
|
16 |
+
|
17 |
+
# Sinogram oluşturma (projeksiyon verileri)
|
18 |
+
theta = np.linspace(0., 180., max(image.shape), endpoint=False)
|
19 |
+
sinogram = radon(image, theta=theta, circle=True)
|
20 |
+
|
21 |
+
# Projeksiyon verilerine Fourier dönüşümü uygulama
|
22 |
+
fourier = fft(sinogram, axis=0)
|
23 |
+
|
24 |
+
# Ramp filtre uygulama
|
25 |
+
freq = np.fft.fftfreq(sinogram.shape[0]).reshape(-1, 1)
|
26 |
+
ramp_filter = np.abs(freq)
|
27 |
+
filtered_fourier = fourier * ramp_filter
|
28 |
+
|
29 |
+
# Ters Fourier dönüşümü uygulama
|
30 |
+
filtered_sinogram = np.real(ifft(filtered_fourier, axis=0))
|
31 |
+
|
32 |
+
# Geri yansıtma (back projection) ile görüntü oluşturma
|
33 |
+
reconstructed_image = iradon(filtered_sinogram, theta=theta, circle=True)
|
34 |
+
|
35 |
+
# Görselleştirme için görüntüler
|
36 |
+
fig, axes = plt.subplots(2, 2, figsize=(10, 10))
|
37 |
+
axes[0, 0].set_title("Orijinal Görüntü")
|
38 |
+
axes[0, 0].imshow(image, cmap="gray")
|
39 |
+
axes[0, 0].axis("off")
|
40 |
+
|
41 |
+
axes[0, 1].set_title("Sinogram")
|
42 |
+
axes[0, 1].imshow(sinogram, cmap="gray", aspect="auto")
|
43 |
+
axes[0, 1].axis("off")
|
44 |
+
|
45 |
+
axes[1, 0].set_title("Filtrelenmiş Sinogram")
|
46 |
+
axes[1, 0].imshow(filtered_sinogram, cmap="gray", aspect="auto")
|
47 |
+
axes[1, 0].axis("off")
|
48 |
+
|
49 |
+
axes[1, 1].set_title("Rekonstürülen Görüntü")
|
50 |
+
axes[1, 1].imshow(reconstructed_image, cmap="gray")
|
51 |
+
axes[1, 1].axis("off")
|
52 |
+
|
53 |
+
plt.tight_layout()
|
54 |
+
|
55 |
+
# Görüntüleri base64 formatında döndürme
|
56 |
+
buf = BytesIO()
|
57 |
+
plt.savefig(buf, format="png")
|
58 |
+
buf.seek(0)
|
59 |
+
encoded_image = base64.b64encode(buf.read()).decode("utf-8")
|
60 |
+
buf.close()
|
61 |
+
plt.close()
|
62 |
+
|
63 |
+
return f"<img src='data:image/png;base64,{encoded_image}'/>"
|
64 |
+
|
65 |
+
# GPT-2 modelini yükleme
|
66 |
+
def gpt2_response(prompt):
|
67 |
+
# GPT-2 modelinden yanıt alma
|
68 |
+
model = gr.load("models/openai-community/gpt2")
|
69 |
+
response = model([prompt])[0]
|
70 |
+
return response
|
71 |
+
|
72 |
+
# Gradio arayüzü tanımlama
|
73 |
+
with gr.Blocks() as demo:
|
74 |
+
gr.Markdown("# GPT-2 Destekli Radon Dönüşümü Görselleştirme")
|
75 |
+
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.")
|
76 |
+
|
77 |
+
with gr.Row():
|
78 |
+
text_prompt = gr.Textbox(label="GPT-2 Prompt", placeholder="Radon dönüşümü için talimat yazın.")
|
79 |
+
gpt2_output = gr.Textbox(label="GPT-2 Yanıtı")
|
80 |
+
|
81 |
+
gpt2_button = gr.Button("GPT-2 Çalıştır")
|
82 |
+
|
83 |
+
with gr.Row():
|
84 |
+
image_input = gr.Image(type="bytes", label="Görüntü Yükle")
|
85 |
+
output = gr.HTML(label="Sonuçlar")
|
86 |
+
|
87 |
+
image_button = gr.Button("Görüntü İşle")
|
88 |
+
|
89 |
+
gpt2_button.click(gpt2_response, inputs=[text_prompt], outputs=[gpt2_output])
|
90 |
+
image_button.click(process_image, inputs=[image_input], outputs=[output])
|
91 |
+
|
92 |
+
if __name__ == "__main__":
|
93 |
+
demo.launch()
|