there is no annotation on out put images :: import os from ultralytics import YOLO from…
- User
there is no annotation on out put images :: import os from ultralytics import YOLO from pdf2image import convert_from_path from PIL import ImageDraw
Load the YOLO model
model = YOLO("D:/Questionbank/yolov10/models/yolov10x_best.pt")
Define PDF path and output path
pdf_path = r"D:\Questionbank\data\PAK-38-2018-19.pdf" output_folder = r"D:\Questionbank\data\output_images"
Create output directory if it doesn't exist
os.makedirs(output_folder, exist_ok=True)
Set the confidence threshold
confidence_threshold = 0.5 # Adjust this value as needed
Convert PDF pages to images
images = convert_from_path(pdf_path)
Process each page
for page_num, img in enumerate(images, start=1): # Convert the image to RGB img = img.convert("RGB")
code# Convert the PIL image to a format suitable for model inference results = model(img) # Print the results to inspect them print(f"Results for page {page_num}:") print(results) # This will give you detailed output of the detections # Draw bounding boxes for detected diagrams draw = ImageDraw.Draw(img) # Check if there are any detections if results[0].boxes is not None: # Check for detected boxes for box in results[0].boxes.xyxy: # Iterate through boxes x_min, y_min, x_max, y_max = map(int, box.tolist()) # Convert to int # Get the class label and confidence score cls = results[0].boxes.cls # Get class indices conf = results[0].boxes.conf # Get confidence scores for label, confidence in zip(cls, conf): # Iterate through class labels and confidence scores label = label.item() # Convert tensor to item confidence = confidence.item() # Convert tensor to item # Only annotate if the detected class is the diagram and confidence is above the threshold if label == 0 and confidence >= confidence_threshold: # Assuming '0' corresponds to 'diagram' draw.rectangle([x_min, y_min, x_max, y_max], outline="red", width=2) print(f"Diagram found on page {page_num} at [{x_min}, {y_min}, {x_max}, {y_max}] with confidence {confidence}") # Save the annotated page image annotated_image_path = f"{output_folder}/page_{page_num}_annotated.png" img.save(annotated_image_path) print(f"Annotated image saved at: {annotated_image_path}") - Assistant