Python imageio 模块,get_reader() 实例源码

我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用imageio.get_reader()

项目:video_labelling_using_youtube8m    作者:LittleWat    | 项目源码 | 文件源码
def get_feature_mat_from_video(video_filename, output_dir='output'):
    yt_vid, extension = video_filename.split('/')[-1].split('.')

    assert extension in ['webm', 'mp4', '3gp']

    mkdir_if_not_exist(output_dir, False)

    output_filename = output_dir + '/' + yt_vid + '.npy'

    vid_reader = imageio.get_reader(video_filename, 'ffmpeg')

    img_list = get_img_list_from_vid_reader(vid_reader, extension)

    base_model = InceptionV3(include_top=True, weights='imagenet')
    model = Model(inputs=base_model.input, outputs=base_model.get_layer('avg_pool').output)

    feature_mat = get_feature_mat(model, img_list)

    np.save(output_filename, feature_mat)

    return feature_mat
项目:convert360    作者:MateusZitelli    | 项目源码 | 文件源码
def main(input_path, output_path, size, input_type, output_type):
    output_size = (size[0] * 3, size[1] * 2)

    reader = imageio.get_reader(input_path)
    metadata = reader.get_meta_data()

    projector = get_projector(input_type, output_type)

    with projector(output_size) as renderer:
        writer_args = {}
        frames = 1
        if 'fps' in metadata:
            # Handle videos
            writer_args['fps'] = metadata['fps']
            frames = metadata['nframes']

        with imageio.get_writer(output_path, **writer_args) as writer:
            if frames > 1:
                render_many(renderer, reader, writer, frames)
            else:
                render_single(renderer, reader, writer)
项目:SlidingWindowVideoTDA    作者:ctralie    | 项目源码 | 文件源码
def loadImageIOVideo(path):
    if not os.path.exists(path):
        print("ERROR: Video path not found: %s"%path)
        return None
    import imageio
    videoReader = imageio.get_reader(path, 'ffmpeg')
    NFrames = videoReader.get_length()
    F0 = videoReader.get_data(0)
    IDims = F0.shape
    I = np.zeros((NFrames, F0.size))
    I[0, :] = np.array(F0.flatten(), dtype = np.float32)/255.0
    for i in range(1, NFrames):
        I[i, :] = np.array(videoReader.get_data(i).flatten(), dtype = np.float32)/255.0
    return (I, IDims)
项目:ImgurPlus    作者:DcSoK    | 项目源码 | 文件源码
def upload_imgur(image):
    try:
        if image.file_path[::-1].split('.')[0][::-1] == "mp4":
            mp4request = requests.get(image.file_path)
            mp4 = BytesIO(mp4request.content)
            gif = imageio.get_reader(mp4,  'ffmpeg')
            fps = gif.get_meta_data()['fps']
            frames = []
            for i,im in enumerate(gif):
                frames.append(im)
            temp = BytesIO()
            imageio.mimsave(temp, frames, format='GIF', fps=fps)
            temp.seek(0)
        elif image.file_path[::-1].split('.')[0][::-1] == "webp":
            imgrequest = requests.get(image.file_path)
            img = BytesIO(imgrequest.content)
            image2 = Image.open(img)
            temp = BytesIO()
            image2.save(temp, 'png')
            temp.seek(0)

        client_id = botconfig.clientid_imgur
        headers = {"Authorization": "Client-ID " + client_id}
        api_key = botconfig.api_imgur
        url = "https://api.imgur.com/3/upload.json"
        rpost = requests.post(
            url, 
            headers = headers,
            data = {
                'key': api_key, 
                'image': b64encode(temp.read()) if image.file_path[::-1].split('.')[0][::-1] == "mp4" or image.file_path[::-1].split('.')[0][::-1] == "webp" else image.file_path, 
                'type': 'base64' if image.file_path[::-1].split('.')[0][::-1] == "mp4" or image.file_path[::-1].split('.')[0][::-1] == "webp" else 'url',
                'name': image.file_id,
                'title': image.file_id + 'Upload by @imgurplusbot'
            }
        )
        return json.loads(rpost.text)["data"]["link"] if rpost.status_code == 200 else "Error uploading image."
    except:
        return "Error uploading image."
项目:video-loop-detection    作者:sunnybala    | 项目源码 | 文件源码
def find_duplicates(res=32):
    # load in the video file
    filename = 'video.mp4'
    vid = imageio.get_reader(filename,  'ffmpeg')
    all_frames = vid.get_length()

    # we'll store the info on repeated frames here
    seen_frames = {}
    duplicate_frames = {}

    for x in range(all_frames): 
        # get frame x
        frame = vid.get_data(x)

        if x % 1000 == 0:
            print("frame count: ",x,"\t",round(x*1.0/all_frames,3)*100,'%')

        # hash our frame
        hashed = ahash(frame, res)

        if seen_frames.get( hashed, None):
            # if we've seen this frame before, add it to the list of frames 
            # that all have the same hashed value in duplicate_frames
            duplicate_frames[hashed].append(x)
        else:
            # if it's the first time seeing a frame, put it in seen_frames
            seen_frames[hashed] = x
            duplicate_frames[hashed] = [x]

    # return a list of lists of duplicate frames
    return [duplicate_frames[x] for x in duplicate_frames if len(duplicate_frames[x]) > 1]
项目:odin    作者:imito    | 项目源码 | 文件源码
def read(path, boxes=None):
  """
  Return
  ------
  Always return 3D images
  (n_frames, channels, width, height)
  """
  import imageio
  vid = imageio.get_reader(path)
  metadata = vid.get_meta_data()
  fps = metadata['fps']
  nb_frames = metadata['nframes']
  if boxes is not None:
    pass
  print(nb_frames)
  exit()
  try:
    frames = []
    for i in vid:
      # it is bizzare why width and height are swapped
      if i.ndim == 3: # swap channel first
        i = i.transpose(2, 1, 0)
      else:
        i = np.expand_dims(i.transpose(1, 0), 1)
      frames.append(i)
  except RuntimeError:
    pass
  frames = np.array(frames, dtype=frames[0].dtype)
  return frames, fps
项目:iclr2017mcnet    作者:rubenvillegas    | 项目源码 | 文件源码
def load_kth_data(f_name, data_path, image_size, K, T): 
  flip = np.random.binomial(1,.5,1)[0]
  tokens = f_name.split()
  vid_path = data_path + tokens[0] + "_uncomp.avi"
  vid = imageio.get_reader(vid_path,"ffmpeg")
  low = int(tokens[1])
  high = np.min([int(tokens[2]),vid.get_length()])-K-T+1
  if low == high:
    stidx = 0 
  else:
    if low >= high: print(vid_path)
    stidx = np.random.randint(low=low, high=high)
  seq = np.zeros((image_size, image_size, K+T, 1), dtype="float32")
  for t in xrange(K+T):
    img = cv2.cvtColor(cv2.resize(vid.get_data(stidx+t),
                       (image_size,image_size)),
                       cv2.COLOR_RGB2GRAY)
    seq[:,:,t] = transform(img[:,:,None])

  if flip == 1:
    seq = seq[:,::-1]

  diff = np.zeros((image_size, image_size, K-1, 1), dtype="float32")
  for t in xrange(1,K):
    prev = inverse_transform(seq[:,:,t-1])
    next = inverse_transform(seq[:,:,t])
    diff[:,:,t-1] = next.astype("float32")-prev.astype("float32")

  return seq, diff
项目:iclr2017mcnet    作者:rubenvillegas    | 项目源码 | 文件源码
def load_s1m_data(f_name, data_path, trainlist, K, T):
  flip = np.random.binomial(1,.5,1)[0]
  vid_path = data_path + f_name
  img_size = [240,320]

  while True:
    try:
      vid = imageio.get_reader(vid_path,"ffmpeg")
      low = 1
      high = vid.get_length()-K-T+1
      if low == high:
        stidx = 0
      else:
        stidx = np.random.randint(low=low, high=high)
      seq = np.zeros((img_size[0], img_size[1], K+T, 3),
                     dtype="float32")
      for t in xrange(K+T):
        img = cv2.resize(vid.get_data(stidx+t),
                         (img_size[1],img_size[0]))[:,:,::-1]
        seq[:,:,t] = transform(img)

      if flip == 1:
        seq = seq[:,::-1]

      diff = np.zeros((img_size[0], img_size[1], K-1, 1),
                      dtype="float32")
      for t in xrange(1,K):
        prev = inverse_transform(seq[:,:,t-1])*255
        prev = cv2.cvtColor(prev.astype("uint8"),cv2.COLOR_BGR2GRAY)
        next = inverse_transform(seq[:,:,t])*255
        next = cv2.cvtColor(next.astype("uint8"),cv2.COLOR_BGR2GRAY)
        diff[:,:,t-1,0] = (next.astype("float32")-prev.astype("float32"))/255.
      break
    except Exception:
      # In case the current video is bad load a random one 
      rep_idx = np.random.randint(low=0, high=len(trainlist))
      f_name = trainlist[rep_idx]
      vid_path = data_path + f_name

  return seq, diff
项目:video-captioning    作者:vijayvee    | 项目源码 | 文件源码
def playVideo(video_urls):
    video = imageio.get_reader(YOUTUBE_CLIPS_DIR + video_urls[0] + '.avi','ffmpeg')
    for frame in video:
        fr = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
        cv2.imshow('frame',fr)
        if cv2.waitKey(40) & 0xFF == ord('q'):
            break
    cv2.destroyAllWindows()
项目:activitynet-essentials    作者:alex-paterson    | 项目源码 | 文件源码
def open_video_by_id(self, id):
        filename = os.path.join(self.videos_path, "v_{}.mp4".format(id))
        return imageio.get_reader(filename, 'ffmpeg')
项目:deepvisualminer    作者:pathbreak    | 项目源码 | 文件源码
def detectvideo(vid_file, detector_xml_path, dest_img_dir):

    if not os.path.exists(dest_img_dir):
        os.makedirs(dest_img_dir)

    detector = cv2.CascadeClassifier(detector_xml_path)

    vid = imageio.get_reader(vid_file, 'ffmpeg')
    # If size and source_size are not equal, then device was probably
    # rotated (like a mobile) and we should compensate for the rotation.
    # Images will have 'source_size' dimensions but we need 'size'.
    metadata = vid.get_meta_data()
    rotate = False
    if metadata['source_size'] != metadata['size']:
        print('Rotating')
        rotate = True

    for i, img in enumerate(vid):
        if rotate:
            #img = np.transpose(img, axes=(1, 0, 2)).copy()
            img = np.rot90(img).copy()

        print('Frame ',i, img.shape)

        gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        min_size = (min(20, gray_img.shape[0] // 10), min(20, gray_img.shape[1] // 10))
        hits = detector.detectMultiScale(gray_img, 1.1, 3, 0, min_size)
        #cv2.groupRectangles(hits, 2)
        print(len(hits), ' hits')

        hits_img = np.copy(img)

        if len(hits) > 0:
            for (x,y,w,h) in hits:
                cv2.rectangle(hits_img, (x,y), (x+w, y+h), (0,0,255), 2)

        cv2.imwrite(os.path.join(dest_img_dir, 'frame-%d.png'%(i)), hits_img)
项目:whitewater-encoder    作者:samiare    | 项目源码 | 文件源码
def __init__(self, path_to_file, **kwargs):
        self.debug = kwargs['debug'] if 'debug' in kwargs else False
        self.paths = {'input': path_to_file,
                      'output': self._get_output_directory(path_to_file)}
        self.options = self._get_options(kwargs)
        self.tracker = FrameTracker(self.options['blocksize'], self.options['grid'])

        try:
            self.video = imageio.get_reader(self.paths['input'])
        except IOError:
            self.exit('video not found')

        self.frame_maps = []
        self.consecutive = 0
项目:video-captioning    作者:vijayvee    | 项目源码 | 文件源码
def extract_feats(filenames,batch_size):
    """Function to extract VGG-16 features for frames in a video.
       Input:
            filenames:  List of filenames of videos to be processes
            batch_size: Batch size for feature extraction
       Writes features in .npy files"""
    model_file = './VGG_ILSVRC_16_layers.caffemodel'
    deploy_file = './VGG16_deploy.prototxt'
    net = caffe.Net(deploy_file,model_file,caffe.TEST)
    layer = 'fc7'
    mean_file = './ilsvrc_2012_mean.npy'
    transformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape})
    transformer.set_mean('data',np.load(mean_file).mean(1).mean(1))
    transformer.set_transpose('data',(2,0,1))
    transformer.set_raw_scale('data',255.0)
    net.blobs['data'].reshape(batch_size,3,224,224)
    print "VGG Network loaded"
    #Read videos and extract features in batches
    for file in filenames:
        vid = imageio.get_reader(file,'ffmpeg')
        curr_frames = []
        for frame in vid:
            frame = skimage.transform.resize(frame,[224,224])
            if len(frame.shape)<3:
                frame = np.repeat(frame,3).reshape([224,224,3])
            curr_frames.append(frame)
        curr_frames = np.array(curr_frames)
        print "Shape of frames: {0}".format(curr_frames.shape)
        idx = map(int,np.linspace(0,len(curr_frames)-1,80))
        curr_frames = curr_frames[idx,:,:,:]
        print "Captured 80 frames: {0}".format(curr_frames.shape)
        curr_feats = []
        for i in range(0,80,batch_size):
            caffe_in = np.zeros([batch_size,3,224,224])
            curr_batch = curr_frames[i:i+batch_size,:,:,:]
            for j in range(batch_size):
                caffe_in[j] = transformer.preprocess('data',curr_batch[j])
            out = net.forward_all(blobs=[layer],**{'data':caffe_in})
            curr_feats.extend(out[layer])
            print "Appended {} features {}".format(j+1,out[layer].shape)
        curr_feats = np.array(curr_feats)
        np.save(file[:-4] + '.npy',curr_feats)
        print "Saved file {}\nExiting".format(file[:-4] + '.npy')