Rahul-Rawat/Log_count
0
1import gradio as gr2from PIL import Image3from ultralytics import YOLO4import tempfile5 6# Load your trained YOLO model7model = YOLO("best.pt")8 9def detect_objects(image):10 # Save the uploaded image to a temporary file11 with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file:12 temp_filename = temp_file.name13 image.save(temp_filename)14 15 # Perform inference on the image and get results16 results = model(temp_filename)17 18 # Extract the first (and only) result19 result = results[0]20 21 # Count the number of detected objects (bounding boxes)22 detected_objects_count = len(result.boxes)23 24 # Create a result image with detections25 result_image = result.show()26 27 return image, result_image, detected_objects_count28 29# Create the Gradio interface with updated API30interface = gr.Interface(31 fn=detect_objects,32 inputs=gr.Image(type="pil"),33 outputs=[34 gr.Image(type="pil", label="Uploaded Image"),35 gr.Image(type="pil", label="Result Image with Detections"),36 gr.Textbox(label="Total number of detected objects")37 ],38 title="Log Counting by RV",39 description="Upload an image to detect and count objects."40)41 42# Launch the Gradio app43if __name__ == "__main__":44 interface.launch(share=True)