Spaces:
Running
Running
Upload 5 files
Browse files- .gitattributes +1 -0
- app.py +78 -0
- harry_potter_short.mp4 +3 -0
- image_test1.jpg +0 -0
- image_test2.jpg +0 -0
- requirements.txt +6 -4
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
harry_potter_short.mp4 filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import cv2
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
# Load model
|
| 8 |
+
model = YOLO("best.pt")
|
| 9 |
+
|
| 10 |
+
# ---------------- IMAGE DETECTION ----------------
|
| 11 |
+
def detect(image):
|
| 12 |
+
results = model.predict(image, conf=0.4, iou=0.5)
|
| 13 |
+
return results[0].plot()
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# ---------------- VIDEO DETECTION ----------------
|
| 17 |
+
def detect_video(video):
|
| 18 |
+
|
| 19 |
+
input_path = video
|
| 20 |
+
output_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
|
| 21 |
+
|
| 22 |
+
cap = cv2.VideoCapture(input_path)
|
| 23 |
+
|
| 24 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 25 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 26 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 27 |
+
|
| 28 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 29 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 30 |
+
|
| 31 |
+
while True:
|
| 32 |
+
ret, frame = cap.read()
|
| 33 |
+
if not ret:
|
| 34 |
+
break
|
| 35 |
+
|
| 36 |
+
results = model.predict(frame, conf=0.4, iou=0.5)
|
| 37 |
+
r = results[0]
|
| 38 |
+
|
| 39 |
+
frame = r.plot()
|
| 40 |
+
out.write(frame)
|
| 41 |
+
|
| 42 |
+
cap.release()
|
| 43 |
+
out.release()
|
| 44 |
+
|
| 45 |
+
return output_path
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# ---------------- UI ----------------
|
| 49 |
+
image_ui = gr.Interface(
|
| 50 |
+
fn=detect,
|
| 51 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 52 |
+
outputs=gr.Image(label="Detection Result"),
|
| 53 |
+
title="Spectacles and Teapots Object Detection - 4124139E",
|
| 54 |
+
description="Upload an image or choose any example at the bottom for object detection.",
|
| 55 |
+
examples=[
|
| 56 |
+
["image_test1.jpg"],
|
| 57 |
+
["image_test2.jpg"],
|
| 58 |
+
],
|
| 59 |
+
cache_examples=False,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
video_ui = gr.Interface(
|
| 63 |
+
fn=detect_video,
|
| 64 |
+
inputs=gr.Video(label="Upload Video (.mp4)"),
|
| 65 |
+
outputs=gr.Video(label="Processed Video"),
|
| 66 |
+
title="Spectacles and Teapots Object Detection - 4124139E",
|
| 67 |
+
description="Upload a short video or choose any example at the bottom for object detection.",
|
| 68 |
+
examples=[
|
| 69 |
+
["harry_potter_short.mp4"]
|
| 70 |
+
]
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
demo = gr.TabbedInterface(
|
| 74 |
+
[image_ui, video_ui],
|
| 75 |
+
["Images", "Video"]
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
harry_potter_short.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:351b6cc9af9d2ee206665c92d69f0d94a85059c90e014735a86d609903b61ad2
|
| 3 |
+
size 40903103
|
image_test1.jpg
ADDED
|
image_test2.jpg
ADDED
|
requirements.txt
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics
|
| 2 |
+
# huggingface_hub
|
| 3 |
+
gradio
|
| 4 |
+
opencv-python
|
| 5 |
+
numpy
|
| 6 |
+
Pillow
|