| import cv2
|
| import torch
|
| from ultralytics import YOLO
|
|
|
|
|
| device = "cuda" if torch.cuda.is_available() else "cpu"
|
| print(f"Çalıştırılan cihaz: {device}")
|
|
|
|
|
| model = YOLO('yolov8x.pt')
|
|
|
|
|
| cap = cv2.VideoCapture(0)
|
|
|
| if not cap.isOpened():
|
| print("Kamera açılamadı. Lütfen kameranın bağlı olduğundan emin olun.")
|
| exit()
|
|
|
| print("Anlık nesne tespiti başlatıldı. Kamerayı kapatmak için 'q' tuşuna basın.")
|
|
|
| try:
|
| while True:
|
| ret, frame = cap.read()
|
| if not ret:
|
| print("Kamera akışında hata oluştu.")
|
| break
|
|
|
|
|
| results = model.predict(frame, device=device, conf=0.5, show=False)
|
|
|
|
|
| detected_objects = []
|
| for result in results:
|
| for box in result.boxes:
|
| label = model.names[int(box.cls)]
|
| confidence = float(box.conf)
|
| detected_objects.append((label, confidence))
|
|
|
|
|
| x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| cv2.putText(frame, f"{label} {confidence:.2f}", (x1, y1 - 10),
|
| cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
|
|
| if detected_objects:
|
| print("Tespit edilen nesneler:")
|
| for obj in detected_objects:
|
| print(f"- Nesne: {obj[0]}, Skor: {obj[1]:.2f}")
|
| else:
|
| print("Nesne tespit edilmedi.")
|
|
|
|
|
| cv2.imshow("Next Vision", frame)
|
|
|
|
|
| if cv2.waitKey(1) & 0xFF == ord('q'):
|
| print("Kapatılıyor...")
|
| break
|
|
|
| except KeyboardInterrupt:
|
| print("Kullanıcı tarafından durduruldu.")
|
|
|
| finally:
|
|
|
| cap.release()
|
| cv2.destroyAllWindows()
|
| print("Program sonlandırıldı.")
|
|
|