diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..72110cb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/* +backups/* diff --git a/pillar_detection.py b/pillar_detection.py index 688f539..23aec8a 100644 --- a/pillar_detection.py +++ b/pillar_detection.py @@ -11,19 +11,22 @@ import time parser = argparse.ArgumentParser() parser.add_argument("-i", "--input_dir", default="./input", help="Input source vid directory") -# parser.add_argument("-i", "--ivtc_dir", default="./0729_work/ivtc", help="Input ivtc vid directory") parser.add_argument("-o", "--output_dir", default="./out", help="Output vid directory") -parser.add_argument("-c", "--crf", default=15, type=int, help="set crf level") -parser.add_argument("-lb", "--add_letterbox", default=False, action="store_true", help="adding letterbox to make 16:9 ratio") +parser.add_argument("-c", "--crf", default=20, type=int, help="set crf level") +parser.add_argument("-sp", "--add_sharpening", default=False, action="store_true", help="add ffmpeg sharpening filter") +parser.add_argument("-lb", "--add_letterbox", default=False, action="store_true", help="add letterbox to make 16:9 ratio") parser.add_argument("-uhd", "--uhd_output", default=False, action="store_true", help="set output resolution to 3840:2160") -parser.add_argument("-mul", "--multi_encoding", default=3, type=int, help="when log file exist, encoding mutiple videos by this args") -parser.add_argument("-s", "--cropsize_scale", default=1, type=int, help="before use crop size from logfile, scale up") +parser.add_argument("-mul", "--multi_encoding", default=2, type=int, help="when log file exist, encoding mutiple videos by this args") +parser.add_argument("-s", "--cropsize_scale", default=1, type=int, help="before use crop size from logfile, scale up the crop size (for reuse when vid rescaled)") parser.add_argument("-log", "--only_logfile", default=False, action="store_true", help="large vid mode. (only write log file.)") parser.add_argument("-man", "--manual_mode", default=False, action="store_true", help="manual cropping without using log file (Ignore log files)") parser.add_argument("-edit", "--edit_mode", default=False, action="store_true", help="get first crop area from log file") parser.add_argument("-d", "--debug", default=False, action="store_true", help="for debugging. (short length video output)") args = parser.parse_args() +# option when only write log file (no encoding) : -log +# options when only edit log file that already exists (no encoding) : -log -edit + # Adjust crop info in display layout. def resizeCropInfo(key, cropPos, frame_width, frame_height): def _IsOutOfBound(_cropPos): @@ -82,6 +85,44 @@ def getTime(): return dt_str +def getVideDuration(file_path): + ffprobe_command = f"ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 \"{file_path}\"" + p = subprocess.run(ffprobe_command, shell=False, stdout=subprocess.PIPE) + _hour = 0 + _min = 0 + _sec = 0 + + _sec = int(float(p.stdout.decode('utf-8'))) + # print(_sec) + + if _sec > 60 : + _min = int(_sec // 60) + _sec = _sec - (60 * _min) + # print(_min, _sec) + if _sec > 3600 : + _hour = int(_sec // 60) + _sec = _sec - (60 * _min) + _min = int(_sec // 60) + _sec = _sec - (60 * _min) + # print(_hour, _min, _sec) + + return [_hour, _min, _sec] + +# for unicode free filename +def imwrite(filename, img, params=None): + try: + ext = os.path.splitext(filename)[1] + result, n = cv2.imencode(ext, img, params) + if result: + with open(filename, mode='w+b') as f: + n.tofile(f) + return True + else: + return False + except Exception as e: + print(e) + return False + def main(): vid_types = ('*.avi', '*.mov', '*.mp4', '*.mxf') @@ -89,6 +130,7 @@ def main(): input_dir = args.input_dir crf = args.crf add_letterbox = args.add_letterbox + add_sharpening = args.add_sharpening uhd_output = args.uhd_output # program modes @@ -102,8 +144,8 @@ def main(): if scale < 1 : print(f"[!!! ERROR !!!]\t Invalid scale option [scale : {scale}]") return - elif multi_how_many < 1 : - print(f"[!!! ERROR !!!]\t Invalid multi_encoding option [multi_encoding : {multi_how_many}]") + elif multi_how_many < 0 : + print(f"[!!! WARRNING !!!]\t \"multi_encoding\" option should be more then 1 [multi_encoding : {multi_how_many}]") return uhd_output_args = "" @@ -114,70 +156,32 @@ def main(): if debug : debug_shortOutput = "-t 15 -ss 30" - # ivtc_dir = args.ivtc_dir output_dir = args.output_dir - - t = time.time - input_list = [] - # ivtc_list = [] for types in vid_types : - input_list += sorted(glob.glob(os.path.join(input_dir, types))) - - """ - File cross check codes. [deprecated, 07/30] - Compare "args.source_dir", "args.ivtc_dir" - check difference of two input, make ivtc version of source_dir videos. - """ - # ivtc_list += sorted(glob.glob(os.path.join(ivtc_dir, "*.mp4"))) - - # if len(ivtc_list) > 0: - # ivtc_input_list = [] - # for i, fpath in enumerate(source_list): - # vid_name = os.path.basename(fpath)[:-4] - # ivtc_name_list = [os.path.basename(x)[:-4] for x in ivtc_list] - - # if not vid_name + "_ivtc" in ivtc_name_list : - # ivtc_input_list += glob.glob(fpath) - # print(f"[INFO]\t Seems \"{os.path.basename(fpath)}\" is stil telecine... add to ivtc processing list") - # else : - # ivtc_input_list = source_list - - # for i, ivtc_input in enumerate(ivtc_input_list) : - # ivtc_input_num = len(ivtc_input_list) - # ivtc_input_name = os.path.basename(ivtc_input) - # ivtc_output_path = os.path.join(ivtc_dir, ivtc_input_name[:-4]) + "_ivtc.mp4" - - # if not os.path.exists(ivtc_dir) : - # os.mkdir(ivtc_dir) - # print(f'[INFO]\t {ivtc_dir} created') - - # print(f"[INFO]\t Work ivtc on \"{ivtc_input_name}\" ===> \"{ivtc_output_path}\" ... [{i + 1}/{ivtc_input_num}]") - # print("[INFO]\t Please wait...") - # start_time = time.time() - # subprocess.run(["ffmpeg", "-ss", "10", "-i", ivtc_input, "-vf", "fieldmatch=mode=pc_n:combmatch=full,bwdif=0:-1:1,decimate", "-c:v", "libx264", "-crf", f"{crf}", - # "-preset", "ultrafast","-loglevel", "quiet", "-y", ivtc_output_path]) - # t = time.time() - start_time - # print("[INFO]\t Work done. [It takes %.2f seconds.]" % t) - # ivtc_dir = [] - # ivtc_dir += sorted(glob.glob(os.path.join(ivtc_dir, "*.mp4"))) - # print("[INFO]\t Input ivtc dir :", ivtc_dir) + input_list += glob.glob(os.path.join(input_dir, types)) + input_list = sorted(input_list) print("[INFO]\t Input dir :", input_dir) - for index, fpath in enumerate(input_list): + + print(str(input_list)) + + for vid_index, fpath in enumerate(input_list): vid_name = os.path.basename(fpath)[:-4] vid_num = len(input_list) isSkipped = False + logs_dir = input_dir logs = '' + print("[INFO]\t Log dir :", logs_dir) # Read log file if exist if not manual_mode: - if not os.path.exists(f"./logs/{vid_name}.txt"): - print(f"[INFO]\t Can`t find log file. [./logs/{vid_name}.txt]") + if not os.path.exists(f"{logs_dir}/{vid_name}.txt"): + print(f"[INFO]\t Can`t find log file. [{logs_dir}/{vid_name}.txt]") edit_mode = False else : - print(f"[INFO]\t Loading log file... [./logs/{vid_name}.txt]") - f = open(f"./logs/{vid_name}.txt", "r") + print(f"[INFO]\t Loading log file... [{logs_dir}/{vid_name}.txt]") + f = open(f"{logs_dir}/{vid_name}.txt", "r") logs = f.read() if logs != '': @@ -192,16 +196,16 @@ def main(): print(f"[INFO]\t Scale up crop area: [w : {cropPos[0]}, h : {cropPos[1]}, x : {cropPos[2]}, y : {cropPos[3]}]") except: - print(f"[!!! WARNNIG !!!]\t Can`t read log file. [./logs/{vid_name}.txt]") + print(f"[!!! WARNNIG !!!]\t Can`t read log file. [{logs_dir}/{vid_name}.txt]") else : - print(f"[!!! WARNNIG !!!]\t Empty log file. [./logs/{vid_name}.txt]") + print(f"[!!! WARNNIG !!!]\t Empty log file. [{logs_dir}/{vid_name}.txt]") if (logs == '') or manual_mode or edit_mode: # TODO : Korean title text bug fix # vid_name = vid_name.encode('utf-8').decode('cp949') - print(f"[INFO]\t Video num : {index + 1} / {vid_num}") + print(f"[INFO]\t [Video num : {vid_index + 1} / {vid_num}]") print("[INFO]\t File to detect crop area: ", fpath) if edit_mode: print("[INFO]\t (Edit mode) Read crop area from log file...") @@ -260,7 +264,8 @@ def main(): key = None isPaused = False - pausedFrame = None + cleanFrame = None + _boxedFrame = None frame = None cv2.namedWindow(f'{vid_name}', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) @@ -283,70 +288,97 @@ def main(): isPaused = False else : isPaused = True - frame = copy.deepcopy(pausedFrame) + # frame = copy.deepcopy(cleanFrame) # Replay elif key == ord('r') or key == ord('R'): if not isPaused: capture = cv2.VideoCapture(fpath) continue - # Move Forward (10s) + # Move Forward (5s) elif key == ord('e') or key == ord('E'): if not isPaused: - capture.set(cv2.CAP_PROP_POS_FRAMES, frame_pos + (fps * 10)) - # Move Backward (10s) + capture.set(cv2.CAP_PROP_POS_FRAMES, frame_pos + (fps * 5)) + # Move Backward (5s) elif key == ord('q') or key == ord('Q'): if not isPaused: - capture.set(cv2.CAP_PROP_POS_FRAMES, frame_pos - (fps * 10)) + capture.set(cv2.CAP_PROP_POS_FRAMES, frame_pos - (fps * 5)) # Display key guide elif key == ord('h') or key == ord('H'): - print("[INFO]\t ### KEY GUIDE ########################################################################################################") - print("[INFO]\t # (MOVE : wasd), (RESIZE : ijkl), (FOR / BACKWARD : q, e), (RESET : r), (PAUSE : p), (SKIP : n), (CONFIRM : enter) #") - print("[INFO]\t ###################################################################################################################### ") + print("[INFO]\t ### KEY GUIDE ##############################################################################################################################################################") + print("[INFO]\t # (MOVE : wasd), (RESIZE : ijkl), (FOR / BACKWARD : q, e), (RESET : r), (PAUSE : p), (SKIP : n), (Preview : v), (Save current frame : b)(CONFIRM : enter) #") + print("[INFO]\t ############################################################################################################################################################################ ") # Skip this Vid elif key == ord('n') or key == ord('N'): print(f"[INFO]\t Skip this video") isSkipped = True - break; + break + # Preview this crop area on new window elif key == ord('v') or key == ord('V'): _x = cropPos[2] _y = cropPos[3] _w = cropPos[0] _h = cropPos[1] - _frame = copy.deepcopy(pausedFrame) - cv2.namedWindow(f'{vid_name}_current_area', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) + _frame = copy.deepcopy(cleanFrame) + cv2.namedWindow(f'{vid_name}_current_area', cv2.WINDOW_KEEPRATIO) cv2.waitKey(10) cv2.imshow(f'{vid_name}_current_area', _frame[_y: (_y + _h), _x: (_x + _w)]) - # cv2.imshow(f'{vid_name}_current_area', _frame[0: int(frame_height), 0: int(frame_width)]) - # cv2.imshow(f'{vid_name}_current_area', _frame[0: int(100), 0: int(100)]) + # Capture, download this crop area in png file + elif key == ord('b') or key == ord('B'): + _x = cropPos[2] + _y = cropPos[3] + _w = cropPos[0] + _h = cropPos[1] + _frame = copy.deepcopy(cleanFrame) + + _vid_name = vid_name.replace(" ","_") + image_dir = "./capture_image" + path = os.path.join(image_dir, _vid_name) + if not os.path.exists(path) : + os.makedirs(path) + print(f'[INFO]\t {path} created') + + image_name = f'{_vid_name}_({frame_pos}-{frame_count})_before.png' + _path = os.path.join(path, image_name) + imwrite(_path, _boxedFrame) + print(f"\n[INFO]\t Saved current frame : {_path}") + + image_name = f'{_vid_name}_({frame_pos}-{frame_count})_after.png' + _path = os.path.join(path, image_name) + imwrite(_path, _frame[_y: (_y + _h), _x: (_x + _w)]) + print(f"[INFO]\t Saved current frame in crop area : {_path}") else: # Crop Area control cropPos = 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 not isPaused: + frame_pos = int(capture.get(cv2.CAP_PROP_POS_FRAMES)) + frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT)) if(frame_pos >= frame_count): break - print("[INFO]\t Current crop area : ", end="") - print(cropPos, end="") - print(f"\t Frame Count ({frame_pos}/{frame_count})\t[PRESS \"h\" TO VIEW KEY GUIDE]", end="\r") - # When paused, hold current frame untill unpause. if isPaused: # deep copy current frame, use that frame for draw rectangle. - pausedFrame = copy.deepcopy(frame) - cv2.rectangle(pausedFrame, (cropPos[2], cropPos[3]), (cropPos[2] + cropPos[0], + # cleanFrame = copy.deepcopy(frame) + _boxedFrame = copy.deepcopy(cleanFrame) + cv2.rectangle(_boxedFrame, (cropPos[2], cropPos[3]), (cropPos[2] + cropPos[0], cropPos[3] + cropPos[1]), (0, 0, 255), thickness=2, lineType=cv2.LINE_8) - cv2.imshow(f'{vid_name}', pausedFrame) + cv2.imshow(f'{vid_name}', _boxedFrame) else : ret, frame = capture.read() - # deep copy current frame for pausing process. - pausedFrame = copy.deepcopy(frame) + # deep copy current frame for clean preview. + cleanFrame = copy.deepcopy(frame) cv2.rectangle(frame, (cropPos[2], cropPos[3]), (cropPos[2] + cropPos[0], cropPos[3] + cropPos[1]), (0, 0, 255), thickness=2, lineType=cv2.LINE_8) - cv2.imshow(f'{vid_name}', frame) + cv2.imshow(f'{vid_name}', frame) + _boxedFrame = copy.deepcopy(frame) + + + print("[INFO]\t Current crop area : ", end="") + print(cropPos, end="") + print(f"\t Frame Count ({frame_pos}/{frame_count})\t[PRESS \"h\" TO VIEW KEY GUIDE]", end="\r") print("") capture.release() @@ -362,16 +394,16 @@ def main(): dt_str = getTime() if not isSkipped: - if not os.path.exists("./logs"): - print(f"[INFO]\t Make log directory [./logs/{vid_name}.txt]") - os.makedirs("./logs") + if not os.path.exists(f"{logs_dir}"): + print(f"[INFO]\t Make log directory [{logs_dir}/{vid_name}.txt]") + os.makedirs(f"{logs_dir}") - if not os.path.exists(f"./logs/{vid_name}.txt"): - print(f"[INFO]\t Save log file in [./logs/{vid_name}.txt]") - f = open(f"./logs/{vid_name}.txt", 'w') + if not os.path.exists(f"{logs_dir}/{vid_name}.txt"): + print(f"[INFO]\t Save log file in [{logs_dir}/{vid_name}.txt]") + f = open(f"{logs_dir}/{vid_name}.txt", 'w') else : - print(f"[INFO]\t Add line in log file [./logs/{vid_name}.txt]") - f = open(f"./logs/{vid_name}.txt", 'a') + print(f"[INFO]\t Add line in log file [{logs_dir}/{vid_name}.txt]") + f = open(f"{logs_dir}/{vid_name}.txt", 'a') f.write(f"{dt_str}\tcrop={cropPos[0]}:{cropPos[1]}:{cropPos[2]}:{cropPos[3]}\n") f.close @@ -398,7 +430,9 @@ def main(): target_ratio = 16 / 9 print("[INFO]\t Target ratio : %.7f" %target_ratio) + # when current ratio is more flat if target_ratio < current_crop_ratio: + # offset : simple equation (widht / (height + offset) = 16/9) offset = (9 * cropPos[0] - 16 * cropPos[1]) / 16 tmp_height = cropPos[1] + offset tmp_height = int(tmp_height) @@ -417,33 +451,64 @@ def main(): scale_arg = f",scale=w={tmp_width}:h={cropPos[1]}" print(f"[INFO]\t [PadInfo] (W : {tmp_width}), (H : {cropPos[1]})") - - if not os.path.exists(output_dir) : os.mkdir(output_dir) - print(f'[INFO]\t {output_dir} created') + print(f'[INFO]\t folder created : {output_dir}') output_path = os.path.join(output_dir, f"{vid_name}_box.mov") ## ffmpeg command setting - ffmpeg_command = f"ffmpeg {debug_shortOutput} -i \"{fpath}\" -vf \"{crop_arg}{scale_arg}{pad_arg}\" -y -pix_fmt yuv420p -c:v libx264 -crf {crf} -c:a copy -preset medium -loglevel error {uhd_output_args} \"{output_path}\"" + if add_sharpening : + sharpen_arg = ',unsharp=7:7:1.5:7:7:1.5' + else : + sharpen_arg = '' + ''' + ffmpeg arguments + -stats : print only encoding progress status (ignore loglevel argument) + -loglevel : make ffmpeg stdout more verbose or not + warning : only warning, error + error : only errors + info : defualt + ''' + loglevel = 'error' + ffmpeg_command = f"ffmpeg {debug_shortOutput} -i \"{fpath}\" -vf \"{crop_arg}{scale_arg}{pad_arg}{sharpen_arg}\"\ + -y -pix_fmt yuv420p -c:v libx264 -crf {crf} -c:a copy -preset medium -loglevel {loglevel}\ + {uhd_output_args} \"{output_path}\"" print(ffmpeg_command) print("[INFO]\t Encoding \"%s\" ..." % vid_name) print("[INFO]\t Video CRF = %d" % int(crf)) + # duration_list[0] : hour, [1] : min, [2] : sec + duration_list = getVideDuration(fpath) + print(f"[INFO]\t Video Duration : {duration_list[0]:02d}:{duration_list[1]:02d}:{duration_list[2]:02d}\ + Current Time : {getTime()}") + st_time = time.time() + if (logs == '') or manual_mode: # Manual Mode (always encoding one by one) # Hold process when encoding last video - if (index + 1) == vid_num : - p = subprocess.run(ffmpeg_command, shell=True) + if (vid_index + 1) == vid_num : + subprocess.Popen(ffmpeg_command + ' -stats', shell=False).wait() else : - p = subprocess.Popen(ffmpeg_command, shell=True) + subprocess.Popen(ffmpeg_command, shell=False) else: - # Log file mode (encoding simultaneously 5 videos) - if (index % multi_how_many == 0) and (index != 0) or ((index + 1) == vid_num): - p = subprocess.run(ffmpeg_command, shell=True) + # Log file mode (encoding simultaneously n videos) + if multi_how_many == 0: + p = subprocess.Popen(ffmpeg_command + ' -stats', shell=False).wait() + + # [21.12.08] progress status coding.. + # p = subprocess.Popen(ffmpeg_command, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + # while p.poll() is None: + # # print('now encoding..', end='\r') + # infos = p.stderr.read().decode('utf-8') + # print('h',infos) + # infos = p.stdout.read().decode('utf-8') + # print('t',infos) + # continue + elif ((vid_index % multi_how_many == 0) and (vid_index != 0)) or ((vid_index + 1) == vid_num): + subprocess.Popen(ffmpeg_command + ' -stats', shell=False).wait() else : - p = subprocess.Popen(ffmpeg_command, shell=True) + subprocess.Popen(ffmpeg_command, shell=False) if __name__ == "__main__": main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ab46249 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +certifi==2021.5.30 +numpy==1.19.5 +opencv-python==4.5.3.56 +wincertstore==0.2