Shatha2030 commited on
Commit
dd0823e
·
verified ·
1 Parent(s): 609a06b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +206 -0
app.py ADDED
@@ -0,0 +1,206 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ from PIL import Image
4
+ import cv2
5
+ import numpy as np
6
+ import tempfile
7
+ import os
8
+ from pathlib import Path
9
+
10
+ # Load the YOLOv8 model
11
+ model = YOLO('yolov8n.pt')
12
+
13
+ def process_image(image):
14
+ """
15
+ Process a single image for object detection
16
+ """
17
+ results = model(image)
18
+ # Get detection information
19
+ boxes = results[0].boxes
20
+ detection_info = []
21
+ for box in boxes:
22
+ class_id = int(box.cls[0])
23
+ class_name = results[0].names[class_id]
24
+ confidence = float(box.conf[0])
25
+ detection_info.append(f"{class_name}: {confidence:.2%}")
26
+
27
+ return Image.fromarray(results[0].plot()), "\n".join(detection_info)
28
+
29
+ def process_video(video_path):
30
+ """
31
+ Process video for object detection
32
+ """
33
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_file:
34
+ output_path = temp_file.name
35
+
36
+ cap = cv2.VideoCapture(video_path)
37
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
38
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
39
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
40
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
41
+
42
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
43
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
44
+
45
+ detection_summary = []
46
+ frame_count = 0
47
+
48
+ try:
49
+ while cap.isOpened():
50
+ ret, frame = cap.read()
51
+ if not ret:
52
+ break
53
+
54
+ frame_count += 1
55
+ results = model(frame)
56
+
57
+ # Collect detection information for this frame
58
+ if frame_count % int(fps) == 0: # Sample every second
59
+ for box in results[0].boxes:
60
+ class_id = int(box.cls[0])
61
+ class_name = results[0].names[class_id]
62
+ detection_summary.append(class_name)
63
+
64
+ annotated_frame = results[0].plot()
65
+ out.write(annotated_frame)
66
+
67
+ finally:
68
+ cap.release()
69
+ out.release()
70
+
71
+ # Create summary of detected objects
72
+ if detection_summary:
73
+ from collections import Counter
74
+ counts = Counter(detection_summary)
75
+ summary = "\n".join([f"{obj}: {count} occurrences" for obj, count in counts.most_common()])
76
+ else:
77
+ summary = "No objects detected"
78
+
79
+ return output_path, summary
80
+
81
+ def detect_objects(media):
82
+ """
83
+ Unified function to handle both image and video inputs
84
+ """
85
+ if media is None:
86
+ return None, None, None, "Please upload an image or video to begin detection.", gr.update(visible=True), gr.update(visible=False)
87
+
88
+ try:
89
+ if isinstance(media, str) and media.lower().endswith(('.mp4', '.avi', '.mov')):
90
+ output_video, detection_summary = process_video(media)
91
+ return (None, output_video, detection_summary,
92
+ "✅ Video processing complete! Check the detection summary below.",
93
+ gr.update(visible=False), gr.update(visible=True))
94
+ else:
95
+ if isinstance(media, str):
96
+ image = cv2.imread(media)
97
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
98
+ else:
99
+ image = media
100
+ processed_image, detection_info = process_image(image)
101
+ return (processed_image, None, detection_info,
102
+ "✅ Image processing complete! Check the detections below.",
103
+ gr.update(visible=True), gr.update(visible=False))
104
+ except Exception as e:
105
+ return None, None, None, f"❌ Error: {str(e)}", gr.update(visible=False), gr.update(visible=False)
106
+
107
+ # Custom CSS for styling
108
+ custom_css = """
109
+ #app-container {
110
+ max-width: 1200px;
111
+ margin: 0 auto;
112
+ padding: 20px;
113
+ }
114
+ #logo-img {
115
+ max-height: 100px;
116
+ margin-bottom: 20px;
117
+ }
118
+ .upload-box {
119
+ border: 2px dashed #ccc;
120
+ padding: 20px;
121
+ text-align: center;
122
+ border-radius: 8px;
123
+ background-color: #f8f9fa;
124
+ margin: 20px 0;
125
+ }
126
+ .results-container {
127
+ background-color: #ffffff;
128
+ border-radius: 8px;
129
+ padding: 15px;
130
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
131
+ margin-top: 20px;
132
+ }
133
+ .detection-info {
134
+ background-color: #f8f9fa;
135
+ padding: 15px;
136
+ border-radius: 8px;
137
+ margin-top: 10px;
138
+ font-family: monospace;
139
+ }
140
+ .center {
141
+ display: flex;
142
+ justify-content: center;
143
+ align-items: center;
144
+ margin-bottom: 1rem;
145
+ }
146
+ """
147
+
148
+ # Create Gradio interface
149
+ with gr.Blocks(css=custom_css) as demo:
150
+ with gr.Column(elem_id="app-container"):
151
+ # Logo and Header
152
+ with gr.Column(elem_classes="center"):
153
+ gr.Image("logo-h.png",
154
+ show_label=False,
155
+ container=False,
156
+ elem_id="logo-img",
157
+ height=100)
158
+
159
+ gr.Markdown("# 🔍 Object Detection")
160
+
161
+ # Upload Section
162
+ with gr.Column(elem_classes="upload-box"):
163
+ gr.Markdown("### 📤 Upload your file")
164
+ input_media = gr.File(
165
+ label="Drag and drop or click to upload (Images: jpg, jpeg, png | Videos: mp4, avi, mov)",
166
+ file_types=["image", "video"]
167
+ )
168
+
169
+ # Status Message
170
+ status_text = gr.Textbox(
171
+ label="Status",
172
+ value="Waiting for upload...",
173
+ interactive=False
174
+ )
175
+
176
+ # Detection Information
177
+ detection_info = gr.Textbox(
178
+ label="Detection Results",
179
+ elem_classes="detection-info",
180
+ interactive=False
181
+ )
182
+
183
+ # Results Section
184
+ with gr.Column(elem_classes="results-container"):
185
+ with gr.Row():
186
+ with gr.Column(visible=False) as image_column:
187
+ output_image = gr.Image(label="Detected Objects")
188
+ with gr.Column(visible=False) as video_column:
189
+ output_video = gr.Video(label="Processed Video")
190
+
191
+ # Handle file upload
192
+ input_media.upload(
193
+ fn=detect_objects,
194
+ inputs=[input_media],
195
+ outputs=[
196
+ output_image,
197
+ output_video,
198
+ detection_info,
199
+ status_text,
200
+ image_column,
201
+ video_column
202
+ ]
203
+ )
204
+
205
+ if __name__ == "__main__":
206
+ demo.launch(share=True)