Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import os
- from pylab import rcParams
- ###############################################
- myPath = 'C:/Users/Lenovo/Desktop/Vortex AUV/Gate video frames/createData'
- cameraNo = 'C:/Users/Lenovo/Desktop/Vortex AUV/Gate videos/29.mp4'
- cameraBrightness = 190 #ONLY USEFUL IF USING WEB CAM
- moduleVal = 10 #SAVE EVERY ITH FRAME TO AVOID REPETITION
- grayImage = False #IMAGES SAVED IN GRAY
- saveData = True #SAVE DATA FLAG
- showImage = True #IMAGE DISPLAY FLAG
- resizeFlag = True
- imgWidth = 180
- imgHeight = 120
- ###############################################
- countFolder = 0
- cap = cv2.VideoCapture(cameraNo)
- cap.set(3, 640)
- cap.set(4, 480)
- cap.set(10, cameraBrightness)
- count = 0
- countSave = 0
- def saveDataFunc():
- global countFolder
- countFolder = 0
- while os.path.exists(myPath + str(countFolder)):
- countFolder = countFolder + 1
- os.makedirs(myPath + str(countFolder))
- if saveData:saveDataFunc()
- breakFlag = 0
- while True:
- success, img = cap.read()
- # Some frames are empty, but if there are more than 31 consecutive frames empty, that makes us sure we finished the video
- if img is None:
- print('--(!) No captured frame -- Continue! ({}/30)'.format(breakFlag))
- breakFlag += 1
- if breakFlag > 30:
- break
- continue
- breakFlag = 0
- if resizeFlag:img = cv2.resize(img,(imgWidth, imgHeight))
- if grayImage:img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- if saveData:
- if count % moduleVal == 0:
- cv2.imwrite(myPath + str(countFolder) +
- '/' + str(countSave) + ".png", img)
- countSave += 1
- count += 1
- if showImage:
- cv2.imshow("Image", img)
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- cap.release()
- cv2.destroyAllWindows()
- print("Done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement