Ejemplo n.º 1
0
from color_filtering_img import apply_color_filter
from pathlib import Path
import cv2
import numpy as np

from img_utils import DisplayUtils, GeneralUtils, ShapeDetector

shape_detector = ShapeDetector()
utils = GeneralUtils()
display_utils = DisplayUtils()


def apply_ops(frame):
    """DEPRECATED, used for testing appying various operations to the frame"""
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # cv2.imshow("gray before", gray)

    # blurred = cv2.bilateralFilter(gray, 25, 15, 75)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    # _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
    thresh = cv2.adaptiveThreshold(blurred, 255,
                                   cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                   cv2.THRESH_BINARY, 201, 0)
    # thresh = cv2.bitwise_or(thresh_norm, thresh_adapt)
    # cv2.imshow("blurred", blurred)
    # cv2.imshow("thresh", thresh)

    median = np.median(gray)
    sigma = 0.33
    lower_thresh = int(max(0, (1.0 - sigma) * median))
    upper_thresh = int(min(255, (1.0 + sigma) * median))
Ejemplo n.º 2
0
from pathlib import Path
import cv2
import numpy as np

from img_utils import DisplayUtils, GeneralUtils, ShapeDetector

shape_detector = ShapeDetector()
utils = GeneralUtils()
display_utils = DisplayUtils()


def apply_color_filter(img, lower=[5, 0, 60], upper=[20, 180, 255]):

    blurred = cv2.medianBlur(img, 5)
    hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)

    lower_brown = np.array(lower)
    upper_brown = np.array(upper)
    brown_mask = cv2.inRange(hsv, lower_brown, upper_brown)
    brown_res = cv2.bitwise_and(img, img, mask=brown_mask)

    brown_cleaned = cv2.morphologyEx(brown_res,
                                     cv2.MORPH_OPEN, (5, 5),
                                     iterations=1)

    brown_blurred = cv2.GaussianBlur(brown_cleaned, (3, 3), 0)

    ### Temp Displaying ###
    # grid = display_utils.create_img_grid(
    #     [
    #         [brown_res, brown_dilated, brown_blurred],
Ejemplo n.º 3
0
#     "E:/code/projects/frc-vision/datasets/target-dataset/vision-videos/orange-teal-target-720p.mp4"
# )
# video_path = Path(
#     "E:/code/projects/frc-vision/datasets/target-dataset/vision-videos/orange-teal-target-1080p.mp4"
# )
# video_path = Path(
#     "E:/code/projects/frc-vision/datasets/target-dataset/vision-videos/vision-video-trees-white-notape-lowres-0.mp4"
# )
video_path = Path(
    "E:/code/projects/frc-vision/datasets/target-dataset/vision-videos/distance-measuring/all_dists.mp4"
)

RESOLUTION_SCALE = 1
KNOWN_DEPTH_WEIGHT = 0.5

utils = GeneralUtils()
shape_detector = ShapeDetector()
display_utils = DisplayUtils()

depth_model = DepthPredModel()
depth_model.load_from_json(meta_path, pixel_to_dist_path)
depth_deque = Deque(maxlen=5)

# Parameters for lucas kanade optical flow
lk_params = dict(
    winSize=(27, 27),
    maxLevel=5,
    criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 1000, 1.01),
)

import numpy as np

from img_utils import DisplayUtils, GeneralUtils, ShapeDetector
import target_detection_img, color_filtering_img

# video_path = Path(
#     "E:/code/projects/frc-vision/datasets/target-dataset/vision-videos/vision-video-horizontal-robot-driving-720p-0.mp4"
# )
# video_path = Path(
#     "E:/code/projects/frc-vision/datasets/target-dataset/vision-videos/vision-video-vertical-robot-driving-1080p-0.mp4"
# )
video_path = Path(
    "E:/code/projects/frc-vision/datasets/target-dataset/vision-videos/vision-video-trees-white-notape-lowres-0.mp4"
)

utils = GeneralUtils()
shape_detector = ShapeDetector()
display_utils = DisplayUtils()


def get_hexagon_points(frame):
    # equalize color and brightness, 2000+ fps
    equalized = cv2.normalize(frame, frame, 0, 255, cv2.NORM_MINMAX)
    # filter by color, ~60fps
    filtered = color_filtering_img.apply_color_filter(equalized)
    # smooth to remove jagged edges, 200+ fps
    smoothed = utils.smoother_edges(filtered, (7, 7), (1, 1))
    # acutal data, ~18 fps
    hexagon = target_detection_img.get_target_corners(smoothed)
    return hexagon
Ejemplo n.º 5
0
from img_utils import DisplayUtils, GeneralUtils, ShapeDetector
import target_detection_img, color_filtering_img
from center_prediction import CenterPredModel

# TODO ideas
# find contours after each mask, then blacken everything under a certain area.
# crop into brown target after using contours to find a rectangle that is large on brown mask
# add threshold that reverts to previous known centriod if over it rather than predicting
# calulate depth
# try expanding roi on point_tracking then running goodFeaturesToTrack on expaned roi

# accesible at drive folder: https://drive.google.com/drive/folders/11khSnQNsxnt0JStAec8j-widmBLOzPsO?usp=sharing
# video name (can use others too): vision-video-trees-white-notape-lowres-0.mp4

utils = GeneralUtils()
shape_detector = ShapeDetector()
display_utils = DisplayUtils()

# keep last 150 frames or last 5 seconds at 30fps
centroids_deque = deque(maxlen=150)
NO_TARGET_THRESH = 15
frames_since_target = 0

CONSTANT_CENTROID_THRESH = 6

FRAMES_FOR_TRAIN = 3
MODEL_DEGREE = 1
center_pred_model = CenterPredModel(degree=MODEL_DEGREE)

frame_count = 0
Ejemplo n.º 6
0
from pathlib import Path

import cv2
import numpy as np

from img_utils import DisplayUtils, GeneralUtils, ShapeDetector
from depth_prediction import DepthPredModel

# zero for completely silent, 1 for just console logs, 2 for displaying frames
VERBOSE_LEVEL = 2
RESOLUTION_SCALE = 0.5
pixel_to_dist_path = Path().cwd() / "target_points.json"
meta_path = Path().cwd() / "meta.json"

utils = GeneralUtils()
shape_detector = ShapeDetector()
display_utils = DisplayUtils()

depth_model = DepthPredModel()
depth_model.load_from_json(meta_path, pixel_to_dist_path)

# Parameters for lucas kanade optical flow
lk_params = dict(
    winSize=(25, 25),
    maxLevel=2,
    criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03),
)


def get_hexagon_points(frame):
    cv2.normalize(frame, frame, 0, 255, cv2.NORM_MINMAX)