如何使用 JavaScript/ReactJS 在 FastAPI 中上传视频列表并使用 OpenCV 处理它们?
我正在尝试从上传单个视频转换为上传和处理多个视频。但是,看来我的代码保存/仅读取第一个视频。我似乎无法弄清楚为什么,因为当我打印上传的文件列表时,它确实包含了所有被忽略的后续视频。
这是后端中的代码的样子:
@app.post("/upload")
def upload_video(fileList: Optional[List[UploadFile]] = File(None)):
videofiles = []
for file in fileList:
print("Uploading:", file.filename)
print(".................................")
extension = file.filename.split(".")[-1] in ("mp4", "avi")
if not extension:
return "Video must be in mp4 or avi format!"
try:
try:
contents = file.file.read()
with temp as f:
print("what's happening: ")
f.write(contents)
videofiles.append(cv2.VideoCapture(temp.name))
print('list of videos uploaded :')
for vidcap in videofiles:
print("video:", vidcap)
# Check if camera opened successfully
if (vidcap.isOpened() == False):
print("Error opening video file")
# get height, width and frame count of the video
width, height = (
int(vidcap.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))
)
print(f"width: {width}")
print(f"height: {height}")
# count the number of frames
frames = vidcap.get(cv2.CAP_PROP_FRAME_COUNT)
fps = vidcap.get(cv2.CAP_PROP_FPS)
# calculate duration of the video
seconds = round(frames / fps)
video_time = datetime.timedelta(seconds=seconds)
print(f"duration in seconds: {seconds}")
print(f"video time: {video_time}")
except Exception:
return {"message": "There was an error uploading the file"}
finally:
file.file.close()
except Exception:
return {"message": "There was an error processing the file"}
finally:
os.remove(temp.name)
count = 0;
for vid in videofiles:
count += 1
print("number of video capture objects uploaded:", count)
return {"uploadStatus": "Complete", "filenames": [f.filename for f in fileList]}
我有一种与视频捕获有关的感觉上传。但是,正如您可以从屏幕截图中看到的那样,视频捕获列表仅具有第一个视频的一个对象。
对可能导致的原因有什么想法吗?
edit :我使用了此问题对于单个视频上传,并以相同的逻辑构建,以通过视频列表进行迭代,但这也不起作用。
I'm trying to switch from uploading a single video to uploading and processing multiple videos. However, it seems that my code saves/reads only the first video. I can't seem to figure out why, as when I print the list of files uploaded, it does include all the subsequent videos that get ignored.
Backend: FastAPI
This is what the code looks like in the backend:
@app.post("/upload")
def upload_video(fileList: Optional[List[UploadFile]] = File(None)):
videofiles = []
for file in fileList:
print("Uploading:", file.filename)
print(".................................")
extension = file.filename.split(".")[-1] in ("mp4", "avi")
if not extension:
return "Video must be in mp4 or avi format!"
try:
try:
contents = file.file.read()
with temp as f:
print("what's happening: ")
f.write(contents)
videofiles.append(cv2.VideoCapture(temp.name))
print('list of videos uploaded :')
for vidcap in videofiles:
print("video:", vidcap)
# Check if camera opened successfully
if (vidcap.isOpened() == False):
print("Error opening video file")
# get height, width and frame count of the video
width, height = (
int(vidcap.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))
)
print(f"width: {width}")
print(f"height: {height}")
# count the number of frames
frames = vidcap.get(cv2.CAP_PROP_FRAME_COUNT)
fps = vidcap.get(cv2.CAP_PROP_FPS)
# calculate duration of the video
seconds = round(frames / fps)
video_time = datetime.timedelta(seconds=seconds)
print(f"duration in seconds: {seconds}")
print(f"video time: {video_time}")
except Exception:
return {"message": "There was an error uploading the file"}
finally:
file.file.close()
except Exception:
return {"message": "There was an error processing the file"}
finally:
os.remove(temp.name)
count = 0;
for vid in videofiles:
count += 1
print("number of video capture objects uploaded:", count)
return {"uploadStatus": "Complete", "filenames": [f.filename for f in fileList]}
This is what I last got from this code:
I have a feeling this has to do with video-capture but I thought this was addressed when I switched from looping through the list of videos with a single video capture to a list of video captures for each video uploaded. But as you can see from the screenshot, the list of video captures only has the one object for the first video.
Any idea on what might be causing this?
Edit: I made use of this question for the single video upload and built on the same logic to iterate through the list of videos, but that didn't work either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面是如何使用 Fetch API< 上传视频列表的示例/a> 在前端, FastAPI 在后端,以及 OpenCV 来处理文件。此示例基于此答案和
工作示例
app.py
templates/index.html
Below is an example on how to upload a list of videos using Fetch API in the frontend and FastAPI in the backend, as well as OpenCV to process the files. This example is based on this answer and this answer.
Working Example
app.py
templates/index.html