[Python] Waste classifier using confidence rate python [closed]

Discussão em 'Python' iniciado por Stack, Julho 3, 2025.

  1. Stack

    Stack Membro Participativo

    from transformers import AutoProcessor, AutoModelForImageClassification
    from PIL import Image
    import torch
    import torch.nn.functional as F

    processor=AutoProcessor.from_pretrained("watersplash/waste-classification")
    model = AutoModelForImageClassification.from_pretrained("watersplash/waste-classification")

    WASTE_INFO = {
    "organic": {
    "harm": "Can release methane if not composted.",
    "handling": "Compost or use municipal organic waste bins."
    },
    "plastic": {
    "harm": "Takes centuries to degrade and pollutes oceans.",
    "handling": "Recycle based on plastic code or use proper disposal services."
    },
    "electronic": {
    "harm": "Contains toxic metals that contaminate soil and water.",
    "handling": "Take to e-waste recycling centers or collection drives."
    },
    }


    def classifyWaste(image_stream):
    image=Image.open(image_stream).convert("RGB")
    inputs=processor(images=image, return_tensors="pt")
    with torch.no_grad():
    outputs = model(**inputs)
    predicted_class=outputs.logits.argmax(-1).item()
    label = model.config.id2label[predicted_class].lower()
    return label

    def classifyClean(image_stream):
    image=Image.open(image_stream).convert("RGB")
    inputs=processor(images=image, return_tensors="pt")
    with torch.no_grad():
    outputs = model(**inputs)
    logits = outputs.logits
    probs = F.softmax(logits, dim=1)
    confidence = probs.max().item()
    return confidence

    def extract_confidence_with_moviepy(video_stream):
    # Save video to temporary file
    with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_file:
    temp_file.write(video_stream.read())
    temp_path = temp_file.name

    clip = VideoFileClip(temp_path)
    duration = int(clip.duration) # in seconds
    confidences = []

    for t in range(0, duration):
    frame = clip.get_frame(t) # returns a NumPy array
    img = Image.fromarray(frame) # Convert to PIL Image
    buffer = BytesIO()
    img.save(buffer, format="JPEG")
    buffer.seek(0)

    result = classifyClean(buffer)
    print(result)# Your function that returns confidence
    confidences.append(result)

    clip.close()
    os.remove(temp_path)

    if not confidences:
    return 0.0

    return sum(confidences) / len(confidences)


    I tried using ai model for this classification but it is not working The code you've shared demonstrates a machine learning pipeline for classifying types of waste (such as organic, plastic, and electronic) using a pretrained transformer-based image classification model hosted on Hugging Face (watersplash/waste-classification). This code uses the transformers library by Hugging Face, PIL for image handling, and PyTorch (torch) for model inference. Here's a breakdown of its functionality in textual paragraph form:

    Continue reading...

Compartilhe esta Página