Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,45 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
# Procesamiento simulado del frame, puedes reemplazarlo con tu modelo
|
| 7 |
-
# En este ejemplo, solo convertimos el frame a escala de grises
|
| 8 |
-
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 9 |
-
return gray_frame
|
| 10 |
-
|
| 11 |
-
# Funci贸n que procesa el video y aplica la detecci贸n de anomal铆as frame por frame
|
| 12 |
-
def process_video(video):
|
| 13 |
-
cap = cv2.VideoCapture(video)
|
| 14 |
-
frames = []
|
| 15 |
-
while cap.isOpened():
|
| 16 |
-
ret, frame = cap.read()
|
| 17 |
-
if not ret:
|
| 18 |
-
break
|
| 19 |
-
# Aplica el modelo de detecci贸n de anomal铆as
|
| 20 |
-
processed_frame = anomaly_detection(frame)
|
| 21 |
-
frames.append(processed_frame)
|
| 22 |
-
cap.release()
|
| 23 |
-
return frames # Devuelve la lista de frames procesados
|
| 24 |
-
|
| 25 |
-
# Configuraci贸n de la interfaz de Gradio para Hugging Face Spaces
|
| 26 |
-
iface = gr.Interface(
|
| 27 |
-
fn=process_video, # Procesa video completo en lugar de frame a frame en tiempo real
|
| 28 |
-
inputs=gr.Video(source="webcam", format="mp4"), # Entrada de video desde la c谩mara del usuario
|
| 29 |
-
outputs=gr.Video(), # Salida como un video procesado
|
| 30 |
)
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import hf_hub_download
|
| 2 |
+
from inference import YOLOv10
|
| 3 |
+
|
| 4 |
+
model_file = hf_hub_download(
|
| 5 |
+
repo_id="onnx-community/yolov10n", filename="onnx/model.onnx"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
)
|
| 7 |
|
| 8 |
+
model = YOLOv10(model_file)
|
| 9 |
+
|
| 10 |
+
def detection(image, conf_threshold=0.3):
|
| 11 |
+
image = cv2.resize(image, (model.input_width, model.input_height))
|
| 12 |
+
new_image = model.detect_objects(image, conf_threshold)
|
| 13 |
+
return new_image
|
| 14 |
+
|
| 15 |
+
import gradio as gr
|
| 16 |
+
from gradio_webrtc import WebRTC
|
| 17 |
+
|
| 18 |
+
css = """.my-group {max-width: 600px !important; max-height: 600px !important;}
|
| 19 |
+
.my-column {display: flex !important; justify-content: center !important; align-items: center !important;}"""
|
| 20 |
+
|
| 21 |
+
with gr.Blocks(css=css) as demo:
|
| 22 |
+
gr.HTML(
|
| 23 |
+
"""
|
| 24 |
+
<h1 style='text-align: center'>
|
| 25 |
+
YOLOv10 Webcam Stream (Powered by WebRTC 鈿★笍)
|
| 26 |
+
</h1>
|
| 27 |
+
"""
|
| 28 |
+
)
|
| 29 |
+
with gr.Column(elem_classes=["my-column"]):
|
| 30 |
+
with gr.Group(elem_classes=["my-group"]):
|
| 31 |
+
image = WebRTC(label="Stream", rtc_configuration=rtc_configuration)
|
| 32 |
+
conf_threshold = gr.Slider(
|
| 33 |
+
label="Confidence Threshold",
|
| 34 |
+
minimum=0.0,
|
| 35 |
+
maximum=1.0,
|
| 36 |
+
step=0.05,
|
| 37 |
+
value=0.30,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
image.stream(
|
| 41 |
+
fn=detection, inputs=[image, conf_threshold], outputs=[image], time_limit=10
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
demo.launch()
|