ma4389 commited on
Commit
9ec87d9
·
verified ·
1 Parent(s): cac7257

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -45
app.py CHANGED
@@ -2,58 +2,24 @@ from ultralytics import YOLO
2
  import gradio as gr
3
  from PIL import Image
4
  import numpy as np
5
- import cv2
6
- import tempfile
7
- import os
8
 
9
  # Load the trained YOLOv8 model for human detection
10
- model = YOLO("best.pt") # Replace with your trained model path
11
 
12
- # Define function to handle both images and videos
13
- def detect_humans(input_file):
14
- # If input is a PIL image (from Gradio image upload)
15
- if isinstance(input_file, Image.Image):
16
- results = model(input_file)
17
- result_img = results[0].plot()
18
- return Image.fromarray(result_img.astype(np.uint8))
19
-
20
- # If input is a video file path
21
- elif isinstance(input_file, str) and os.path.isfile(input_file):
22
- cap = cv2.VideoCapture(input_file)
23
-
24
- # Video writer setup
25
- fourcc = cv2.VideoWriter_fourcc(*"mp4v")
26
- temp_output = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
27
- out = cv2.VideoWriter(temp_output.name, fourcc, int(cap.get(cv2.CAP_PROP_FPS)),
28
- (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
29
- int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))
30
-
31
- while True:
32
- ret, frame = cap.read()
33
- if not ret:
34
- break
35
- results = model(frame)
36
- annotated_frame = results[0].plot()
37
- out.write(annotated_frame)
38
-
39
- cap.release()
40
- out.release()
41
-
42
- return temp_output.name # Return path to processed video file
43
-
44
- else:
45
- return None
46
 
47
- # Build Gradio interface (accepts both images and videos)
48
  interface = gr.Interface(
49
  fn=detect_humans,
50
- inputs=gr.File(type="file", label="Upload Image or Video"),
51
- outputs=[
52
- gr.Image(type="pil", label="Detected Humans (Image)"),
53
- gr.Video(label="Detected Humans (Video)")
54
- ],
55
  title="Human Detection (YOLOv8)",
56
- description="Upload an image or video. The model will detect humans using your trained YOLOv8 model."
57
  )
58
 
 
59
  interface.launch(debug=True)
 
2
  import gradio as gr
3
  from PIL import Image
4
  import numpy as np
 
 
 
5
 
6
  # Load the trained YOLOv8 model for human detection
7
+ model = YOLO("best.pt") # Replace with the correct path to your human detection model
8
 
9
+ # Define prediction function
10
+ def detect_humans(image):
11
+ results = model(image) # Perform inference
12
+ result_img = results[0].plot() # Draw bounding boxes
13
+ return Image.fromarray(result_img.astype(np.uint8)) # Return image with boxes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ # Build Gradio interface
16
  interface = gr.Interface(
17
  fn=detect_humans,
18
+ inputs=gr.Image(type="pil", label="Upload Image"),
19
+ outputs=gr.Image(type="pil", label="Detected Humans"),
 
 
 
20
  title="Human Detection (YOLOv8)",
21
+ description="Upload an image. The model will detect humans using your trained YOLOv8 model."
22
  )
23
 
24
+ # Launch the interface
25
  interface.launch(debug=True)