Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auxiliary scripts version 1 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 167 additions & 0 deletions GRIP.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import cv2
import numpy
import math
from enum import Enum

class GripPipeline:
"""This is a generated class from GRIP.
To use the pipeline first create a Pipeline instance and set the sources,
next call the process method,
finally get and use the outputs.
"""

def __init__(self):
"""initializes all values to presets or None if need to be set
"""
self.__source0 = None
self.__resize_image_input = self.__source0
self.__resize_image_width = 480.0
self.__resize_image_height = 360.0
self.__resize_image_interpolation = cv2.INTER_CUBIC
self.resize_image_output = None

self.__hsv_threshold_input = self.resize_image_output
self.__hsv_threshold_hue = [55.22229698255782, 129.6694696198746]
self.__hsv_threshold_saturation = [0.0, 130.98122866894198]
self.__hsv_threshold_value = [206.38489208633092, 255.0]
self.hsv_threshold_output = None

self.__find_contours_input = self.hsv_threshold_output
self.__find_contours_external_only = False
self.find_contours_output = None

self.__filter_contours_contours = self.find_contours_output
self.__filter_contours_min_area = 300.0
self.__filter_contours_min_perimeter = 0.0
self.__filter_contours_min_width = 10.0
self.__filter_contours_max_width = 1000.0
self.__filter_contours_min_height = 20.0
self.__filter_contours_max_height = 1000.0
self.__filter_contours_solidity = [0, 100]
self.__filter_contours_max_vertices = 1000000.0
self.__filter_contours_min_vertices = 0.0
self.__filter_contours_min_ratio = 0.0
self.__filter_contours_max_ratio = 1000.0
self.filter_contours_output = None


def process(self, source0):
"""Runs the pipeline.
Sets outputs to new values.
Requires all sources to be set.
"""
#Step Resize_Image0:
self.__resize_image_input = source0
(self.resize_image_output ) = self.__resize_image(self.__resize_image_input, self.__resize_image_width, self.__resize_image_height, self.__resize_image_interpolation)

#Step HSV_Threshold0:
self.__hsv_threshold_input = self.resize_image_output
(self.hsv_threshold_output ) = self.__hsv_threshold(self.__hsv_threshold_input, self.__hsv_threshold_hue, self.__hsv_threshold_saturation, self.__hsv_threshold_value)

#Step Find_Contours0:
self.__find_contours_input = self.hsv_threshold_output
(self.find_contours_output ) = self.__find_contours(self.__find_contours_input, self.__find_contours_external_only)

#Step Filter_Contours0:
self.__filter_contours_contours = self.find_contours_output
(self.filter_contours_output ) = self.__filter_contours(self.__filter_contours_contours, self.__filter_contours_min_area, self.__filter_contours_min_perimeter, self.__filter_contours_min_width, self.__filter_contours_max_width, self.__filter_contours_min_height, self.__filter_contours_max_height, self.__filter_contours_solidity, self.__filter_contours_max_vertices, self.__filter_contours_min_vertices, self.__filter_contours_min_ratio, self.__filter_contours_max_ratio)

def set_source0(self, value):
"""Sets source0 to given value checking for correct type.
"""
assert isinstance(value, numpy.ndarray) , "Source must be of type numpy.ndarray"
self.__source0 = value



@staticmethod
def __resize_image(input, width, height, interpolation):
"""Scales and image to an exact size.
Args:
input: A numpy.ndarray.
Width: The desired width in pixels.
Height: The desired height in pixels.
interpolation: Opencv enum for the type fo interpolation.
Returns:
A numpy.ndarray of the new size.
"""
return cv2.resize(input, ((int)(width), (int)(height)), 0, 0, interpolation)

@staticmethod
def __hsv_threshold(input, hue, sat, val):
"""Segment an image based on hue, saturation, and value ranges.
Args:
input: A BGR numpy.ndarray.
hue: A list of two numbers the are the min and max hue.
sat: A list of two numbers the are the min and max saturation.
lum: A list of two numbers the are the min and max value.
Returns:
A black and white numpy.ndarray.
"""
out = cv2.cvtColor(input, cv2.COLOR_BGR2HSV)
return cv2.inRange(out, (hue[0], sat[0], val[0]), (hue[1], sat[1], val[1]))

@staticmethod
def __find_contours(input, external_only):
"""Sets the values of pixels in a binary image to their distance to the nearest black pixel.
Args:
input: A numpy.ndarray.
external_only: A boolean. If true only external contours are found.
Return:
A list of numpy.ndarray where each one represents a contour.
"""
if(external_only):
mode = cv2.RETR_EXTERNAL
else:
mode = cv2.RETR_LIST
method = cv2.CHAIN_APPROX_SIMPLE
im2, contours, hierarchy =cv2.findContours(input, mode=mode, method=method)
return contours

@staticmethod
def __filter_contours(input_contours, min_area, min_perimeter, min_width, max_width,
min_height, max_height, solidity, max_vertex_count, min_vertex_count,
min_ratio, max_ratio):
"""Filters out contours that do not meet certain criteria.
Args:
input_contours: Contours as a list of numpy.ndarray.
min_area: The minimum area of a contour that will be kept.
min_perimeter: The minimum perimeter of a contour that will be kept.
min_width: Minimum width of a contour.
max_width: MaxWidth maximum width.
min_height: Minimum height.
max_height: Maximimum height.
solidity: The minimum and maximum solidity of a contour.
min_vertex_count: Minimum vertex Count of the contours.
max_vertex_count: Maximum vertex Count.
min_ratio: Minimum ratio of width to height.
max_ratio: Maximum ratio of width to height.
Returns:
Contours as a list of numpy.ndarray.
"""
output = []
for contour in input_contours:
x,y,w,h = cv2.boundingRect(contour)
if (w < min_width or w > max_width):
continue
if (h < min_height or h > max_height):
continue
area = cv2.contourArea(contour)
if (area < min_area):
continue
if (cv2.arcLength(contour, True) < min_perimeter):
continue
hull = cv2.convexHull(contour)
solid = 100 * area / cv2.contourArea(hull)
if (solid < solidity[0] or solid > solidity[1]):
continue
if (len(contour) < min_vertex_count or len(contour) > max_vertex_count):
continue
ratio = (float)(w) / h
if (ratio < min_ratio or ratio > max_ratio):
continue
output.append(contour)
return output



47 changes: 47 additions & 0 deletions Mapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import codecs
import time


import sys
from networktables import NetworkTables

import logging
import tkinter as tk
from tkinter import messagebox
from tkinter import filedialog

root = tk.Tk()
messagebox.showinfo('Warning','请选择小地图创建的txt文件')

root.withdraw()
file_path = filedialog.askopenfilename()



logging.basicConfig(level=logging.DEBUG)

NetworkTables.initialize(server='roborio-5449-frc.local')
sd = NetworkTables.getTable('SmartDashboard')

X = []
Y = []
Z = []


while True:

a = sd.getNumber('X','N/A')
b = sd.getNumber('Y','N/A')
c = sd.getNumber('Heading','N/A')

X.append(a)
Y.append(b)
Z.append(c)


with codecs.open(file_path,'w','gbk') as f:

f.writelines('{0} {1} {2}'.format(X[-1],Y[-1],Z[-1]))



65 changes: 65 additions & 0 deletions usb_vision.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/python3

"""
Detects stronghold goals using a usb camera plugged into raspberry pi
"""
import cv2
import networktables
from networktables import NetworkTables
from rgb_test_demo import GripPipeline
from array import *
import numpy as np


def reflection_tape_detection(pipeline):

center_x_positions = ['N/A']
center_y_positions = ['N/A']
widths = ['N/A']
heights = ['N/A']

for contour in pipeline.filter_contours_output:
x, y, w, h = cv2.boundingRect(contour)
center_x_positions.append(int(x + w / 2))
center_y_positions.append(int(y + h / 2))
widths.append(int(w))
heights.append(int(y))

table = NetworkTables.getTable("SmartDashBoard")
table.putValue("centerX", center_x_positions)
table.putValue("centerY", center_y_positions)
table.putValue("width", widths)
table.putValue("height", heights)

return [center_x_positions,center_y_positions,widths,heights]


def main():
print('Initializing NetworkTables')
NetworkTables.initialize(server='10.54.49.2')

print('Creating video capture')
cap = cv2.VideoCapture(0)

print('Creating pipeline')
pipeline = GripPipeline()

print('Running pipeline')
while True:
have_frame, frame = cap.read()
if have_frame:
pipeline.process(frame)
a = reflection_tape_detection(pipeline)
print(a)
for i in range(1,len(a[0])):
cv2.rectangle(frame,(a[0][i],a[1][i]),(a[0][i]+a[2][i],a[1][i]+a[3][i]),(255,0,0))

cv2.imshow('frame',frame)
cv2.waitKey(1)


print('Capture closed')


if __name__ == '__main__':
main()