def find_hsv_range(): my_range = np.array([40, 40, 40]) camera = VisionEG.USBCamera(0, 100, 100) window = VisionEG.CameraWindow("Find_HSV_ranges", camera) window.open() def crop(frame, x, y, w, h): """ Crops the frame """ return frame[y:y + h, x:x + w] med = None while True: HSV_frame, frame = window.show_and_get_color_frame(cv.COLOR_BGR2HSV) if frame is None: break if window.last_key_pressed == 'r': box = cv.selectROI('Find_HSV_ranges', frame) cropped_frame = crop(HSV_frame, *box) med = np.median(cropped_frame, axis=(0, 1)).astype(int) if type(med) is not np.ndarray: med = np.array([med]) break if med is None: return "You haven't chosen any area" ranges = np.array([med - my_range, med + my_range]) for tmp_range in ranges: for i, num in enumerate(tmp_range): num = min(255, max(0, num)) tmp_range[i] = num print(ranges)
def distance_filter(contours, params, min_distance, max_distance): passed_contours = [] for contour in contours: tmp_distance_shape = VisionEG.Shape_for_distance(contour, params) if (tmp_distance_shape.distance > min_distance and tmp_distance_shape.distance < max_distance): passed_contours.append(tmp_distance_shape) return passed_contours
import sys sys.path.append('..') import VisionEG as veg import numpy as np import cv2 as cv def crop(frame, x, y, w, h): """ Crops the frame """ return frame[y:y + h, x:x + w] window = veg.FeedWindow("Find_HSV_ranges_from_image") my_range = np.array([40, 40, 40]) path = input("Enter the file name >>> ") frame = cv.imread(path) hsv_frame = cv.cvtColor(frame, cv.COLOR_BGR2HSV) box = cv.selectROI("Find_HSV_ranges_from_image", frame) cropped_frame = crop(hsv_frame, *box) med = np.median(cropped_frame, axis=(0, 1)).astype(int) if type(med) is not np.ndarray: med = np.array([med]) ranges = np.array([med - my_range, med + my_range]) for tmp_range in ranges: for i, num in enumerate(tmp_range): num = min(255, max(0, num)) tmp_range[i] = num
import VisionEG as veg def find_fov(bbox, dimension, z, camera_dimension): return [ np.arctan(dimension[i] / z) * camera_dimension[i] / bbox[i + 2] for i in range(2) ] print('place object in the middle of the frame and press r') width = float(input('Enter object width in meters >>> ')) height = float(input('Enter object height in meters >>> ')) z = float( input('Enter distance from object in the Z axis in meter units >>> ')) camera = veg.USBCamera(0, 100, 100) camera.set_width(640) camera.set_height(480) window = veg.CameraWindow('Find_fov', camera) window.open() while True: frame = window.show_and_get_frame() k = window.last_key_pressed if k == 'r': bbox = cv.selectROI('Find_fov', frame) fov = find_fov(bbox, (width, height), z, (camera.get_width(), camera.get_height())) break cv.destroyAllWindows() print(f'width fov: {fov[0]} \nheight fov: {fov[1]}')
def __init__(self, contour, params): self.area = VisionEG.get_shape_area(contour) self.contour = contour self.distance = self.calculate_distance_with_function(params)
def draw(self, frame_to_draw_on): return VisionEG.draw_rectangle(frame_to_draw_on, self.point1, self.point2)
def __init__(self, contour): self.contour = contour self.center = VisionEG.get_shape_center(contour) self.area = VisionEG.get_shape_area(contour)
def calculate_angle(self, camera) -> float: distance_from_center = VisionEG.distance_from_center__rect( self.point1, self.point2, camera.middle) angle = VisionEG.find_angle(distance_from_center, camera.fov, camera.image_total_pixels) return angle
def __init__(self, contour): self.point1, self.point2 = VisionEG.get_rectangle_value(contour) self.area = VisionEG.get_rect_area(self.point1, self.point2) self.center = VisionEG.middle_of_rect(self.point1, self.point2)
def draw(self, frame_to_draw_on): return VisionEG.draw_circle(frame_to_draw_on, self.center, self.radius)
def __init__(self, contour): self.center, self.radius = VisionEG.get_circle_value(contour) self.area = VisionEG.get_circle_area(self.radius)
def draw(self, frame_to_draw_on, color=(0, 255, 0), thick=1): frame_to_draw_on = VisionEG.deepcopy(frame_to_draw_on) cv.drawContours(frame_to_draw_on, [self.contour], 0, color, thick) return frame_to_draw_on
def calculate_angle(self, camera) -> float: distance_from_center_of_image = VisionEG.distance_from_center( self.center, camera.middle) angle = VisionEG.find_angle(distance_from_center_of_image, camera.fov, camera.image_total_pixels) return angle
def calculate_distance(self, object_height: float, camera_height: float, camera_vertical_angle: float, camera_vertical_fov: float, center_of_image: tuple) -> float: image_total_vertical_pixels = center_of_image[1] * 2 vertical_distance_from_center = VisionEG.vertical_distance_from_center(self.center, center_of_image) vertical_angle = VisionEG.find_vertical_angle(vertical_distance_from_center, camera_vertical_fov, image_total_vertical_pixels) distance = VisionEG.find_distance_new_function(object_height, camera_height, camera_vertical_angle, vertical_angle) return distance
import sys sys.path.append('..') import cv2 as cv import VisionEG as veg camera = veg.USBCamera(0, veg.Cameras.LIFECAM_3000.focal_length, veg.Cameras.LIFECAM_3000.fov) camera.set_width(640) camera.set_height(480) shape_window = veg.FeedWindow("shape") shape_window.open() distance_with_areas = [] while True: hsv_frame, frame = camera.get_colored_frame(cv.COLOR_BGR2HSV) if frame is None: exit() tresh = cv.inRange(hsv_frame, veg.HSV_ranges.LIFECAM_3000.Reflector.low, veg.HSV_ranges.LIFECAM_3000.Reflector.high) tresh = veg.median_blur(tresh, 7) shape_window.show_frame(tresh) contours = veg.find_contours(tresh) if len(contours) > 0: contour = max(contours, key=cv.contourArea) if contour is None: continue shape = veg.Shape(contour)
import cv2 as cv import VisionEG as veg from networktables import NetworkTables import os # NetworkTables initialization NetworkTables.initialize(server='10.71.12.2') table = NetworkTables.getTable('SmartDashboard') window = veg.FeedWindow("window") window.open() low_hsv_range = (table.getNumber("lowh", veg.HSV_ranges.LIFECAM_3000.Reflector.lowH), table.getNumber("lows", veg.HSV_ranges.LIFECAM_3000.Reflector.lowS), table.getNumber("lowv", veg.HSV_ranges.LIFECAM_3000.Reflector.lowV)) high_hsv_range = (table.getNumber("highh", veg.HSV_ranges.LIFECAM_3000.Reflector.highH), table.getNumber("highs", veg.HSV_ranges.LIFECAM_3000.Reflector.highS), table.getNumber("highv", veg.HSV_ranges.LIFECAM_3000.Reflector.highV)) def set_default(): table.putNumber("Distance", -1) table.putNumber("Angle", 360) table.putBoolean("SeePowerPort", False)