Spaces:
Sleeping
Sleeping
app update
Browse files
app.py
CHANGED
@@ -1,10 +1,8 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import snapshot_download
|
3 |
-
from ultralytics import YOLO
|
4 |
import os
|
5 |
-
from PIL import Image
|
6 |
-
import cv2
|
7 |
-
import tempfile
|
8 |
|
9 |
#public model path location
|
10 |
#MODEL_REPO_ID = "mintheinwin/3907578Y"
|
@@ -12,93 +10,34 @@ import tempfile
|
|
12 |
#Organizations model path location
|
13 |
MODEL_REPO_ID = "ITI107-2024S2/3907578Y"
|
14 |
|
15 |
-
#
|
16 |
def load_model(repo_id):
|
17 |
download_dir = snapshot_download(repo_id)
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
return detection_model
|
21 |
-
|
22 |
-
|
23 |
detection_model = load_model(MODEL_REPO_ID)
|
24 |
|
25 |
#Student ID
|
26 |
student_info = "Student Id: 3907578Y, Name: Min Thein Win"
|
27 |
|
28 |
-
#
|
29 |
-
def
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
33 |
return out_pilimg
|
34 |
-
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
break
|
45 |
-
|
46 |
-
# Detection
|
47 |
-
result = detection_model.predict(frame, conf=0.5, iou=0.5)
|
48 |
-
annotated_frame = result[0].plot()
|
49 |
-
frames.append(annotated_frame)
|
50 |
-
|
51 |
-
cap.release()
|
52 |
-
|
53 |
-
# Save annotated video
|
54 |
-
height, width, _ = frames[0].shape
|
55 |
-
output_path = os.path.join(temp_dir, "annotated_video.mp4")
|
56 |
-
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), 20, (width, height))
|
57 |
-
|
58 |
-
for frame in frames:
|
59 |
-
out.write(frame)
|
60 |
-
|
61 |
-
out.release()
|
62 |
-
return output_path
|
63 |
-
|
64 |
-
# Unified prediction function
|
65 |
-
def unified_predict(file):
|
66 |
-
if isinstance(file, Image.Image):
|
67 |
-
# If the input is a PIL Image, treat it as an image
|
68 |
-
return predict_image(file)
|
69 |
-
elif isinstance(file, str) and file.endswith(('.mp4', '.avi', '.mov')):
|
70 |
-
# If the input is a video file path, treat it as a video
|
71 |
-
return predict_video(file)
|
72 |
-
else:
|
73 |
-
raise ValueError("Unsupported file type. Please upload an image or a video.")
|
74 |
-
|
75 |
-
# UI Interface
|
76 |
-
with gr.Blocks() as interface:
|
77 |
-
gr.Markdown("# Wild Animal Detection (Tiger/Lion)")
|
78 |
-
gr.Markdown(student_info)
|
79 |
-
|
80 |
-
# Unified Section
|
81 |
-
with gr.Row():
|
82 |
-
with gr.Column():
|
83 |
-
gr.Markdown("### Upload an Image or Video:")
|
84 |
-
input_file = gr.File(label="Input File")
|
85 |
-
|
86 |
-
with gr.Column():
|
87 |
-
gr.Markdown("### Detection Results:")
|
88 |
-
output_display = gr.Output(label="Output")
|
89 |
-
|
90 |
-
submit_btn = gr.Button("Detect Objects")
|
91 |
-
|
92 |
-
def process_file(file):
|
93 |
-
if file.name.endswith((".jpg", ".jpeg", ".png")):
|
94 |
-
pil_image = Image.open(file.name)
|
95 |
-
return predict_image(pil_image)
|
96 |
-
elif file.name.endswith((".mp4", ".avi", ".mov")):
|
97 |
-
return predict_video(file.name)
|
98 |
-
else:
|
99 |
-
return "Unsupported file type. Please upload an image or a video."
|
100 |
-
|
101 |
-
submit_btn.click(fn=process_file, inputs=input_file, outputs=output_display)
|
102 |
-
|
103 |
-
# Launch app
|
104 |
-
interface.launch(share=True)
|
|
|
1 |
+
from ultralytics import YOLO
|
2 |
+
from PIL import Image
|
3 |
import gradio as gr
|
4 |
from huggingface_hub import snapshot_download
|
|
|
5 |
import os
|
|
|
|
|
|
|
6 |
|
7 |
#public model path location
|
8 |
#MODEL_REPO_ID = "mintheinwin/3907578Y"
|
|
|
10 |
#Organizations model path location
|
11 |
MODEL_REPO_ID = "ITI107-2024S2/3907578Y"
|
12 |
|
13 |
+
#load model
|
14 |
def load_model(repo_id):
|
15 |
download_dir = snapshot_download(repo_id)
|
16 |
+
print(download_dir)
|
17 |
+
path = os.path.join(download_dir, "best_int8_openvino_model")
|
18 |
+
print(path)
|
19 |
+
detection_model = YOLO(path, task='detect')
|
20 |
return detection_model
|
21 |
+
|
|
|
22 |
detection_model = load_model(MODEL_REPO_ID)
|
23 |
|
24 |
#Student ID
|
25 |
student_info = "Student Id: 3907578Y, Name: Min Thein Win"
|
26 |
|
27 |
+
#prdeict
|
28 |
+
def predict(pilimg):
|
29 |
+
source = pilimg
|
30 |
+
result = detection_model.predict(source, conf=0.5, iou=0.5)
|
31 |
+
img_bgr = result[0].plot()
|
32 |
+
out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
|
33 |
return out_pilimg
|
34 |
+
|
35 |
+
#UI interface
|
36 |
+
gr.Markdown("# Wild Animal Detection (Tiger/Lion)")
|
37 |
+
gr.Markdown(student_info)
|
38 |
+
gr.Interface(fn=predict,
|
39 |
+
inputs=gr.Image(type="pil",label="Input"),
|
40 |
+
outputs=gr.Image(type="pil",label="Output"),
|
41 |
+
title="Wild Animal Detection (Tiger/Lion)",
|
42 |
+
description=student_info,
|
43 |
+
).launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|