Views
No views yet
1# Example Code: To Test model in Google Colab Just Copy Paste
2
3# Install necessary libraries
4!pip install ultralytics
5!pip install -q huggingface_hub
6
7from huggingface_hub import hf_hub_download
8from ultralytics import YOLO
9import cv2
10import numpy as np
11from PIL import Image
12import matplotlib.pyplot as plt
13
14# Step 1: Download the YOLO model weights from your Hugging Face repository
15weights_path = hf_hub_download(repo_id="krishnamishra8848/Nepal-Vehicle-License-Plate-Detection", filename="last.pt")
16
17# Step 2: Load the YOLO model
18model = YOLO(weights_path)
19
20# Step 3: Function to process and display results
21def detect_license_plate(image_path):
22 # Load and preprocess the image
23 image = Image.open(image_path).convert('RGB')
24 img = np.array(image)
25 img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
26
27 # Perform inference
28 results = model(img)
29
30 # Draw bounding boxes and confidence scores
31 for result in results:
32 if hasattr(result, 'boxes') and result.boxes is not None:
33 for box, conf in zip(result.boxes.xyxy, result.boxes.conf):
34 x1, y1, x2, y2 = map(int, box) # Convert to integers
35 cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2) # Green rectangle
36 label = f"Confidence: {conf:.2f}"
37 cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
38
39 # Display the image with bounding boxes
40 plt.figure(figsize=(10, 10))
41 plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
42 plt.axis("off")
43 plt.show()
44
45# Step 4: Upload an image and run inference
46from google.colab import files
47uploaded = files.upload() # Use Colab's file uploader
48
49for filename in uploaded.keys():
50 print(f"Processing {filename}...")
51 detect_license_plate(filename)
52
53