Advertisement
Velaxers

Video to frames.py

Oct 7th, 2020 (edited)
2,289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. import cv2
  2. import os
  3. from pylab import rcParams
  4.  
  5. ###############################################
  6.  
  7. myPath = 'C:/Users/Lenovo/Desktop/Vortex AUV/Gate video frames/createData'
  8. cameraNo = 'C:/Users/Lenovo/Desktop/Vortex AUV/Gate videos/29.mp4'
  9. cameraBrightness = 190 #ONLY USEFUL IF USING WEB CAM
  10. moduleVal = 10 #SAVE EVERY ITH FRAME TO AVOID REPETITION
  11. grayImage = False #IMAGES SAVED IN GRAY
  12. saveData = True #SAVE DATA FLAG
  13. showImage = True #IMAGE DISPLAY FLAG
  14. resizeFlag = True
  15. imgWidth = 180
  16. imgHeight = 120
  17.  
  18. ###############################################
  19.  
  20. countFolder = 0
  21. cap = cv2.VideoCapture(cameraNo)
  22. cap.set(3, 640)
  23. cap.set(4, 480)
  24. cap.set(10, cameraBrightness)
  25.  
  26. count = 0
  27. countSave = 0
  28.  
  29. def saveDataFunc():
  30.     global countFolder
  31.     countFolder = 0
  32.     while os.path.exists(myPath + str(countFolder)):
  33.         countFolder = countFolder + 1
  34.     os.makedirs(myPath + str(countFolder))
  35.  
  36. if saveData:saveDataFunc()
  37.  
  38. breakFlag = 0
  39. while True:
  40.     success, img = cap.read()
  41.     # Some frames are empty, but if there are more than 31 consecutive frames empty, that makes us sure we finished the video
  42.     if img is None:
  43.         print('--(!) No captured frame -- Continue! ({}/30)'.format(breakFlag))
  44.         breakFlag += 1
  45.         if breakFlag > 30:
  46.             break
  47.         continue
  48.     breakFlag = 0
  49.  
  50.     if resizeFlag:img = cv2.resize(img,(imgWidth, imgHeight))
  51.     if grayImage:img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  52.     if saveData:
  53.         if count % moduleVal == 0:
  54.             cv2.imwrite(myPath + str(countFolder) +
  55.                     '/' + str(countSave) + ".png", img)
  56.             countSave += 1
  57.         count += 1
  58.    
  59.  
  60.     if showImage:
  61.         cv2.imshow("Image", img)
  62.  
  63.     if cv2.waitKey(1) & 0xFF == ord('q'):
  64.         break
  65.  
  66. cap.release()
  67. cv2.destroyAllWindows()
  68. print("Done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement