From 3909c429d51e0a1a9ed777ad22139dbfeefea534 Mon Sep 17 00:00:00 2001 From: JunHo Kim Date: Thu, 1 Jul 2021 16:27:21 +0900 Subject: [PATCH] adding crop info validation func.\n Need to handle ffmpeg`s Concealing bitstream error --- pillar_detection.py | 147 ++++++++++++++++++++++++-------------------- 1 file changed, 80 insertions(+), 67 deletions(-) diff --git a/pillar_detection.py b/pillar_detection.py index 651bfea..eacdc0b 100644 --- a/pillar_detection.py +++ b/pillar_detection.py @@ -11,7 +11,11 @@ parser.add_argument("-i", "--input_dir", default="./") args = parser.parse_args() -def resizeCropInfo(key, cropPos): +def resizeCropInfo(key, cropPos, frame_width, frame_height): + def _IsOutOfBound(w, h, x, y): + if (w + x) > frame_width or (h + y) > frame_height: + return True + # Left if key == 0x250000: cropPos[2] -= 1 @@ -45,9 +49,14 @@ def resizeCropInfo(key, cropPos): cropPos[1] += 1 cropPos[3] += 1 + if cropPos[2] < 0 : + cropPos[2] = 0 + if cropPos[3] < 0 : + cropPos[3] = 0 + if key > 0: print(cropPos) - + return cropPos # cropdetect 에서 얻은 정보 => cv2로 그림 그리면서 confirm. @@ -58,72 +67,76 @@ def resizeCropInfo(key, cropPos): # cropinfo 로그로 쓸수 있게 txt로 저장 def main(): - # fpath = "01.mp4" - fpath = "01. Group S - I Swear MV.avi" - print ("File to detect crop: %s " % fpath) - p = subprocess.Popen(["ffmpeg", "-ss", "30", "-i", fpath, "-vf", "cropdetect=limit=38:reset=30", "-vframes", "500", "-f", "rawvideo", "-y", "cropPos.mp4"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) - infos = p.stderr.read().decode('utf-8') - # print (infos) - # allCrops = re.findall(CROP_DETECT_LINE + ".*", infos) - allCrops = re.findall("crop=\S+", infos) - # print ("Crop Infos : ") - # print (allCrops) - mostCommonCrop = Counter(allCrops).most_common(1) - print ("most common crop: %s" % mostCommonCrop) - global crop - crop = mostCommonCrop[0][0] - - cropPos = crop.split(":") - - # delete "crop=" - cropPos[0] = cropPos[0][5:] - - cropPos = [int(x) for x in cropPos] - - vid_name = os.path.basename(fpath) - capture = cv2.VideoCapture(fpath) - - # play speed : 33ms / frame - while True: - key = cv2.waitKeyEx(1) - # 27 == ESC - if key == 27: - break - - resizeCropInfo(key, cropPos) - - frame_pos = capture.get(cv2.CAP_PROP_POS_FRAMES) - frame_count = capture.get(cv2.CAP_PROP_FRAME_COUNT) - - if(frame_pos == frame_count): - capture.open(vid) - - ret, frame = capture.read() - - cv2.rectangle(frame, (cropPos[2],cropPos[3]), (cropPos[2] + cropPos[0],cropPos[3] + cropPos[1]), (0, 0, 255), thickness=1, lineType=cv2.LINE_8) - # cv2.rectangle(frame, (4,43), (717,431), (0, 0, 255), thickness=1, lineType=cv2.LINE_8) - cv2.imshow(f'{vid_name}', frame) - - capture.release() - cv2.destroyAllWindows() - - target_width = 960 - target_height = 540 - # ffmpeg filter args - crop_arg = f"crop={cropPos[0]}:{cropPos[1]}:{cropPos[2]}:{cropPos[3]}," - if (target_height / target_height) >= (cropPos[0] / cropPos[1]): - tmp_width = target_height * (cropPos[0] / cropPos[1]) - scale_arg = f"scale=w={tmp_width}:h={target_height}," - else: - tmp_height = target_width * (cropPos[1] / cropPos[0]) - scale_arg = f"scale=w={target_width}:h={tmp_height}," - - pad_arg = f"pad={target_width}:{target_height}:(ow-iw)/2:(ih-oh)/2," + vid_paths = sorted(glob.glob(os.path.join(args.input_dir, '*.avi'))) - if False: - p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg + "setsar=sar=1", "-y", "-pix_fmt", "yuv420p", "-c:v", "libx264", "-crf", "15", "-preset", "medium", "tmp.mp4"]) - else: - p = subprocess.Popen(["ffmpeg", "-t", "15", "-ss", "30", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg + "setsar=sar=1", "-y", "-pix_fmt", "yuv420p", "-c:v", "libx264", "-crf", "15", "-preset", "medium", "tmp.mp4"]) + for fpath in vid_paths : + vid_name = os.path.basename(fpath) + fpath = "01. Group S - I Swear MV.avi" + + print ("File to detect crop: %s " % fpath) + p = subprocess.Popen(["ffmpeg", "-ss", "30", "-i", fpath, "-vf", "cropdetect=limit=38:reset=30", "-vframes", "1500", "-f", "rawvideo", "-y", "cropPos.mp4"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + infos = p.stderr.read().decode('utf-8') + # print (infos) + allCrops = re.findall("crop=\S+", infos) + # print ("Crop Infos : ") + # print (allCrops) + mostCommonCrop = Counter(allCrops).most_common(1) + print ("most common crop: %s" % mostCommonCrop) + global crop + crop = mostCommonCrop[0][0] + + cropPos = crop.split(":") + # delete "crop=" + cropPos[0] = cropPos[0][5:] + cropPos = [int(x) for x in cropPos] + + capture = cv2.VideoCapture(fpath) + frame_width = capture.get(cv2.CAP_PROP_FRAME_WIDTH) + frame_height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT) + # play speed : 33ms / frame + while True: + key = cv2.waitKeyEx(33) + # 27 == ESC + if key == 27: + break + + resizeCropInfo(key, cropPos, frame_width, frame_height) + + frame_pos = capture.get(cv2.CAP_PROP_POS_FRAMES) + frame_count = capture.get(cv2.CAP_PROP_FRAME_COUNT) + + if(frame_pos == frame_count): + capture.open(vid) + + ret, frame = capture.read() + + cv2.rectangle(frame, (cropPos[2],cropPos[3]), (cropPos[2] + cropPos[0],cropPos[3] + cropPos[1]), (0, 0, 255), thickness=1, lineType=cv2.LINE_8) + # cv2.rectangle(frame, (4,43), (717,431), (0, 0, 255), thickness=1, lineType=cv2.LINE_8) + cv2.imshow(f'{vid_name}', frame) + + capture.release() + cv2.destroyAllWindows() + + target_width = 960 + target_height = 540 + # ffmpeg filter args + crop_arg = f"crop={cropPos[0]}:{cropPos[1]}:{cropPos[2]}:{cropPos[3]}," + print (f"crop={cropPos[0]}:{cropPos[1]}:{cropPos[2]}:{cropPos[3]},") + + if (target_height / target_height) >= (cropPos[0] / cropPos[1]): + tmp_width = target_height * (cropPos[0] / cropPos[1]) + scale_arg = f"scale=w={tmp_width}:h={target_height}," + else: + tmp_height = target_width * (cropPos[1] / cropPos[0]) + scale_arg = f"scale=w={target_width}:h={tmp_height}," + + pad_arg = f"pad={target_width}:{target_height}:(ow-iw)/2:(ih-oh)/2," + + print("Encoding \"%s\"" %vid_name) + if False: + p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg + "setsar=sar=1", "-y", "-pix_fmt", "yuv420p", "-c:v", "libx264", "-crf", "15", "-preset", "medium","-loglevel", "error", f"_{vid_name}_cropped.mp4"]) + else: + p = subprocess.Popen(["ffmpeg", "-t", "15", "-ss", "30", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg + "setsar=sar=1", "-y", "-pix_fmt", "yuv420p", "-c:v", "libx264", "-crf", "15", "-preset", "medium","-loglevel", "error", f"_{vid_name}_cropped.mp4"]) if __name__ == "__main__":