|
| 1 | +import argparse |
| 2 | +import cv2 |
| 3 | +import numpy as np |
| 4 | +import pickle |
| 5 | +from pathlib import Path |
| 6 | +from shapely.geometry import Polygon as shapely_poly |
| 7 | +from mrcnn.model import MaskRCNN |
| 8 | +import mrcnn.utils |
| 9 | +import mrcnn.config |
| 10 | +import os |
| 11 | +import sys |
| 12 | +import time |
| 13 | + |
| 14 | + |
| 15 | +class Config(mrcnn.config.Config): |
| 16 | + NAME = "model_config" |
| 17 | + IMAGES_PER_GPU = 1 |
| 18 | + GPU_COUNT = 1 |
| 19 | + NUM_CLASSES = 81 |
| 20 | + |
| 21 | + |
| 22 | +def download_model_weights(model_path): |
| 23 | + if not os.path.exists(model_path): |
| 24 | + print("Downloading pre-trained weights...") |
| 25 | + mrcnn.utils.download_trained_weights(model_path) |
| 26 | + |
| 27 | + |
| 28 | +def load_parking_regions(regions_path): |
| 29 | + regions_file = Path(regions_path) |
| 30 | + if regions_file.exists(): |
| 31 | + with open(regions_file, 'rb') as f: |
| 32 | + parked_car_boxes = pickle.load(f) |
| 33 | + return parked_car_boxes |
| 34 | + else: |
| 35 | + print("Error: Could not find the regions file.") |
| 36 | + sys.exit(1) |
| 37 | + |
| 38 | + |
| 39 | +def get_car_boxes(boxes, class_ids): |
| 40 | + cars = [] |
| 41 | + for i, box in enumerate(boxes): |
| 42 | + if class_ids[i] in [3, 8, 6]: |
| 43 | + cars.append(box) |
| 44 | + return np.array(cars) |
| 45 | + |
| 46 | + |
| 47 | +def compute_overlaps(parked_car_boxes, car_boxes): |
| 48 | + new_car_boxes = [] |
| 49 | + for box in car_boxes: |
| 50 | + y1, x1, y2, x2 = box |
| 51 | + p1 = (x1, y1) |
| 52 | + p2 = (x2, y1) |
| 53 | + p3 = (x2, y2) |
| 54 | + p4 = (x1, y2) |
| 55 | + new_car_boxes.append([p1, p2, p3, p4]) |
| 56 | + |
| 57 | + overlaps = np.zeros((len(parked_car_boxes), len(new_car_boxes))) |
| 58 | + for i, park_area in enumerate(parked_car_boxes): |
| 59 | + for j, car_box in enumerate(new_car_boxes): |
| 60 | + polygon1_shape = shapely_poly(park_area) |
| 61 | + polygon2_shape = shapely_poly(car_box) |
| 62 | + |
| 63 | + polygon_intersection = polygon1_shape.intersection(polygon2_shape).area |
| 64 | + polygon_union = polygon1_shape.union(polygon2_shape).area |
| 65 | + iou = polygon_intersection / polygon_union |
| 66 | + overlaps[i][j] = iou |
| 67 | + |
| 68 | + return overlaps |
| 69 | + |
| 70 | + |
| 71 | +def draw_parking_area(frame, parking_area, color=(71, 27, 92), thickness=2): |
| 72 | + cv2.drawContours(frame, [np.array(parking_area)], contourIdx=-1, color=color, thickness=thickness) |
| 73 | + |
| 74 | + |
| 75 | +def draw_overlay(frame, overlay, alpha): |
| 76 | + cv2.addWeighted(overlay, alpha, frame, 1 - alpha, 0, frame) |
| 77 | + |
| 78 | + |
| 79 | +def process_video(video_path, regions_path, output_path): |
| 80 | + parked_car_boxes = load_parking_regions(regions_path) |
| 81 | + |
| 82 | + config = Config() |
| 83 | + model = MaskRCNN(mode="inference", model_dir=model_dir, config=config) |
| 84 | + model_path = os.path.join(model_dir, "mask_rcnn_coco.h5") |
| 85 | + download_model_weights(model_path) |
| 86 | + model.load_weights(model_path, by_name=True) |
| 87 | + |
| 88 | + alpha = 0.6 |
| 89 | + video_capture = cv2.VideoCapture(video_path) |
| 90 | + video_FourCC = cv2.VideoWriter_fourcc(*'XVID') |
| 91 | + video_fps = video_capture.get(cv2.CAP_PROP_FPS) |
| 92 | + video_size = (int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) |
| 93 | + out = cv2.VideoWriter(output_path, video_FourCC, video_fps, video_size) |
| 94 | + |
| 95 | + while video_capture.isOpened(): |
| 96 | + success, frame = video_capture.read() |
| 97 | + if not success: |
| 98 | + break |
| 99 | + |
| 100 | + start_time = time.time() |
| 101 | + rgb_image = frame[:, :, ::-1] |
| 102 | + results = model.detect([rgb_image], verbose=0) |
| 103 | + inference_time = time.time() - start_time |
| 104 | + |
| 105 | + cars = get_car_boxes(results[0]['rois'], results[0]['class_ids']) |
| 106 | + overlaps = compute_overlaps(parked_car_boxes, cars) |
| 107 | + |
| 108 | + overlay = frame.copy() |
| 109 | + |
| 110 | + for park_area, overlap_areas in zip(parked_car_boxes, overlaps): |
| 111 | + max_iou_overlap = np.max(overlap_areas) |
| 112 | + if max_iou_overlap < 0.15: |
| 113 | + draw_parking_area(overlay, park_area) |
| 114 | + |
| 115 | + draw_overlay(frame, overlay, alpha) |
| 116 | + cv2.putText(frame, f"Inference Time: {inference_time:.2f}s", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) |
| 117 | + |
| 118 | + cv2.imshow('Parking Space Detection', frame) |
| 119 | + out.write(frame) |
| 120 | + if cv2.waitKey(1) & 0xFF == ord('q'): |
| 121 | + break |
| 122 | + |
| 123 | + video_capture.release() |
| 124 | + out.release() |
| 125 | + cv2.destroyAllWindows() |
| 126 | + print("Output saved as", output_path) |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + parser = argparse.ArgumentParser() |
| 131 | + parser.add_argument('video_path', help="Video file") |
| 132 | + parser.add_argument('regions_path', help="Regions file") |
| 133 | + parser.add_argument('--output', '-o', help="Output file", default="output.avi") |
| 134 | + args = parser.parse_args() |
| 135 | + |
| 136 | + video_path = args.video_path |
| 137 | + regions_path = args.regions_path |
| 138 | + output_path = args.output |
| 139 | + |
| 140 | + process_video(video_path, regions_path, output_path) |
0 commit comments