Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -281,8 +281,6 @@ async def run_video_processing(file_path: str):
|
|
281 |
# Cette fonction va utiliser l'exécuteur pour éviter de bloquer le thread principal
|
282 |
loop = asyncio.get_event_loop()
|
283 |
result = await loop.run_in_executor(executor, predict_video, file_path)
|
284 |
-
# Vous pouvez ici stocker ou traiter les résultats
|
285 |
-
print(result) # ou enregistrez dans une base de données, etc.
|
286 |
|
287 |
|
288 |
def predict_video(video):
|
@@ -342,11 +340,8 @@ def predict_video(video):
|
|
342 |
|
343 |
return results
|
344 |
|
345 |
-
import json
|
346 |
-
import numpy as np
|
347 |
-
|
348 |
def ComputeStatistics(df):
|
349 |
-
# Calculer
|
350 |
goalScore1 = df['top1'].str.startswith("Goal_1").sum()
|
351 |
goalConceeded = df['top1'].str.startswith("Goal_2").sum()
|
352 |
totalShots1 = df['top1'].str.startswith("Shot_1").sum()
|
@@ -355,6 +350,20 @@ def ComputeStatistics(df):
|
|
355 |
goal1_5 = df['top1'].str.startswith("Goal_1-5").sum()
|
356 |
save1 = (df['top1'] == "Block_2-1").sum() # Compter uniquement si top1 est exactement "Block_2-1"
|
357 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
358 |
# Calculer le temps du premier Goal_1
|
359 |
first_goal1_row = df[df['top1'].str.startswith("Goal_1")].iloc[0] if not df[df['top1'].str.startswith("Goal_1")].empty else None
|
360 |
timeFirstGoal1 = (1 / 30) * first_goal1_row['start_frame'] if first_goal1_row is not None else None
|
@@ -372,7 +381,15 @@ def ComputeStatistics(df):
|
|
372 |
"goal1_5": goal1_5,
|
373 |
"save1": save1,
|
374 |
"timeFirstGoal1": timeFirstGoal1,
|
375 |
-
"convertionRate1": convertionRate1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
376 |
}
|
377 |
|
378 |
# Convertir les valeurs non compatibles en types natifs avant la sérialisation
|
@@ -392,63 +409,3 @@ def ComputeStatistics(df):
|
|
392 |
|
393 |
|
394 |
|
395 |
-
|
396 |
-
"""
|
397 |
-
def predict_video(video):
|
398 |
-
print("##### predict_video started #####")
|
399 |
-
|
400 |
-
# Charger les frames de la vidéo
|
401 |
-
frames = load_video(video, offload_to_cpu=True)
|
402 |
-
|
403 |
-
# Découper les frames en petits segments de 8 frames
|
404 |
-
segment_size = MAX_SEQ_LENGTH
|
405 |
-
total_frames = len(frames)
|
406 |
-
print("total_frames = ", total_frames)
|
407 |
-
segments = []
|
408 |
-
|
409 |
-
for i in range(0, total_frames, segment_size):
|
410 |
-
# Découper un segment de 8 frames (ou moins si c'est la fin de la vidéo)
|
411 |
-
segment = frames[i:i+segment_size]
|
412 |
-
segments.append((i, segment)) # Conserver l'index du début du segment et le segment de frames
|
413 |
-
|
414 |
-
# Dictionnaire pour stocker les résultats
|
415 |
-
results = []
|
416 |
-
|
417 |
-
# Analyser chaque segment de 8 frames
|
418 |
-
for start_idx, segment in segments:
|
419 |
-
frame_features = prepare_single_video(segment)
|
420 |
-
probabilities = model.predict(frame_features)[0]
|
421 |
-
|
422 |
-
# Obtenir le top 5 des classes les plus probables
|
423 |
-
top_5_indices = np.argsort(probabilities)[::-1][:5]
|
424 |
-
top_5_classes = {class_labels[i]: float(probabilities[i]) for i in top_5_indices}
|
425 |
-
|
426 |
-
# Ajouter les informations du segment dans les résultats
|
427 |
-
result = {
|
428 |
-
"start_frame": start_idx,
|
429 |
-
"end_frame": min(start_idx + segment_size - 1, total_frames - 1), # Assurer que la frame finale n'excède pas le nombre total de frames
|
430 |
-
"top_5": top_5_classes
|
431 |
-
}
|
432 |
-
print(result)
|
433 |
-
results.append(result)
|
434 |
-
|
435 |
-
# Sauvegarder le résultat dans un fichier JSON
|
436 |
-
video_file_path = video # Remplacez par le chemin réel
|
437 |
-
# Déduire le répertoire et le nom du fichier sans extension
|
438 |
-
output_dir = video_file_path.parent # Répertoire de la vidéo
|
439 |
-
output_filename = video_file_path.stem # Nom sans extension de la vidéo
|
440 |
-
# Créer le chemin complet pour le fichier de sortie JSON
|
441 |
-
output_file_path = output_dir / f"{output_filename}.json"
|
442 |
-
# Sauvegarder le résultat dans un fichier JSON
|
443 |
-
with open(output_file_path, "w") as f:
|
444 |
-
json.dump(results, f)
|
445 |
-
print(output_file_path)
|
446 |
-
with open(output_file_path, "r") as file:
|
447 |
-
file_content = file.read()
|
448 |
-
print(file_content)
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
return results
|
453 |
-
"""
|
454 |
-
|
|
|
281 |
# Cette fonction va utiliser l'exécuteur pour éviter de bloquer le thread principal
|
282 |
loop = asyncio.get_event_loop()
|
283 |
result = await loop.run_in_executor(executor, predict_video, file_path)
|
|
|
|
|
284 |
|
285 |
|
286 |
def predict_video(video):
|
|
|
340 |
|
341 |
return results
|
342 |
|
|
|
|
|
|
|
343 |
def ComputeStatistics(df):
|
344 |
+
# Calculer les statistiques supplémentaires
|
345 |
goalScore1 = df['top1'].str.startswith("Goal_1").sum()
|
346 |
goalConceeded = df['top1'].str.startswith("Goal_2").sum()
|
347 |
totalShots1 = df['top1'].str.startswith("Shot_1").sum()
|
|
|
350 |
goal1_5 = df['top1'].str.startswith("Goal_1-5").sum()
|
351 |
save1 = (df['top1'] == "Block_2-1").sum() # Compter uniquement si top1 est exactement "Block_2-1"
|
352 |
|
353 |
+
# Statistiques supplémentaires
|
354 |
+
totalShots2 = df['top1'].str.startswith("Shot_2").sum()
|
355 |
+
totalGoal2 = df['top1'].str.startswith("Goal_2").sum()
|
356 |
+
totalGoal1 = df['top1'].str.startswith("Goal_1").sum()
|
357 |
+
totalBlock1 = (df['top1'] == "Block_1-1").sum() # Exact match pour "Block_1-1"
|
358 |
+
totalBlock2 = (df['top1'] == "Block_2-1").sum() # Exact match pour "Block_2-1"
|
359 |
+
|
360 |
+
# Calcul de la victoire
|
361 |
+
vistory = 1 if totalGoal1 > totalGoal2 else 2
|
362 |
+
|
363 |
+
# Calcul des taux de sauvegarde
|
364 |
+
saveRate1 = totalBlock1 / (totalBlock1 + totalGoal2) if (totalBlock1 + totalGoal2) > 0 else 0
|
365 |
+
saveRate2 = totalBlock2 / (totalBlock2 + totalGoal1) if (totalBlock2 + totalGoal1) > 0 else 0
|
366 |
+
|
367 |
# Calculer le temps du premier Goal_1
|
368 |
first_goal1_row = df[df['top1'].str.startswith("Goal_1")].iloc[0] if not df[df['top1'].str.startswith("Goal_1")].empty else None
|
369 |
timeFirstGoal1 = (1 / 30) * first_goal1_row['start_frame'] if first_goal1_row is not None else None
|
|
|
381 |
"goal1_5": goal1_5,
|
382 |
"save1": save1,
|
383 |
"timeFirstGoal1": timeFirstGoal1,
|
384 |
+
"convertionRate1": convertionRate1,
|
385 |
+
"totalShots2": totalShots2,
|
386 |
+
"totalGoal2": totalGoal2,
|
387 |
+
"totalGoal1": totalGoal1,
|
388 |
+
"totalBlock1": totalBlock1,
|
389 |
+
"totalBlock2": totalBlock2,
|
390 |
+
"vistory": vistory,
|
391 |
+
"saveRate1": saveRate1,
|
392 |
+
"saveRate2": saveRate2
|
393 |
}
|
394 |
|
395 |
# Convertir les valeurs non compatibles en types natifs avant la sérialisation
|
|
|
409 |
|
410 |
|
411 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|