Spaces:
Sleeping
Sleeping
Update app.py
Browse fileson zip pour limiter le nombre d'appel au dataset
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
# https://Og2-FoosballAnalytics.hf.space/
|
2 |
-
|
3 |
from fastapi import FastAPI, File, Form, UploadFile, HTTPException
|
4 |
from pathlib import Path
|
5 |
import os
|
@@ -533,31 +533,45 @@ async def extract_and_store_frames(file_name: str = Form(...)):
|
|
533 |
frames_dir = f"frames/{video_stem}/"
|
534 |
os.makedirs(frames_dir, exist_ok=True)
|
535 |
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
549 |
|
550 |
cap.release()
|
551 |
|
552 |
-
# Upload
|
553 |
-
|
554 |
-
api.upload_file(path_or_fileobj=
|
555 |
-
path_in_repo=f"frames/{video_stem}
|
556 |
repo_id=DATASET_REPO,
|
557 |
repo_type="dataset",
|
558 |
token=HF_TOKEN)
|
559 |
|
560 |
-
|
|
|
|
|
|
|
|
|
561 |
except Exception as e:
|
562 |
raise HTTPException(status_code=500, detail=f"Erreur lors de l'extraction des frames : {str(e)}")
|
563 |
|
|
|
1 |
# https://Og2-FoosballAnalytics.hf.space/
|
2 |
+
import zipfile
|
3 |
from fastapi import FastAPI, File, Form, UploadFile, HTTPException
|
4 |
from pathlib import Path
|
5 |
import os
|
|
|
533 |
frames_dir = f"frames/{video_stem}/"
|
534 |
os.makedirs(frames_dir, exist_ok=True)
|
535 |
|
536 |
+
# Créer un fichier ZIP pour stocker toutes les images
|
537 |
+
zip_filename = f"{frames_dir}{video_stem}_frames.zip"
|
538 |
+
with zipfile.ZipFile(zip_filename, "w", zipfile.ZIP_DEFLATED) as zipf:
|
539 |
+
frame_id = 0
|
540 |
+
while True:
|
541 |
+
ret, frame = cap.read()
|
542 |
+
if not ret:
|
543 |
+
break
|
544 |
+
|
545 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
546 |
+
img = Image.fromarray(frame_rgb).resize(frame_size)
|
547 |
+
|
548 |
+
# Sauvegarder chaque image temporairement avant de la zipper
|
549 |
+
temp_image_filename = f"{frames_dir}{video_stem}_{frame_id}.png"
|
550 |
+
img.save(temp_image_filename)
|
551 |
+
|
552 |
+
# Ajouter l'image dans le fichier ZIP
|
553 |
+
zipf.write(temp_image_filename, arcname=f"{video_stem}_{frame_id}.png")
|
554 |
+
|
555 |
+
# Supprimer l'image temporaire pour libérer de l'espace
|
556 |
+
os.remove(temp_image_filename)
|
557 |
+
|
558 |
+
frame_id += 1
|
559 |
|
560 |
cap.release()
|
561 |
|
562 |
+
# Upload du fichier ZIP dans le dataset Hugging Face
|
563 |
+
with open(zip_filename, "rb") as f:
|
564 |
+
api.upload_file(path_or_fileobj=f,
|
565 |
+
path_in_repo=f"frames/{video_stem}_frames.zip",
|
566 |
repo_id=DATASET_REPO,
|
567 |
repo_type="dataset",
|
568 |
token=HF_TOKEN)
|
569 |
|
570 |
+
# Supprimer le fichier ZIP local après upload
|
571 |
+
os.remove(zip_filename)
|
572 |
+
|
573 |
+
return {"status": "success", "message": f"{frame_id} frames extraites et stockées dans un fichier ZIP."}
|
574 |
+
|
575 |
except Exception as e:
|
576 |
raise HTTPException(status_code=500, detail=f"Erreur lors de l'extraction des frames : {str(e)}")
|
577 |
|