Create svision_client.py
Browse files- svision_client.py +118 -0
svision_client.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
|
| 3 |
+
|
| 4 |
+
from gradio_client import Client, handle_file
|
| 5 |
+
from typing import Any, Dict, List, Optional, Tuple, Union
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
# Connect to the remote Space
|
| 9 |
+
svision_client = Client("VeuReu/svision")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def extract_scenes(video_path: str, threshold: float = 30.0, offset_frames: int = 5, crop_ratio: float = 0.1):
|
| 13 |
+
"""
|
| 14 |
+
Call the /scenes_extraction endpoint of the remote Space VeuReu/svision.
|
| 15 |
+
|
| 16 |
+
Parameters
|
| 17 |
+
----------
|
| 18 |
+
video_path : str
|
| 19 |
+
Path to the input video file.
|
| 20 |
+
threshold : float, optional
|
| 21 |
+
Scene change detection threshold; higher values make detection less sensitive.
|
| 22 |
+
offset_frames : int, optional
|
| 23 |
+
Number of frames to include before and after a detected scene boundary.
|
| 24 |
+
crop_ratio : float, optional
|
| 25 |
+
Ratio for cropping borders before performing scene detection.
|
| 26 |
+
|
| 27 |
+
Returns
|
| 28 |
+
-------
|
| 29 |
+
Any
|
| 30 |
+
Response returned by the remote /scenes_extraction endpoint.
|
| 31 |
+
"""
|
| 32 |
+
result = svision_client.predict(
|
| 33 |
+
video_file={"video": handle_file(video_path)},
|
| 34 |
+
threshold=threshold,
|
| 35 |
+
offset_frames=offset_frames,
|
| 36 |
+
crop_ratio=crop_ratio,
|
| 37 |
+
api_name="/scenes_extraction"
|
| 38 |
+
)
|
| 39 |
+
return result
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def keyframes_every_second_extraction(video_path: str):
|
| 43 |
+
"""
|
| 44 |
+
Call the /keyframes_every_second_extraction endpoint of the remote Space VeuReu/svision.
|
| 45 |
+
|
| 46 |
+
Parameters
|
| 47 |
+
----------
|
| 48 |
+
video_path : str
|
| 49 |
+
Path to the input video file.
|
| 50 |
+
|
| 51 |
+
Returns
|
| 52 |
+
-------
|
| 53 |
+
Any
|
| 54 |
+
Response returned by the remote /keyframes_every_second_extraction endpoint.
|
| 55 |
+
"""
|
| 56 |
+
result = svision_client.predict(
|
| 57 |
+
video_path={"video": handle_file(video_path)},
|
| 58 |
+
api_name="/keyframes_every_second_extraction"
|
| 59 |
+
)
|
| 60 |
+
return result
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def add_ocr_and_faces(imagen_path: str, informacion_image: Dict[str, Any], face_col: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 64 |
+
"""
|
| 65 |
+
Call the /add_ocr_and_faces endpoint of the remote Space VeuReu/svision.
|
| 66 |
+
|
| 67 |
+
This function sends an image together with metadata and face collection data
|
| 68 |
+
to perform OCR, face detection, and annotation enhancement.
|
| 69 |
+
|
| 70 |
+
Parameters
|
| 71 |
+
----------
|
| 72 |
+
imagen_path : str
|
| 73 |
+
Path to the input image file.
|
| 74 |
+
informacion_image : Dict[str, Any]
|
| 75 |
+
Dictionary containing image-related metadata.
|
| 76 |
+
face_col : List[Dict[str, Any]]
|
| 77 |
+
List of dictionaries representing detected faces or face metadata.
|
| 78 |
+
|
| 79 |
+
Returns
|
| 80 |
+
-------
|
| 81 |
+
Dict[str, Any]
|
| 82 |
+
Processed output containing OCR results, face detection data, and annotations.
|
| 83 |
+
"""
|
| 84 |
+
print("Calling svision to add OCR and face detection...")
|
| 85 |
+
informacion_image_str = json.dumps(informacion_image)
|
| 86 |
+
face_col_str = json.dumps(face_col)
|
| 87 |
+
|
| 88 |
+
result = svision_client.predict(
|
| 89 |
+
image=handle_file(imagen_path),
|
| 90 |
+
informacion_image=informacion_image_str,
|
| 91 |
+
face_col=face_col_str,
|
| 92 |
+
api_name="/add_ocr_and_faces"
|
| 93 |
+
)
|
| 94 |
+
return result
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def extract_descripcion_escena(imagen_path: str) -> str:
|
| 98 |
+
"""
|
| 99 |
+
Call the /describe_images endpoint of the remote Space VeuReu/svision.
|
| 100 |
+
|
| 101 |
+
This function sends an image to receive a textual description of its visual content.
|
| 102 |
+
|
| 103 |
+
Parameters
|
| 104 |
+
----------
|
| 105 |
+
imagen_path : str
|
| 106 |
+
Path to the input image file.
|
| 107 |
+
|
| 108 |
+
Returns
|
| 109 |
+
-------
|
| 110 |
+
str
|
| 111 |
+
Description generated for the given image.
|
| 112 |
+
"""
|
| 113 |
+
print("Calling svision to describe the scene...")
|
| 114 |
+
result = svision_client.predict(
|
| 115 |
+
images=[{"image": handle_file(imagen_path)}],
|
| 116 |
+
api_name="/describe_images"
|
| 117 |
+
)
|
| 118 |
+
return result
|