Spaces:
Sleeping
Sleeping
import cv2 | |
import numpy as np | |
import os | |
def is_bgr(image): | |
""" | |
Heuristic check to see if an image is in BGR format. | |
This function checks if the values at a specific pixel match the expected BGR to RGB order. | |
""" | |
# Choose a pixel to check (e.g., the top-left corner) | |
pixel = image[0, 0] | |
# Check if the pixel values match BGR order | |
# This is a heuristic; it assumes the color channels have distinct values. | |
return pixel[0] > pixel[1] and pixel[1] > pixel[2] | |
def convert_bgr_to_rgb(image): | |
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
def convert_rgb_to_bgr(image): | |
return cv2.cvtColor(image, cv2.COLOR_RGB2BGR) | |
def crop_video(input_path, output_path, x, y, width, height): | |
# Open the input video | |
cap = cv2.VideoCapture(input_path) | |
if not cap.isOpened(): | |
print("Error: Could not open video.") | |
return | |
# Get the width and height of the frames | |
original_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | |
original_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |
fps = cap.get(cv2.CAP_PROP_FPS) | |
# Define the codec and create VideoWriter object | |
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # You can also use other codecs | |
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height)) | |
while cap.isOpened(): | |
ret, frame = cap.read() | |
if not ret: | |
break | |
# Crop the frame | |
cropped_frame = frame[y:y+height, x:x+width] | |
# Write the cropped frame to the output video file | |
out.write(cropped_frame) | |
# Release everything if job is finished | |
cap.release() | |
out.release() | |
cv2.destroyAllWindows() | |
print(f"Cropping completed. Saved to {output_path}") |