Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.69 KB | None | 0 0
  1. # imports
  2. from collections import deque
  3. import os, sys, time
  4. import numpy as np
  5. import cv2
  6.  
  7. # init camera
  8. camera = cv2.VideoCapture(0)  ### <<<=== SET THE CORRECT CAMERA NUMBER
  9. camera.set(3, 1280)  # set frame width
  10. camera.set(4, 720)  # set frame height
  11. time.sleep(1)
  12. print(camera.get(3), camera.get(4))
  13.  
  14. # master frame
  15. master = None
  16.  
  17. pts = deque(maxlen=10)
  18. (dX, dY) = (0, 0)
  19. direction = ""
  20.  
  21. while 1:
  22.  
  23.     # grab a frame
  24.     (grabbed, frame0) = camera.read()
  25.  
  26.     # end of feed
  27.     if not grabbed:
  28.         break
  29.  
  30.     # gray frame
  31.     frame1 = cv2.cvtColor(frame0, cv2.COLOR_BGR2GRAY)
  32.  
  33.     # blur frame
  34.     frame2 = cv2.GaussianBlur(frame1, (15, 15), 0)
  35.  
  36.     # initialize master
  37.     if master is None:
  38.         master = frame2
  39.         continue
  40.  
  41.     # delta frame
  42.     frame3 = cv2.absdiff(master, frame2)
  43.  
  44.     # threshold frame
  45.     frame4 = cv2.threshold(frame3, 15, 255, cv2.THRESH_BINARY)[1]
  46.  
  47.     # dilate the thresholded image to fill in holes
  48.     kernel = np.ones((2, 2), np.uint8)
  49.     frame5 = cv2.erode(frame4, kernel, iterations=4)
  50.     frame5 = cv2.dilate(frame5, kernel, iterations=8)
  51.  
  52.     # find contours on thresholded image
  53.     nada, contours, nada = cv2.findContours(frame5.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  54.  
  55.     # make coutour frame
  56.     frame6 = frame0.copy()
  57.  
  58.     # target contours
  59.     targets = []
  60.  
  61.     # loop over the contours
  62.     for c in contours:
  63.  
  64.         # if the contour is too small, ignore it
  65.         if cv2.contourArea(c) < 500:
  66.             continue
  67.  
  68.         # contour data
  69.         M = cv2.moments(c)
  70.         # print( M )
  71.         cx = int(M['m10'] / M['m00'])
  72.         cy = int(M['m01'] / M['m00'])
  73.         x, y, w, h = cv2.boundingRect(c)
  74.         rx = x + int(w / 2)
  75.         ry = y + int(h / 2)
  76.         ca = cv2.contourArea(c)
  77.  
  78.         # plot contours
  79.         cv2.drawContours(frame6, [c], 0, (0, 0, 255), 2)
  80.         cv2.rectangle(frame6, (x, y), (x + w, y + h), (0, 255, 0), 2)
  81.         cv2.circle(frame6, (cx, cy), 2, (0, 0, 255), 2)
  82.         cv2.circle(frame6, (rx, ry), 2, (0, 255, 0), 2)
  83.         # save target contours
  84.         targets.append((cx, cy, ca))
  85.  
  86.     # make target
  87.     mx = 0
  88.     my = 0
  89.     if targets:
  90.  
  91.         # average centroid adjusted for contour size
  92.         # area = 0
  93.         # for x,y,a in targets:
  94.         #    mx += x*a
  95.         #    my += y*a
  96.         #    area += a
  97.         # mx = int(round(mx/area,0))
  98.         # my = int(round(my/area,0))
  99.  
  100.         # centroid of largest contour
  101.         area = 0
  102.         for x, y, a in targets:
  103.             if a > area:
  104.                 mx = x
  105.                 my = y
  106.                 area = a
  107.  
  108.     # plot target
  109.     tr = 20
  110.     frame7 = frame0.copy()
  111.     if targets:
  112.         cv2.circle(frame7, (mx, my), tr, (0, 0, 255, 0), 2)
  113.         cv2.line(frame7, (mx - tr, my), (mx + tr, my), (0, 0, 255, 0), 2)
  114.         cv2.line(frame7, (mx, my - tr), (mx, my + tr), (0, 0, 255, 0), 2)
  115.  
  116.  
  117.         pts.appendleft((mx, my))
  118.  
  119.  
  120.     # loop over the set of tracked points
  121.     for i in np.arange(1, len(pts)):
  122.         # if either of the tracked points are None, ignore
  123.         # them
  124.         if pts[i - 1] is None or pts[i] is None:
  125.             continue
  126.  
  127.         # check to see if enough points have been accumulated in
  128.         # the buffer
  129.         if len(pts) >= 10 and i == 1 and pts[-10] is not None:
  130.             # compute the difference between the x and y
  131.             # coordinates and re-initialize the direction
  132.             # text variables
  133.             dX = pts[-10][0] - pts[i][0]
  134.             dY = pts[-10][1] - pts[i][1]
  135.             (dirX, dirY) = ("", "")
  136.  
  137.             # ensure there is significant movement in the
  138.             # x-direction
  139.             if np.abs(dX) > 20:
  140.                 dirX = "East" if np.sign(dX) == 1 else "West"
  141.  
  142.             # ensure there is significant movement in the
  143.             # y-direction
  144.             if np.abs(dY) > 20:
  145.                 dirY = "North" if np.sign(dY) == 1 else "South"
  146.  
  147.             # handle when both directions are non-empty
  148.             if dirX != "" and dirY != "":
  149.                 direction = "{}-{}".format(dirY, dirX)
  150.  
  151.             # otherwise, only one direction is non-empty
  152.             else:
  153.                 direction = dirX if dirX != "" else dirY
  154.  
  155.     print(direction, 'direction')
  156.     cv2.putText(frame7, direction, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 3)
  157.  
  158.     # update master
  159.     master = frame2
  160.  
  161.     cv2.imshow("Frame7: Target", frame7)
  162.  
  163.     # key delay and action
  164.     key = cv2.waitKey(1) & 0xFF
  165.     if key == ord('q'):
  166.         break
  167.     elif key != 255:
  168.         print('key:', [chr(key)])
  169.  
  170. # release camera
  171. camera.release()
  172.  
  173. # close all windows
  174. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement