Skip to content

Instantly share code, notes, and snippets.

@aliyevorkhan
Created October 31, 2020 20:41
Show Gist options
  • Save aliyevorkhan/e85ccaf28133dc0c9e5eabbac86ce703 to your computer and use it in GitHub Desktop.
Save aliyevorkhan/e85ccaf28133dc0c9e5eabbac86ce703 to your computer and use it in GitHub Desktop.
import os
import cv2
src_path = os.path.abspath(os.path.join(os.getcwd(), "bolu_tunnel_nvr"))
print(src_path)
ch1_dst_path = os.path.abspath(os.path.join(os.getcwd(), "ch1"))
ch2_dst_path = os.path.abspath(os.path.join(os.getcwd(), "ch2"))
print(ch1_dst_path)
print(ch2_dst_path)
all_files = os.listdir(src_path)
print(all_files)
ch1_videos = []
ch2_videos = []
print('[INFO] Reading ch1 videos..')
print(50*'-')
print()
for file in all_files:
if file.startswith('Ch1'):
ch1_videos.append(file)
else:
ch2_videos.append(file)
print('[INFO] Done!')
print(50*'*')
print()
print('[INFO] Processing ch1 videos..')
print(50*'*')
print()
for video in ch1_videos:
cap = cv2.VideoCapture(os.path.join(src_path, video))
i = 0
counter = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print('[INFO] {'+video+'} completed!')
print(50*'-')
break
if counter % 5 == 0:
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray_frame = cv2.resize(gray_frame, (240,135))
cv2.imwrite(ch1_dst_path+'/'+video.split('.')[0]+'_'+str(i)+'.tif',gray_frame)
i+=1
counter +=1
cap.release()
os.remove(os.path.abspath(os.path.join(src_path, video)))
print('[INFO] {'+video+'} deleted!')
print(50*'-')
print()
#cv2.destroyAllWindows()
print('[INFO] Done!')
print(50*'*')
print()
print('[INFO] Reading ch2 videos..')
print(50*'*')
print()
for video in ch2_videos:
cap = cv2.VideoCapture(os.path.join(src_path, video))
i = 0
counter = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print('[INFO] {'+video+'} completed!')
print(50*'-')
break
if counter % 5 == 0:
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray_frame = cv2.resize(gray_frame, (240,135))
cv2.imwrite(ch2_dst_path+'/'+video.split('.')[0]+'_'+str(i)+'.tif',gray_frame)
i+=1
counter +=1
cap.release()
os.remove(os.path.abspath(os.path.join(src_path, video)))
print('[INFO] {'+video+'} deleted!')
print(50*'-')
print()
#cap.destroyAllWindows()
print('[INFO] Done!')
print(50*'*')
print()
print('[INFO] Dataset is ready!')
print(50*'*')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment