Spaces:
Sleeping
Sleeping
| ''' | |
| Original Source: https://huggingface.co/spaces/atalaydenknalbant/Yolo11 | |
| Modified and Changed a bit | |
| ''' | |
| import gradio as gr | |
| from PIL import Image, ImageDraw, ImageFont | |
| from ultralytics import YOLO | |
| import cv2 | |
| import numpy as np | |
| import tempfile | |
| class_names = {0: 'group', 1: 'bird'} | |
| def yolo_inference(input_type, image, video, conf_threshold, iou_threshold, max_detection): | |
| model_id = "best.pt" | |
| model = YOLO(model_id) | |
| if input_type == "Image": | |
| if image is None: | |
| width, height = 640, 480 | |
| blank_image = Image.new("RGB", (width, height), color="white") | |
| draw = ImageDraw.Draw(blank_image) | |
| message = "No image provided" | |
| font = ImageFont.load_default() | |
| bbox = draw.textbbox((0, 0), message, font=font) | |
| text_width = bbox[2] - bbox[0] | |
| text_height = bbox[3] - bbox[1] | |
| text_x = (width - text_width) / 2 | |
| text_y = (height - text_height) / 2 | |
| draw.text((text_x, text_y), message, fill="black", font=font) | |
| return blank_image, None, "" | |
| results = model.predict( | |
| source=image, | |
| conf=conf_threshold, | |
| iou=iou_threshold, | |
| imgsz=640, | |
| max_det=max_detection, | |
| show_labels=True, | |
| show_conf=True, | |
| ) | |
| for r in results: | |
| image_array = r.plot() | |
| annotated_image = Image.fromarray(image_array[..., ::-1]) | |
| confidences = r.boxes.conf.cpu().numpy().tolist() | |
| class_ids = r.boxes.cls.cpu().numpy().tolist() | |
| detection_data = { | |
| class_names.get(int(cls), f"class_{int(cls)}"): f"{conf:.2f}" | |
| for cls, conf in zip(class_ids, confidences) | |
| } | |
| if not detection_data: | |
| detection_data = "" | |
| return annotated_image, None, detection_data | |
| elif input_type == "Video": | |
| if video is None: | |
| width, height = 640, 480 | |
| blank_image = Image.new("RGB", (width, height), color="white") | |
| draw = ImageDraw.Draw(blank_image) | |
| message = "No video provided" | |
| font = ImageFont.load_default() | |
| bbox = draw.textbbox((0, 0), message, font=font) | |
| text_width = bbox[2] - bbox[0] | |
| text_height = bbox[3] - bbox[1] | |
| text_x = (width - text_width) / 2 | |
| text_y = (height - text_height) / 2 | |
| draw.text((text_x, text_y), message, fill="black", font=font) | |
| temp_video_file = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name | |
| fourcc = cv2.VideoWriter_fourcc(*"mp4v") | |
| out = cv2.VideoWriter(temp_video_file, fourcc, 1, (width, height)) | |
| frame = cv2.cvtColor(np.array(blank_image), cv2.COLOR_RGB2BGR) | |
| out.write(frame) | |
| out.release() | |
| return None, temp_video_file, "" | |
| cap = cv2.VideoCapture(video) | |
| fps = cap.get(cv2.CAP_PROP_FPS) if cap.get(cv2.CAP_PROP_FPS) > 0 else 25 | |
| frames = [] | |
| while True: | |
| ret, frame = cap.read() | |
| if not ret: | |
| break | |
| pil_frame = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) | |
| results = model.predict( | |
| source=pil_frame, | |
| conf=conf_threshold, | |
| iou=iou_threshold, | |
| imgsz=640, | |
| max_det=max_detection, | |
| show_labels=True, | |
| show_conf=True, | |
| ) | |
| for r in results: | |
| annotated_frame_array = r.plot() | |
| annotated_frame = cv2.cvtColor(annotated_frame_array, cv2.COLOR_BGR2RGB) | |
| frames.append(annotated_frame) | |
| cap.release() | |
| if len(frames) == 0: | |
| return None, None, "" | |
| height_out, width_out, _ = frames[0].shape | |
| temp_video_file = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name | |
| fourcc = cv2.VideoWriter_fourcc(*"mp4v") | |
| out = cv2.VideoWriter(temp_video_file, fourcc, fps, (width_out, height_out)) | |
| for f in frames: | |
| f_bgr = cv2.cvtColor(f, cv2.COLOR_RGB2BGR) | |
| out.write(f_bgr) | |
| out.release() | |
| return None, temp_video_file, "" | |
| else: | |
| return None, None,"" | |
| def update_visibility(input_type): | |
| if input_type == "Image": | |
| return gr.update(visible=True), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=True) | |
| else: | |
| return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False) | |
| def yolo_inference_for_examples(image, conf_threshold, iou_threshold, max_detection): | |
| annotated_image, _, detection_data = yolo_inference( | |
| input_type="Image", | |
| image=image, | |
| video=None, | |
| conf_threshold=conf_threshold, | |
| iou_threshold=iou_threshold, | |
| max_detection=max_detection | |
| ) | |
| return gr.update(value="Image"), annotated_image, detection_data | |
| def clear_fields(): | |
| return ( | |
| None, # image | |
| None, # video | |
| "Image", # input_type | |
| 0.25, # conf_threshold | |
| 0.45, # iou_threshold | |
| 300, # max_detection | |
| None, # output_image | |
| None, # output_video | |
| "" # detection_label (float, not string) | |
| ) | |
| with gr.Blocks() as app: | |
| gr.Markdown("# Yolo11: Bird Detections. Is there a bird or not ? ") | |
| gr.Markdown("Upload image(s) or video(s) for inference using YOLO11 model") | |
| with gr.Row(): | |
| with gr.Column(): | |
| image = gr.Image(type="pil", label="Image", visible=True) | |
| video = gr.Video(label="Video", visible=False) | |
| input_type = gr.Radio(choices=["Image", "Video"], value="Image", label="Input Type") | |
| conf_threshold = gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence Threshold") | |
| iou_threshold = gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU Threshold") | |
| max_detection = gr.Slider(minimum=1, maximum=300, step=1, value=300, label="Max Detection") | |
| with gr.Row(): | |
| infer_button = gr.Button("Detect Objects") | |
| clear_button = gr.Button("Clear") | |
| with gr.Column(): | |
| output_image = gr.Image(type="pil", label="Annotated Image", visible=True) | |
| output_video = gr.Video(label="Annotated Video", visible=False) | |
| detection_label = gr.Label(label="Detections", visible=True) | |
| input_type.change( | |
| fn=update_visibility, | |
| inputs=input_type, | |
| outputs=[image, video, output_image, output_video, detection_label], | |
| ) | |
| infer_button.click( | |
| fn=yolo_inference, | |
| inputs=[input_type, image, video, conf_threshold, iou_threshold, max_detection], | |
| outputs=[output_image, output_video, detection_label], | |
| ) | |
| clear_button.click( | |
| fn=clear_fields, | |
| inputs=[], | |
| outputs=[ | |
| image, video, input_type, | |
| conf_threshold, iou_threshold, max_detection, | |
| output_image, output_video, detection_label | |
| ], | |
| ) | |
| gr.Examples( | |
| examples=[ | |
| ["test1.jpg", 0.25, 0.45, 300], | |
| ["test2.jpg", 0.25, 0.45, 300], | |
| ["test3.jpg", 0.25, 0.45, 300], | |
| ["test4.jpg", 0.25, 0.45, 300], | |
| ["test5.jpg", 0.25, 0.45, 300], | |
| ], | |
| fn=yolo_inference_for_examples, | |
| inputs=[image, conf_threshold, iou_threshold, max_detection], | |
| outputs=[input_type, output_image, detection_label], | |
| label="Examples (Images)", | |
| ) | |
| if __name__ == '__main__': | |
| app.launch() | |