[Python] Close button not enabled while rendering an image using OpenCV

Discussão em 'Python' iniciado por Stack, Fevereiro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I don't see the close button on a Mac machine, but this code works fine on a Windows machine while trying to render the image using OpenCV library on the imshow function.

    def detect_objects_in_image(image_path: str):
    frame = cv2.imread(image_path)
    if frame is None:
    typer.echo(f"Error: Unable to load image {image_path}.")
    return

    results = model(frame, imgsz=640, conf=0.5)

    for result in results:
    for box in result.boxes:
    x1, y1, x2, y2 = map(int, box.xyxy[0])
    class_id, confidence = int(box.cls[0]), float(box.conf[0])

    cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
    cv2.putText(frame, f"Class {class_id}: {confidence:.2f}", (x1, y1 - 10),
    cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)

    cv2.imshow("Image Analysis", frame)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    Continue reading...

Compartilhe esta Página