CoolFace
Apppublic

MalekCode03/EyesCare2

sourceHugging Faceccupdated 2y agoView on Hugging Face
0likes
app.py76 linesDownload Raw Back to root
1import gradio as gr2import torch3from ultralyticsplus import YOLO, render_result4 5 6# torch.hub.download_url_to_file(7#     'https://en.wikipedia.org/wiki/Human_eye#/media/File:Human_eye,_anterior_view.jpg', 'one.jpg')8# torch.hub.download_url_to_file(9#     'https://glaucoma.org/wp-content/uploads/2024/01/amazing-eye-close-up_900-585x540.jpg', 'two.jpg')10# torch.hub.download_url_to_file(11#     'https://www.nvisioncenters.com/wp-content/uploads//woman-blue-eye-close-up-352x235.jpg', 'three.jpg')12 13 14def yoloV8_func(image: gr.Image = None,15                image_size: int = 640,16                conf_threshold: float = 0.70,17                iou_threshold: float = 0.50):18    """This function performs YOLOv8 object detection on the given image.19 20    Args:21        image (gr.Image, optional): Input image to detect objects on. Defaults to None.22        image_size (int, optional): Desired image size for the model. Defaults to 640.23        conf_threshold (float, optional): Confidence threshold for object detection. Defaults to 0.7.24        iou_threshold (float, optional): Intersection over Union threshold for object detection. Defaults to 0.50.25    """26    # Load the YOLOv8 model from the 'best.pt' checkpoint27    #model_path = "EyesCareEyeDetectModel.pt"28    model = YOLO("yolov8n.pt")29 30    # Perform object detection on the input image using the YOLOv8 model31    results = model.predict(image,32                            conf=conf_threshold,33                            iou=iou_threshold,34                            imgsz=image_size)35 36    # Print the detected objects' information (class, coordinates, and probability)37    box = results[0].boxes38    print("Object type:", box.cls)39    print("Coordinates:", box.xyxy)40    print("Probability:", box.conf)41 42    # Render the output image with bounding boxes around detected objects43    render = render_result(model=model, image=image, result=results[0])44    return render45 46 47inputs = [48    gr.Image(type="filepath", label="Input Image"),49    gr.Slider(minimum=320, maximum=1280, value=640,50              step=32, label="Image Size"),51    gr.Slider(minimum=0.0, maximum=1.0, value=0.25,52              step=0.05, label="Confidence Threshold"),53    gr.Slider(minimum=0.0, maximum=1.0, value=0.45,54              step=0.05, label="IOU Threshold"),55]56 57outputs = gr.Image(type="filepath", label="Output Image")58 59title = "YOLOv9 101: Custom Object Detection on Eye"60 61examples = [['one.jpg', 640, 0.5, 0.7],62            ['two.jpg', 640, 0.5, 0.6],63            ['three.jpg', 640, 0.5, 0.8]]64 65yolo_app = gr.Interface(66    fn=yoloV8_func,67    inputs=inputs,68    outputs=outputs,69    title=title,70    examples=examples,71    cache_examples=True,72)73 74# Launch the Gradio interface in debug mode with queue enabled75yolo_app.launch(debug=True, enable_queue=True)76