Spaces:
Runtime error
Runtime error
Commit ·
13aeb5f
1
Parent(s): 4e56be0
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageDraw
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
|
| 7 |
+
nn = tf.keras.models.load_model('nn.keras')
|
| 8 |
+
|
| 9 |
+
def detect_object(img):
|
| 10 |
+
draw = ImageDraw.Draw(img)
|
| 11 |
+
|
| 12 |
+
img_array = img.convert("L")
|
| 13 |
+
img_array = np.array(img_array)
|
| 14 |
+
orig_h, orig_w = img_array.shape
|
| 15 |
+
|
| 16 |
+
img_re = cv2.resize(img_array, (64, 64))
|
| 17 |
+
img_norm = img_re.astype('float32') / 255.0
|
| 18 |
+
|
| 19 |
+
result = nn.predict(img_norm.reshape(1, -1))
|
| 20 |
+
result *= [orig_w, orig_h, orig_w, orig_h]
|
| 21 |
+
|
| 22 |
+
xmin, ymin, xmax, ymax = result[0]
|
| 23 |
+
xmax += xmin
|
| 24 |
+
ymax += ymin
|
| 25 |
+
|
| 26 |
+
draw.rectangle([xmin, ymin, xmax, ymax], outline="red", width=2)
|
| 27 |
+
|
| 28 |
+
return img
|
| 29 |
+
|
| 30 |
+
interface = gr.Interface(
|
| 31 |
+
fn=detect_object,
|
| 32 |
+
inputs=gr.Image(type="pil", label="Upload an Image"),
|
| 33 |
+
outputs=gr.Image(type="pil", label="Detected object"),
|
| 34 |
+
title="Object Detection",
|
| 35 |
+
description="Simple Neural Network Model For Object Detection"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
interface.launch(share=True)
|