Advertisement
Cpt_Jellyfish

EEEE4008: Alternative 'Recon-Scout' Platform Raspberry Pi (Master) Code

May 13th, 2021
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.92 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. from picamera.array import PiRGBArray
  4. from picamera import PiCamera
  5. from smbus import SMBus
  6. import time
  7. import dropbox
  8.  
  9. #Dropbox setup
  10. token = "" #add dropbox API token here
  11. DBX = dropbox.Dropbox(token)
  12.  
  13. #I2C setup
  14. addr = 0x8
  15. bus = SMBus(1)
  16.  
  17. #red RGB colour values
  18. lower_range1 = np.array([0,50,50])
  19. upper_range1 = np.array([7,255,255])
  20. lower_range2 = np.array([173,50,50])
  21. upper_range2 = np.array([180,255,255])
  22.  
  23. #reference images
  24. ref1 = cv2.imread("Grab.png")
  25. ref2 = cv2.imread("Fire.png")
  26.  
  27. #convert from RGB to HSV
  28. ref1 = cv2.cvtColor(ref1, cv2.COLOR_BGR2HSV)
  29. mask1 = cv2.inRange(ref1, lower_range1, upper_range1)
  30. mask2 = cv2.inRange(ref1, lower_range2, upper_range2)
  31. ref1 = mask1|mask2
  32.  
  33. ref2 = cv2.cvtColor(ref2, cv2.COLOR_BGR2HSV)
  34. mask1 = cv2.inRange(ref2, lower_range1, upper_range1)
  35. mask2 = cv2.inRange(ref2, lower_range2, upper_range2)
  36. ref2 = mask1|mask2
  37.  
  38. #setup camera
  39. camera = PiCamera()
  40. camera.rotation = 270
  41. camera.resolution = (640, 480)
  42. camera.framerate = 10
  43. rawCapture = PiRGBArray(camera, size=(640,480))
  44.  
  45. def routeProcess():
  46.     route = bus.read(address)
  47.    
  48.     #write route to text file
  49.     f = open("maze.txt", "w")
  50.     f.write(route)
  51.     f.close()
  52.    
  53.     #upload text file to Dropbox
  54.     DBX.files_upload(stream.read(), "/maze.txt", mode=dropbox.files.WriteMode.overwrite)
  55.    
  56.     while True:
  57.         #latch
  58.  
  59.  
  60. for frame in camera.capture_continuous(rawCapture, format = "bgr", use_video_port = True):
  61.     img = frame.array
  62.    
  63.     #convert to HSV
  64.     hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  65.  
  66.     mask1 = cv2.inRange(hsv, lower_range1, upper_range1)
  67.     mask2 = cv2.inRange(hsv, lower_range2, upper_range2)
  68.     mask = mask1|mask2
  69.  
  70.     #cv2.imshow("Image", img)
  71.     #cv2.imshow("Mask", mask)
  72.  
  73.     #find contours
  74.     contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  75.     approx = []
  76.     approxcontour = []
  77.     maxcontour = -1
  78.     if len(contours) > 0:
  79.         a = 0
  80.        
  81.         #store the largest contour
  82.         for i in range(len(contours)):
  83.             approxTemp = cv2.approxPolyDP(contours[i], 30, True)
  84.             approx.insert(i, approxTemp)
  85.             area = cv2.contourArea(contours[i])
  86.             if area > a :
  87.                 a = area
  88.                 maxcontour = i
  89.                
  90.         #print(approx[maxcontour])
  91.         approxcontour = approx[maxcontour]
  92.     if len(approxcontour) != 4:
  93.         maxcontour = -1
  94.  
  95.     #imgContour = img        
  96.     #imgContour = cv2.drawContours(imgContour, [maxcontour], 0, (255,255,0), 3)
  97.  
  98.     #cv2.imshow("Contour", imgContour)
  99.  
  100.     match1 = 0
  101.     match2 = 0
  102.    
  103.     #transform the largest contour to the window size
  104.     if (maxcontour != -1):
  105.        
  106.         pts1 = np.float32([approxcontour[0],approxcontour[1],approxcontour[2],approxcontour[3]])
  107.         pts2 = np.float32([[439,0],[0,0],[0,439],[439,439]])
  108.  
  109.         matrix = cv2.getPerspectiveTransform(pts1,pts2)
  110.  
  111.         transformed = cv2.warpPerspective(img, matrix, (439, 439))
  112.         #cv2.imshow("Transformed", transformed)
  113.  
  114.         transformed = cv2.cvtColor(transformed, cv2.COLOR_BGR2HSV)
  115.  
  116.         mask1 = cv2.inRange(transformed, lower_range1, upper_range1)
  117.         mask2 = cv2.inRange(transformed, lower_range2, upper_range2)
  118.         transformed = mask1|mask2
  119.        
  120.         #compare the frame against the reference images
  121.         comp1 = transformed|ref1
  122.         match1 = 100-((cv2.countNonZero(comp1)/(439*439))*100)
  123.         comp2 = transformed|ref2
  124.         match2 = 100-((cv2.countNonZero(comp2)/(439*439))*100)
  125.        
  126.     if match1 > 75:
  127.         print("Grab Symbol")
  128.         bus.write_byte(addr, 0x1)
  129.         routeProcess()
  130.     else if match2 > 75:
  131.         print("Fire Symbol")
  132.         bus.write_byte(addr, 0x1)
  133.         routeProcess()
  134.     else:
  135.         print("No Symbol")
  136.         bus.write_byte(addr, 0x0)
  137.    
  138.     rawCapture.truncate(0)
  139.     time.sleep(0.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement