我需要构建一个工具,可以帮助我确定视频是否包含信箱或邮筒(两侧的黑线或白线),并查看该字母/邮筒是否占据视频的 10% 以上。 由于信箱要么是黑色的,要么是白色的,我想通过查找黑色或白色像素的列/行来明确检查边框是否为黑色(<5) or white (>250)。
我用 GUI 制作了一个脚本来上传视频,但它无法正常工作。它没有显示正确的百分比。我怎样才能做得更准确? 这是我的代码:
from turtle import width
import cv2
import numpy as np
import tkinter as tk
from tkinter import filedialog
# Global variable to hold the result label
result_label = None
def calculate_border_size(video_path, border_pixels=20, black_threshold=5, white_threshold=250):
# Open video capture
cap = cv2.VideoCapture(video_path)
# Check if the video capture is successfully opened
if not cap.isOpened():
raise ValueError("Error: Could not open video file.")
# Initialize variables to store cumulative results
black_rows_sum = 0
white_rows_sum = 0
frame_count = 0
height, width = None, None
while True:
ret, frame = cap.read()
if not ret:
break
if height is None or width is None:
height, width = frame.shape[:2]
# Extract the border regions
border_regions = np.concatenate((frame[:, :border_pixels], frame[:, -border_pixels:]), axis=1)
# Count black and white rows in the border regions
black_rows = np.sum(np.sum(border_regions < black_threshold, axis=0) == frame.shape[0])
white_rows = np.sum(np.sum(border_regions > white_threshold, axis=0) == frame.shape[0])
# Accumulate results
black_rows_sum += black_rows
white_rows_sum += white_rows
frame_count += 1
cap.release()
# Calculate the average black and white rows
avg_black_rows = black_rows_sum / frame_count
avg_white_rows = white_rows_sum / frame_count
# Calculate the percentage of black rows compared to the whole screen height
percentage_black_rows = (avg_black_rows / height) * 100
percentage_white_rows = (avg_white_rows / height) * 100
# Check if the percentage of black rows exceeds 20% of the screen height
is_letterboxing = percentage_black_rows > 20 or percentage_white_rows > 20
return is_letterboxing, percentage_white_rows, percentage_black_rows
# Function to handle the video upload button
def upload_video():
video_path = filedialog.askopenfilename(filetypes=[("Video files", "*.mp4;*.avi")])
if video_path:
analyze_video(video_path)
# Function to analyze the video and display results
def analyze_video(video_path):
global result_label # Declare result_label as a global variable
try:
is_letterboxing, percentage_white_rows, percentage_black_rows = calculate_border_size(video_path)
result_label.config(text=f"Is Letterboxing: {is_letterboxing}\nPercentage Black Rows: {percentage_black_rows}\nPercentage White Rows: {percentage_white_rows}")
except ValueError as e:
result_label.config(text=str(e))
# GUI setup
root = tk.Tk()
root.title("Video Analysis")
# Button to upload video
upload_button = tk.Button(root, text="Upload Video", command=upload_video)
upload_button.pack(pady=20)
# Label to display results
result_label = tk.Label(root, text="")
result_label.pack()
# Run the GUI
root.mainloop()
你做错了一件事。假设视频中有 300 帧,只有 60 帧有宽度 70 的黑条,其余帧没有黑条或白条。
avg_black_rows = black_rows_sum / frame_count
avg_white_rows = white_rows_sum / frame_count
# Calculate the percentage of black rows compared to the whole screen height
percentage_black_rows = (avg_black_rows / height) * 100
percentage_white_rows = (avg_white_rows / height) * 100
你计算 B 的方式