from transformers import AutoModelForObjectDetection, AutoImageProcessor import torch from PIL import Image def load_huggingface_model(): """ Load a pre-trained object detection model from Hugging Face. For example, we are using Facebook's DETR (Detection Transformer). """ # Load a Hugging Face pre-trained model for object detection model = AutoModelForObjectDetection.from_pretrained("facebook/detr-resnet-50") processor = AutoImageProcessor.from_pretrained("facebook/detr-resnet-50") return model, processor def detect_faults_from_huggingface(image_path): """ Detect faults in the given image using Hugging Face's model (DETR in this case). Args: - image_path (str): Path to the image file Returns: - results (list): Detected objects and their confidence scores. """ model, processor = load_huggingface_model() # Load image image = Image.open(image_path) # Preprocess the image inputs = processor(images=image, return_tensors="pt") # Run the model outputs = model(**inputs) # Post-process the output to get detections target_sizes = torch.tensor([image.size[::-1]]) # Reversing the image size (height, width) results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0] return results