Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- from picamera.array import PiRGBArray
- from picamera import PiCamera
- from smbus import SMBus
- import time
- import dropbox
- #Dropbox setup
- token = "" #add dropbox API token here
- DBX = dropbox.Dropbox(token)
- #I2C setup
- addr = 0x8
- bus = SMBus(1)
- #red RGB colour values
- lower_range1 = np.array([0,50,50])
- upper_range1 = np.array([7,255,255])
- lower_range2 = np.array([173,50,50])
- upper_range2 = np.array([180,255,255])
- #reference images
- ref1 = cv2.imread("Grab.png")
- ref2 = cv2.imread("Fire.png")
- #convert from RGB to HSV
- ref1 = cv2.cvtColor(ref1, cv2.COLOR_BGR2HSV)
- mask1 = cv2.inRange(ref1, lower_range1, upper_range1)
- mask2 = cv2.inRange(ref1, lower_range2, upper_range2)
- ref1 = mask1|mask2
- ref2 = cv2.cvtColor(ref2, cv2.COLOR_BGR2HSV)
- mask1 = cv2.inRange(ref2, lower_range1, upper_range1)
- mask2 = cv2.inRange(ref2, lower_range2, upper_range2)
- ref2 = mask1|mask2
- #setup camera
- camera = PiCamera()
- camera.rotation = 270
- camera.resolution = (640, 480)
- camera.framerate = 10
- rawCapture = PiRGBArray(camera, size=(640,480))
- def routeProcess():
- route = bus.read(address)
- #write route to text file
- f = open("maze.txt", "w")
- f.write(route)
- f.close()
- #upload text file to Dropbox
- DBX.files_upload(stream.read(), "/maze.txt", mode=dropbox.files.WriteMode.overwrite)
- while True:
- #latch
- for frame in camera.capture_continuous(rawCapture, format = "bgr", use_video_port = True):
- img = frame.array
- #convert to HSV
- hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
- mask1 = cv2.inRange(hsv, lower_range1, upper_range1)
- mask2 = cv2.inRange(hsv, lower_range2, upper_range2)
- mask = mask1|mask2
- #cv2.imshow("Image", img)
- #cv2.imshow("Mask", mask)
- #find contours
- contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
- approx = []
- approxcontour = []
- maxcontour = -1
- if len(contours) > 0:
- a = 0
- #store the largest contour
- for i in range(len(contours)):
- approxTemp = cv2.approxPolyDP(contours[i], 30, True)
- approx.insert(i, approxTemp)
- area = cv2.contourArea(contours[i])
- if area > a :
- a = area
- maxcontour = i
- #print(approx[maxcontour])
- approxcontour = approx[maxcontour]
- if len(approxcontour) != 4:
- maxcontour = -1
- #imgContour = img
- #imgContour = cv2.drawContours(imgContour, [maxcontour], 0, (255,255,0), 3)
- #cv2.imshow("Contour", imgContour)
- match1 = 0
- match2 = 0
- #transform the largest contour to the window size
- if (maxcontour != -1):
- pts1 = np.float32([approxcontour[0],approxcontour[1],approxcontour[2],approxcontour[3]])
- pts2 = np.float32([[439,0],[0,0],[0,439],[439,439]])
- matrix = cv2.getPerspectiveTransform(pts1,pts2)
- transformed = cv2.warpPerspective(img, matrix, (439, 439))
- #cv2.imshow("Transformed", transformed)
- transformed = cv2.cvtColor(transformed, cv2.COLOR_BGR2HSV)
- mask1 = cv2.inRange(transformed, lower_range1, upper_range1)
- mask2 = cv2.inRange(transformed, lower_range2, upper_range2)
- transformed = mask1|mask2
- #compare the frame against the reference images
- comp1 = transformed|ref1
- match1 = 100-((cv2.countNonZero(comp1)/(439*439))*100)
- comp2 = transformed|ref2
- match2 = 100-((cv2.countNonZero(comp2)/(439*439))*100)
- if match1 > 75:
- print("Grab Symbol")
- bus.write_byte(addr, 0x1)
- routeProcess()
- else if match2 > 75:
- print("Fire Symbol")
- bus.write_byte(addr, 0x1)
- routeProcess()
- else:
- print("No Symbol")
- bus.write_byte(addr, 0x0)
- rawCapture.truncate(0)
- time.sleep(0.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement