From 958e0661397b1fb7a7e1ac5d24e36932dae916b6 Mon Sep 17 00:00:00 2001 From: DavidLCPyML Date: Thu, 23 Feb 2023 18:36:14 -0600 Subject: [PATCH 1/2] Added a test thing --- pcr/main.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pcr/main.py b/pcr/main.py index 4378824b..d916113a 100644 --- a/pcr/main.py +++ b/pcr/main.py @@ -11,6 +11,8 @@ from xarm.wrapper import XArmAPI from gizzmos import CustomGripper, PressureSensor +print('test commit to show something') + class RunJob(): SPEED = 50 ZERO_POS = [0, 0, 0, 0, 0, 0] From e83b425aa6a1a40fd9b80c050ad8c1c019e22350 Mon Sep 17 00:00:00 2001 From: DavidLCPyML Date: Mon, 6 Mar 2023 19:44:27 -0600 Subject: [PATCH 2/2] 'functionized' circle detection with TestTubeCV class, added comments - needs improvement in accuracy --- pcr/gizzmos.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++++- pcr/main.py | 9 +++--- 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/pcr/gizzmos.py b/pcr/gizzmos.py index 39f126b9..4c7bf5bb 100644 --- a/pcr/gizzmos.py +++ b/pcr/gizzmos.py @@ -46,4 +46,78 @@ def adjust_volume(self, volume: float) -> str: return None, None def check_connection(self) -> bool: - return True \ No newline at end of file + return True + + +import cv2 as cv +import numpy as np +class TestTubeCV: + def __init__(self): + self.videoCapture = cv.VideoCapture(0, cv.CAP_DSHOW) + self.prevCircle = None + self.dist = lambda x1, y1, x2, y2: (x1 - x2) ** 2 + (y1 - y2) ** 2 + self.debug = False + + def predict(self, frame): + """ + Interprets a single frame given to it by the video feed. + Preprocesses the image by first converting it to greyscale, performing a median blur, and then utilizing a Hough Transform to generate circles. + Of the given Hough circles, will filter out any circles that overlap with already predicted circles. + Will get x- and y- corrdinate of each predicted circle's center. + + DISCLAIMER: As of right now, Hough Transform, RANSAC, or some mixture of the two doesn't work the best. + + Args: frame (cv.image): the input image to interpret from and predict on. + Returns: pred (list(int, int)): a list of tuples consisting of the x- and y- coordinates of the predicted circles. + """ + pred = [] + + # blur image for canny detection (Hough Transform) + gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) + gray = cv.medianBlur(gray, 13) + + # docstring of HoughCircles: HoughCircles(image, method, dp, minDist[, circles[, param1[, param2[, minRadius[, maxRadius]]]]]) -> circles + circles = cv.HoughCircles(gray, cv.HOUGH_GRADIENT, dp=1.0, minDist=10, + param1 = 60, + param2 = 75, + minRadius = 1, maxRadius = 50) + if circles is not None: + circles = np.uint16(np.around(circles)) + chosen = None + for i in circles[0, :]: + if chosen is None: + chosen = i + if self.prevCircle is not None: + if self.dist(chosen[0], chosen[1], self.prevCircle[0], self.prevCircle[1]) <= self.dist(i[0], i[1], self.prevCircle[0], self.prevCircle[1]): + chosen = i + cv.circle(frame, (i[0], i[1]), 1, (0, 100, 100), 3) + cv.circle(frame, (i[0], i[1]), i[2], (255, 0 , 255), 3) + + self.prevCircle = chosen + pred.append((chosen[0], chosen[1])) + + + cv.imshow("circles", frame) + cv.imshow("grayscale", gray) + return pred + + def getCircles(self): + """ + Reads in a frame from a birds' eye view video feed, predicts circles, and prints the x- and y- coordinates of the center. + Args: None + Returns: None + """ + while True: + ret, frame = self.videoCapture.read() + if not ret: + break + + predicted = self.predict(frame) + if predicted != []: + print(predicted) + + if cv.waitKey(1) == ord('q'): + break + self.videoCapture.release() + cv.destroyAllWindows() + diff --git a/pcr/main.py b/pcr/main.py index d916113a..2af9953b 100644 --- a/pcr/main.py +++ b/pcr/main.py @@ -1,17 +1,18 @@ #!/usr/bin/env python3 import os import sys + +# Add xArm python modules to the PATH +sys.path.append(os.path.join(os.path.dirname(__file__), '..')) + import time import json import argparse from mypy_types import RequestDict import logging -from pcr.gizzmos import AtalantaModule from xarm.wrapper import XArmAPI -from gizzmos import CustomGripper, PressureSensor - -print('test commit to show something') +from pcr.gizzmos import CustomGripper, PressureSensor, AtalantaModule class RunJob(): SPEED = 50