Advertisement
Guest User

Untitled

a guest
Nov 11th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. #!/usr/bin/python
  2. import numpy as np
  3. import cv2
  4. import cv2.cv
  5. import hal
  6.  
  7. h = hal.component("capture")
  8. h.newpin("blur", hal.HAL_FLOAT, hal.HAL_IN)
  9. h.newpin("xloc", hal.HAL_FLOAT, hal.HAL_OUT)
  10. h.newpin("yloc", hal.HAL_FLOAT, hal.HAL_OUT)
  11. h.newpin("diam", hal.HAL_FLOAT, hal.HAL_OUT)
  12. h.newpin("xmax", hal.HAL_FLOAT, hal.HAL_OUT)
  13. h.newpin("ymax", hal.HAL_FLOAT, hal.HAL_OUT)
  14. h.newpin("trigger", hal.HAL_BIT, hal.HAL_IN)
  15. h.newpin("capt-diam", hal.HAL_FLOAT, hal.HAL_IN)
  16. h.newpin("capt-tol", hal.HAL_FLOAT, hal.HAL_IN)
  17. h.newpin("triggered", hal.HAL_BIT, hal.HAL_OUT)
  18. h.newpin("samples", hal.HAL_FLOAT, hal.HAL_IN)
  19. h.newpin("samptol", hal.HAL_FLOAT, hal.HAL_IN)
  20.  
  21.  
  22. h.ready()
  23.  
  24. cap = cv2.VideoCapture(0)
  25. h['xmax'] = float(cap.get(3))
  26. h['ymax'] = float(cap.get(4))
  27.  
  28. # h['blur'] = 1
  29. xpos = 0
  30. ypos = 0
  31. xmin = 0
  32. ymin = 0
  33. xmax = 0
  34. ymax = 0
  35. radc = 0
  36. track = 0
  37.  
  38.  
  39. try:
  40. while(True):
  41. # Capture frame-by-frame
  42. ret, frame = cap.read()
  43.  
  44. # Our operations on the frame come here
  45. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  46. blurit = cv2.blur(gray,(int(h['blur']),int(h['blur'])))
  47. # Display the resulting frame
  48.  
  49. circles = cv2.HoughCircles(blurit,cv2.cv.CV_HOUGH_GRADIENT,1,20,param1=50,param2=35,minRadius=int(h['capt-diam']/2)-int(h['capt-tol']),maxRadius=int(h['capt-diam']/2)+int(h['capt-tol']))
  50.  
  51. cv2.circle(blurit,(320,240),int(h['capt-diam']/2)-int(h['capt-tol']),(246,11,11),1)
  52. cv2.circle(blurit,(320,240),int(h['capt-diam']/2)+int(h['capt-tol']),(246,11,11),1)
  53. if h['xloc'] > 0:
  54. cv2.circle(blurit,(int(h['xloc']),int(h['yloc'])),int(h['diam']/2)+int(h['capt-tol']),(246,11,11),1)
  55.  
  56.  
  57. # print circles
  58.  
  59. if not h['trigger'] and h['triggered']:
  60. h['triggered'] = False
  61. track = 0
  62. xpos = 0
  63. ypos = 0
  64. radc = 0
  65. xmin = 0
  66. ymin = 0
  67. xmax = 0
  68. ymax = 0
  69.  
  70. if circles is not None and h['trigger'] and not h['triggered']:
  71. # print circles
  72. circconv = np.uint16(np.around(circles))
  73. for i in circconv[0,:]:
  74. # draw the outer circle
  75. cv2.circle(blurit,(i[0],i[1]),i[2],(246,11,11),1)
  76. # draw the center of the circle
  77. cv2.circle(blurit,(i[0],i[1]),2,(246,11,11),1)
  78. xpos = xpos + i[0]
  79. ypos = ypos + i[1]
  80. radc = radc + i[2]
  81.  
  82. track = track + 1
  83.  
  84. if i[0] < xmin or xmin is 0:
  85. xmin = i[0]
  86. if i[0] > xmax or xmax is 0:
  87. xmax = i[0]
  88. if i[1] < ymin or ymin is 0:
  89. ymin = i[1]
  90. if i[1] > ymax or ymax is 0:
  91. ymax = i[1]
  92.  
  93.  
  94. if abs(ymax-ymin) > h['samptol'] or abs(xmax-xmin)>h['samptol']:
  95. track = 0
  96. xpos = 0
  97. ypos = 0
  98. radc = 0
  99. xmin = 0
  100. ymin = 0
  101. xmax = 0
  102. ymax = 0
  103.  
  104. if track >= h['samples']:
  105. h['xloc'] = float(xpos)/track
  106. h['yloc'] = float(ypos)/track
  107. h['diam'] = (float(radc)/track)*2
  108. h['triggered'] = True
  109. img = cv2.flip(blurit, 1)
  110. cv2.line(img,(320,0),(320,480),(255,0,0),1)
  111. cv2.line(img,(0,240),(640,240),(255,0,0),1)
  112. cv2.imshow('frame',img)
  113. if cv2.waitKey(1) & 0xFF == ord('q'):
  114. break
  115.  
  116. except KeyboardInterrupt:
  117. cap.release()
  118. cv2.destroyAllWindows
  119. raise SystemExit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement