File size: 2,464 Bytes
6755a2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- coding: utf-8 -*-

import traceback
import argparse
import os

from moviepy.editor import VideoFileClip
import numpy as np


#   rmBlackBorder: remove the black borders of one image
#   return: cropped image
def det_image_black_border(
    src,  # input image
    thres,  # threshold for cropping: sum([r,g,b] - [0,0,0](black))
    shrink,  # number of pixels to shrink after the blackBorders removed
):
    #   remove the black border on both right and left side
    nRow = src.shape[0]
    nCol = src.shape[1]
    left = 0
    right = nCol

    # for j in range(0, nCol):
    #     if src[:, j].mean() <= thres:
    #         left = j + 1
    #     else:
    #         break
    #
    # for j in range(nCol - 1, -1, -1):
    #     if src[:, j].mean() <= thres:
    #         right = j
    #     else:
    #         break

    black_idx = np.where(src.mean(axis=0) <= thres)[0].tolist()
    for i in black_idx:
        if left < i < nCol // 2:
            left = i
        elif nCol // 2 < i < right:
            right = i

    if right - left > 0:
        left = left + shrink
        right = right - shrink
    else:
        left = 0
        right = nCol

    #   remove the black border on both up and down side
    up = 0
    bottom = nRow

    # for i in range(0, nRow):
    #     if src[i, :].mean() <= thres:
    #         up = i + 1
    #     else:
    #         break
    #
    # for i in range(nRow - 1, -1, -1):
    #     if src[i, :,].mean() <= thres:
    #         bottom = i
    #     else:
    #         break

    black_idx = np.where(src.mean(axis=1) <= thres)[0].tolist()
    for i in black_idx:
        if up < i < nRow // 2:
            up = i
        elif nRow // 2 < i < bottom:
            bottom = i

    if bottom - up > 0:
        top = up + shrink
        bottom = bottom - shrink
    else:
        top = 0
        bottom = nRow

    return (left, top, right, bottom)


def det_video_black_border(video_path):
    video = VideoFileClip(video_path)
    duration = video.duration
    test_duration = 600
    video = video.subclip(
        t_start=duration / 2 - test_duration / 2, t_end=duration / 2 + test_duration / 2
    )
    frame_num = 0
    for frame in video.iter_frames(fps=1):
        frame = frame.astype(np.int64)
        if frame_num == 0:
            frame_sum = frame
        else:
            frame_sum += frame
        frame_num += 1
    frame = frame_sum / frame_num
    return det_image_black_border(frame, 5, 0)