Spaces:
Runtime error
Runtime error
portalniy-dev
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2, preprocess_input, decode_predictions
|
4 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
5 |
+
|
6 |
+
# Load pre-trained MobileNetV2 model
|
7 |
+
model = MobileNetV2(weights="imagenet")
|
8 |
+
|
9 |
+
def predict_objects(img):
|
10 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
11 |
+
img_resized = cv2.resize(img, (224, 224))
|
12 |
+
img_array = img_to_array(img_resized)
|
13 |
+
img_array_expanded = preprocess_input(img_array.reshape((1, *img_array.shape)))
|
14 |
+
|
15 |
+
predictions = model.predict(img_array_expanded)
|
16 |
+
label = decode_predictions(predictions, top=1)[0][0][1]
|
17 |
+
|
18 |
+
return img, label
|
19 |
+
|
20 |
+
def draw_box(img, label):
|
21 |
+
height, width, _ = img.shape
|
22 |
+
cv2.rectangle(img, (0, 0), (width, height), (0, 255, 0), 2)
|
23 |
+
cv2.putText(img, label, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
|
24 |
+
return img
|
25 |
+
|
26 |
+
def object_detection(frame):
|
27 |
+
frame, label = predict_objects(frame)
|
28 |
+
frame_with_box = draw_box(frame, label)
|
29 |
+
return frame_with_box
|
30 |
+
|
31 |
+
gr.Interface(
|
32 |
+
fn=object_detection,
|
33 |
+
inputs=gr.Image(source="webcam", streaming=True),
|
34 |
+
outputs="image",
|
35 |
+
live=True
|
36 |
+
).launch()
|