import gradio as gr import cv2 import cvzone from ultralytics import YOLO import numpy as np # Load the model model = YOLO("yolo_trained(300).pt") classNames = ["fake", "real"] confidence_threshold = 0.6 def process_image(img): # Convert the image to the correct format img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) # Run detection results = model(img, size=640) for r in results: boxes = r.boxes for box in boxes: x1, y1, x2, y2 = [int(x) for x in box.xyxy[0]] conf = max(0, min(math.ceil(box.conf[0] * 100) / 100, 1)) # Normalize confidence cls = int(box.cls[0]) if conf > confidence_threshold: color = (0, 255, 0) if classNames[cls] == 'real' else (0, 0, 255) cvzone.cornerRect(img, (x1, y1, x2 - x1, y2 - y1), colorC=color, colorR=color) cvzone.putTextRect(img, f'{classNames[cls].upper()} {int(conf * 100)}%', (max(0, x1), max(35, y1)), scale=2, thickness=4, colorR=color, colorB=color) # Convert the image back to RGB img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) return img iface = gr.Interface(fn=process_image, inputs=gr.inputs.Image(type="pil", label="Upload an image or use your webcam"), outputs=gr.outputs.Image(type="pil", label="Processed Image"), title="Face Liveness Detection with YOLOv8", description="Upload an image or use your webcam to detect live faces. The model will identify if the face is real or fake.") if __name__ == "__main__": iface.launch()