Spaces:
Sleeping
Sleeping
Update app.py
Browse filesrecupere class et player depuis csv du dataset et renvoie dans le json
app.py
CHANGED
@@ -512,8 +512,6 @@ async def get_video_frames(file_name: str, frame_id: int) -> dict:
|
|
512 |
try:
|
513 |
# URL du fichier vidéo dans le dataset
|
514 |
video_url = f"https://huggingface.co/datasets/{DATASET_REPO}/resolve/main/{file_name}"
|
515 |
-
|
516 |
-
# Télécharger la vidéo
|
517 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
518 |
response = requests.get(video_url, headers=headers)
|
519 |
|
@@ -522,56 +520,49 @@ async def get_video_frames(file_name: str, frame_id: int) -> dict:
|
|
522 |
|
523 |
# Charger la vidéo en mémoire
|
524 |
video_bytes = io.BytesIO(response.content)
|
525 |
-
|
526 |
-
# Écriture dans un fichier temporaire pour OpenCV
|
527 |
temp_video_path = "/tmp/temp_video.mp4"
|
528 |
with open(temp_video_path, "wb") as f:
|
529 |
f.write(video_bytes.getvalue())
|
530 |
|
531 |
-
# Ouvrir la vidéo avec OpenCV
|
532 |
cap = cv2.VideoCapture(temp_video_path)
|
533 |
-
|
534 |
if not cap.isOpened():
|
535 |
raise HTTPException(status_code=500, detail="Impossible de charger la vidéo")
|
536 |
|
537 |
-
# Obtenir le nombre total de frames dans la vidéo
|
538 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
539 |
-
|
540 |
-
|
541 |
-
start_frame = max(0, frame_id )
|
542 |
-
end_frame = min(total_frames, frame_id + 30) # extraction de 12 framees
|
543 |
|
544 |
frames = []
|
545 |
-
frame_size = (128, 128)
|
|
|
|
|
|
|
|
|
|
|
|
|
546 |
|
547 |
-
# Lire les frames dans la plage définie
|
548 |
for i in range(start_frame, end_frame):
|
549 |
cap.set(cv2.CAP_PROP_POS_FRAMES, i)
|
550 |
ret, frame = cap.read()
|
551 |
if not ret:
|
552 |
-
break
|
553 |
|
554 |
-
# Convertir la frame BGR en RGB
|
555 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
556 |
-
|
557 |
-
# Redimensionner la frame
|
558 |
img = Image.fromarray(frame_rgb).resize(frame_size)
|
559 |
-
|
560 |
-
# Sauvegarder l'image dans un buffer
|
561 |
img_byte_arr = io.BytesIO()
|
562 |
img.save(img_byte_arr, format="PNG")
|
563 |
img_byte_arr.seek(0)
|
564 |
-
|
565 |
-
# Encoder l'image en base64 avec le préfixe Bubble
|
566 |
img_base64 = f"data:image/png;base64,{base64.b64encode(img_byte_arr.getvalue()).decode('utf-8')}"
|
567 |
|
568 |
-
|
569 |
-
|
|
|
|
|
570 |
|
571 |
-
|
572 |
|
|
|
573 |
return {"status": "success", "frames": frames}
|
574 |
-
|
575 |
except Exception as e:
|
576 |
raise HTTPException(status_code=500, detail=f"Erreur lors de l'extraction des frames : {str(e)}")
|
577 |
|
|
|
512 |
try:
|
513 |
# URL du fichier vidéo dans le dataset
|
514 |
video_url = f"https://huggingface.co/datasets/{DATASET_REPO}/resolve/main/{file_name}"
|
|
|
|
|
515 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
516 |
response = requests.get(video_url, headers=headers)
|
517 |
|
|
|
520 |
|
521 |
# Charger la vidéo en mémoire
|
522 |
video_bytes = io.BytesIO(response.content)
|
|
|
|
|
523 |
temp_video_path = "/tmp/temp_video.mp4"
|
524 |
with open(temp_video_path, "wb") as f:
|
525 |
f.write(video_bytes.getvalue())
|
526 |
|
|
|
527 |
cap = cv2.VideoCapture(temp_video_path)
|
|
|
528 |
if not cap.isOpened():
|
529 |
raise HTTPException(status_code=500, detail="Impossible de charger la vidéo")
|
530 |
|
|
|
531 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
532 |
+
start_frame = max(0, frame_id)
|
533 |
+
end_frame = min(total_frames, frame_id + 30)
|
|
|
|
|
534 |
|
535 |
frames = []
|
536 |
+
frame_size = (128, 128)
|
537 |
+
csv_filename = Path(file_name).stem + ".csv"
|
538 |
+
csv_path = UPLOAD_DIR / csv_filename
|
539 |
+
|
540 |
+
df_annotations = None
|
541 |
+
if csv_path.exists():
|
542 |
+
df_annotations = pd.read_csv(csv_path)
|
543 |
|
|
|
544 |
for i in range(start_frame, end_frame):
|
545 |
cap.set(cv2.CAP_PROP_POS_FRAMES, i)
|
546 |
ret, frame = cap.read()
|
547 |
if not ret:
|
548 |
+
break
|
549 |
|
|
|
550 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
|
|
|
551 |
img = Image.fromarray(frame_rgb).resize(frame_size)
|
|
|
|
|
552 |
img_byte_arr = io.BytesIO()
|
553 |
img.save(img_byte_arr, format="PNG")
|
554 |
img_byte_arr.seek(0)
|
|
|
|
|
555 |
img_base64 = f"data:image/png;base64,{base64.b64encode(img_byte_arr.getvalue()).decode('utf-8')}"
|
556 |
|
557 |
+
class_label, player = None, None
|
558 |
+
if df_annotations is not None and i in df_annotations["frame"].values:
|
559 |
+
row = df_annotations[df_annotations["frame"] == i].iloc[0]
|
560 |
+
class_label, player = row["class"], row["player"]
|
561 |
|
562 |
+
frames.append({"frame_index": i, "image": img_base64, "class": class_label, "player": player})
|
563 |
|
564 |
+
cap.release()
|
565 |
return {"status": "success", "frames": frames}
|
|
|
566 |
except Exception as e:
|
567 |
raise HTTPException(status_code=500, detail=f"Erreur lors de l'extraction des frames : {str(e)}")
|
568 |
|