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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
import os import datetime import imutils import time import cv2
// 设置视频所在的根目录,Windows示例:C:\\Users\\abc\\desktop base_path = '/share/xiaomi_camera_videos/788b2a9faf02'
// 获取文件列表函数 def get_filelist(dir): Filelist = [] for home, dirs, files in os.walk(dir): for file_name in files: Filelist.append(os.path.join(home, file_name)) return Filelist
// 检测视频是否有活动画面 def is_video_occupied(video_path): camera = cv2.VideoCapture(video_path) total = camera.get(cv2.CAP_PROP_FRAME_COUNT) fps = camera.get(cv2.CAP_PROP_FPS) step = (total - 100) / 9
lastFrame = None occupied = False
for i in range(100, int(total), int(step)): camera.set(cv2.CAP_PROP_POS_FRAMES, i) camera.grab() (grabbed, frame) = camera.read()
if not grabbed: break
frame = imutils.resize(frame, width=500) camera_area = frame.shape[0] * frame.shape[1] gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (21, 21), 0)
if lastFrame is None: lastFrame = gray continue
frameDelta = cv2.absdiff(lastFrame, gray) thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2) (cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
lastFrame = gray
for c in cnts: ca = cv2.contourArea(c) if ca < 500 or ca > camera_area/2: continue occupied = True break else: continue break
camera.release() return occupied,frame
// 获取文件列表 filelist = get_filelist(base_path) // 创建缩略图文件夹 snap_path = os.path.join(os.path.abspath(os.path.join(base_path,os.path.pardir)),'snapshots') if not os.path.exists(snap_path): os.makedirs(snap_path) delete = 0 snap = 0 left = len(filelist) // 循环文件列表 for video_path in filelist: date_name = os.path.basename(os.path.dirname(video_path)) file_name = os.path.splitext(os.path.basename(video_path))[0] jpg_path = os.path.join(snap_path,date_name+'_'+file_name+'.jpg') if os.path.exists(jpg_path): snap = snap + 1 else: occupied, snapshot = is_video_occupied(video_path) // 动态视频截图保存 if (occupied): cv2.imwrite(jpg_path, snapshot) snap = snap + 1 // 静态视频直接删除 else: os.remove(video_path) delete = delete + 1
left = left - 1 // 屏幕显示进度 cur_time = datetime.datetime.now().strftime("%Y.%m.%d-%H:%M:%S") print(f'{cur_time} -> {snap} snap, {delete} delete, {left} left; {date_name}_{file_name}') // 删除空文件夹 for root, dirs, files in os.walk(base_path): if dirs == files: os.rmdir(root)
|