Spaces:
Runtime error
Runtime error
File size: 1,233 Bytes
13aeb5f 7c2b1a0 13aeb5f | 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 | import gradio as gr
from PIL import Image, ImageDraw
import numpy as np
import tensorflow as tf
nn = tf.keras.models.load_model('nn.keras')
def detect_object(img: Image.Image) -> Image.Image:
img_gray = img.convert("L")
arr_gray = np.array(img_gray) # shape = (H, W)
orig_h, orig_w = arr_gray.shape
img_resized = img_gray.resize((64, 64)) # PIL resize
arr_resized = np.array(img_resized).astype("float32") / 255.0
inp = arr_resized.reshape(1, -1)
pred = nn.predict(inp)[0]
x_norm, y_norm, w_norm, h_norm = pred
xmin = x_norm * orig_w
ymin = y_norm * orig_h
xmax = xmin + (w_norm * orig_w)
ymax = ymin + (h_norm * orig_h)
img_out = img.convert("RGB")
draw = ImageDraw.Draw(img_out)
draw.rectangle(
[int(xmin), int(ymin), int(xmax), int(ymax)],
outline="red",
width=2
)
return img_out
interface = gr.Interface(
fn=detect_object,
inputs=gr.Image(type="pil", label="Upload an Image"),
outputs=gr.Image(type="pil", label="Detected object"),
title="Object Detection",
description="Simple Neural Network Model For Object Detection"
)
if __name__ == "__main__":
interface.launch(share=True) |