File size: 1,362 Bytes
3f43833
 
 
261a482
3f43833
 
 
 
 
 
 
 
 
 
261a482
3f43833
d1692c9
3f43833
d1692c9
3f43833
 
d1692c9
3f43833
d1692c9
3f43833
 
 
 
 
 
 
d1692c9
3f43833
 
261a482
3f43833
 
 
7e65dd3
3f43833
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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