Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
import cv2
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
model_path = "best_int8_openvino_model"
|
10 |
+
|
11 |
+
def load_model(repo_id):
|
12 |
+
download_dir = snapshot_download(repo_id)
|
13 |
+
print(download_dir)
|
14 |
+
path = os.path.join(download_dir, "best_int8_openvino_model")
|
15 |
+
print(path)
|
16 |
+
detection_model = YOLO(path, task='detect')
|
17 |
+
return detection_model
|
18 |
+
|
19 |
+
|
20 |
+
# Image prediction function
|
21 |
+
def predict_image(input_image):
|
22 |
+
source = input_image
|
23 |
+
result = detection_model.predict(source, conf=0.5, iou=0.6)
|
24 |
+
img_bgr = result[0].plot()
|
25 |
+
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB) # Convert BGR to RGB
|
26 |
+
output_image = Image.fromarray(img_rgb) # Use the RGB image for output
|
27 |
+
return output_image
|
28 |
+
|
29 |
+
# Video prediction function
|
30 |
+
def predict_video(input_video):
|
31 |
+
# Get the original filename (without path)
|
32 |
+
original_filename = os.path.basename(input_video.name)
|
33 |
+
|
34 |
+
# Get the name without the extension and append '_detected'
|
35 |
+
base_filename, _ = os.path.splitext(original_filename)
|
36 |
+
output_video = base_filename + "_detected.mp4"
|
37 |
+
|
38 |
+
# Read video file
|
39 |
+
video_capture = cv2.VideoCapture(input_video.name)
|
40 |
+
frames = []
|
41 |
+
|
42 |
+
while True:
|
43 |
+
ret, frame = video_capture.read()
|
44 |
+
if not ret:
|
45 |
+
break
|
46 |
+
|
47 |
+
# Process each frame
|
48 |
+
result = detection_model.predict(frame, conf=0.5, iou=0.6)
|
49 |
+
img_bgr = result[0].plot() # Get the frame with detected objects
|
50 |
+
frames.append(img_bgr) # Add the RGB frame to the list
|
51 |
+
|
52 |
+
# Release video capture
|
53 |
+
video_capture.release()
|
54 |
+
|
55 |
+
# Convert frames to video
|
56 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for mp4
|
57 |
+
out = cv2.VideoWriter(output_video, fourcc, 30, (frames[0].shape[1], frames[0].shape[0]))
|
58 |
+
|
59 |
+
for frame in frames:
|
60 |
+
out.write(frame) # Write each frame to video
|
61 |
+
|
62 |
+
out.release()
|
63 |
+
|
64 |
+
return output_video # Return the path to the output video
|
65 |
+
|
66 |
+
|
67 |
+
REPO_ID = "AI-Pagoda/4673483T"
|
68 |
+
detection_model = load_model(REPO_ID)
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
# Create Gradio interface with tabs
|
73 |
+
with gr.Blocks() as app:
|
74 |
+
with gr.Tabs():
|
75 |
+
with gr.Tab("Image Detection"):
|
76 |
+
gr.Interface(fn=predict_image,
|
77 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
78 |
+
outputs=gr.Image(type="pil", label="Download Image"),
|
79 |
+
title="Image Object Detection",
|
80 |
+
description="Upload an image to detect Snake and Lizard.")
|
81 |
+
|
82 |
+
with gr.Tab("Video Detection"):
|
83 |
+
gr.Interface(fn=predict_video,
|
84 |
+
inputs=gr.File(label="Upload Video"),
|
85 |
+
outputs=gr.File(label="Download Video"),
|
86 |
+
title="Video Object Detection",
|
87 |
+
description="Upload an image to detect Snake and Lizard.")
|
88 |
+
|
89 |
+
app.launch()
|