python识别视频黑屏或者低清晰度
第一步:获取视频第一帧图片
https://www.cnblogs.com/pythonywy/p/13749735.html
第二步:进行识别
import os
import numpy as np
import cv2 as cv
from skimage import filters, io
class DetectException(Exception):
pass
class DetectionUtil:
DARK_THRES_PIXEL = 20
DARK_THRES_PERCENT = 0.90
QUALITY_THRES = 30
def unqualified_detection(self, image_path):
"""black screen or low quality."""
# image = cv.imread(image_path)
try:
image = io.imread(image_path)
except Exception as e:
raise DetectException(e)
image = image[:, :, ::-1]
gray_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
row, col = gray_image.shape[:2]
pixel_count = row*col
dark_condition = (gray_image <self.DARK_THRES_PIXEL)
dark_array = gray_image[dark_condition]
if dark_array.size / pixel_count >= self.DARK_THRES_PERCENT:
return True
quality = self.genengrad_detection(image)
if quality < self.QUALITY_THRES:
return True
return False
def _preprocess_image(self, image):
re_img = cv.resize(image, (800, 900), interpolation=cv.INTER_CUBIC)
gray_image = cv.cvtColor(re_img, cv.COLOR_BGR2GRAY)
image_matrix = np.matrix(gray_image)
return image_matrix
def genengrad_detection(self, image):
matrix =self._preprocess_image(image)
return np.sqrt(np.sum(filters.sobel(matrix)**2))
if __name__ == "__main__":
print(DetectionUtil().unqualified_detection(r"视频封面地址"))
#true为黑屏或者低分辨率