File size: 8,418 Bytes
6166c03 2cce14f 6166c03 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | <<<<<<< HEAD
import gradio as gr
import os
import json
from ultralytics import YOLO
import supervision as sv
# --- 1. CONFIGURATION ---
# Folder where the script is
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# Go up one level to Om_Singh
PROJECT_DIR = os.path.dirname(BASE_DIR) # parent folder of app/
# Paths for outputs in video_and_json folder
VIDEO_DIR = os.path.join(PROJECT_DIR, "video_and_json") # Om_Singh/video_and_json
os.makedirs(VIDEO_DIR, exist_ok=True) # ensure folder exists
MODEL_PATH = os.path.join(BASE_DIR, "best.pt") # model stays in app/
OUTPUT_VIDEO_PATH = os.path.join(VIDEO_DIR, "output.mp4")
OUTPUT_JSON_PATH = os.path.join(VIDEO_DIR, "result.json")
def process_video(INPUT_VIDEO_PATH, OUTPUT_VIDEO_PATH, OUTPUT_JSON_PATH):
print("Loading model...")
model = YOLO(MODEL_PATH)
print("Initializing tracker and annotators...")
tracker = sv.ByteTrack()
box_annotator = sv.BoxAnnotator(thickness=5)
label_annotator = sv.LabelAnnotator(text_position=sv.Position.TOP_CENTER, text_scale = 1, text_thickness= 1)
sv.LabelAnnotator()
frame_generator = sv.get_video_frames_generator(source_path=INPUT_VIDEO_PATH)
video_info = sv.VideoInfo.from_video_path(INPUT_VIDEO_PATH)
results_list = []
with sv.VideoSink(target_path=OUTPUT_VIDEO_PATH, video_info=video_info) as sink:
print("Processing video frames...")
for frame_number, frame in enumerate(frame_generator):
# Run YOLO prediction
results = model(frame)[0]
detections = sv.Detections.from_ultralytics(results)
# Update tracker
tracked_detections = tracker.update_with_detections(detections=detections)
# Prepare labels
labels = [
f"ID: {det[4]} {model.model.names[int(det[3])]}"
for det in tracked_detections
]
# Annotate frame
annotated_frame = box_annotator.annotate(scene=frame.copy(), detections=tracked_detections)
annotated_frame = label_annotator.annotate(scene=annotated_frame, detections=tracked_detections, labels=labels)
# Save tracking info
for det in tracked_detections:
bbox = det[0]
conf = det[2]
class_id = int(det[3])
tracker_id = det[4]
results_list.append({
"frame_number": frame_number,
"track_id": int(tracker_id),
"class": model.model.names[class_id],
"confidence": float(conf),
"bounding_box": [int(coord) for coord in bbox]
})
# Write annotated frame
sink.write_frame(frame=annotated_frame)
if frame_number % 30 == 0:
print(f"Processed frame {frame_number}...")
print("Video processing complete. Saving results...")
with open(OUTPUT_JSON_PATH, 'w') as f:
json.dump(results_list, f, indent=4)
print("--- All tasks finished successfully! ---")
# --- Main processing function ---
def process(input_video):
output_video = "output.mp4"
output_json = "result.json"
# During processing: red text
status_html = "<p style='color:red; font-weight:bold;'>Processing...</p>"
# Run video processing
process_video(input_video, output_video, output_json)
# After processing: green text
status_html = "<p style='color:limegreen; font-weight:bold;'>Processing complete!</p>"
return status_html, output_video, output_json
# --- Gradio UI ---
with gr.Blocks() as demo:
gr.Markdown("<h1 style='text-align:center;'>Vehicle and Pedestrian Tracker</h1>")
input_video = gr.Video(label="Upload Video")
start_btn = gr.Button("Start Tracking")
status_display = gr.HTML("") # Initially empty
output_video = gr.Video(label="Processed Video")
output_json = gr.File(label="Download JSON Output")
start_btn.click(
fn=process,
inputs=input_video,
outputs=[status_display, output_video, output_json]
)
if __name__ == "__main__":
demo.launch(share = True)
=======
import gradio as gr
import os
import json
from ultralytics import YOLO
import supervision as sv
# --- 1. CONFIGURATION ---
# Folder where the script is
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# Go up one level to Om_Singh
PROJECT_DIR = os.path.dirname(BASE_DIR) # parent folder of app/
# Paths for outputs in video_and_json folder
VIDEO_DIR = os.path.join(PROJECT_DIR, "video_and_json") # Om_Singh/video_and_json
os.makedirs(VIDEO_DIR, exist_ok=True) # ensure folder exists
MODEL_PATH = os.path.join(BASE_DIR, "best.pt") # model stays in app/
OUTPUT_VIDEO_PATH = os.path.join(VIDEO_DIR, "output.mp4")
OUTPUT_JSON_PATH = os.path.join(VIDEO_DIR, "result.json")
def process_video(INPUT_VIDEO_PATH, OUTPUT_VIDEO_PATH, OUTPUT_JSON_PATH):
print("Loading model...")
model = YOLO(MODEL_PATH)
print("Initializing tracker and annotators...")
tracker = sv.ByteTrack()
box_annotator = sv.BoxAnnotator(thickness=5)
label_annotator = sv.LabelAnnotator(text_position=sv.Position.TOP_CENTER, text_scale = 1, text_thickness= 1)
sv.LabelAnnotator()
frame_generator = sv.get_video_frames_generator(source_path=INPUT_VIDEO_PATH)
video_info = sv.VideoInfo.from_video_path(INPUT_VIDEO_PATH)
results_list = []
with sv.VideoSink(target_path=OUTPUT_VIDEO_PATH, video_info=video_info) as sink:
print("Processing video frames...")
for frame_number, frame in enumerate(frame_generator):
# Run YOLO prediction
results = model(frame)[0]
detections = sv.Detections.from_ultralytics(results)
# Update tracker
tracked_detections = tracker.update_with_detections(detections=detections)
# Prepare labels
labels = [
f"ID: {det[4]} {model.model.names[int(det[3])]}"
for det in tracked_detections
]
# Annotate frame
annotated_frame = box_annotator.annotate(scene=frame.copy(), detections=tracked_detections)
annotated_frame = label_annotator.annotate(scene=annotated_frame, detections=tracked_detections, labels=labels)
# Save tracking info
for det in tracked_detections:
bbox = det[0]
conf = det[2]
class_id = int(det[3])
tracker_id = det[4]
results_list.append({
"frame_number": frame_number,
"track_id": int(tracker_id),
"class": model.model.names[class_id],
"confidence": float(conf),
"bounding_box": [int(coord) for coord in bbox]
})
# Write annotated frame
sink.write_frame(frame=annotated_frame)
if frame_number % 30 == 0:
print(f"Processed frame {frame_number}...")
print("Video processing complete. Saving results...")
with open(OUTPUT_JSON_PATH, 'w') as f:
json.dump(results_list, f, indent=4)
print("--- All tasks finished successfully! ---")
# --- Main processing function ---
def process(input_video):
output_video = "output.mp4"
output_json = "result.json"
# During processing: red text
status_html = "<p style='color:red; font-weight:bold;'>Processing...</p>"
# Run video processing
process_video(input_video, output_video, output_json)
# After processing: green text
status_html = "<p style='color:limegreen; font-weight:bold;'>Processing complete!</p>"
return status_html, output_video, output_json
# --- Gradio UI ---
with gr.Blocks() as demo:
gr.Markdown("<h1 style='text-align:center;'>Vehicle and Pedestrian Tracker</h1>")
input_video = gr.Video(label="Upload Video")
start_btn = gr.Button("Start Tracking")
status_display = gr.HTML("") # Initially empty
output_video = gr.Video(label="Processed Video")
output_json = gr.File(label="Download JSON Output")
start_btn.click(
fn=process,
inputs=input_video,
outputs=[status_display, output_video, output_json]
)
if __name__ == "__main__":
demo.launch(share = True)
>>>>>>> e23ece8b8c3eb0b71e331ad00e70aa6c54f85b8b
|