Initial commit with Gradio app
Browse files- app.py +40 -0
- last.pt +3 -0
- requirements.txt +8 -0
- video_process.py +156 -0
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import uuid
|
| 4 |
+
from video_process import process_video
|
| 5 |
+
|
| 6 |
+
MODEL_PATH = "last.pt" # Your YOLO model in the same folder
|
| 7 |
+
CONF_THRESHOLD = 0.2
|
| 8 |
+
PROCESSED_DIR = "processed_video"
|
| 9 |
+
|
| 10 |
+
os.makedirs(PROCESSED_DIR, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
def process_uploaded_video(video):
|
| 13 |
+
if video is None:
|
| 14 |
+
return "No video provided", None
|
| 15 |
+
|
| 16 |
+
input_path = video.name
|
| 17 |
+
unique_id = str(uuid.uuid4())
|
| 18 |
+
output_path = os.path.join(PROCESSED_DIR, f"processed_{unique_id}.mp4")
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
process_video(
|
| 22 |
+
input_video_path=input_path,
|
| 23 |
+
output_video_path=output_path,
|
| 24 |
+
model_path=MODEL_PATH,
|
| 25 |
+
conf_threshold=CONF_THRESHOLD
|
| 26 |
+
)
|
| 27 |
+
return "Processing complete!", output_path
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"Error: {str(e)}", None
|
| 30 |
+
|
| 31 |
+
demo = gr.Interface(
|
| 32 |
+
fn=process_uploaded_video,
|
| 33 |
+
inputs=gr.Video(label="Upload a video"),
|
| 34 |
+
outputs=[gr.Text(label="Status"), gr.Video(label="Processed Video")],
|
| 35 |
+
title="YOLO Video Processor",
|
| 36 |
+
description="Upload a video and get it processed using a YOLO model with segmentation."
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
demo.launch()
|
last.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:43f90e59438941cec791991a5e897dc3fe46f430a9aef9ea87963273172d4f02
|
| 3 |
+
size 6765293
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
opencv-python
|
| 3 |
+
numpy
|
| 4 |
+
tqdm
|
| 5 |
+
ultralytics
|
| 6 |
+
torch
|
| 7 |
+
pandas
|
| 8 |
+
Pillow
|
video_process.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
from tqdm import tqdm
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
import os
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
def process_video(input_video_path, output_video_path, model_path, conf_threshold=0.2):
|
| 9 |
+
"""
|
| 10 |
+
Process a video file for segmentation
|
| 11 |
+
Args:
|
| 12 |
+
input_video_path: Path to input video file
|
| 13 |
+
output_video_path: Path to save the processed video
|
| 14 |
+
model_path: Path to the YOLO model weights
|
| 15 |
+
conf_threshold: Confidence threshold for predictions
|
| 16 |
+
"""
|
| 17 |
+
# Load the model with custom settings
|
| 18 |
+
torch.set_warn_always(False) # Suppress warnings
|
| 19 |
+
|
| 20 |
+
# Load the model
|
| 21 |
+
model = YOLO(model_path)
|
| 22 |
+
|
| 23 |
+
# Open the video file
|
| 24 |
+
cap = cv2.VideoCapture(input_video_path)
|
| 25 |
+
|
| 26 |
+
# Get video properties
|
| 27 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 28 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 29 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 30 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 31 |
+
|
| 32 |
+
# Create video writer object
|
| 33 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 34 |
+
out = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
|
| 35 |
+
|
| 36 |
+
# Process each frame
|
| 37 |
+
with tqdm(total=total_frames, desc="Processing video") as pbar:
|
| 38 |
+
while cap.isOpened():
|
| 39 |
+
ret, frame = cap.read()
|
| 40 |
+
if not ret:
|
| 41 |
+
break
|
| 42 |
+
|
| 43 |
+
# Perform prediction with segmentation
|
| 44 |
+
results = model(frame, conf=conf_threshold, verbose=False)
|
| 45 |
+
|
| 46 |
+
# Get the plotted frame with segmentation
|
| 47 |
+
plotted_frame = results[0].plot()
|
| 48 |
+
|
| 49 |
+
# Convert from RGB to BGR for OpenCV
|
| 50 |
+
plotted_frame = cv2.cvtColor(plotted_frame, cv2.COLOR_RGB2BGR)
|
| 51 |
+
|
| 52 |
+
# Write the frame to output video
|
| 53 |
+
out.write(plotted_frame)
|
| 54 |
+
|
| 55 |
+
# Update progress bar
|
| 56 |
+
pbar.update(1)
|
| 57 |
+
|
| 58 |
+
# Release resources
|
| 59 |
+
cap.release()
|
| 60 |
+
out.release()
|
| 61 |
+
cv2.destroyAllWindows()
|
| 62 |
+
|
| 63 |
+
print(f"Video processing complete. Output saved to: {output_video_path}")
|
| 64 |
+
|
| 65 |
+
def extract_frames_for_analysis(video_path, output_dir, frame_interval=30):
|
| 66 |
+
"""
|
| 67 |
+
Extract frames from video for detailed analysis
|
| 68 |
+
Args:
|
| 69 |
+
video_path: Path to input video file
|
| 70 |
+
output_dir: Directory to save extracted frames
|
| 71 |
+
frame_interval: Extract every nth frame
|
| 72 |
+
"""
|
| 73 |
+
# Create output directory if it doesn't exist
|
| 74 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 75 |
+
|
| 76 |
+
# Open the video file
|
| 77 |
+
cap = cv2.VideoCapture(video_path)
|
| 78 |
+
|
| 79 |
+
frame_count = 0
|
| 80 |
+
frame_number = 0
|
| 81 |
+
|
| 82 |
+
while cap.isOpened():
|
| 83 |
+
ret, frame = cap.read()
|
| 84 |
+
if not ret:
|
| 85 |
+
break
|
| 86 |
+
|
| 87 |
+
if frame_count % frame_interval == 0:
|
| 88 |
+
# Save frame
|
| 89 |
+
frame_path = os.path.join(output_dir, f'frame_{frame_number:04d}.jpg')
|
| 90 |
+
cv2.imwrite(frame_path, frame)
|
| 91 |
+
frame_number += 1
|
| 92 |
+
|
| 93 |
+
frame_count += 1
|
| 94 |
+
|
| 95 |
+
cap.release()
|
| 96 |
+
print(f"Extracted {frame_number} frames to {output_dir}")
|
| 97 |
+
|
| 98 |
+
def analyze_video_frames(frames_dir, model_path, output_csv_path):
|
| 99 |
+
"""
|
| 100 |
+
Analyze extracted frames and save results to CSV
|
| 101 |
+
Args:
|
| 102 |
+
frames_dir: Directory containing extracted frames
|
| 103 |
+
model_path: Path to the YOLO model weights
|
| 104 |
+
output_csv_path: Path to save the analysis results
|
| 105 |
+
"""
|
| 106 |
+
import pandas as pd
|
| 107 |
+
from PIL import Image
|
| 108 |
+
|
| 109 |
+
# Load the model
|
| 110 |
+
model = YOLO(model_path)
|
| 111 |
+
|
| 112 |
+
# Initialize lists to store results
|
| 113 |
+
results = []
|
| 114 |
+
|
| 115 |
+
# Process each frame
|
| 116 |
+
for frame_name in tqdm(os.listdir(frames_dir), desc="Analyzing frames"):
|
| 117 |
+
if not frame_name.endswith(('.jpg', '.jpeg', '.png')):
|
| 118 |
+
continue
|
| 119 |
+
|
| 120 |
+
frame_path = os.path.join(frames_dir, frame_name)
|
| 121 |
+
frame = Image.open(frame_path)
|
| 122 |
+
|
| 123 |
+
# Perform prediction
|
| 124 |
+
pred = model(frame, conf=0.2, verbose=False)
|
| 125 |
+
|
| 126 |
+
# Extract results
|
| 127 |
+
for r in pred:
|
| 128 |
+
if hasattr(r, 'masks'):
|
| 129 |
+
for mask in r.masks:
|
| 130 |
+
mask_data = mask.data.cpu().numpy()
|
| 131 |
+
results.append({
|
| 132 |
+
'frame': frame_name,
|
| 133 |
+
'mask_data': mask_data.tolist()
|
| 134 |
+
})
|
| 135 |
+
|
| 136 |
+
# Convert results to DataFrame and save to CSV
|
| 137 |
+
df = pd.DataFrame(results)
|
| 138 |
+
df.to_csv(output_csv_path, index=False)
|
| 139 |
+
print(f"Analysis results saved to: {output_csv_path}")
|
| 140 |
+
|
| 141 |
+
if __name__ == "__main__":
|
| 142 |
+
# Example usage
|
| 143 |
+
model_path = "best.pt"
|
| 144 |
+
input_video = "path/to/input/video.mp4"
|
| 145 |
+
output_video = "path/to/output/video.mp4"
|
| 146 |
+
frames_dir = "path/to/output/frames"
|
| 147 |
+
output_csv = "path/to/output/analysis.csv"
|
| 148 |
+
|
| 149 |
+
# Process video
|
| 150 |
+
process_video(input_video, output_video, model_path)
|
| 151 |
+
|
| 152 |
+
# Extract frames for analysis
|
| 153 |
+
extract_frames_for_analysis(input_video, frames_dir)
|
| 154 |
+
|
| 155 |
+
# Analyze frames
|
| 156 |
+
analyze_video_frames(frames_dir, model_path, output_csv)
|