Spaces:
Sleeping
Sleeping
File size: 12,454 Bytes
53c7c09 b0dcef4 31216a6 b0dcef4 f137487 867f506 b0dcef4 867f506 b0dcef4 53c7c09 b0dcef4 53c7c09 b0dcef4 53c7c09 b0dcef4 53c7c09 b0dcef4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# from fastapi import FastAPI, UploadFile, File
# import cv2
# import torch
# import pandas as pd
# from PIL import Image
# from transformers import AutoImageProcessor, AutoModelForImageClassification
# from tqdm import tqdm
# import json
# import shutil
# from fastapi.middleware.cors import CORSMiddleware
# from fastapi.responses import HTMLResponse
# app = FastAPI()
# # Add CORS middleware to allow requests from localhost:8080 (or any origin you specify)
# app.add_middleware(
# CORSMiddleware,
# # allow_origins=["http://localhost:8080"], # Replace with the URL of your Vue.js app
# allow_origins=["http://localhost:8080"], # Replace with the URL of your Vue.js app
# allow_credentials=True,
# allow_methods=["*"], # Allows all HTTP methods (GET, POST, etc.)
# allow_headers=["*"], # Allows all headers (such as Content-Type, Authorization, etc.)
# )
# # Charger le processor et le modèle fine-tuné depuis le chemin local
# local_model_path = r'./vit-finetuned-ucf101'
# processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")
# model = AutoModelForImageClassification.from_pretrained(local_model_path)
# # model = AutoModelForImageClassification.from_pretrained("2nzi/vit-finetuned-ucf101")
# model.eval()
# # Fonction pour classifier une image
# def classifier_image(image):
# image_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# inputs = processor(images=image_pil, return_tensors="pt")
# with torch.no_grad():
# outputs = model(**inputs)
# logits = outputs.logits
# predicted_class_idx = logits.argmax(-1).item()
# predicted_class = model.config.id2label[predicted_class_idx]
# return predicted_class
# # Fonction pour traiter la vidéo et identifier les séquences de "Surfing"
# def identifier_sequences_surfing(video_path, intervalle=0.5):
# cap = cv2.VideoCapture(video_path)
# frame_rate = cap.get(cv2.CAP_PROP_FPS)
# total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# frame_interval = int(frame_rate * intervalle)
# resultats = []
# sequences_surfing = []
# frame_index = 0
# in_surf_sequence = False
# start_timestamp = None
# with tqdm(total=total_frames, desc="Traitement des frames de la vidéo", unit="frame") as pbar:
# success, frame = cap.read()
# while success:
# if frame_index % frame_interval == 0:
# timestamp = round(frame_index / frame_rate, 2) # Maintain precision to the centisecond level
# classe = classifier_image(frame)
# resultats.append({"Timestamp": timestamp, "Classe": classe})
# if classe == "Surfing" and not in_surf_sequence:
# in_surf_sequence = True
# start_timestamp = timestamp
# elif classe != "Surfing" and in_surf_sequence:
# # Vérifier l'image suivante pour confirmer si c'était une erreur ponctuelle
# success_next, frame_next = cap.read()
# next_timestamp = round((frame_index + frame_interval) / frame_rate, 2)
# classe_next = None
# if success_next:
# classe_next = classifier_image(frame_next)
# resultats.append({"Timestamp": next_timestamp, "Classe": classe_next})
# # Si l'image suivante est "Surfing", on ignore l'erreur ponctuelle
# if classe_next == "Surfing":
# success = success_next
# frame = frame_next
# frame_index += frame_interval
# pbar.update(frame_interval)
# continue
# else:
# # Sinon, terminer la séquence "Surfing"
# in_surf_sequence = False
# end_timestamp = timestamp
# sequences_surfing.append((start_timestamp, end_timestamp))
# success, frame = cap.read()
# frame_index += 1
# pbar.update(1)
# # Si on est toujours dans une séquence "Surfing" à la fin de la vidéo
# if in_surf_sequence:
# sequences_surfing.append((start_timestamp, round(frame_index / frame_rate, 2)))
# cap.release()
# dataframe_sequences = pd.DataFrame(sequences_surfing, columns=["Début", "Fin"])
# return dataframe_sequences
# # Fonction pour convertir les séquences en format JSON
# def convertir_sequences_en_json(dataframe):
# events = []
# blocks = []
# for idx, row in dataframe.iterrows():
# block = {
# "id": f"Surfing{idx + 1}",
# "start": round(row["Début"], 2),
# "end": round(row["Fin"], 2)
# }
# blocks.append(block)
# event = {
# "event": "Surfing",
# "blocks": blocks
# }
# events.append(event)
# return events
# @app.post("/analyze_video/")
# async def analyze_video(file: UploadFile = File(...)):
# with open("uploaded_video.mp4", "wb") as buffer:
# shutil.copyfileobj(file.file, buffer)
# dataframe_sequences = identifier_sequences_surfing("uploaded_video.mp4", intervalle=1)
# json_result = convertir_sequences_en_json(dataframe_sequences)
# return json_result
# @app.get("/", response_class=HTMLResponse)
# async def index():
# return (
# """
# <html>
# <body>
# <h1>Hello world!</h1>
# <p>This `/` is the most simple and default endpoint.</p>
# <p>If you want to learn more, check out the documentation of the API at
# <a href='/docs'>/docs</a> or
# <a href='https://2nzi-video-sequence-labeling.hf.space/docs' target='_blank'>external docs</a>.
# </p>
# </body>
# </html>
# """
# )
# # Lancer l'application avec uvicorn (command line)
# # uvicorn main:app --reload
# # http://localhost:8000/docs#/
# # (.venv) PS C:\Users\antoi\Documents\Work_Learn\Labeling-Deploy\FastAPI> uvicorn main:app --host 0.0.0.0 --port 8000 --workers 1
from fastapi import FastAPI, UploadFile, File
import cv2
import torch
import pandas as pd
from PIL import Image
from transformers import AutoImageProcessor, AutoModelForImageClassification
from tqdm import tqdm
import json
import shutil
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
app = FastAPI()
# Add CORS middleware to allow requests from localhost:8080 (or any origin you specify)
app.add_middleware(
CORSMiddleware,
# allow_origins=["http://localhost:8080"], # Replace with the URL of your Vue.js app
allow_origins=["http://localhost:8080"], # Replace with the URL of your Vue.js app
allow_credentials=True,
allow_methods=["*"], # Allows all HTTP methods (GET, POST, etc.)
allow_headers=["*"], # Allows all headers (such as Content-Type, Authorization, etc.)
)
# Charger le processor et le modèle fine-tuné depuis le chemin local
local_model_path = r'./vit-finetuned-ucf101'
processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")
model = AutoModelForImageClassification.from_pretrained(local_model_path)
# model = AutoModelForImageClassification.from_pretrained("2nzi/vit-finetuned-ucf101")
model.eval()
# Fonction pour classifier une image
def classifier_image(image):
image_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
inputs = processor(images=image_pil, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predicted_class_idx = logits.argmax(-1).item()
predicted_class = model.config.id2label[predicted_class_idx]
return predicted_class
# Fonction pour traiter la vidéo et identifier les séquences de "Surfing"
def identifier_sequences_surfing(video_path, intervalle=0.5):
cap = cv2.VideoCapture(video_path)
frame_rate = cap.get(cv2.CAP_PROP_FPS)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frame_interval = int(frame_rate * intervalle)
resultats = []
sequences_surfing = []
frame_index = 0
in_surf_sequence = False
start_timestamp = None
with tqdm(total=total_frames, desc="Traitement des frames de la vidéo", unit="frame") as pbar:
success, frame = cap.read()
while success:
if frame_index % frame_interval == 0:
timestamp = round(frame_index / frame_rate, 2) # Maintain precision to the centisecond level
classe = classifier_image(frame)
resultats.append({"Timestamp": timestamp, "Classe": classe})
if classe == "Surfing" and not in_surf_sequence:
in_surf_sequence = True
start_timestamp = timestamp
elif classe != "Surfing" and in_surf_sequence:
# Vérifier l'image suivante pour confirmer si c'était une erreur ponctuelle
success_next, frame_next = cap.read()
next_timestamp = round((frame_index + frame_interval) / frame_rate, 2)
classe_next = None
if success_next:
classe_next = classifier_image(frame_next)
resultats.append({"Timestamp": next_timestamp, "Classe": classe_next})
# Si l'image suivante est "Surfing", on ignore l'erreur ponctuelle
if classe_next == "Surfing":
success = success_next
frame = frame_next
frame_index += frame_interval
pbar.update(frame_interval)
continue
else:
# Sinon, terminer la séquence "Surfing"
in_surf_sequence = False
end_timestamp = timestamp
sequences_surfing.append((start_timestamp, end_timestamp))
success, frame = cap.read()
frame_index += 1
pbar.update(1)
# Si on est toujours dans une séquence "Surfing" à la fin de la vidéo
if in_surf_sequence:
sequences_surfing.append((start_timestamp, round(frame_index / frame_rate, 2)))
cap.release()
dataframe_sequences = pd.DataFrame(sequences_surfing, columns=["Début", "Fin"])
return dataframe_sequences
# Fonction pour convertir les séquences en format JSON
def convertir_sequences_en_json(dataframe):
events = []
blocks = []
for idx, row in dataframe.iterrows():
block = {
"id": f"Surfing{idx + 1}",
"start": round(row["Début"], 2),
"end": round(row["Fin"], 2)
}
blocks.append(block)
event = {
"event": "Surfing",
"blocks": blocks
}
events.append(event)
return events
import os
import tempfile
@app.post("/analyze_video/")
async def analyze_video(file: UploadFile = File(...)):
# Utiliser tempfile pour créer un fichier temporaire
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4", dir="/tmp") as tmp:
shutil.copyfileobj(file.file, tmp)
tmp_path = tmp.name
# Analyser la vidéo
dataframe_sequences = identifier_sequences_surfing(tmp_path, intervalle=1)
json_result = convertir_sequences_en_json(dataframe_sequences)
# Supprimer le fichier temporaire après utilisation
os.remove(tmp_path)
return {"filename": file.filename, "result": json_result}
@app.get("/", response_class=HTMLResponse)
async def index():
return (
"""
<html>
<body>
<h1>Hello world!</h1>
<p>This `/` is the most simple and default endpoint.</p>
<p>If you want to learn more, check out the documentation of the API at
<a href='/docs'>/docs</a> or
<a href='https://2nzi-video-sequence-labeling.hf.space/docs' target='_blank'>external docs</a>.
</p>
</body>
</html>
"""
)
# Lancer l'application avec uvicorn (command line)
# uvicorn main:app --reload
# http://localhost:8000/docs#/
# (.venv) PS C:\Users\antoi\Documents\Work_Learn\Labeling-Deploy\FastAPI> uvicorn main:app --host 0.0.0.0 --port 8000 --workers 1 |