Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,18 +4,16 @@ import cv2
|
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
|
7 |
-
# Load
|
8 |
MODEL_PATH = "Best_Model1.pt"
|
9 |
model = YOLO(MODEL_PATH)
|
10 |
|
11 |
-
def
|
12 |
# Convert PIL image to OpenCV format
|
13 |
image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
14 |
|
15 |
# Run detection
|
16 |
results = model(image_cv)
|
17 |
-
|
18 |
-
# Extract detection info
|
19 |
detections = results[0].boxes.data.tolist()
|
20 |
num_detections = len(detections)
|
21 |
|
@@ -35,23 +33,40 @@ def detect_flash_gradio(image: Image.Image):
|
|
35 |
|
36 |
return output_image, message
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
# Gradio interface
|
39 |
with gr.Blocks() as demo:
|
40 |
gr.Markdown("Flash Detection with YOLOv8")
|
41 |
-
gr.Markdown("Upload an image or
|
42 |
|
43 |
with gr.Tab("Upload Image"):
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
with gr.Tab("Use Webcam"):
|
51 |
-
|
52 |
webcam_btn = gr.Button("Detect Flash")
|
53 |
-
|
54 |
-
|
55 |
-
webcam_btn.click(fn=
|
56 |
|
57 |
demo.launch()
|
|
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
|
7 |
+
# Load YOLOv8 model
|
8 |
MODEL_PATH = "Best_Model1.pt"
|
9 |
model = YOLO(MODEL_PATH)
|
10 |
|
11 |
+
def detect_flash(image: Image.Image):
|
12 |
# Convert PIL image to OpenCV format
|
13 |
image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
14 |
|
15 |
# Run detection
|
16 |
results = model(image_cv)
|
|
|
|
|
17 |
detections = results[0].boxes.data.tolist()
|
18 |
num_detections = len(detections)
|
19 |
|
|
|
33 |
|
34 |
return output_image, message
|
35 |
|
36 |
+
def extract_first_frame(video):
|
37 |
+
cap = cv2.VideoCapture(video)
|
38 |
+
success, frame = cap.read()
|
39 |
+
cap.release()
|
40 |
+
if success:
|
41 |
+
# Convert BGR to RGB
|
42 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
43 |
+
return Image.fromarray(frame_rgb)
|
44 |
+
return None
|
45 |
+
|
46 |
+
def process_webcam_video(video):
|
47 |
+
image = extract_first_frame(video)
|
48 |
+
if image:
|
49 |
+
return detect_flash(image)
|
50 |
+
else:
|
51 |
+
return None, "Could not read frame from video."
|
52 |
+
|
53 |
# Gradio interface
|
54 |
with gr.Blocks() as demo:
|
55 |
gr.Markdown("Flash Detection with YOLOv8")
|
56 |
+
gr.Markdown("Upload an image or record from your webcam to detect flash.")
|
57 |
|
58 |
with gr.Tab("Upload Image"):
|
59 |
+
img_input = gr.Image(type="pil", label="Upload Image")
|
60 |
+
detect_btn = gr.Button("Detect Flash")
|
61 |
+
output_img = gr.Image(type="pil", label="Detected Image")
|
62 |
+
output_text = gr.Textbox(label="Detection Info")
|
63 |
+
detect_btn.click(fn=detect_flash, inputs=img_input, outputs=[output_img, output_text])
|
64 |
+
|
65 |
+
with gr.Tab("Use Webcam (Record Video)"):
|
66 |
+
webcam_video = gr.Video(label="Record from Webcam")
|
67 |
webcam_btn = gr.Button("Detect Flash")
|
68 |
+
webcam_img = gr.Image(type="pil", label="Detected Image")
|
69 |
+
webcam_text = gr.Textbox(label="Detection Info")
|
70 |
+
webcam_btn.click(fn=process_webcam_video, inputs=webcam_video, outputs=[webcam_img, webcam_text])
|
71 |
|
72 |
demo.launch()
|