Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Yolo.py
- import cv2
- import numpy as np
- # Load YOLO model
- yolo_config = 'yolov3.cfg'
- yolo_weights = 'yolov3.weights'
- labels_path = 'coco.names'
- # Load class labels
- with open(labels_path, 'r') as f:
- classes = [line.strip() for line in f.readlines()]
- # Initialize YOLO model
- net = cv2.dnn.readNet(yolo_weights, yolo_config)
- # Set backend to GPU if available, otherwise use CPU
- net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
- net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
- # Load input image
- image_path = 'image.png'
- image = cv2.imread(image_path)
- if image is None:
- print("Error: Could not read image file. Check the file path.")
- exit()
- (h, w) = image.shape[:2]
- # Prepare input blob
- blob = cv2.dnn.blobFromImage(image, 1/255.0, (416, 416), swapRB=True, crop=False)
- net.setInput(blob)
- # Get YOLO output layers
- layer_names = net.getUnconnectedOutLayersNames()
- layer_outputs = net.forward(layer_names)
- # Initialize lists for detections
- boxes, confidences, class_ids = [], [], []
- for output in layer_outputs:
- for detection in output:
- scores = detection[5:]
- class_id = np.argmax(scores)
- confidence = scores[class_id]
- if confidence > 0.7: # Modify threshold here (e.g., d=0.7)
- box = detection[0:4] * np.array([w, h, w, h])
- (center_x, center_y, width, height) = box.astype('int')
- x = int(center_x - width / 2)
- y = int(center_y - height / 2)
- boxes.append([x, y, int(width), int(height)])
- confidences.append(float(confidence))
- class_ids.append(class_id)
- # Perform non-max suppression
- indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
- # Draw bounding boxes and labels
- for i in indices.flatten():
- x, y, w, h = boxes[i]
- label = f"{classes[class_ids[i]]}: {confidences[i]:.2f}"
- cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
- cv2.putText(image, label, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
- # Display results
- cv2.imshow("YOLO Object Detection", image)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- #ssd.py
- import cv2
- import numpy as np
- # Load SSD model
- ssd_prototxt = 'MobileNetSSD_deploy.prototxt'
- ssd_model = 'MobileNetSSD_deploy.caffemodel'
- labels_path = 'coco.names'
- # Load class labels
- with open(labels_path, 'r') as f:
- classes = [line.strip() for line in f.readlines()]
- # Initialize SSD model
- net = cv2.dnn.readNetFromCaffe(ssd_prototxt, ssd_model)
- # Load input image
- image_path = 'image4.png'
- image = cv2.imread(image_path)
- if image is None:
- print("Error: Could not read image file. Check the file path.")
- exit()
- (h, w) = image.shape[:2]
- # Prepare input blob
- blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 0.007843, (300, 300), 127.5)
- net.setInput(blob)
- # Forward pass to get detections
- detections = net.forward()
- # Post-process detections
- for i in range(detections.shape[2]):
- confidence = detections[0, 0, i, 2]
- if confidence > 0.7: # Modify threshold here (e.g., d=0.7)
- idx = int(detections[0, 0, i, 1])
- box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
- (startX, startY, endX, endY) = box.astype('int')
- label = f"{classes[idx]}: {confidence:.2f}"
- cv2.rectangle(image, (startX, startY), (endX, endY), (0, 255, 0), 2)
- cv2.putText(image, label, (startX, startY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
- # Display results
- cv2.imshow("SSD Object Detection", image)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement