Download api.py from MiniAiLive/MiniAiLive-FaceLivenessDetection-WebAPI: direct link, hf CLI and curl.
- Browser
- Download file 1.26 kB
-
https://huggingface.co/spaces/MiniAiLive/MiniAiLive-FaceLivenessDetection-WebAPI/resolve/main/api.py
- Command line
-
hf download hf://spaces/MiniAiLive/MiniAiLive-FaceLivenessDetection-WebAPI/api.py
-
curl -L -o api.py https://huggingface.co/spaces/MiniAiLive/MiniAiLive-FaceLivenessDetection-WebAPI/resolve/main/api.py
1.26 kB
| import os | |
| import requests | |
| import cv2 | |
| import numpy as np | |
| API_URL = os.environ.get( | |
| "LIVENESS_API_URL", | |
| "http://127.0.0.1:8092/api/check_liveness" | |
| ) | |
| def _load_image(image): | |
| if image is None: | |
| return None | |
| if isinstance(image, str): | |
| img = cv2.imread(image) | |
| if img is None: | |
| return None | |
| return img | |
| return image | |
| def check_liveness(image) -> dict: | |
| img = _load_image(image) | |
| if img is None: | |
| return {"error": "No image provided"} | |
| success, img_encoded = cv2.imencode('.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, 95]) | |
| if not success: | |
| return {"error": "Failed to encode image"} | |
| try: | |
| response = requests.post( | |
| API_URL, | |
| files={"image": ("image.jpg", img_encoded.tobytes(), "image/jpeg")}, | |
| timeout=30 | |
| ) | |
| response.raise_for_status() | |
| return response.json() | |
| except requests.exceptions.ConnectionError: | |
| return {"error": "Cannot connect to liveness detection server. Make sure the SDK server is running."} | |
| except requests.exceptions.Timeout: | |
| return {"error": "Request timed out"} | |
| except requests.exceptions.RequestException as e: | |
| return {"error": f"API request failed: {str(e)}"} | |