Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
from ultralytics import YOLO
|
2 |
from PIL import Image
|
3 |
import gradio as gr
|
4 |
-
from tqdm.auto import tqdm
|
5 |
from huggingface_hub import snapshot_download
|
|
|
6 |
import os
|
7 |
import tempfile
|
8 |
import cv2 # OpenCV for video processing
|
@@ -12,9 +12,7 @@ model_path = "best_int8_openvino_model"
|
|
12 |
|
13 |
def load_model(repo_id):
|
14 |
download_dir = snapshot_download(repo_id)
|
15 |
-
|
16 |
-
path = os.path.join(download_dir, "best_int8_openvino_model")
|
17 |
-
print(path)
|
18 |
detection_model = YOLO(path, task='detect')
|
19 |
return detection_model
|
20 |
|
@@ -26,18 +24,15 @@ def process_image(pilimg):
|
|
26 |
out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
|
27 |
return out_pilimg
|
28 |
|
29 |
-
|
30 |
def process_video(video):
|
31 |
-
print(video)
|
32 |
video_reader = cv2.VideoCapture(video)
|
33 |
-
print(video_reader)
|
34 |
nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT))
|
35 |
frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
36 |
frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH))
|
37 |
fps = video_reader.get(cv2.CAP_PROP_FPS)
|
38 |
|
39 |
temp_dir = tempfile.mkdtemp()
|
40 |
-
output_path = os.path.join(temp_dir, "
|
41 |
video_writer = cv2.VideoWriter(output_path,
|
42 |
cv2.VideoWriter_fourcc(*'mp4v'),
|
43 |
fps,
|
@@ -64,11 +59,13 @@ def process_video(video):
|
|
64 |
cv2.waitKey(1)
|
65 |
return output_path
|
66 |
|
67 |
-
|
68 |
REPO_ID = "leryongfeng/iti107_assignment"
|
69 |
detection_model = load_model(REPO_ID)
|
|
|
|
|
70 |
message = "Sword and Bow detection model"
|
71 |
|
|
|
72 |
# Create the interface for image upload
|
73 |
image_interface = gr.Interface(fn=process_image,
|
74 |
inputs=gr.Image(type="pil"),
|
|
|
1 |
from ultralytics import YOLO
|
2 |
from PIL import Image
|
3 |
import gradio as gr
|
|
|
4 |
from huggingface_hub import snapshot_download
|
5 |
+
from tqdm.auto import tqdm
|
6 |
import os
|
7 |
import tempfile
|
8 |
import cv2 # OpenCV for video processing
|
|
|
12 |
|
13 |
def load_model(repo_id):
|
14 |
download_dir = snapshot_download(repo_id)
|
15 |
+
path = os.path.join(download_dir, "best_int8_openvino_model")
|
|
|
|
|
16 |
detection_model = YOLO(path, task='detect')
|
17 |
return detection_model
|
18 |
|
|
|
24 |
out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
|
25 |
return out_pilimg
|
26 |
|
|
|
27 |
def process_video(video):
|
|
|
28 |
video_reader = cv2.VideoCapture(video)
|
|
|
29 |
nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT))
|
30 |
frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
31 |
frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH))
|
32 |
fps = video_reader.get(cv2.CAP_PROP_FPS)
|
33 |
|
34 |
temp_dir = tempfile.mkdtemp()
|
35 |
+
output_path = os.path.join(temp_dir, "annotated_video.mp4")
|
36 |
video_writer = cv2.VideoWriter(output_path,
|
37 |
cv2.VideoWriter_fourcc(*'mp4v'),
|
38 |
fps,
|
|
|
59 |
cv2.waitKey(1)
|
60 |
return output_path
|
61 |
|
|
|
62 |
REPO_ID = "leryongfeng/iti107_assignment"
|
63 |
detection_model = load_model(REPO_ID)
|
64 |
+
|
65 |
+
# Define a message to display at the top of the app
|
66 |
message = "Sword and Bow detection model"
|
67 |
|
68 |
+
|
69 |
# Create the interface for image upload
|
70 |
image_interface = gr.Interface(fn=process_image,
|
71 |
inputs=gr.Image(type="pil"),
|