| | import numpy as np |
| | import cv2 |
| | import torch |
| | import sys |
| | import pathlib |
| | temp = pathlib.PosixPath |
| | pathlib.PosixPath = pathlib.WindowsPath |
| |
|
| | yolov5_path = pathlib.PosixPath('yolov5') |
| | sys.path.append(str(yolov5_path)) |
| |
|
| | from models.experimental import attempt_load |
| | from utils.general import non_max_suppression, scale_boxes |
| |
|
| | model = attempt_load('best.pt') |
| | model.eval() |
| | pathlib.PosixPath = temp |
| |
|
| | cap = cv2.VideoCapture(0) |
| |
|
| | while cap.isOpened(): |
| | ret, frame = cap.read() |
| | if not ret: |
| | print("Failed to grab frame") |
| | break |
| |
|
| | |
| | img = cv2.resize(frame, (640, 640)) |
| | img = img[:, :, ::-1] |
| | img = np.ascontiguousarray(img) |
| | img = torch.from_numpy(img).float() |
| | img /= 255.0 |
| |
|
| | |
| | img = img.permute(2, 0, 1).unsqueeze(0) |
| |
|
| | |
| | with torch.no_grad(): |
| | pred = model(img)[0] |
| |
|
| | |
| | pred = non_max_suppression(pred, conf_thres=0.25, iou_thres=0.45) |
| |
|
| | |
| | for det in pred: |
| | if det is not None and len(det): |
| | |
| | det[:, :4] = scale_boxes(img.shape[2:], det[:, :4], frame.shape).round() |
| |
|
| | |
| | for *xyxy, conf, cls in reversed(det): |
| | label = f'{model.names[int(cls)]}: {conf:.2f}' |
| | cv2.rectangle(frame, (int(xyxy[0]), int(xyxy[1])), (int(xyxy[2]), int(xyxy[3])), (0, 255, 0), 2) |
| | cv2.putText(frame, label, (int(xyxy[0]), int(xyxy[1] - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) |
| |
|
| | |
| | cv2.imshow('Deteksi Cadar Masker', frame) |
| |
|
| | |
| | if cv2.waitKey(1) & 0xFF == ord('q'): |
| | break |
| |
|
| | |
| | cap.release() |
| | cv2.destroyAllWindows() |