1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Stepper motor and camera issues

Discussão em 'Python' iniciado por Stack, Setembro 5, 2025.

  1. Stack

    Stack Membro Participativo

    So I want to run the stepper motor a little but faster and always have the camera running. Basically, to have this system be more efficient. It runs for a few seconds, camera turns on then turns off, then the stepper rotates some more in this same cycle. If there is a way to always have the camera on and for the motor to spin faster that would be great.

    import os
    import time
    import cv2
    from gpiozero import OutputDevice
    from ultralytics import YOLO

    # --- Stepper Motor Setup ---
    STEP_PIN = 17
    DIR_PIN = 18
    step = OutputDevice(STEP_PIN)
    direction = OutputDevice(DIR_PIN)
    direction.on() # Set direction

    def step_motor(steps, delay=0.001):
    for _ in range(steps):
    step.on()
    time.sleep(delay)
    step.off()
    time.sleep(delay)

    # --- YOLO Model Setup ---
    model = YOLO("/home/alex/crackDetection/gen1/best.onnx", task="detect")

    # --- Image Capture Settings ---
    IMAGE_PATH = "/home/alex/crackDetection/latest.jpg"
    CAPTURE_CMD = f"rpicam-still -o {IMAGE_PATH} --width 2028 --height 1520 --timeout 1000"

    def capture_image():
    os.system(CAPTURE_CMD)

    def run_inference(image_path):
    img_gray = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    if img_gray is None:
    return None, None

    img_rgb = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2RGB)
    results = model(img_rgb, conf=0.18)
    boxes = results[0].boxes

    if boxes is not None and len(boxes) > 0:
    confs = boxes.conf.cpu().numpy()
    max_index = confs.argmax()
    confidence = confs[max_index]
    boxes.data = boxes.data[max_index].unsqueeze(0)
    return confidence, results[0]
    return None, None

    # --- Main Loop: Rotate + Capture + Analyze ---
    print(" Starting 8-position scan. Press Ctrl+C to stop.")

    best_conf = 0
    best_position = None
    best_result = None

    try:
    steps_per_increment = 100 # Adjust as needed
    for i in range(8):
    print(f"\n Position {i+1}/8")
    capture_image()
    confidence, result = run_inference(IMAGE_PATH)

    if confidence is not None:
    print(f" Confidence at position {i+1}: {confidence:.2f}")
    if confidence > best_conf:
    best_conf = confidence
    best_position = i + 1
    best_result = result
    else:
    print("✅ No crack detected.")

    step_motor(steps_per_increment)
    time.sleep(0.5)

    if best_result:
    print(f"\n⚠️ Highest confidence crack detected at position {best_position} with confidence {best_conf:.2f}")
    best_result.show()
    else:
    print("\n✅ No cracks detected in any position.")

    except KeyboardInterrupt:
    print(" Interrupted by user.")```

    Continue reading...

Compartilhe esta Página