-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
insert selected parking spots into relations
- Loading branch information
1 parent
01f8354
commit 6100eb8
Showing
2 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import sys | ||
import cv2 | ||
import logging | ||
import psycopg2 | ||
import argparse | ||
|
||
from shapely.geometry import Polygon | ||
from utils.config import config | ||
from utils import events | ||
|
||
|
||
ROOT_DIR = "src" | ||
TABLES_SQL_FILE = os.path.join(ROOT_DIR, "tables.sql") | ||
CONFIG_INI_FILE = os.path.join(ROOT_DIR, "config.ini") | ||
|
||
|
||
def main(video_file, num, loc): | ||
# connect to database | ||
logging.info("Connecting to database") | ||
db_params = config(filename=CONFIG_INI_FILE, section="postgresql") | ||
conn = psycopg2.connect(**db_params) | ||
cur = conn.cursor() | ||
|
||
# Create necessary tables | ||
cur.execute("SAVEPOINT table_creation") | ||
try: | ||
logging.info("Instantiating relations") | ||
cur.execute(open(TABLES_SQL_FILE).read()) | ||
except psycopg2.errors.DuplicateObject: | ||
logging.info("Relations are already instantiated") | ||
cur.execute("ROLLBACK TO SAVEPOINT table_creation") | ||
else: | ||
cur.execute("RELEASE SAVEPOINT table_creation") | ||
|
||
# Fetch video metadata | ||
cap = cv2.VideoCapture(video_file) | ||
res_x = int(cap.get(3)) | ||
res_y = int(cap.get(4)) | ||
fps = int(cap.get(cv2.CAP_PROP_FPS)) | ||
|
||
# Populate entries for cameras table | ||
# If the chosen camera exists, then | ||
# jump to populating spots | ||
query = """INSERT INTO cameras VALUES | ||
(%s, %s, %s, %s, %s);""" | ||
|
||
cur.execute("SELECT id from cameras;") | ||
ids = [r[0] for r in cur.fetchall()] | ||
if num not in ids: | ||
cur.execute(query, (num, loc, res_x, res_y, fps)) | ||
else: | ||
logging.warning("Modifying existing camera") | ||
|
||
# Initialize parking lots that will be later analyzed | ||
query = """INSERT INTO spots VALUES | ||
(DEFAULT, %s, ST_SetSRID(%s::geometry, %s), false, false)""" | ||
logging.info("Select areas of interest") | ||
ret, frame = cap.read() | ||
coords = events.select_area(frame) | ||
|
||
# Populate entries for spots table | ||
for pts in coords: | ||
polygon = events.sort2cyclic(pts) | ||
spot = Polygon(polygon) | ||
cur.execute(query, (num, spot.wkb_hex, 4326)) | ||
|
||
# Commit changes and close connection | ||
logging.info("Committing changes and closing connection to database") | ||
conn.commit() | ||
cur.close() | ||
conn.close() | ||
|
||
|
||
def parse_arguments(argv): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("video_file", type=str, | ||
help="path/to/video.mp4") | ||
parser.add_argument("num", type=int, | ||
help="camera id that will be reading the video file") | ||
parser.add_argument("loc", type=str, | ||
help="description of camera's location") | ||
return vars(parser.parse_args()) | ||
|
||
|
||
if __name__ == "__main__": | ||
logging.basicConfig(level=logging.INFO) | ||
main(**parse_arguments(sys.argv[1:])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import cv2 | ||
import numpy as np | ||
|
||
|
||
# Constants | ||
IDX = 0 | ||
|
||
|
||
def fetch_points_callback(event, x, y, flags, param): | ||
global IDX | ||
|
||
if event == cv2.EVENT_LBUTTONDOWN: | ||
param[IDX].append((x, y)) | ||
|
||
elif event == cv2.EVENT_RBUTTONDOWN: | ||
IDX += 1 | ||
param[IDX] = [] | ||
|
||
|
||
def select_area(frame): | ||
# initialize data | ||
polygons = {IDX: []} | ||
window_name = "Select area" | ||
|
||
# set callback | ||
cv2.namedWindow(window_name) | ||
cv2.setMouseCallback( | ||
window_name, fetch_points_callback, param=polygons) | ||
|
||
# fetch points | ||
cv2.imshow(window_name, frame) | ||
cv2.waitKey(0) & 0xFF | ||
cv2.destroyWindow(window_name) | ||
|
||
# polygon must have at least three points | ||
coords = list(polygons.values()) | ||
for pts in coords: | ||
assert len(pts) > 2, "Polygon must have at least 3 points" | ||
|
||
return coords | ||
|
||
|
||
def sort2cyclic(pts): | ||
# TODO: add documentation; basic idea is to order | ||
# the polygons in a cyclic fashion so as to make | ||
# them closed polygons. | ||
corner_angles = [] | ||
pts = np.asarray(pts) | ||
centroid = np.mean(pts, axis=0) | ||
|
||
for x, y in pts: | ||
theta = np.arctan2(y - centroid[1], x - centroid[0]) | ||
corner_angles.append((x, y, theta)) | ||
|
||
corner_angles.sort(key=lambda k: k[2]) | ||
cyclic = [(x, y) for x, y, theta in corner_angles] | ||
# Polygon must be closed as well | ||
cyclic.append(cyclic[0]) | ||
|
||
return cyclic |