CoolFace
Apppublic

DSatishchandra/Solar_Panels

sourceHugging Faceapache-2.0updated 1y agoView on Hugging Face
0likes
object_detection.py41 linesDownload Raw Back to models
1from transformers import AutoModelForObjectDetection, AutoImageProcessor2import torch3from PIL import Image4 5def load_huggingface_model():6    """7    Load a pre-trained object detection model from Hugging Face.8    For example, we are using Facebook's DETR (Detection Transformer).9    """10    # Load a Hugging Face pre-trained model for object detection11    model = AutoModelForObjectDetection.from_pretrained("facebook/detr-resnet-50")12    processor = AutoImageProcessor.from_pretrained("facebook/detr-resnet-50")13    14    return model, processor15 16def detect_faults_from_huggingface(image_path):17    """18    Detect faults in the given image using Hugging Face's model (DETR in this case).19    Args:20    - image_path (str): Path to the image file21    22    Returns:23    - results (list): Detected objects and their confidence scores.24    """25    model, processor = load_huggingface_model()26    27    # Load image28    image = Image.open(image_path)29 30    # Preprocess the image31    inputs = processor(images=image, return_tensors="pt")32    33    # Run the model34    outputs = model(**inputs)35 36    # Post-process the output to get detections37    target_sizes = torch.tensor([image.size[::-1]])  # Reversing the image size (height, width)38    results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]39 40    return results41