When you try to close the window manually, opened through Python (I am using Visual Studio), the window closes for a split second before reopening. CLI allows the window to be closed manually but I would like to avoid using that so I can more extensively use YOLO later.
Please give me some leniency as I am fairly new to python and I know this code will be shoddy.
Initially, I tried to use a while loop with a counter before using cv2 to close everything:
from ultralytics import YOLO
import cv2
# run yolo predict window
model = YOLO("yolov8m.pt")
results = model.predict(source="0", show = True)
running = True
while running:
k = k + 1
print(k)
# wait for enough time to have passed for the app to open
if k == 30000:
cv2.destroyAllWindows()
I eventually figured out that this wouldn’t work as it would sequentially run the predict window and wouldn’t stop running it, meaning it wouldn’t close until it was closed (recursive much…)
So I’ve been trying to use the multiprocess module, which eventually ended me up here:
from ultralytics import YOLO
import cv2
import multiprocessing
import multiprocessing.process
running = True
def camera():
model = YOLO("yolov8m.pt")
results = model.predict(source="0", show=True)
def counter():
k = 1
while running:
k = k + 1
print(k)
if k == 40000:
cv2.destroyAllWindows()
break
if __name__ == '__main__':
counterProcess = multiprocessing.Process(target=counter)
cameraProcess = multiprocessing.Process(target=camera)
counterProcess.start()
cameraProcess.start()
counterProcess.join()
cameraProcess.join()
But cv2.destroyAllWindows()
doesn’t close the camera window. Why?
Is there a different close function to use or should this be working?
Jack Measham is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.