Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Video as Frames OpenCV{PY}

Tags:

python

opencv

I would like to create a program which saves .jpg images taken from the webcam(frames). What my program do for now is, opening a webcam, taking one and only one frame, and then everything stops.

What i would like to have is more than one frame My error-code is this one:

import numpy as np
import cv2
cap = cv2.VideoCapture(0)
count = 0

while True:
   # Capture frame-by-frame
   ret, frame = cap.read()

   # Our operations on the frame come here
   gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
   cv2.imwrite("frame%d.jpg" % ret, frame)     # save frame as JPEG file
   count +=1

   # Display the resulting frame
   cv2.imshow('frame',gray)
   if cv2.waitKey(10):
      break
like image 973
BioShock Avatar asked Dec 09 '14 12:12

BioShock


People also ask

How do you save pictures from a video on OpenCV?

OpenCV also allows us to save that operated video for further usage. For saving images, we use cv2. imwrite() which saves the image to a specified file location.


2 Answers

Actually it sounds like you are always saving your image with the same name because you are concatenating ret instead of count in the imwrite method

try this :

name = "frame%d.jpg"%count
cv2.imwrite(name, frame)     # save frame as JPEG file
like image 152
floppy12 Avatar answered Oct 13 '22 10:10

floppy12


use this -

count = 0
cv2.imwrite("frame%d.jpg" % count, frame)
count = count+1
like image 34
prtkp Avatar answered Oct 13 '22 10:10

prtkp