1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Connecting OBJECT DETECTION YOLOv8 with Automatic Solenoid Unlock/Lock Response

Discussão em 'Python' iniciado por Stack, Outubro 3, 2024 às 12:52.

  1. Stack

    Stack Membro Participativo

    everyone let me introduce my self i am 17 years old wanna learn for my project i wanna add a simple mechanism. for my project which is one the non-biodegradeable things are detected it will unlock the solenoid lock. btw im using pycharm and python as a computer language. let me hear your thoughts and discussion about my simple code. TY!

    I'm trying to code my project using a basic mechanism using a Solenoid Lock and unlock mechanism. Once the item is identified, the entire code works; I just want to add that when the object is found, it prints // Bottle.identified... This print will automatically open the solenoid after a 5-8 second wait. I'm trying to watch tutorials on YouTube, but I'd want to know if you have any opinions or great suggestions.

    import cv2
    import supervision as sv
    from ultralytics import YOLO
    import numpy as np

    model = YOLO('yolov8n.pt')

    bounding_box_annotator = sv.BoxAnnotator()
    label_annotator = sv.LabelAnnotator()

    cap = cv2.VideoCapture(0)

    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)

    print("Camera initialized and ready.")

    CONFIDENCE_THRESHOLD = 0.4

    object_class_ids = [39, 44, 42, 41]

    while True:
    ret, frame = cap.read()

    if not ret:
    print("Failed to capture frame. Exiting.")
    break

    results = model.predict(frame)

    detections = sv.Detections.from_ultralytics(results[0])

    confidence_mask = detections.confidence > CONFIDENCE_THRESHOLD
    class_mask = np.isin(detections.class_id, object_class_ids)
    filtered_mask = np.logical_and(confidence_mask, class_mask)
    filtered_detections = detections[filtered_mask]

    if len(filtered_detections) > 0:
    print(f"Filtered detections: {filtered_detections}")

    annotated_image = bounding_box_annotator.annotate(scene=frame, detections=filtered_detections)
    annotated_image = label_annotator.annotate(scene=annotated_image, detections=filtered_detections)
    else:

    annotated_image = frame

    cv2.imshow('Camera Feed with Annotations', annotated_image)

    if cv2.waitKey(1) & 0xFF == ord('q'):
    print("'q' pressed, closing...")
    break

    cap.release()
    cv2.destroyAllWindows()

    Continue reading...

Compartilhe esta Página