Created
September 8, 2016 23:04
-
-
Save ShawnHymel/6321b12ef7bf8feb2ac4a818ed7116d4 to your computer and use it in GitHub Desktop.
EdiBot 2.0 Python Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| # Debugging options | |
| SHOW_IMAGE = True | |
| VERBOSE = True | |
| # HSV color thresholds for YELLOW | |
| THRESHOLD_LOW = (15, 215, 50); | |
| THRESHOLD_HIGH = (35, 255, 255); | |
| # Camera resolution | |
| CAMERA_WIDTH = 640 | |
| CAMERA_HEIGHT = 240 | |
| # Blob detector options | |
| detectorParams = cv2.SimpleBlobDetector_Params() | |
| detectorParams.minThreshold = 10 | |
| detectorParams.maxThreshold = 200 | |
| detectorParams.filterByArea = True | |
| detectorParams.minArea = 40 | |
| detectorParams.maxArea = 70000 | |
| detectorParams.filterByCircularity = False | |
| detectorParams.minCircularity = 0.1 | |
| detectorParams.filterByConvexity = False | |
| detectorParams.minConvexity = 0.5 | |
| detectorParams.filterByInertia = False | |
| detectorParams.minInertiaRatio = 0.5 | |
| detectorParams.filterByColor = False | |
| ###MAIN### | |
| # Initialize camera | |
| cam = cv2.VideoCapture(0) | |
| cam.set(3, CAMERA_WIDTH) | |
| cam.set(4, CAMERA_HEIGHT) | |
| # Create blob detector | |
| detector = cv2.SimpleBlobDetector(detectorParams) | |
| while True: | |
| # Get image from camera | |
| ret_val, img = cam.read() | |
| # Blur image to remove noise | |
| img = cv2.GaussianBlur(img, (3, 3), 0) | |
| # Convert image from BGR to HSV | |
| img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | |
| # Set pixels to white if in color range, others to black | |
| img = cv2.inRange(img, THRESHOLD_LOW, THRESHOLD_HIGH) | |
| # Dilate image to make white blobs larger | |
| img = cv2.dilate(img, None, iterations = 1) | |
| # Find the largest blob | |
| keypoints = detector.detect(255 - img) | |
| blob = None | |
| blobSize = 0 | |
| if keypoints: | |
| for k in keypoints: | |
| if k.size > blobSize: | |
| blob = k | |
| blobSize = k.size | |
| # Print blob location and size | |
| if VERBOSE: | |
| if blob != None: | |
| print "(" + str(blob.pt[0]) + "," + str(blob.pt[1]) + ") " + \ | |
| str(blob.size) | |
| # Show image window (if debugging) | |
| if SHOW_IMAGE: | |
| img = cv2.drawKeypoints(img, [blob]) | |
| cv2.imshow('my webcam', img) | |
| cv2.waitKey(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment