Ramzan0553 commited on
Commit
1c33f29
·
verified ·
1 Parent(s): 27caa87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -27
app.py CHANGED
@@ -33,40 +33,25 @@ def detect_flash(image: Image.Image):
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()
 
33
 
34
  return output_image, message
35
 
36
+ def clear_all():
37
+ return None, None, ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  # Gradio interface
40
  with gr.Blocks() as demo:
41
  gr.Markdown("Flash Detection with YOLOv8")
42
+ gr.Markdown("Upload an image to detect flash.")
43
 
44
+ with gr.Row():
45
  img_input = gr.Image(type="pil", label="Upload Image")
 
46
  output_img = gr.Image(type="pil", label="Detected Image")
 
 
47
 
48
+ output_text = gr.Textbox(label="Detection Info", lines=6)
49
+
50
+ with gr.Row():
51
+ detect_btn = gr.Button("Detect Flash")
52
+ clear_btn = gr.Button("Clear")
53
+
54
+ detect_btn.click(fn=detect_flash, inputs=img_input, outputs=[output_img, output_text])
55
+ clear_btn.click(fn=clear_all, inputs=[], outputs=[img_input, output_img, output_text])
56
 
57
  demo.launch()