Skip to content

Instantly share code, notes, and snippets.

@lachlanagnew
Last active August 10, 2018 02:39
Show Gist options
  • Save lachlanagnew/7828b5c26e821af7332f8c125fe51339 to your computer and use it in GitHub Desktop.
Save lachlanagnew/7828b5c26e821af7332f8c125fe51339 to your computer and use it in GitHub Desktop.
import numpy as np
import cv2
import os
import sys
import time
import picamera
import picamera.array
import argparse
def objectFound():
print("Found")
def filter(image):
lower_range = np.array([101,200,0])
upper_range = np.array([109,255,255])
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.GaussianBlur(hsv, (11, 11), 0)
mask = cv2.inRange(mask, lower_range, upper_range)
res = cv2.bitwise_and(hsv, hsv, mask=mask)
res = cv2.cvtColor(cv2.cvtColor(res, cv2.COLOR_HSV2BGR), cv2.COLOR_BGR2GRAY)
res = cv2.erode(res, None, iterations=3)
res = cv2.dilate(res, None, iterations=4)
newImage = image
_ , contours, _ = cv2.findContours(res, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(newImage, contours, -1, (255, 255, 0), 1)
for cnt in contours:
M = cv2.moments(cnt)
(x,y),radius = cv2.minEnclosingCircle(cnt)
if(radius > 10):
center = (int(x),int(y))
radius = int(radius)
cv2.circle(newImage,center,radius,(0,255,0),2)
objectFound()
return newImage
with picamera.PiCamera() as camera:
with picamera.array.PiRGBArray(camera) as output:
camera.resolution = (640, 480)
camera.framerate = 30
while(1):
camera.capture(output, 'bgr')
try:
img = output.array
output.truncate(0)
cv2.imshow('img',filter(img))
if 0xFF & cv2.waitKey(5) == 27:
break
except KeyboardInterrupt:
pass
print ('KB interrupted')
print ('Process Aborted!')
break
except Exception as e:
exc_type, exc_obj, tb = sys.exc_info()
lineno = tb.tb_lineno
print ('Error : ' + str(e) + " @ line " + str(lineno))
finally:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment