Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. import libjevois as jevois
  2. import cv2
  3. import numpy as np
  4.  
  5. class DetectGreenObjects:
  6. def __init__(self):
  7. self.stuff = 0
  8.  
  9. def process(self, inframe, outframe):
  10. source0 = inimg = inframe.getCvBGR()
  11. outimg = inimg = inframe.getCvBGR()
  12. self.timer.start()
  13.  
  14. def getArea(con): # Gets the area of the contour
  15. return cv2.contourArea(con)
  16.  
  17. def getYcoord(con): # Gets the Y coordinate of the contour
  18. M = cv2.moments(con)
  19. cy = int(M['m01']/M['m00'])
  20. return cy
  21.  
  22. def getXcoord(con): # Gets the X coordinate of the contour
  23. M = cv2.moments(con)
  24. cy = int(M['m10']/M['m00'])
  25. return cy
  26.  
  27. def sortByArea(conts): # Returns an array sorted by area from smallest to largest
  28. contourNum = len(conts) # Gets number of contours
  29. # sortedBy now has all the contours sorted by area
  30. sortedBy = sorted(conts, key=getArea)
  31. return sortedBy
  32.  
  33. # Draws all contours on original image in red
  34. cv2.drawContours(outimg, self.filter_contours_output, -1, (0, 0, 255), 1)
  35.  
  36. # Gets number of contours
  37. contourNum = len(self.filter_contours_output)
  38.  
  39. # Sorts contours by the smallest area first
  40. newContours = sortByArea(self.filter_contours_output)
  41.  
  42. # Send the contour data over Serial
  43. for i in range(contourNum):
  44. cnt = newContours[i]
  45. # Get the stats of the contour including width and height
  46. x, y, w, h = cv2.boundingRect(cnt)
  47.  
  48. # which contour, 0 is first
  49. toSend = ("CON" + str(i) +
  50. "area" + str(getArea(cnt)) + # Area of contour
  51. # x-coordinate of contour, -500 to 500 rounded to 2 decimal
  52. "x" + str(round((getXcoord(cnt)*1000/320)-500, 2)) +
  53. # y-coordinate of contour, -375 to 375 rounded to 2 decimal
  54. "y" + str(round(375-getYcoord(cnt)*750/240, 2)) +
  55. # Height of contour, 0-750 rounded to 2 decimal
  56. "h" + str(round(h*750/240, 2)) +
  57. "w" + str(round(w*1000/320, 2))) # Width of contour, 0-1000 rounded to 2 decimal
  58. jevois.sendSerial(toSend)
  59.  
  60. # Write a title:
  61. cv2.putText(outimg, "Aaron's JeVois Code", (3, 20),
  62. cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
  63.  
  64. # Write frames/s info from our timer into the edge map (NOTE: does not account for output conversion time):
  65. fps = self.timer.stop()
  66. #height, width, channels = outimg.shape # if outimg is grayscale, change to: height, width = outimg.shape
  67. height, width, channels = outimg.shape
  68. cv2.putText(outimg, fps, (3, height - 6),
  69. cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
  70.  
  71. # Convert our BGR output image to video output format and send to host over USB. If your output image is not
  72. # BGR, you can use sendCvGRAY(), sendCvRGB(), or sendCvRGBA() as appropriate:
  73. outframe.sendCvBGR(outimg)
  74. # outframe.sendCvGRAY(outimg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement