import numpy as np
import cv2
import yaml
fn_yaml = r"C:\Users\hp\Desktop\Projects\CarParkMan\Khare_yml_01.yml"
# Load the parking data from the YAML file
with open(fn_yaml, 'r') as stream:
parking_data = yaml.safe_load(stream)
# Initialize lists for parking contours, bounding rectangles, and masks
parking_contours = []
parking_bounding_rects = []
parking_masks = []
# Process each parking space in the data
for park in parking_data:
points = np.array(park['points'])
rect = cv2.boundingRect(points)
points_shifted = points.copy()
points_shifted[:, 0] = points[:, 0] - rect[0]
points_shifted[:, 1] = points[:, 1] - rect[1]
parking_contours.append(points)
parking_bounding_rects.append(rect)
mask = cv2.drawContours(np.zeros((rect[3], rect[2]), dtype=np.uint8), [points_shifted], contourIdx=-1,
color=255, thickness=-1, lineType=cv2.LINE_8)
mask = mask == 255
parking_masks.append(mask)
# Determine the grid size based on the bounding rectangles
grid_cols = int(np.max([rect[0] + rect[2] for rect in parking_bounding_rects]))
grid_rows = int(np.max([rect[1] + rect[3] for rect in parking_bounding_rects]))
# Create an empty image to hold the grid view
grid_view = np.ones((grid_rows, grid_cols), dtype=np.uint8)
# grid_view = np.zeros((grid_rows, grid_cols), dtype=np.uint8)
# Add each parking space mask to the grid view
for mask, rect in zip(parking_masks, parking_bounding_rects):
x, y, w, h = rect
grid_view[y:y+h, x:x +
w] = np.bitwise_or(grid_view[y:y+h, x:x+w], mask.astype(np.uint8))
# Display the grid view
cv2.imshow('Parking Spaces', grid_view)
cv2.waitKey(0)
cv2.destroyAllWindows()
I'm working on a Python script using OpenCV to process parking data from a YAML file and display a grid view of the parking spaces. However, the grid view is showing up as a black image with no visible parking spaces.
The code I'm using is attached above.
I've already tried a few things to troubleshoot the issue, including:
Checking the YAML file to ensure the parking data is loaded correctly Verifying the parking contours and masks Checking the grid view to ensure the parking space masks are being added correctly However, I'm still not able to identify the issue. Why is the grid view is showing up as a black image?