Skip to content

Instantly share code, notes, and snippets.

@eruffaldi
Last active September 27, 2017 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eruffaldi/d927285736899c10160c0726b64569fc to your computer and use it in GitHub Desktop.
Save eruffaldi/d927285736899c10160c0726b64569fc to your computer and use it in GitHub Desktop.
python cross api videoio
import cv2
import sys
try:
import imageio
except:
print "missing imageio"
imageio = None
try:
import skvideo.io
except:
print "missing skvideo.io"
skvideo = None
class VideoIOVideoCapture:
def __init__(self,name):
self.r = skvideo.io.vreader(name)
self.g = self.r.nextFrame()
# imageio is BGR
# opencv is RGB
def read(self):
r = self.g.next()
return (r is not None,r)
class VideoIOVideoCaptureFFMPEG:
def __init__(self,name):
self.r = skvideo.io.FFmpegReader(name)
self.g = self.r.nextFrame()
# imageio is BGR
# opencv is RGB
def read(self):
r = self.g.next()
return (r is not None,r)
class ImageIOVideoCapture:
def __init__(self,name):
self.r = imageio.get_reader(name)
# imageio is BGR
# opencv is RGB
def read(self):
r = self.r.get_next_data()
return (r is not None,r)
def main():
if len(sys.argv) == 1:
print "videocaptest: videofile [mode=imageio|cv|scikit|scikitffmpeg]"
mode = "cv"
if len(sys.argv) > 2:
mode = sys.argv[2]
if mode == "imageio":
a = ImageIOVideoCapture(sys.argv[1])
elif mode == "scikit":
a = ImageIOVideoCapture(sys.argv[1])
elif mode == "scikitffmpeg":
a = VideoIOVideoCaptureFFMPEG(sys.argv[1])
elif mode == "cv":
a = cv2.VideoCapture(sys.argv[1])
else:
print "unknown mode"
return
while True:
r,img = a.read()
if img is None:
break
cv2.imshow("ciao",img)
cv2.waitKey(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment