Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLOv10
|
| 3 |
+
import cv2
|
| 4 |
+
import spaces
|
| 5 |
+
|
| 6 |
+
# Load YOLOv10 model
|
| 7 |
+
model = YOLOv10.from_pretrained('jameslahm/yolov10x')
|
| 8 |
+
|
| 9 |
+
# Define object categories to classify activities
|
| 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 associated objects as needed
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
# Function to map detected objects to categorized activities
|
| 19 |
+
def categorize_activity(detected_objects):
|
| 20 |
+
activity_summary = {}
|
| 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 activity_summary:
|
| 25 |
+
activity_summary[activity] = 0
|
| 26 |
+
activity_summary[activity] += 1 # Increase count for that activity
|
| 27 |
+
|
| 28 |
+
return activity_summary
|
| 29 |
+
|
| 30 |
+
# Function to process the video and generate the journal
|
| 31 |
+
@spaces.GPU
|
| 32 |
+
def generate_journal(video):
|
| 33 |
+
cap = cv2.VideoCapture(video)
|
| 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 |
+
# Make predictions using YOLOv10
|
| 43 |
+
results = model.predict(source=frame)
|
| 44 |
+
detected_objects = [res.name for res in results]
|
| 45 |
+
|
| 46 |
+
# Get current timestamp in the video
|
| 47 |
+
timestamp = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000 # Convert ms to seconds
|
| 48 |
+
|
| 49 |
+
# Categorize the detected objects into activities
|
| 50 |
+
activity_summary = categorize_activity(detected_objects)
|
| 51 |
+
|
| 52 |
+
# Store the activities with their timestamp
|
| 53 |
+
for activity, count in activity_summary.items():
|
| 54 |
+
if activity not in journal_entries:
|
| 55 |
+
journal_entries[activity] = []
|
| 56 |
+
journal_entries[activity].append(f"At {timestamp:.2f} seconds: {count} objects related to {activity}")
|
| 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 |
+
formatted_journal.extend(entries)
|
| 65 |
+
|
| 66 |
+
return "\n".join(formatted_journal)
|
| 67 |
+
|
| 68 |
+
# Gradio interface for uploading video and generating journal
|
| 69 |
+
iface = gr.Interface(
|
| 70 |
+
fn=generate_journal,
|
| 71 |
+
inputs=gr.Video(label="Upload Video"),
|
| 72 |
+
outputs=gr.Textbox(label="Generated Daily Journal"),
|
| 73 |
+
title="AI-Powered Daily Journal"
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
iface.launch()
|