def __init__(self): #Zachary's house test # self.minH = 169; self.minS = 109; self.minV = 56 # self.maxH = 180; self.maxS = 235; self.maxV = 135 #Zachary's red trash can # self.minH = 169; self.minS = 105; self.minV = 41 # self.maxH = 179; self.maxS = 194; self.maxV = 76 #C4 Pink Goal self.minH = 152 self.minS = 120 self.minV = 85 self.maxH = 180 self.maxS = 255 self.maxV = 255 self.fps = 0 self.prev = 0 self.capture = ThreadedWebcam() # UnthreadedWebcam() self.capture.start() params = cv2.SimpleBlobDetector_Params() self.detector = cv2.SimpleBlobDetector_create(params) fs = cv2.FileStorage("params.yaml", cv2.FILE_STORAGE_READ) if not fs.isOpened(): print("No params") exit(1) self.detector.read(fs.root()) fs.release()
def __init__(self): # Initialize the threaded camera # You can run the unthreaded camera instead by changing the line below. # Look for any differences in frame rate and latency. self.camera = ThreadedWebcam() # UnthreadedWebcam() self.camera.start() # Initialize the SimpleBlobDetector params = cv.SimpleBlobDetector_Params() self.detector = cv.SimpleBlobDetector_create(params) # Attempt to open a SimpleBlobDetector parameters file if it exists, # Otherwise, one will be generated. # These values WILL need to be adjusted for accurate and fast blob detection. fs = cv.FileStorage("params.yaml", cv.FILE_STORAGE_READ); #yaml, xml, or json if fs.isOpened(): self.detector.read(fs.root()) else: print("WARNING: params file not found! Creating default file.") fs2 = cv.FileStorage("params.yaml", cv.FILE_STORAGE_WRITE) self.detector.write(fs2) fs2.release() fs.release() if self.SHOW_BLOBS: # Create windows cv.namedWindow(self.WINDOW)
def __init__(self): #C4 Pink Goal for CoMR self.minH = 152 self.minS = 120 self.minV = 85 self.maxH = 180 self.maxS = 255 self.maxV = 255 self.fps = 0 self.prev = 0 self.capture = ThreadedWebcam() # UnthreadedWebcam() self.capture.start() params = cv2.SimpleBlobDetector_Params() self.detector = cv2.SimpleBlobDetector_create(params) fs = cv2.FileStorage("params.yaml", cv2.FILE_STORAGE_READ) if not fs.isOpened(): print("No params") exit(1) self.detector.read(fs.root()) fs.release()
global maxS maxS = max(val, minS + 1) cv.setTrackbarPos("Max Sat", WINDOW1, maxS) def onMaxVTrackbar(val): global minV global maxV maxV = max(val, minV + 1) cv.setTrackbarPos("Max Val", WINDOW1, maxV) # Initialize the threaded camera # You can run the unthreaded camera instead by changing the line below. # Look for any differences in frame rate and latency. camera = ThreadedWebcam() # UnthreadedWebcam() #camera = UnthreadedWebcam() camera.start() # Initialize the SimpleBlobDetector params = cv.SimpleBlobDetector_Params() detector = cv.SimpleBlobDetector_create(params) # Attempt to open a SimpleBlobDetector parameters file if it exists, # Otherwise, one will be generated. # These values WILL need to be adjusted for accurate and fast blob detection. fs = cv.FileStorage("params.yaml", cv.FILE_STORAGE_READ) #yaml, xml, or json if fs.isOpened(): detector.read(fs.root()) else:
class Camera(): FPS_SMOOTHING = 0.9 SHOW_BLOBS = False # Window names WINDOW = "Adjustable Mask - Press Esc to quit" # Default HSV ranges # Note: the range for hue is 0-180, not 0-255 minH = 160; minS = 120; minV = 0; maxH = 180; maxS = 210; maxV = 255; def __init__(self): # Initialize the threaded camera # You can run the unthreaded camera instead by changing the line below. # Look for any differences in frame rate and latency. self.camera = ThreadedWebcam() # UnthreadedWebcam() self.camera.start() # Initialize the SimpleBlobDetector params = cv.SimpleBlobDetector_Params() self.detector = cv.SimpleBlobDetector_create(params) # Attempt to open a SimpleBlobDetector parameters file if it exists, # Otherwise, one will be generated. # These values WILL need to be adjusted for accurate and fast blob detection. fs = cv.FileStorage("params.yaml", cv.FILE_STORAGE_READ); #yaml, xml, or json if fs.isOpened(): self.detector.read(fs.root()) else: print("WARNING: params file not found! Creating default file.") fs2 = cv.FileStorage("params.yaml", cv.FILE_STORAGE_WRITE) self.detector.write(fs2) fs2.release() fs.release() if self.SHOW_BLOBS: # Create windows cv.namedWindow(self.WINDOW) def get_blobs(self): # Get a frame frame = self.camera.read() # Blob detection works better in the HSV color space # (than the RGB color space) so the frame is converted to HSV. frame_hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) # Create a mask using the given HSV range mask = cv.inRange(frame_hsv, (self.minH, self.minS, self.minV), (self.maxH, self.maxS, self.maxV)) # Run the SimpleBlobDetector on the mask. # The results are stored in a vector of 'KeyPoint' objects, # which describe the location and size of the blobs. keypoints = self.detector.detect(mask) if self.SHOW_BLOBS: # Display the frame cv.imshow(self.WINDOW, mask) return keypoints # Just stops the robot def stop(self): self.camera.stop() def ctrlC(self): print("Stopping camera") self.stop()
class Camera(object): def __init__(self): #C4 Pink Goal for CoMR self.minH = 152 self.minS = 120 self.minV = 85 self.maxH = 180 self.maxS = 255 self.maxV = 255 self.fps = 0 self.prev = 0 self.capture = ThreadedWebcam() # UnthreadedWebcam() self.capture.start() params = cv2.SimpleBlobDetector_Params() self.detector = cv2.SimpleBlobDetector_create(params) fs = cv2.FileStorage("params.yaml", cv2.FILE_STORAGE_READ) if not fs.isOpened(): print("No params") exit(1) self.detector.read(fs.root()) fs.release() def getBlobs(self): frame = self.capture.read() frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) mask = cv2.inRange(frame_hsv, (self.minH, self.minS, self.minV), (self.maxH, self.maxS, self.maxV)) keypoints = self.detector.detect(mask) frame_with_keypoints = cv2.drawKeypoints( frame, keypoints, None, color=(0, 255, 0), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) cv2.putText(frame_with_keypoints, "{} blobs".format(len(keypoints)), (5, 35), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0)) return keypoints def getBlobsColored(self, color): frame = self.capture.read() frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) mask = cv2.inRange(frame_hsv, (color['minH'], color['minS'], color['minV']), (color['maxH'], color['maxS'], color['maxV'])) keypoints = self.detector.detect(mask) frame_with_keypoints = cv2.drawKeypoints( frame, keypoints, None, color=(0, 255, 0), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) cv2.putText(frame_with_keypoints, "{} blobs".format(len(keypoints)), (5, 35), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0)) return keypoints def getBlobStats(self): stats = { 'leftmost': 640, 'rightmost': 0, 'highest': 0, 'lowest': 480, 'averageX': 0, 'averageY': 0, 'blobs': False, 'maxDiameter': 0, 'totalArea': 0 } keypoints = self.getBlobs('unknown') for key in keypoints: stats['blobs'] = True x = key.pt[0] y = key.pt[1] diameter = key.size if x < stats['leftmost']: stats['leftmost'] = x if x > stats['rightmost']: stats['rightmost'] = x if y < stats['lowest']: stats['lowest'] = y if y > stats['highest']: stats['highest'] = y if diameter > stats['maxDiameter']: stats['maxDiameter'] = diameter stats['averageX'] += x stats['averageY'] += y stats['totalArea'] += (diameter / 2) * (diameter / 2) * math.pi if len(keypoints) > 0: stats['averageX'] /= len(keypoints) stats['averageY'] /= len(keypoints) return stats def getBlobStatsColored(self, color): stats = { 'leftmost': 640, 'rightmost': 0, 'highest': 0, 'lowest': 480, 'averageX': 0, 'averageY': 0, 'blobs': False, 'maxDiameter': 0, 'totalArea': 0 } keypoints = self.getBlobsColored(color) for key in keypoints: stats['blobs'] = True x = key.pt[0] y = key.pt[1] diameter = key.size if x < stats['leftmost']: stats['leftmost'] = x if x > stats['rightmost']: stats['rightmost'] = x if y < stats['lowest']: stats['lowest'] = y if y > stats['highest']: stats['highest'] = y if diameter > stats['maxDiameter']: stats['maxDiameter'] = diameter stats['averageX'] += x stats['averageY'] += y stats['totalArea'] += (diameter / 2) * (diameter / 2) * math.pi if len(keypoints) > 0: stats['averageX'] /= len(keypoints) stats['averageY'] /= len(keypoints) return stats def getDifference(self): p1 = self.capture.read() sleep(10) p2 = self.capture.read() difference = abs(cv2.subtract(p1, p2)) b, g, r = cv2.split(difference) if cv2countNonZero(b) == 0 and cv2.countNonZero( g) == 0 and cv2.countNonZero(r) == 0: print("The images are exactly the same. Is something wrong?")
class Camera(object): def __init__(self): #Zachary's house test # self.minH = 169; self.minS = 109; self.minV = 56 # self.maxH = 180; self.maxS = 235; self.maxV = 135 #Zachary's red trash can # self.minH = 169; self.minS = 105; self.minV = 41 # self.maxH = 179; self.maxS = 194; self.maxV = 76 #C4 Pink Goal self.minH = 152 self.minS = 120 self.minV = 85 self.maxH = 180 self.maxS = 255 self.maxV = 255 self.fps = 0 self.prev = 0 self.capture = ThreadedWebcam() # UnthreadedWebcam() self.capture.start() params = cv2.SimpleBlobDetector_Params() self.detector = cv2.SimpleBlobDetector_create(params) fs = cv2.FileStorage("params.yaml", cv2.FILE_STORAGE_READ) if not fs.isOpened(): print("No params") exit(1) self.detector.read(fs.root()) fs.release() def getBlobs(self): frame = self.capture.read() frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) mask = cv2.inRange(frame_hsv, (self.minH, self.minS, self.minV), (self.maxH, self.maxS, self.maxV)) keypoints = self.detector.detect(mask) frame_with_keypoints = cv2.drawKeypoints( frame, keypoints, None, color=(0, 255, 0), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) cv2.putText(frame_with_keypoints, "{} blobs".format(len(keypoints)), (5, 35), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0)) return keypoints def getBlobsColored(self, color): frame = self.capture.read() frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) mask = cv2.inRange(frame_hsv, (color['minH'], color['minS'], color['minV']), (color['maxH'], color['maxS'], color['maxV'])) keypoints = self.detector.detect(mask) frame_with_keypoints = cv2.drawKeypoints( frame, keypoints, None, color=(0, 255, 0), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) cv2.putText(frame_with_keypoints, "{} blobs".format(len(keypoints)), (5, 35), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0)) return keypoints def getBlobStats(self): stats = { 'leftmost': 640, 'rightmost': 0, 'highest': 0, 'lowest': 480, 'averageX': 0, 'averageY': 0, 'blobs': False, 'maxDiameter': 0, 'totalArea': 0 } keypoints = self.getBlobs('unknown') for key in keypoints: stats['blobs'] = True x = key.pt[0] y = key.pt[1] diameter = key.size if x < stats['leftmost']: stats['leftmost'] = x if x > stats['rightmost']: stats['rightmost'] = x if y < stats['lowest']: stats['lowest'] = y if y > stats['highest']: stats['highest'] = y if diameter > stats['maxDiameter']: stats['maxDiameter'] = diameter stats['averageX'] += x stats['averageY'] += y stats['totalArea'] += (diameter / 2) * (diameter / 2) * math.pi if len(keypoints) > 0: stats['averageX'] /= len(keypoints) stats['averageY'] /= len(keypoints) return stats def getBlobStatsColored(self, color): stats = { 'leftmost': 640, 'rightmost': 0, 'highest': 0, 'lowest': 480, 'averageX': 0, 'averageY': 0, 'blobs': False, 'maxDiameter': 0, 'totalArea': 0 } keypoints = self.getBlobsColored(color) for key in keypoints: stats['blobs'] = True x = key.pt[0] y = key.pt[1] diameter = key.size if x < stats['leftmost']: stats['leftmost'] = x if x > stats['rightmost']: stats['rightmost'] = x if y < stats['lowest']: stats['lowest'] = y if y > stats['highest']: stats['highest'] = y if diameter > stats['maxDiameter']: stats['maxDiameter'] = diameter stats['averageX'] += x stats['averageY'] += y stats['totalArea'] += (diameter / 2) * (diameter / 2) * math.pi if len(keypoints) > 0: stats['averageX'] /= len(keypoints) stats['averageY'] /= len(keypoints) return stats
params.minThreshold = 127 params.maxThreshold = 128 params.minRepeatability = 0 params.minDistBetweenBlobs = 10 params.filterByColor = 1 params.blobColor = 255 params.filterByArea = 1 params.minArea = 5000 params.maxArea = 300000 params.filterByCircularity = 0 params.filterByInertia = 0 params.filterByConvexity = 0 detector = cv2.SimpleBlobDetector_create(params) # Create a VideoCapture object and read from input file # If the input is the camera, pass 0 instead of the video file name cap = ThreadedWebcam() cap.start() #PID control for rotation k = 1 minSpeed = -0.2 maxSpeed = 0.2 error = 0 u = 0 xposition = 0 desiredx = 320 #center of 640 pixel wide window print("Enter x coordinate of pink goal:") pinkx = float(input()) print("Enter y coordinate of pink goal:") pinky = float(input())
class Camera(): FPS_SMOOTHING = 0.9 SHOW_BLOBS = False # Window names WINDOW = "Adjustable Mask - Press Esc to quit" # Default HSV ranges # Note: the range for hue is 0-180, not 0-255 # green color_0 = {'minH': 36, 'minS': 60, 'minV': 125, 'maxH': 41, 'maxS': 255, 'maxV': 255} # orange color_1 = {'minH': 11, 'minS': 188, 'minV': 94, 'maxH': 22, 'maxS': 255, 'maxV': 255} #pink color_2 = {'minH': 162, 'minS': 103, 'minV': 165, 'maxH': 173, 'maxS': 237, 'maxV': 255} #blue color_3 = {'minH': 95, 'minS': 108, 'minV': 132, 'maxH': 118, 'maxS': 210, 'maxV': 212} def __init__(self): # Initialize the threaded camera # You can run the unthreaded camera instead by changing the line below. # Look for any differences in frame rate and latency. self.camera = ThreadedWebcam() # UnthreadedWebcam() self.camera.start() # Initialize the SimpleBlobDetector params = cv.SimpleBlobDetector_Params() self.detector = cv.SimpleBlobDetector_create(params) # Attempt to open a SimpleBlobDetector parameters file if it exists, # Otherwise, one will be generated. # These values WILL need to be adjusted for accurate and fast blob detection. fs = cv.FileStorage("params.yaml", cv.FILE_STORAGE_READ); #yaml, xml, or json if fs.isOpened(): self.detector.read(fs.root()) else: print("WARNING: params file not found! Creating default file.") fs2 = cv.FileStorage("params.yaml", cv.FILE_STORAGE_WRITE) self.detector.write(fs2) fs2.release() fs.release() #set white balance for color detection system('v4l2-ctl -c white_balance_auto_preset=0') system('v4l2-ctl -c red_balance=1450') system('v4l2-ctl -c blue_balance=2300') if self.SHOW_BLOBS: # Create windows cv.namedWindow(self.WINDOW) def get_blobs(self, id=0): color = {} if (id == 0): #green color = self.color_0 elif (id == 1): #orange color = self.color_1 elif (id == 2): #pink color = self.color_2 elif (id ==3): #blue color = self.color_3 # Get a frame frame = self.camera.read() # Blob detection works better in the HSV color space # (than the RGB color space) so the frame is converted to HSV. frame_hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) # Create a mask using the given HSV range mask = cv.inRange(frame_hsv, (color['minH'], color['minS'], color['minV']), (color['maxH'], color['maxS'], color['maxV'])) # Run the SimpleBlobDetector on the mask. # The results are stored in a vector of 'KeyPoint' objects, # which describe the location and size of the blobs. keypoints = self.detector.detect(mask) if self.SHOW_BLOBS: # Display the frame cv.imshow(self.WINDOW, mask) return keypoints # Just stops the robot def stop(self): self.camera.stop() def ctrlC(self): print("Stopping camera") self.stop() def color_get(self): # ["green","orange", "pink", "blue"] color_list= [None,None,None,None] for x in range(0,4): t_list = [] t_c = 0 f_c = 0 for y in range(0,12): blobs = self.get_blobs(x) if len(blobs) > 0: ## t_list.append(True) t_c = t_c + 1 # print("found {}".format(color[x])) else: f_c = f_c + 1 ## t_list.append(False) #do majority rule instead? if (t_c > f_c): color_list[x] = True else: color_list[x] = False ## if any(t_list): ## color_list[x] = True ## else: ## color_list[x] = False print(color_list) ## def only1(l): true_found = False index = -1 for v in color_list: if v: # a True was found! if true_found: # found too many True's return False else: # found the first True index = color_list.index(v) true_found = True # found zero or one True value if not any(color_list): return None else: return index
lSensor.start_ranging(VL53L0X.VL53L0X_GOOD_ACCURACY_MODE) # Connect the right sensor and start measurement GPIO.output(RSHDN, GPIO.HIGH) time.sleep(0.01) rSensor.start_ranging(VL53L0X.VL53L0X_GOOD_ACCURACY_MODE) # Connect the front sensor and start measurement GPIO.output(FSHDN, GPIO.HIGH) time.sleep(0.01) fSensor.start_ranging(VL53L0X.VL53L0X_GOOD_ACCURACY_MODE) # Initialize the threaded camera # You can run the unthreaded camera instead by changing the line below. # Look for any differences in frame rate and latency. camera = ThreadedWebcam() # UnthreadedWebcam() camera.start() # Initialize the SimpleBlobDetector params = cv.SimpleBlobDetector_Params() detector = cv.SimpleBlobDetector_create(params) # Attempt to open a SimpleBlobDetector parameters file if it exists, # Otherwise, one will be generated. # These values WILL need to be adjusted for accurate and fast blob detection. fs = cv.FileStorage("params.yaml", cv.FILE_STORAGE_READ) #yaml, xml, or json if fs.isOpened(): detector.read(fs.root()) else: print("WARNING: params file not found! Creating default file.")
from UnthreadedWebcam import UnthreadedWebcam FPS_SMOOTHING = 0.9 minH = 0 minS = 81 minV = 92 maxH = 180 maxS = 255 maxV = 255 fps = 0 prev = 0 # Uncomment below to use the slower webcam capture = ThreadedWebcam() # UnthreadedWebcam() capture.start() params = cv.SimpleBlobDetector_Params() detector = cv.SimpleBlobDetector_create(params) fs = cv.FileStorage("params.yaml", cv.FILE_STORAGE_READ) if not fs.isOpened(): print("No params") exit(1) detector.read(fs.root()) fs.release() def updateValues(): minH = 0
from UnthreadedWebcam import UnthreadedWebcam FPS_SMOOTHING = 0.9 minH = 0 minS = 80 minV = 40 maxH = 180 maxS = 255 maxV = 255 fps = 0 prev = 0 # Uncomment below to use the slower webcam capture = ThreadedWebcam() # UnthreadedWebcam() capture.start() params = cv.SimpleBlobDetector_Params() detector = cv.SimpleBlobDetector_create(params) fs = cv.FileStorage("params.yaml", cv.FILE_STORAGE_READ) if not fs.isOpened(): print("No params") exit(1) detector.read(fs.root()) fs.release() while True: now = time.time() fps = (fps * FPS_SMOOTHING + (1 / (now - prev)) * (1.0 - FPS_SMOOTHING)) prev = now