Spaces:
Runtime error
Runtime error
File size: 2,317 Bytes
15872da 8ca3087 15872da 8ca3087 15872da ce71368 6632d28 0147689 6632d28 ce71368 761ccc2 ce71368 b0b4e93 ce71368 15872da ce71368 15872da ce71368 cd15fd0 ce71368 15872da ce71368 15872da ce71368 15872da ce71368 15872da ce71368 |
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 |
import torch
import gradio as gr
from PIL import Image
import numpy as np
# بارگذاری مدل
model_path = "sapiens_0.3b_render_people_epoch_100_torchscript.pt2"
model = torch.jit.load(model_path, map_location=torch.device('cpu'))
model.eval()
def predict(image):
try:
print("Predict function called")
# تغییر اندازه تصویر به 768x1024
image = image.resize((768, 1024)) # تغییر اندازه به 768x1024
# پیشپردازش تصویر
image = image.convert("RGB")
input_tensor = np.array(image)
input_tensor = input_tensor.transpose(2, 0, 1) # تبدیل از HWC به CHW
input_tensor = input_tensor[np.newaxis, :] # افزودن بعد batch
input_tensor = input_tensor / 255.0 # نرمالسازی
input_tensor = torch.from_numpy(input_tensor).float()
print(f"Input tensor shape: {input_tensor.shape}")
# اجرای مدل
with torch.no_grad():
output = model(input_tensor)
print(f"Output tensor shape: {output.shape}")
# پسپردازش خروجی
output_image = output.squeeze().cpu().numpy()
# اگر خروجی تک کاناله است، به RGB تبدیل میشود
if output_image.ndim == 2: # فقط در صورتی که تک کاناله است
output_image = np.stack([output_image] * 3, axis=-1)
elif output_image.shape[0] == 1: # اگر کانال اول 1 است، آن را به RGB تبدیل کنید
output_image = np.tile(output_image, (3, 1, 1))
output_image = output_image.transpose(1, 2, 0)
output_image = (output_image * 255).astype(np.uint8)
output_image = Image.fromarray(output_image)
print("Output image generated successfully")
return output_image
except Exception as e:
print(f"Error during prediction: {str(e)}")
return None
# تعریف رابط Gradio
iface = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil", label="Input Image"),
outputs=gr.Image(type="pil", label="Output Image"),
title="Sapiens Model Inference",
description="Upload an image to process with the Sapiens model."
)
if __name__ == "__main__":
iface.launch()
|