Spaces:
Runtime error
Runtime error
Chia Woon Yap
commited on
Upload 2 files
Browse files- app.py +56 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""app
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1GzjDFYPEtsFsBFnhi3x3B0vWyCE-Dtpb
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import os
|
| 11 |
+
from PIL import Image
|
| 12 |
+
import gradio as gr
|
| 13 |
+
from huggingface_hub import snapshot_download
|
| 14 |
+
|
| 15 |
+
# Ensure necessary packages are installed
|
| 16 |
+
try:
|
| 17 |
+
from ultralytics import YOLO
|
| 18 |
+
except ImportError:
|
| 19 |
+
import subprocess
|
| 20 |
+
subprocess.check_call(["pip", "install", "ultralytics"])
|
| 21 |
+
from ultralytics import YOLO
|
| 22 |
+
|
| 23 |
+
model_path = "/Users/markk/Downloads/best_int8_openvino_model"
|
| 24 |
+
|
| 25 |
+
def load_model(repo_id):
|
| 26 |
+
download_dir = snapshot_download(repo_id)
|
| 27 |
+
print(download_dir)
|
| 28 |
+
path = os.path.join(download_dir, "best_int8_openvino_model")
|
| 29 |
+
print(path)
|
| 30 |
+
detection_model = YOLO(path, task='detect')
|
| 31 |
+
return detection_model
|
| 32 |
+
|
| 33 |
+
def predict(pilimg):
|
| 34 |
+
source = pilimg
|
| 35 |
+
result = detection_model.predict(source, conf=0.5, iou=0.6)
|
| 36 |
+
img_bgr = result[0].plot()
|
| 37 |
+
out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
|
| 38 |
+
|
| 39 |
+
return out_pilimg
|
| 40 |
+
|
| 41 |
+
def predict_video(video_path):
|
| 42 |
+
result = detection_model.predict(video_path, conf=0.5, iou=0.6)
|
| 43 |
+
return result[0].save("output_video.mp4") # Save processed video
|
| 44 |
+
|
| 45 |
+
REPO_ID = "Lesterchia174/Monkey_Durian"
|
| 46 |
+
detection_model = load_model(REPO_ID)
|
| 47 |
+
|
| 48 |
+
gr.Interface(
|
| 49 |
+
fn=predict,
|
| 50 |
+
inputs=gr.Image(type="pil"),
|
| 51 |
+
outputs=gr.Image(type="pil")
|
| 52 |
+
).launch(share=True)
|
| 53 |
+
|
| 54 |
+
# Process the new video file
|
| 55 |
+
video_path = "Monkey_Durian.mp4"
|
| 56 |
+
predict_video(video_path)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics
|
| 2 |
+
huggingface_hub
|