import gradio as gr import cv2 import requests import os from ultralytics import YOLO file_urls = [ 'https://huggingface.co/spaces/iamsuman/ripe-and-unripe-tomatoes-detection/resolve/main/samples/riped_tomato_93.jpeg?download=true', 'https://huggingface.co/spaces/iamsuman/ripe-and-unripe-tomatoes-detection/resolve/main/samples/unriped_tomato_18.jpeg?download=true', 'https://huggingface.co/spaces/iamsuman/ripe-and-unripe-tomatoes-detection/resolve/main/samples/tomatoes.mp4?download=true', ] def download_file(url, save_name): url = url if not os.path.exists(save_name): file = requests.get(url) open(save_name, 'wb').write(file.content) for i, url in enumerate(file_urls): if 'mp4' in file_urls[i]: download_file( file_urls[i], f"video.mp4" ) else: download_file( file_urls[i], f"image_{i}.jpg" ) model = YOLO('best.pt') path = [['image_0.jpg'], ['image_1.jpg']] video_path = [['video.mp4']] def show_preds_image(image_path): image = cv2.imread(image_path) outputs = model.predict(source=image_path) results = outputs[0].cpu().numpy() # Print the detected objects' information (class, coordinates, and probability) box = results[0].boxes names = model.model.names boxes = results.boxes for box, conf, cls in zip(boxes.xyxy, boxes.conf, boxes.cls): x1, y1, x2, y2 = map(int, box) class_name = names[int(cls)] print(class_name, "class_name", class_name.lower() == 'ripe') if class_name.lower() == 'ripe': color = (0, 0, 255) # Red for ripe else: color = (0, 255, 0) # Green for unripe # Draw rectangle around object cv2.rectangle( image, (x1, y1), (x2, y2), color=color, thickness=2, lineType=cv2.LINE_AA ) # Display class label on top of rectangle label = f"{class_name.capitalize()}: {conf:.2f}" cv2.putText(image, label, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, # Use the same color as the rectangle 2, cv2.LINE_AA) # Convert image to RGB (Gradio expects RGB format) return cv2.cvtColor(image, cv2.COLOR_BGR2RGB) inputs_image = [ gr.components.Image(type="filepath", label="Input Image"), ] outputs_image = [ gr.components.Image(type="numpy", label="Output Image"), ] interface_image = gr.Interface( fn=show_preds_image, inputs=inputs_image, outputs=outputs_image, title="Ripe And Unripe Tomatoes Detection", examples=path, cache_examples=False, ) def show_preds_video(video_path): cap = cv2.VideoCapture(video_path) while(cap.isOpened()): ret, frame = cap.read() if ret: frame_copy = frame.copy() outputs = model.predict(source=frame) results = outputs[0].cpu().numpy() boxes = results.boxes confidences = boxes.conf classes = boxes.cls names = model.model.names for box, conf, cls in zip(boxes.xyxy, confidences, classes): x1, y1, x2, y2 = map(int, box) # Determine color based on class class_name = names[int(cls)] if class_name.lower() == 'ripe': color = (0, 0, 255) # Red for ripe else: color = (0, 255, 0) # Green for unripe # Draw rectangle around object cv2.rectangle( frame_copy, (x1, y1), (x2, y2), color=color, thickness=2, lineType=cv2.LINE_AA ) # Display class label on top of rectangle with capitalized class name label = f"{class_name.capitalize()}: {conf:.2f}" cv2.putText( frame_copy, label, (x1, y1 - 10), # Position slightly above the top of the rectangle cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, # Use the same color as the rectangle 1, cv2.LINE_AA ) # Convert frame to RGB (Gradio expects RGB format) yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB) else: break cap.release() inputs_video = [ gr.components.Video(label="Input Video"), ] outputs_video = [ gr.components.Image(type="numpy", label="Output Image"), ] interface_video = gr.Interface( fn=show_preds_video, inputs=inputs_video, outputs=outputs_video, title="Ripe And Unripe Tomatoes Detection", examples=video_path, cache_examples=False, ) gr.TabbedInterface( [interface_image, interface_video], tab_names=['Image inference', 'Video inference'] ).queue().launch()