v2 add frame name update

This commit is contained in:
13339479676 2022-01-02 14:57:30 +08:00
parent 6f39102b31
commit 7b183dab41
1 changed files with 11 additions and 6 deletions

View File

@ -11,6 +11,7 @@ import glob
import re
import os
def parse_args(known=False):
parser = argparse.ArgumentParser(description='OpenCV Webcam Script')
parser.add_argument('--device', '-dev', default=0,
@ -33,6 +34,8 @@ def parse_args(known=False):
default=[640, 480], type=int, nargs='+', help='resize frame save')
parser.add_argument('--resizeRatio_frame', '-rrf',
default=1.0, type=float, help='resize ratio frame save')
parser.add_argument('--frame_namePrefix', '-fnp',
default="frame", type=str, help='frame name prefix')
args = parser.parse_known_args()[0] if known else parser.parse_args()
return args
@ -57,7 +60,7 @@ def increment_path(path, exist_ok=False, sep='', mkdir=False):
# webcam opencv
def webcam_opencv(device_index, quit_key, is_autoSaveFrame, frame_saveDir, frame_nSave, is_headSaveFrame, frame_capKey, is_resizeFrame, resize_frame, resizeRatio_frame):
def webcam_opencv(device_index, quit_key, is_autoSaveFrame, frame_saveDir, frame_nSave, is_headSaveFrame, frame_capKey, is_resizeFrame, resize_frame, resizeRatio_frame, frame_namePrefix):
# 判断快捷键冲突
if (quit_key == frame_capKey):
print(f'快捷键冲突! 程序结束!')
@ -98,10 +101,10 @@ def webcam_opencv(device_index, quit_key, is_autoSaveFrame, frame_saveDir, frame
frame_new = cv2.resize(
frame, (w_resize, h_resize), interpolation=cv2.INTER_AREA) # 重塑
cv2.imwrite(
f'./{frame_samePath}/{frame_num}.jpg', frame_new)
f'./{frame_samePath}/{frame_namePrefix}-{frame_num}.jpg', frame_new)
else:
cv2.imwrite(
f'./{frame_samePath}/{frame_num}.jpg', frame)
f'./{frame_samePath}/{frame_namePrefix}-{frame_num}.jpg', frame)
if (is_headSaveFrame): # 手动保存
if cv2.waitKey(20) & 0xFF == ord(frame_capKey): # 保存键
if (is_resizeFrame): # 重塑
@ -110,12 +113,13 @@ def webcam_opencv(device_index, quit_key, is_autoSaveFrame, frame_saveDir, frame
frame_new = cv2.resize(
frame, (w_resize, h_resize), interpolation=cv2.INTER_AREA)
cv2.imwrite(
f'./{frame_samePath}/{frame_num}.jpg', frame_new)
f'./{frame_samePath}/{frame_namePrefix}-{frame_num}.jpg', frame_new)
else:
cv2.imwrite(
f'./{frame_samePath}/{frame_num}.jpg', frame)
f'./{frame_samePath}/{frame_namePrefix}-{frame_num}.jpg', frame)
if cv2.waitKey(20) & 0xFF == ord(quit_key): # 退出 ord字符转ASCII码
break
print(f'一共处理了{frame_num}')
# 释放缓存
cap.release()
@ -139,10 +143,11 @@ def main(args):
resize_frame = args.resize_frame
is_resizeFrame = args.is_resizeFrame
resizeRatio_frame = args.resizeRatio_frame
frame_namePrefix = args.frame_namePrefix
# 调用webcam opencv
webcam_opencv(device_index, quit_key, is_autoSaveFrame, frame_saveDir, frame_nSave,
is_headSaveFrame, frame_capKey, is_resizeFrame, resize_frame, resizeRatio_frame)
is_headSaveFrame, frame_capKey, is_resizeFrame, resize_frame, resizeRatio_frame, frame_namePrefix)
if __name__ == '__main__':