Spaces:
Runtime error
Runtime error
File size: 4,975 Bytes
2acde75 ae60ab6 2acde75 ae60ab6 d3f5a3e ae60ab6 d3f5a3e ae60ab6 d3f5a3e ae60ab6 e14c37a ae60ab6 e14c37a ae60ab6 e14c37a ae60ab6 e14c37a ae60ab6 d3f5a3e ae60ab6 e14c37a ae60ab6 e14c37a d3f5a3e ae60ab6 d3f5a3e ae60ab6 d3f5a3e 2acde75 ae60ab6 d3f5a3e ae60ab6 7a3447b d3f5a3e ae60ab6 2acde75 d3f5a3e ae60ab6 d3f5a3e ae60ab6 e14c37a abd0699 |
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 |
import os
import gradio as gr
import requests
import json
from PIL import Image
import cv2
import numpy as np
# Your existing CSS remains unchanged
# I'll keep your helper functions (convert_fun, get_attributes, check_liveness) mostly the same
# but add better error handling
def get_attributes(frame):
if frame is None:
return None, "No image provided"
url = "https://recognito.p.rapidapi.com/api/analyze_face"
try:
# If frame is a numpy array from webcam, convert to bytes
if isinstance(frame, np.ndarray):
_, buffer = cv2.imencode('.jpg', frame)
files = {'image': ('image.jpg', buffer.tobytes(), 'image/jpeg')}
else:
files = {'image': open(frame, 'rb')}
headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
r = requests.post(url=url, files=files, headers=headers)
except Exception as e:
return None, f"Error processing image: {str(e)}"
# Rest of your get_attributes function remains the same
# ... (keeping your existing logic)
return face_crop, one_line_attribute
def check_liveness(frame):
if frame is None:
return None, "No image provided", -200
url = "https://recognito-faceliveness.p.rapidapi.com/api/check_liveness"
try:
if isinstance(frame, np.ndarray):
_, buffer = cv2.imencode('.jpg', frame)
files = {'image': ('image.jpg', buffer.tobytes(), 'image/jpeg')}
else:
files = {'image': open(frame, 'rb')}
headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
r = requests.post(url=url, files=files, headers=headers)
except Exception as e:
return None, f"Error processing image: {str(e)}", -200
# Rest of your check_liveness function remains the same
# ... (keeping your existing logic)
return face_crop, liveness_result, liveness_score
def analyze_face(frame):
if frame is None:
return [None, "No image provided", "Please provide an image or enable webcam"]
face_crop_1, liveness_result, liveness_score = check_liveness(frame)
face_crop_2, attribute = get_attributes(frame)
face_crop = face_crop_1 if face_crop_1 is not None else face_crop_2
return [face_crop, liveness_result, attribute]
# Modified interface with webcam support
with gr.Blocks(css=css) as demo:
gr.Markdown(
# Your existing header markdown
)
with gr.Tabs():
with gr.Tab("Face Recognition"):
# Keeping your existing Face Recognition tab unchanged
# ...
with gr.Tab("Face Liveness, Analysis"):
with gr.Row():
with gr.Column(scale=1):
# Modified input to include webcam
face_input = gr.Image(
label="Image (Upload or Webcam)",
sources=["upload", "webcam"],
type='filepath', # Will be None for webcam frames
elem_classes="example-image",
streaming=True # Enable continuous webcam streaming
)
gr.Examples(
['examples/att_1.jpg', 'examples/att_2.jpg', 'examples/att_3.jpg'],
inputs=face_input
)
with gr.Blocks():
with gr.Column(scale=1, elem_classes="block-background"):
analyze_face_button = gr.Button("Analyze Face", variant="primary", size="lg")
with gr.Row(elem_classes="face-row"):
face_output = gr.Image(
value="icons/face.jpg",
label="Face",
scale=0,
elem_classes="face-image"
)
liveness_result = gr.Markdown("")
attribute_result = gr.Markdown("")
# Updated event handler for both upload and webcam
analyze_face_button.click(
analyze_face,
inputs=face_input,
outputs=[face_output, liveness_result, attribute_result]
)
# Add streaming event for webcam
face_input.stream(
analyze_face,
inputs=face_input,
outputs=[face_output, liveness_result, attribute_result],
_js="""(image) => {
if (image && image.data) return image;
return null;
}"""
)
gr.HTML(
# Your existing footer HTML
)
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False) |