Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,65 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLOv10
|
| 3 |
import cv2
|
|
|
|
|
|
|
| 4 |
import spaces
|
| 5 |
|
| 6 |
-
# Load YOLOv10 model
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
# Define
|
| 10 |
activity_categories = {
|
| 11 |
"Working": ["laptop", "computer", "keyboard", "office chair"],
|
| 12 |
"Meal Time": ["fork", "spoon", "plate", "food"],
|
| 13 |
"Exercise": ["dumbbell", "bicycle", "yoga mat", "treadmill"],
|
| 14 |
"Outdoors": ["car", "tree", "bicycle", "road"],
|
| 15 |
-
# Add more categories and
|
| 16 |
}
|
| 17 |
|
| 18 |
# Function to map detected objects to categorized activities
|
| 19 |
def categorize_activity(detected_objects):
|
| 20 |
-
|
| 21 |
|
| 22 |
for activity, objects in activity_categories.items():
|
| 23 |
if any(obj in detected_objects for obj in objects):
|
| 24 |
-
if activity not in
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
-
return
|
| 29 |
|
| 30 |
-
# Function to process the video and generate
|
| 31 |
@spaces.GPU
|
| 32 |
-
def
|
| 33 |
-
cap = cv2.VideoCapture(
|
| 34 |
-
frame_rate = cap.get(cv2.CAP_PROP_FPS)
|
| 35 |
journal_entries = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
while cap.isOpened():
|
| 38 |
ret, frame = cap.read()
|
| 39 |
if not ret:
|
| 40 |
break
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Get current timestamp in the video
|
| 47 |
timestamp = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000 # Convert ms to seconds
|
|
@@ -50,27 +68,41 @@ def generate_journal(video):
|
|
| 50 |
activity_summary = categorize_activity(detected_objects)
|
| 51 |
|
| 52 |
# Store the activities with their timestamp
|
| 53 |
-
for activity,
|
| 54 |
if activity not in journal_entries:
|
| 55 |
journal_entries[activity] = []
|
| 56 |
-
journal_entries[activity].append(f"At {timestamp:.2f} seconds: {
|
|
|
|
|
|
|
| 57 |
|
| 58 |
cap.release()
|
| 59 |
|
| 60 |
-
# Create a formatted journal
|
| 61 |
formatted_journal = []
|
| 62 |
for activity, entries in journal_entries.items():
|
| 63 |
formatted_journal.append(f"**{activity}:**")
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
return
|
| 67 |
|
| 68 |
-
# Gradio interface for uploading video and generating journal
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLOv10
|
| 3 |
import cv2
|
| 4 |
+
import torch
|
| 5 |
+
import os
|
| 6 |
import spaces
|
| 7 |
|
| 8 |
+
# Load YOLOv10 model with CUDA if available
|
| 9 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 10 |
+
model = YOLOv10.from_pretrained('jameslahm/yolov10x').to(device)
|
| 11 |
|
| 12 |
+
# Define activity categories based on detected objects
|
| 13 |
activity_categories = {
|
| 14 |
"Working": ["laptop", "computer", "keyboard", "office chair"],
|
| 15 |
"Meal Time": ["fork", "spoon", "plate", "food"],
|
| 16 |
"Exercise": ["dumbbell", "bicycle", "yoga mat", "treadmill"],
|
| 17 |
"Outdoors": ["car", "tree", "bicycle", "road"],
|
| 18 |
+
# Add more categories and objects as needed
|
| 19 |
}
|
| 20 |
|
| 21 |
# Function to map detected objects to categorized activities
|
| 22 |
def categorize_activity(detected_objects):
|
| 23 |
+
categorized_activities = {}
|
| 24 |
|
| 25 |
for activity, objects in activity_categories.items():
|
| 26 |
if any(obj in detected_objects for obj in objects):
|
| 27 |
+
if activity not in categorized_activities:
|
| 28 |
+
categorized_activities[activity] = []
|
| 29 |
+
categorized_activities[activity].append(detected_objects)
|
| 30 |
|
| 31 |
+
return categorized_activities
|
| 32 |
|
| 33 |
+
# Function to process the video, detect objects, and generate a categorized journal with images
|
| 34 |
@spaces.GPU
|
| 35 |
+
def generate_journal_with_images(video_path):
|
| 36 |
+
cap = cv2.VideoCapture(video_path)
|
|
|
|
| 37 |
journal_entries = {}
|
| 38 |
+
saved_images = []
|
| 39 |
+
frame_count = 0
|
| 40 |
+
output_folder = "detected_frames"
|
| 41 |
+
os.makedirs(output_folder, exist_ok=True) # Create folder to store images
|
| 42 |
|
| 43 |
while cap.isOpened():
|
| 44 |
ret, frame = cap.read()
|
| 45 |
if not ret:
|
| 46 |
break
|
| 47 |
|
| 48 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 49 |
+
|
| 50 |
+
# Make predictions using YOLOv10 on the current frame
|
| 51 |
+
results = model.predict(source=frame_rgb, device=device)
|
| 52 |
+
|
| 53 |
+
# Draw bounding boxes on the frame
|
| 54 |
+
results.render() # Render the results on the image (this modifies the frame in-place)
|
| 55 |
+
|
| 56 |
+
# Save the image with bounding boxes
|
| 57 |
+
frame_filename = os.path.join(output_folder, f"frame_{frame_count}.jpg")
|
| 58 |
+
cv2.imwrite(frame_filename, frame_rgb[:, :, ::-1]) # Convert back to BGR for saving
|
| 59 |
+
saved_images.append(frame_filename)
|
| 60 |
+
|
| 61 |
+
# Extract labels (class indices) and map them to class names
|
| 62 |
+
detected_objects = [model.names[int(box.cls)] for box in results.boxes]
|
| 63 |
|
| 64 |
# Get current timestamp in the video
|
| 65 |
timestamp = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000 # Convert ms to seconds
|
|
|
|
| 68 |
activity_summary = categorize_activity(detected_objects)
|
| 69 |
|
| 70 |
# Store the activities with their timestamp
|
| 71 |
+
for activity, objects in activity_summary.items():
|
| 72 |
if activity not in journal_entries:
|
| 73 |
journal_entries[activity] = []
|
| 74 |
+
journal_entries[activity].append((f"At {timestamp:.2f} seconds: {', '.join(objects[0])}", frame_filename))
|
| 75 |
+
|
| 76 |
+
frame_count += 1
|
| 77 |
|
| 78 |
cap.release()
|
| 79 |
|
| 80 |
+
# Create a formatted journal output
|
| 81 |
formatted_journal = []
|
| 82 |
for activity, entries in journal_entries.items():
|
| 83 |
formatted_journal.append(f"**{activity}:**")
|
| 84 |
+
for entry, image_path in entries:
|
| 85 |
+
formatted_journal.append((entry, image_path))
|
| 86 |
|
| 87 |
+
return formatted_journal
|
| 88 |
|
| 89 |
+
# Gradio interface for uploading video and generating journal with images
|
| 90 |
+
def display_journal_with_images(video):
|
| 91 |
+
journal_with_images = generate_journal_with_images(video)
|
| 92 |
+
|
| 93 |
+
# Create the final display with text and images
|
| 94 |
+
display_items = []
|
| 95 |
+
for entry, image_path in journal_with_images:
|
| 96 |
+
display_items.append((entry, image_path))
|
| 97 |
+
|
| 98 |
+
return display_items
|
| 99 |
+
|
| 100 |
+
# Define Gradio Blocks for custom display
|
| 101 |
+
with gr.Blocks() as iface:
|
| 102 |
+
video_input = gr.Video(label="Upload Video")
|
| 103 |
+
output_gallery = gr.Gallery(label="Generated Daily Journal with Images").style(grid=[2], height='auto')
|
| 104 |
+
run_button = gr.Button("Generate Journal")
|
| 105 |
+
|
| 106 |
+
run_button.click(fn=display_journal_with_images, inputs=video_input, outputs=output_gallery)
|
| 107 |
|
| 108 |
iface.launch()
|