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
Comments
To trim videos based on start and end timestamps using OpenCV ( 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, To send your |
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
The text was updated successfully, but these errors were encountered: