【问题标题】:Read an video file object from S3 and use it for further processing through Opencv从 S3 读取视频文件对象并通过 Opencv 将其用于进一步处理
【发布时间】:2019-03-09 23:53:23
【问题描述】:
import boto3

import cv2
import numpy as np
s3 = boto3.resource('s3')
vid = (s3.Object('bucketname', 'video.blob').get()['Body'].read())
cap = cv2.VideoCapture(vid)

这是我的代码。我在 s3 存储桶中有一个视频文件。我想用 OpenCV 对它做一些处理,我不想下载它。所以我试图将该视频文件存储到vid。现在的问题是type(vid)byte,这是导致第6 行出现此错误TypeError: an integer is required (got type bytes) 的原因。我尝试将其转换为整数或字符串,但无法转换。

在尝试将字节转换为整数时:我提到了this,但遇到了长度问题。这只是一个示例视频文件。当转换为字节对象时,我想要处理的实际文件会很大。

尝试将对象作为字符串获取然后将其转换为整数:我提到了this。即使这似乎对我不起作用。

如果有人能帮我解决这个问题,我将不胜感激。如果您对我的问题有任何不确定之处,请发表评论,我会尽力提供更多详细信息。

【问题讨论】:

  • 由于您似乎想从某个网络资源访问视频,您可能需要一些包含 URL 的字符串——给VideoCapture 构造函数的整数标识符仅用于识别本地摄像机。跨度>
  • 视频文件的url为强制下载url。我正在尝试通过更改设置使其可见。即使如果我可以让它显示,我也无法通过 OpenCv 访问它,因为 VideoCapture 仅适用于 https://www.example.com/myimage.mp4 而对于像 https://www.youtube.com/watch?v=jNQXAC9IVRw 这样的视频链接它不会起作用。请问还有其他可能的解决方案吗?
  • 感谢您的帮助。这是相关的,但它并不能完全解决我的问题。
  • 嗨!你有办法解决吗? @VinayVarma

标签: python-3.x opencv amazon-s3 boto3 video-processing


【解决方案1】:

如果从 url 流式传输视频是一种可接受的解决方案,我认为这是最简单的解决方案。您只需要生成一个 url 即可从中读取视频。

import boto3
import cv2

s3_client = boto3.client('s3')
        
bucket = 'bucketname'      
key = 'video.blob' 
            
url = s3_client.generate_presigned_url('get_object', 
                                       Params = {'Bucket': bucket, 'Key': key}, 
                                       ExpiresIn = 600) #this url will be available for 600 seconds
    
cap = cv2.VideoCapture(url)
       
ret, frame = cap.read() 

您应该看到您能够从该 url 读取和处理帧。

【讨论】:

    【解决方案2】:

    参考下面有用的代码 sn-p 对 S3 存储桶执行各种操作。

    import boto3
    s3 = boto3.resource('s3', region_name='us-east-2')
    

    用于在 s3 中列出存储桶

    for bucket in s3.buckets.all():
        print(bucket.name)
    

    在 s3 中创建存储桶

    my_bucket=s3.create_bucket(Bucket='Bucket Name', CreateBucketConfiguration={
        'LocationConstraint': 'us-east-2'
    })
    

    列出桶内的对象

    my_bucket = s3.Bucket('Bucket Name')
    for file in my_bucket.objects.all():
        print (file.key)        
    

    从当前目录上传文件

    import os
    print(os.getcwd())
    
    fileName="B01.jpg"
    bucketName="Bucket Name"
    file = open(fileName)
    s3.meta.client.upload_file(fileName, bucketName, 'test2.txt')   
    

    从存储桶中读取图像/视频

    import matplotlib.pyplot as plt
    s3 = boto3.resource('s3', region_name='us-east-2')
    bucket = s3.Bucket('Bucket Name')   # bucket name
    object = bucket.Object('maisie_williams.jpg')  # image name
    object.download_file('B01.jpg')            #donwload image with this name
    img=plt.imread('B01.jpg')              #read the downloaded image
    imgplot = plt.imshow(img)              #plot the image
    plt.show(imgplot)  
    

    从一个存储桶中读取,然后将其转储到另一个存储桶中

    import boto3
    s3 = boto3.resource('s3', region_name='us-east-2')
    bucket = s3.Bucket('Bucket Name')   # bucket name
    object = bucket.Object('maisie_williams.jpg')  # image name
    object.download_file('B01.jpg')
    
    
    
    fileName="B01.jpg"
    bucketName="Bucket Name"
    file = open(fileName)
    s3.meta.client.upload_file(fileName, bucketName, 'testz.jpg')
    

    如果您有访问密钥,那么您可能可以执行以下操作

    keys = pd.read_csv('accessKeys.csv')
            #creating Session for S3 buckets
            session = boto3.session.Session(aws_access_key_id=keys['Access key ID'][0],
                aws_secret_access_key=keys['Secret access key'][0])
            s3 = session.resource('s3')
    
            buck = s3.Bucket('Bucket Name')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多