Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import torch
- from ultralytics import YOLO
- # Load model yang sudah ditraining
- model = YOLO("runs/detect/train_20250214_104914/weights/best.pt")
- # Buka webcam (0 = kamera utama, 1 jika menggunakan kamera eksternal)
- cap = cv2.VideoCapture(0)
- # Atur resolusi video (opsional, bisa disesuaikan)
- cap.set(3, 640) # Lebar
- cap.set(4, 480) # Tinggi
- while cap.isOpened():
- ret, frame = cap.read()
- if not ret:
- break
- # Konversi warna (YOLO menggunakan format RGB)
- rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
- # Gunakan model untuk mendeteksi wajah
- results = model(rgb_frame, conf=0.3) # Turunkan confidence jika perlu
- # Gambar hasil deteksi di frame
- for r in results:
- for box in r.boxes:
- x1, y1, x2, y2 = map(int, box.xyxy[0]) # Koordinat kotak deteksi
- confidence = float(box.conf[0]) # Confidence score
- # Gambar kotak deteksi di wajah
- cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
- cv2.putText(frame, f"Face {confidence:.2f}", (x1, y1 - 10),
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
- # Tampilkan hasil di layar
- cv2.imshow("YOLOv8 Face Detection", frame)
- # Tekan 'q' untuk keluar
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- # Tutup semua proses
- cap.release()
- cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment