import cv2
# Load YOLO
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
# Load class labels
classes = []
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
# Load image
image = cv2.imread("image.jpg")
# Get image dimensions
height, width, _ = image.shape
# Preprocess image
blob = cv2.dnn.blobFromImage(image, 1/255.0, (416, 416), swapRB=True, crop=False)
# Set input to the network
net.setInput(blob)
# Perform forward pass and get output
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
outputs = net.forward(output_layers)
# Process detection results
for output in outputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# Calculate top-left corner coordinates
x = int(center_x - w / 2)
y = int(center_y - h / 2)
# Draw bounding box and label
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
label = f"{classes[class_id]}: {confidence:.2f}"
cv2.putText(image, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Display the result
cv2.imshow("Object Detection", image)
cv2.waitKey(0)
cv2.destroyAllWindows()