目前,我正在根据以下代码检索视频帧:
# Retrieve Images from Videos
for video in list_videos:
vidcap = cv2.VideoCapture(video)
success, image = vidcap.read()
count = 0
print("Successfully retrieving videos from ", video)
while success:
cv2.imwrite('.'+video.split('.')[-2]+'/'+'frame%d.png' %count, image)
success, image = vidcap.read()
count += 1
vidcap.release()
此代码需要大量时间才能从视频中提取帧。有没有更快的方法来实现此代码?
答案 0 :(得分:0)
花费最多时间的代码行是:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<UserSecretsId>aspnet-xxx</UserSecretsId>
<ApplicationInsightsResourceId>/subscriptions/xxx/resourcegroups/xxx/providers/microsoft.insights/components/xxx</ApplicationInsightsResourceId>
<ApplicationInsightsAnnotationResourceId>/subscriptions/xxx/resourcegroups/MyIndigoHealth/providers/microsoft.insights/components/xxx</ApplicationInsightsAnnotationResourceId>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<TypeScriptToolsVersion>3.1</TypeScriptToolsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.6.1" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" PrivateAssets="All" />
<PackageReference Include="Neleus.DependencyInjection.Extensions" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Areas\Identity\Services\" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Connected Services" />
</ItemGroup>
</Project>
使该行更快的一种方法是在将其写入硬盘之前调整图像的大小(使其更小):
cv2.imwrite('.'+video.split('.')[-2]+'/'+'frame%d.png' %count, image)
另一种更快处理视频的方法是将图像写入内存,而不是硬盘。仅当您有足够的RAM来容纳图像时,此方法才有效。这可能会导致灾难性的计算机崩溃:
cv2.resize(image, (640,480))