-
-
Save wryan94/826a047e41fffd55d5970cb0c99785e1 to your computer and use it in GitHub Desktop.
This file contains 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 time | |
# import from parent directory | |
import sys | |
import os.path | |
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))) | |
import DynamicObjectV2 | |
Obj = DynamicObjectV2.Class | |
import numpy as np | |
import cv2 | |
from picamera import PiCamera | |
import os | |
from picamera.array import PiRGBArray | |
def init(self): | |
# put your self.registerOutput here | |
self.registerOutput("shape", Obj("found", False, "type", "none")) | |
def run (self): | |
try: | |
# put your init and global variables here | |
width = 440 | |
height = 280 | |
Square = 0 | |
Circle = 0 | |
Triangle = 0 | |
detect = 0 | |
shape = " " | |
pShape = " " | |
Found = False | |
camera = PiCamera() | |
camera.resolution = (width, height) | |
camera.framerate = 32 | |
camera.hflip = True | |
rawCapture = PiRGBArray(camera, size=(width,height)) | |
while 1: | |
for image in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): | |
frame = image.array | |
size = 20 | |
x = (width/2)- size | |
y = (height/2) - size | |
w = (width/2) + size | |
h = (height/2) + size | |
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) #Converts image to greyscale | |
blur = cv2.GaussianBlur(gray,(5,5),0) | |
thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2) | |
image,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) # Identify shape sides | |
for cnt in contours: | |
approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True); # Counting sides of shapes | |
if len(approx)==4: | |
Square = Square + 1; | |
detect = detect+1; | |
elif len(approx) > 8: | |
Circle = Circle + 1; | |
detect = detect+1; | |
else: | |
pass; | |
rawCapture.truncate(0) | |
pShape=shape | |
if (Circle > Square): | |
Circle = 0 | |
Square = 0 | |
if shape != "Circle": | |
self.output("shape", Obj("found", False, "type", " ")) | |
self.output("shape", Obj("found", True, "type", "Circle")) | |
shape = "Circle" | |
elif (Square > Circle): | |
Square = 0 | |
Circle = 0 | |
Triangle = 0 | |
if shape != "Square": | |
self.output("shape", Obj("found", False, "type", " ")) | |
self.output("shape", Obj("found", True, "type", "Square")) | |
shape = "Square" | |
if detect == 30: | |
break; | |
except: | |
pass; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment