abdiOS

Untitled

Mar 6th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.15 KB | None | 0 0
  1. from __future__ import print_function
  2. import numpy as np
  3. import cv2 as cv
  4. import time
  5.  
  6. lk_params = dict( winSize  = (15, 15),
  7.                   maxLevel = 2,
  8.                   criteria = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 0.03))
  9.  
  10. fgbg = cv.bgsegm.createBackgroundSubtractorMOG()
  11. filename=open("txt.txt","w")
  12.  
  13. track_len = 10
  14. detect_interval = 5
  15. tracks = []
  16. cam = cv.VideoCapture('fishtest.flv')
  17. frame_count = 0
  18.  
  19. def update_tracks(p0,p0r,tracks,p1):
  20.     d = abs(p0-p0r).reshape(-1, 2).max(-1)
  21.     good = d < 1
  22.     new_tracks = []
  23.  
  24.  
  25.     for tr, (x, y), good_flag in zip(tracks, p1.reshape(-1, 2), good):
  26.         #Zips tracks the points and whether its true or not into a list/array? dunno
  27.         #unpacks into tr(integer values of co-ordinates)/ (x,y) floating values and good flag which is boolean
  28.         #checks if point is good or not if it is then it appends the floating value to the integer
  29.         if not good_flag:
  30.             continue
  31.         tr.append((x, y))
  32.         #drawcircles
  33.         cv.circle(vis, (x, y), 2, (0, 255, 0), -1)
  34.         #delete if too long only keep 10 tracks at any given time
  35.  
  36.         if len(tr) > track_len:
  37.             del tr[0]
  38.         new_tracks.append(tr)
  39.  
  40.        
  41.     tracks = new_tracks
  42.     cv.polylines(vis, [np.int32(tr) for tr in tracks], False, (0, 255, 0))
  43.     return tracks
  44.  
  45. def update_and_drawBox(contours,boxpoints):
  46.     for i in range(0, len(contours)):
  47.             cnt = contours[i]
  48.             cnt=cnt.astype(np.float32)
  49.            
  50.             if (len(cnt)>9):
  51.                 x,y,w,h = cv.boundingRect(cnt)
  52.                 cx=x+(w/2)
  53.                 cy=y+(h/2)
  54.                 boxpoints.append([x,y])
  55.                 boxpoints.append([x+w,y+h])
  56.                 boxpoints.append([x+w,y])
  57.                 boxpoints.append([x,y+h])
  58.                 boxpoints.append([cx,cy])
  59.  
  60.                 cv.rectangle(vis,(x,y),(x+w,y+h),(255,0,0),2)
  61.  
  62.     boxpoints=np.asarray(boxpoints).astype(np.float32)
  63.     return boxpoints
  64.  
  65. while True:
  66.     ret, frame = cam.read()
  67.     fgmask = fgbg.apply(frame)
  68.     frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
  69.     vis = frame.copy()
  70.     boxpoints=[]
  71.  
  72.     #find contours
  73.     im2,contours,hierarchy = cv.findContours(fgmask,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
  74.  
  75.     if len(tracks) > 0:
  76.         p0 = np.float32([tr[-1] for tr in tracks]).reshape(-1, 1, 2)
  77.         p1, _st, _err = cv.calcOpticalFlowPyrLK(previousFrame, frame_gray, p0, None, **lk_params)
  78.         p0r, _st, _err = cv.calcOpticalFlowPyrLK(frame_gray, previousFrame, p1, None, **lk_params)
  79.         tracks=update_tracks(p0,p0r,tracks,p1)
  80.    
  81.     if frame_count % detect_interval == 0 and len(contours) !=0:
  82.         p = update_and_drawBox(contours,boxpoints)
  83.      
  84.         if p is not None:
  85.             for x, y in p.reshape(-1, 2):
  86.                 #-1 unknown leave up to numpy stop forgetting ya twat
  87.                 tracks.append([(x, y)])
  88.    
  89.  
  90.     frame_count += 1
  91.     previousFrame = frame_gray
  92.     cv.imshow('frame1', vis)
  93.     cv.imshow("frame2",fgmask)
  94.     cv.imshow("frame",frame)
  95.  
  96.     if cv.waitKey(30) == ord('q'):
  97.         break
  98.  
  99. cv.destroyAllWindows()
Add Comment
Please, Sign In to add comment