tcteh86 commited on
Commit
20834b3
·
verified ·
1 Parent(s): 38b3aee

Upload 3 files

Browse files
Files changed (3) hide show
  1. Chicken_Rabbit_Detection.pt +3 -0
  2. app.py +98 -0
  3. requirements.txt +3 -0
Chicken_Rabbit_Detection.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:37801d43221f81f8116d09951d6303d597ed9a1a0169e2b5a2bd43f3c53f1e1d
3
+ size 19195546
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ultralytics import YOLO
2
+ from PIL import Image
3
+ import gradio as gr
4
+ from huggingface_hub import snapshot_download
5
+ from huggingface_hub import hf_hub_download
6
+ import os
7
+ import tempfile
8
+
9
+ MODEL_ID = "Chicken_Rabbit_Detection.pt"
10
+ REPO_ID = "ITI121-25S2/1552444F"
11
+
12
+ def load_model(repo_id):
13
+ # Download the single model file from Hugging Face model repository
14
+
15
+ path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_ID)
16
+
17
+ print(path)
18
+ detection_model = YOLO(path, task='detect')
19
+ return detection_model
20
+
21
+
22
+ def predict(input_data):
23
+ """
24
+ Process either an image (PIL Image) or a video file path.
25
+ Returns processed image or video file path.
26
+ """
27
+ # Check if input is a PIL Image (image upload)
28
+ if isinstance(input_data, Image.Image):
29
+ # Image processing
30
+ source = input_data
31
+ result = detection_model.predict(source, conf=0.5, iou=0.6)
32
+ img_bgr = result[0].plot()
33
+ out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
34
+ return out_pilimg
35
+
36
+ # Otherwise, it's a video file path (string)
37
+ elif isinstance(input_data, str) and os.path.isfile(input_data):
38
+ # Video processing
39
+ # Get the actual temp directory path (resolves /var to /private/var on macOS)
40
+ temp_dir = os.path.realpath(tempfile.gettempdir())
41
+
42
+ # Process video with YOLO
43
+ result = detection_model.predict(
44
+ source=input_data,
45
+ conf=0.75,
46
+ iou=0.6,
47
+ save=True,
48
+ project=temp_dir,
49
+ name='gradio_video_output',
50
+ exist_ok=True
51
+ )
52
+
53
+ # Find the output video file
54
+ # YOLO saves to runs/detect/gradio_video_output/ by default
55
+ save_dir = result[0].save_dir
56
+ # detect_dir = os.path.join(temp_dir, 'runs', 'detect', 'gradio_video_output')
57
+ # Resolve any symlinks to get the actual path
58
+ # detect_dir = os.path.realpath(detect_dir) if os.path.exists(detect_dir) else detect_dir
59
+
60
+ if os.path.exists(save_dir):
61
+ # Find the video file in the output directory
62
+ video_files = [f for f in os.listdir(save_dir) if f.endswith(('.mp4', '.avi', '.mov', '.mkv'))]
63
+ if video_files:
64
+ output_path = os.path.join(save_dir, video_files[0])
65
+ # Resolve the final path to handle any symlinks
66
+ output_path = os.path.realpath(output_path)
67
+ return output_path
68
+
69
+ # Fallback: return input if processing failed
70
+ return input_data
71
+
72
+ else:
73
+ raise ValueError("Input must be either a PIL Image or a video file path")
74
+
75
+ detection_model = load_model(REPO_ID)
76
+
77
+ # Create interface with both image and video inputs
78
+ with gr.Blocks() as demo:
79
+ gr.Markdown("# 🐔🐇 Chicken & Rabbit Detection")
80
+ gr.Markdown(
81
+ "Upload an image or video file to detect **chickens and rabbits** "
82
+ "using a trained YOLO object detection model."
83
+ )
84
+
85
+ with gr.Tabs():
86
+ with gr.TabItem("Image"):
87
+ image_input = gr.Image(type="pil", label="Upload Image")
88
+ image_output = gr.Image(type="pil", label="Detected Chickens & Rabbits")
89
+ image_btn = gr.Button("Detect")
90
+ image_btn.click(fn=predict, inputs=image_input, outputs=image_output)
91
+
92
+ with gr.TabItem("Video"):
93
+ video_input = gr.Video(label="Upload Video")
94
+ video_output = gr.Video(label="Detected Chickens & Rabbits")
95
+ video_btn = gr.Button("Detect")
96
+ video_btn.click(fn=predict, inputs=video_input, outputs=video_output)
97
+
98
+ demo.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ ultralytics
2
+ huggingface_hub
3
+ gradio