Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to trim videos in yolo using cv2 #12009

Open
1 task done
anishka07 opened this issue May 10, 2024 · 1 comment
Open
1 task done

how to trim videos in yolo using cv2 #12009

anishka07 opened this issue May 10, 2024 · 1 comment
Labels
question Further information is requested

Comments

@anishka07
Copy link

Search before asking

Question

how to trim videos based on start and end timestamp

Additional

def detect_anomaly(self, frame, conf: float = 0.5) -> List[AnomalyDetectionResult] | str:
results = self.model.detect(frame)
tracked_objects: List = []
try:
for result in results:
boxes = result.boxes
for box in boxes:
confidence = round(float(box.conf), 2)
if confidence >= conf:
detected_object_index = int(box.cls)
class_name = str(self.model.names[detected_object_index])
if class_name == "person":
timestamp: int = int(datetime.now().timestamp() * 1000)
tracked_objects.append(
AnomalyDetectionResult(
timestamp=timestamp,
source=self.source_url,
class_name=class_name,
confidence=confidence
)
)
return tracked_objects
except Exception as e:
error = f"Error: {e}"
return error
this is my code to send the data into pydantic format. Now i want the code to trim the data when a person is detected up to the part where the person leaves the frame. How do i do that please answer.
this is my pydantic class, also if i want to send this data into the database, how should i send it
this is the pydantic class:
class AnomalyDetectionResult(BaseModel):
timestamp: int = 0
source: str = ''
class_name: str = ''
# location: str = ''
confidence: float = 0.0

@anishka07 anishka07 added the question Further information is requested label May 10, 2024
@glenn-jocher
Copy link
Member

To trim videos based on start and end timestamps using OpenCV (cv2), you'll need to first capture your video, then check frames against your time conditions, and save the relevant section. Below is a simplified example showing how to trim a video when a person is detected until they leave the frame:

import cv2
from datetime import datetime

def trim_video(input_video_path, output_video_path, start_time, end_time):
    cap = cv2.VideoCapture(input_video_path)
    fps = int(cap.get(cv2.CAP_PROP_FPS))
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    out = None

    while True:
        ret, frame = cap.read()
        if not ret:
            break

        current_time = cap.get(cv2.CAP_PROP_POS_MSEC)
        if start_time <= current_time <= end_time:
            if out is None:
                size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
                out = cv2.VideoWriter(output_video_path, fourcc, fps, size)
            out.write(frame)
        elif current_time > end_time:
            break

    cap.release()
    if out:
        out.release()
        
cap = cv2.VideoCapture('path/to/input.mp4')
start_time, end_time = None, None

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break
    results = model.detect(frame)
    for result in results:
        if 'person' in [model.names[int(box.cls)] for box in result.boxes if box.conf >= 0.5]:
            if start_time is None:
                start_time = cap.get(cv2.CAP_PROP_POS_MSEC)
        else:
            if start_time is not None:
                end_time = cap.get(cv2.CAP_PROP_POS_MSEC)
                break
    if end_time:
        break

trim_video('path/to/input.mp4', 'path/to/output.mp4', start_time, end_time)

In this script, model.detect() should be your function that analyzes frames to detect objects. Adjust start_time and end_time based on your conditions. Ensure your detection model is loaded and replace 'path/to/input.mp4' and 'path/to/output.mp4' with your actual file paths.

To send your AnomalyDetectionResult to a database, consider using a library like SQLAlchemy for ORM-based interactions, or direct database connection libraries (like psycopg2 for PostgreSQL, PyMySQL for MySQL) depending on your database choice. Use appropriate database insertion commands in the try block where you append AnomalyDetectionResult.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants