|
import gradio as gr |
|
from ultralytics import YOLOv10 |
|
import cv2 |
|
import spaces |
|
|
|
|
|
model = YOLOv10.from_pretrained('jameslahm/yolov10x') |
|
|
|
|
|
activity_categories = { |
|
"Working": ["laptop", "computer", "keyboard", "office chair"], |
|
"Meal Time": ["fork", "spoon", "plate", "food"], |
|
"Exercise": ["dumbbell", "bicycle", "yoga mat", "treadmill"], |
|
"Outdoors": ["car", "tree", "bicycle", "road"], |
|
|
|
} |
|
|
|
|
|
def categorize_activity(detected_objects): |
|
activity_summary = {} |
|
|
|
for activity, objects in activity_categories.items(): |
|
if any(obj in detected_objects for obj in objects): |
|
if activity not in activity_summary: |
|
activity_summary[activity] = 0 |
|
activity_summary[activity] += 1 |
|
|
|
return activity_summary |
|
|
|
|
|
@spaces.GPU |
|
def generate_journal(video): |
|
cap = cv2.VideoCapture(video) |
|
frame_rate = cap.get(cv2.CAP_PROP_FPS) |
|
journal_entries = {} |
|
|
|
while cap.isOpened(): |
|
ret, frame = cap.read() |
|
if not ret: |
|
break |
|
|
|
|
|
results = model.predict(source=frame) |
|
detected_objects = [res.name for res in results] |
|
|
|
|
|
timestamp = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000 |
|
|
|
|
|
activity_summary = categorize_activity(detected_objects) |
|
|
|
|
|
for activity, count in activity_summary.items(): |
|
if activity not in journal_entries: |
|
journal_entries[activity] = [] |
|
journal_entries[activity].append(f"At {timestamp:.2f} seconds: {count} objects related to {activity}") |
|
|
|
cap.release() |
|
|
|
|
|
formatted_journal = [] |
|
for activity, entries in journal_entries.items(): |
|
formatted_journal.append(f"**{activity}:**") |
|
formatted_journal.extend(entries) |
|
|
|
return "\n".join(formatted_journal) |
|
|
|
|
|
iface = gr.Interface( |
|
fn=generate_journal, |
|
inputs=gr.Video(label="Upload Video"), |
|
outputs=gr.Textbox(label="Generated Daily Journal"), |
|
title="AI-Powered Daily Journal" |
|
) |
|
|
|
iface.launch() |
|
|