Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. rang = cv2.inRange(flow,np.array(20),np.array(255))
  2.  
  3. TypeError: <unknown> is not a numpy array
  4.  
  5. import cv2
  6. import numpy as np
  7.  
  8. cap = cv2.VideoCapture(0)
  9.  
  10. cap.set(3,320)
  11. cap.set(4,240)
  12. _,prev = cap.read()
  13. prevG = cv2.cvtColor(prev, cv2.COLOR_RGB2GRAY)
  14. kernel = np.ones((5,5),np.uint8)
  15. font = cv2.FONT_HERSHEY_SIMPLEX
  16.  
  17. while True:
  18. _,next = cap.read()
  19. nextG = cv2.cvtColor(next, cv2.COLOR_RGB2GRAY)
  20. flow = np.array(abs(np.array(nextG,np.float32)-np.array(prevG,np.float32)),np.uint8)
  21. cv2.imshow('flow',flow)
  22. rang = cv2.inRange(flow,np.array(20),np.array(255))
  23. cv2.imshow('rang',rang)
  24. opening = cv2.morphologyEx(rang, cv2.MORPH_OPEN, kernel)
  25. closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel)
  26. cv2.imshow('closing',closing)
  27.  
  28. contours,_ = cv2.findContours(closing,1,2)
  29.  
  30. M = [0,0]
  31. n = 0
  32.  
  33. for cnt in contours:
  34. x,y,w,h = cv2.boundingRect(cnt)
  35. if w>15 and h>15 and w<200 and h<200:
  36. M[0] += x + float(w)/2.
  37. M[1] += y + float(h)/2.
  38. n += 1
  39.  
  40. if M[0]!=0 and M[0]!=0:
  41. M = np.array(M)
  42. NewCen = PrevCen + 0.9*(M-PrevCen)
  43. cntX = int(NewCen[0]/n)
  44. cntY = int(NewCen[1]/n)
  45. cv2.circle(next,(cntX,cntY),5,(130,50,200),-1)
  46. cv2.putText(next,str(cntX)+','+str(cntY),(cntX+10,cntY+10),font,1,(130,50,200))
  47. PrevCen = NewCen
  48.  
  49. prevG = nextG
  50.  
  51. cv2.imshow('next',next)
  52. k = cv2.waitKey(1) & 0xFF
  53. if k == 27: break
  54.  
  55. cap.release()
  56. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement