Image_Sense / app.py
MehmetK's picture
Update app.py
f483af5 verified
raw
history blame
3.1 kB
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()