-
Notifications
You must be signed in to change notification settings - Fork 0
/
gagcheinsick.py
91 lines (71 loc) · 2.87 KB
/
gagcheinsick.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import cv2
import numpy as np
# YOLOv4 모델과 가중치를 CUDA 지원으로 로드
net = cv2.dnn.readNet('yolov4.weights', 'yolov4.cfg')
# CUDA 백엔드를 활성화
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
# YOLO 클래스 이름을 로드
with open('../aicartest/coco.names', 'r') as f:
classes = f.read().strip().split('\n')
# Open a connection to the webcam (0 is usually the default camera)
cap = cv2.VideoCapture(0)
while True:
# Read a frame from the webcam
ret, frame = cap.read()
if not ret:
break
# Get frame dimensions
height, width = frame.shape[:2]
# Create a blob from the frame and set input
blob = cv2.dnn.blobFromImage(frame, 1/255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
# Get the output layer names
output_layer_names = net.getUnconnectedOutLayersNames()
# Run forward pass
detection_results = net.forward(output_layer_names)
# Initialize lists to store detected class IDs, confidences, and bounding boxes
class_ids = []
confidences = []
boxes = []
# Minimum confidence threshold for detections
conf_threshold = 0.5
# Non-maximum suppression threshold
nms_threshold = 0.4
# Loop through each detection result
for result in detection_results:
for detection in result:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > conf_threshold:
# Scale the bounding box coordinates back to the original frame
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# Calculate the top-left corner of the bounding box
x = int(center_x - w / 2)
y = int(center_y - h / 2)
class_ids.append(class_id)
confidences.append(float(confidence))
boxes.append([x, y, w, h])
# Apply non-maximum suppression to remove overlapping boxes
indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold)
# Draw the bounding boxes and labels on the frame
for i in indices:
# i = i[0]
box = boxes[i]
x, y, w, h = box
label = str(classes[class_ids[i]])
confidence = confidences[i]
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(frame, f'{label} {confidence:.2f}', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Display the result
cv2.imshow('Object Detection', frame)
# Exit the loop if 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the webcam and close all OpenCV windows
cap.release()
cv2.destroyAllWindows()