Пример #1
0
    def reinit(self):
        print "Press key `p` to pause the video to start tracking"
        while True:
            # Retrieve an image and Display it.
            self.img = self.reimg.getimage()
            if (cv2.waitKey(10) == ord('p')):
                break
            cv2.namedWindow("Image", 1)
            cv2.imshow("Image", self.img)
        cv2.destroyWindow("Image")
        # Co-ordinates of objects to be tracked
        # will be stored in a list named `points`
        points = get_points.run(self.img)

        if not points:
            print "ERROR: No object to be tracked."
            exit()

        cv2.namedWindow("Image", 1)
        cv2.imshow("Image", self.img)
        # Initial co-ordinates of the object to be tracked
        # Create the tracker object
        self.tracker = dlib.correlation_tracker()
        # Provide the tracker the initial position of the object
        self.tracker.start_track(self.img, dlib.rectangle(*points[0]))
        print "all inited"
Пример #2
0
    def handle_first_frame(self, cap):
        points, result_image = get_points.run(cap)
        p1, p2 = points
        left, top, right, bottom = [p1[0], p1[1], p2[0], p2[1]]
        self.last_x, self.last_y = self.get_center(points[0], points[1])

        self.tracker.start_track(result_image, dlib.rectangle(left, top, right, bottom))
Пример #3
0
    def __init__(self, ip, size=224, port=6789):
        self.reimg = reciever(size=size, port=port)
        self.contraler = contral_by_point(ip=ip)
        self.sender1 = send_instru(ip, 31423)
        self.on = 0
        cv2.namedWindow("Image", 0)
        print "Press key `p` to pause the video to start tracking"
        while True:
            # Retrieve an image and Display it.
            self.img = self.reimg.getimage()
            if (cv2.waitKey(10) == ord('p')):
                break

            cv2.imshow("Image", self.img)
        # Co-ordinates of objects to be tracked
        # will be stored in a list named `points`
        points = get_points.run(self.img)
        self.contraler.set_initpoint((points[0][0], points[0][1]),
                                     (points[0][2], points[0][3]))
        if not points:
            print "ERROR: No object to be tracked."
            exit()

        cv2.imshow("Image", self.img)
        # Initial co-ordinates of the object to be tracked
        # Create the tracker object
        self.tracker = dlib.correlation_tracker()
        # Provide the tracker the initial position of the object
        self.tracker.start_track(self.img, dlib.rectangle(*points[0]))
        print "all inited"
Пример #4
0
def run(source):
    cap = cv2.VideoCapture(source)

    print("Press 'p' to pause the video and start tracking")
    while True:
        ret, img = cap.read()
        if cv2.waitKey(10) == ord('p'):
            break
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
    cv2.destroyWindow('Image')

    points = get_points.run(img)
    rect = Rect(points[0][0], points[0][1], points[0][2], points[0][3])
    if not points:
        print("ERROR: No object to be tracked.")
        exit()

    cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
    cv2.imshow("Image", img)

    tracker = track.Tracker(rect)
Пример #5
0
def run(source=0, dispLoc=False):
    # Create the VideoCapture object
    cam = cv2.VideoCapture(source)

    # If Camera Device is not opened, exit the program
    if not cam.isOpened():
        print ("Video device or file couldn't be opened")
        exit()
    
    print ("Press key `p` to pause the video to start tracking")
    while True:
        # Retrieve an image and Display it.
        retval, img = cam.read()
        if not retval:
            print ("Cannot capture frame device")
            exit()
        if(cv2.waitKey(10)==ord('p')):
            break
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
    cv2.destroyWindow("Image")

    # Co-ordinates of objects to be tracked 
    # will be stored in a list named `points`
    points = get_points.run(img) 

    if not points:
        print ("ERROR: No object to be tracked.")
        exit()
    
    cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
    cv2.imshow("Image", img)
    
    imglist=[img,points]
    pickle.dump( imglist, open( "save.p", "wb" ) )

    if cv2.waitKey(1) == 27:
        cam.release() 
        cv.destroyAllWindows()
Пример #6
0
                interpolation=cv2.INTER_CUBIC)

# Uncomment to do image thresholding

# gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
# blur = cv2.GaussianBlur(gray,(5,5),0)
# ret,thresh = cv2.threshold(blur,180,255,0)
# thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
# thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
# im = thresh
# cv2.imshow("Selected", im)
# cv2.waitKey(0)

fields_dict = []
while True:
    points = get_points.run(im)
    fields_dict.append(points)
    print("Points = ", points)  # Get the bounding box from user
    temp_img = im
    cv2.rectangle(temp_img, (points[0][0], points[0][1]),
                  (points[0][2], points[0][3]), (255, 0, 0), 2)
    cv2.imshow("Selected", temp_img)
    key = cv2.waitKey(0)
    if key == 27:
        break

print(fields_dict)

for val in fields_dict:
    x1 = val[0][0]
    y1 = val[0][1]
Пример #7
0
search_params = dict(checks = 50)

#Object of Fast Approximate Nearest Neighbor Search Library
flann = cv2.FlannBasedMatcher(index_params, search_params)


cap = cv2.VideoCapture(0)

while(1):
    ret,frame = cap.read()
    if ret:
        cv2.imshow('frame',frame)
        #press 'p' to give ROI
        if cv2.waitKey(FRAME_TIME) == ord('p'):
            cv2.destroyWindow('frame')
            points = get_points.run(frame)
            break

roi = frame[points[0][1]:points[0][3],points[0][0]:points[0][2]]
gray_roi =  cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)

h,w = gray_roi.shape
pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)

#Feature detection and description using SIFT            
kp1, des1 = sift.detectAndCompute(gray_roi,None)

#Feature detection and description using SURF
kp2, des2 = surf.detectAndCompute(gray_roi,None)
print kp1
print kp2
def run(source=0, dispLoc=False):

    cam = cv2.VideoCapture(source)
    cam.set(3, 320)

    cam.set(4, 240)

    if not cam.isOpened():
        print("Video device or file couldn't be opened")
        exit()

    print("Press key `p` to pause the video to start tracking")
    while True:

        retval, img = cam.read()
        if not retval:
            print("Cannot capture frame device")
            exit()
        if (cv2.waitKey(10) == ord('p')):
            break
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
    cv2.destroyWindow("Image")

    points = get_points.run(img)
    print(points)
    if not points:
        print("ERROR: No object to be tracked.")
        exit()

    cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
    cv2.imshow("Image", img)

    tracker = dlib.correlation_tracker()

    tracker.start_track(img, dlib.rectangle(*points[0]))

    while True:
        retval, img = cam.read()
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        fullbody = fullbody_cascade.detectMultiScale(gray, 1.3, 3)
        vehicles = vehicle_cascade.detectMultiScale(gray, 1.3, 3)
        font = cv2.FONT_HERSHEY_SIMPLEX
        for (vx, vy, vw, vh) in vehicles:
            cv2.putText(img, 'Vehicle', (vx, vy - 3), font, 0.5, (0, 0, 255),
                        2, cv2.LINE_AA)
            cv2.rectangle(img, (vx, vy), (vx + vw, vy + vh), (0, 0, 255), 2)

        for (x, y, w, h) in fullbody:
            cv2.putText(img, 'Human', (x, y - 3), font, 0.5, (255, 0, 0), 2,
                        cv2.LINE_AA)
            cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
            roi_gray = gray[y:y + h, x:x + w]
            roi_color = img[y:y + h, x:x + w]

        cv2.imshow('Image', img)

        if not retval:
            print("Cannot capture frame device | CODE TERMINATING :(")
            exit()

        tracker.update(img)

        rect = tracker.get_position()
        pt1 = (int(rect.left()), int(rect.top()))
        pt2 = (int(rect.right()), int(rect.bottom()))
        cv2.rectangle(img, pt1, pt2, (255, 255, 255), 3)
        print("Object tracked at [{}, {}] \r").format(pt1, pt2),
        if dispLoc:
            loc = (int(rect.left()), int(rect.top() - 20))
            txt = "Object tracked at [{}, {}]".format(pt1, pt2)
            cv2.putText(img, txt, loc, cv2.FONT_HERSHEY_SIMPLEX, .5,
                        (255, 255, 255), 1)
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)

        if cv2.waitKey(1) == 27:
            break

    cam.release()
def run(source=0, dispLoc=False):
    # Create the VideoCapture object
    cam = cv2.VideoCapture(source)

    # If Camera Device is not opened, exit the program
    if not cam.isOpened():
        print("Video device or file couldn't be opened")
        exit()

    print("Press key `p` to pause the video to start tracking")
    while True:
        # Retrieve an image and Display it.
        retval, img = cam.read()
        if not retval:
            print("Cannot capture frame device")
            exit()
        if (0xFF & cv2.waitKey(10)) == ord('p'):
            break
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
    cv2.destroyWindow("Image")

    # Co-ordinates of objects to be tracked
    # will be stored in a list named `points`
    points = get_points.run(img)

    if not points:
        print("ERROR: No object to be tracked.")
        exit()

    cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
    cv2.imshow("Image", img)

    # Initial co-ordinates of the object to be tracked
    # Create the tracker object
    tracker = dlib.correlation_tracker()
    # Provide the tracker the initial position of the object
    tracker.start_track(img, dlib.rectangle(*points[0]))

    while True:
        # Read frame from device or file
        retval, img = cam.read()
        if not retval:
            print("Cannot capture frame device | CODE TERMINATING :(")
            exit()
        # Update the tracker
        tracker.update(img)
        # Get the position of the object, draw a
        # bounding box around it and display it.
        rect = tracker.get_position()
        pt1 = (int(rect.left()), int(rect.top()))
        pt2 = (int(rect.right()), int(rect.bottom()))
        cv2.rectangle(img, pt1, pt2, (255, 255, 255), 3)
        print("Object tracked at [{}, {}] \r".format(pt1, pt2), end=' ')
        if dispLoc:
            loc = (int(rect.left()), int(rect.top() - 20))
            txt = "Object tracked at [{}, {}]".format(pt1, pt2)
            cv2.putText(img, txt, loc, cv2.FONT_HERSHEY_SIMPLEX, .5,
                        (255, 255, 255), 1)
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
        # Continue until the user presses ESC key
        if (0xFF & cv2.waitKey(1)) == 27:
            break

    # Relase the VideoCapture object
    cam.release()
Пример #10
0
def run(source=0, dispLoc=False):
    # Create the VideoCapture object
    cam = cv2.VideoCapture(source)

    # If Camera Device is not opened, exit the program
    if not cam.isOpened():
        print("Video device or file couldn't be opened")
        exit()

    print("Press key `p` to pause the video to start tracking")
    while True:
        # Retrieve an image and Display it.
        retval, img = cam.read()
        if not retval:
            print("Cannot capture frame device")
            exit()
        if (cv2.waitKey(10) == ord('p')):
            break
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
    cv2.destroyWindow("Image")

    # Co-ordinates of objects to be tracked
    # will be stored in a list named `points`
    points = get_points.run(img, multi=True)
    print(points)
    if not points:
        print("ERROR: No object to be tracked.")
        exit()

    cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
    cv2.imshow("Image", img)

    # Initial co-ordinates of the object to be tracked
    # Create the tracker object
    tracker = [dlib.correlation_tracker() for _ in range(len(points))]
    # Provide the tracker the initial position of the object
    [
        tracker[i].start_track(img, dlib.rectangle(*rect))
        for i, rect in enumerate(points)
    ]
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # Connect the socket to the port where the server is listening
    #server_address = ('localhost', 9999)
    #print (sys.stderr, 'connecting to %s port %s' % server_address)
    #sock.connect(server_address)
    file = open('coordinate.txt', 'w')
    while True:
        # Read frame from device or file
        retval, img = cam.read()
        if not retval:
            print("Cannot capture frame device | CODE TERMINATION :( ")
            exit()
        # Update the tracker
        start = time.time()
        for i in range(len(tracker)):
            start_track_time = time.time()
            tracker[i].update(img)
            # Get the position of th object, draw a
            # bounding box around it and display it.
            rect = tracker[i].get_position()
            end_track_time = time.time()
            print("object track time : ", end_track_time - start_track_time)
            pt1 = (int(rect.left()), int(rect.top()))
            pt2 = (int(rect.right()), int(rect.bottom()))
            if not (pt1[0] < 0 or pt1[1] < 0 or pt2[0] < 0 or pt2[1] < 0):
                ##draw here
                start_draw_time = time.time()
                cv2.rectangle(img, pt1, pt2, (255, 255, 255), 3)
                end_draw_time = time.time()
                print("box draw time : ", end_draw_time - start_draw_time)
                print("Object {} tracked at [{}, {}] \r".format(i, pt1, pt2))
                a = str(int(rect.left()))
                b = str(int(rect.top()))
                c = str(int(rect.right()))
                d = str(int(rect.bottom()))
                value = (a + " " + b + " " + c + " " + d)
                file.write(value + "\n")
                #sock.sendall(value.encode('utf-8'))
                if dispLoc:
                    loc = (int(rect.left()), int(rect.top() - 20))
                    txt = "Object tracked at [{}, {}]".format(pt1, pt2)
                    cv2.putText(img, txt, loc, cv2.FONT_HERSHEY_SIMPLEX, .5,
                                (255, 255, 255), 1)
        end = time.time()
        ##print frames/second here
        print("fps: ", (1 / (end - start)))
        start_disp_time = time.time()
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
        end_disp_time = time.time()
        print("image disp time : ", end_disp_time - start_disp_time)
        print("\n \n")
        # Continue until the user presses ESC key
        if cv2.waitKey(1) == 27:
            break

    # Relase the VideoCapture object
    file.close()
    #sock.close()
    cam.release()
Пример #11
0
def run(source=0, dispLoc=False):
    # Create the VideoCapture object
    # cam = cv2.VideoCapture(source)
    cam = cv2.VideoCapture(0)
    # If Camera Device is not opened, exit the program
    if not cam.isOpened():
        print "Video device or file couldn't be opened"
        exit()

    print "Press key `p` to pause the video to start tracking"
    while True:
        # Retrieve an image and Display it.
        retval, img = cam.read()
        if not retval:
            print "Cannot capture frame device"
            exit()
        if (cv2.waitKey(10) == ord('p')):
            break

        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
    cv2.destroyWindow("Image")

    # Co-ordinates of objects to be tracked
    # will be stored in a list named `points`
    points = get_points.run(img, multi=True)

    if not points:
        print "ERROR: No object to be tracked."
        exit()

    cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
    cv2.imshow("Image", img)

    # Initial co-ordinates of the object to be tracked
    # Create the tracker object
    tracker = [dlib.correlation_tracker() for _ in xrange(len(points))]
    # Provide the tracker the initial position of the object
    [
        tracker[i].start_track(img, dlib.rectangle(*rect))
        for i, rect in enumerate(points)
    ]

    while True:
        # Read frame from device or file
        retval, img = cam.read()
        if not retval:
            print "Cannot capture frame device | CODE TERMINATION :( "
            exit()
        # Update the tracker
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

        # define range of blue color in HSV
        lower_green = np.array([121, 103, 040])
        upper_green = np.array([180, 255, 255])

        #Threshold the HSV image to get only blue colors
        green_mask = cv2.inRange(hsv, lower_green, upper_green)
        mask = green_mask

        #Bitwise-AND mask and original image
        res = cv2.bitwise_and(img, img, mask=mask)

        (contours, hierarchy) = cv2.findContours(green_mask, cv2.RETR_TREE,
                                                 cv2.CHAIN_APPROX_SIMPLE)
        for pic, contour in enumerate(contours):
            area = cv2.contourArea(contour)

            #print area
            if (area > 250):
                x, y, w, h = cv2.boundingRect(contour)
                M = cv2.moments(contour)
                cx = int(M['m10'] / M['m00'])
                cy = int(M['m01'] / M['m00'])
                #print x,y,w,h
                imgr = cv2.rectangle(img, (x - 40, y), (x + w + 30, y + h),
                                     (0, 0, 255), 2)
                cv2.putText(img, "Pen drive", (x, y), cv2.FONT_HERSHEY_SIMPLEX,
                            0.7, (0, 0, 255), 2)
                #cv2.putText(img,"C",(cx,cy),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,255),2)
        for i in xrange(len(tracker)):
            tracker[i].update(img)
            # Get the position of th object, draw a
            # bounding box around it and display it.
            rect = tracker[i].get_position()
            pt1 = (int(rect.left()), int(rect.top()))
            pt2 = (int(rect.right()), int(rect.bottom()))
            cv2.rectangle(img, pt1, pt2, (255, 0, 255), 3)
            print "Object {} tracked at [{}, {}] \r".format(i, pt1, pt2),

            loc = (int(rect.left()), int(rect.top() - 20))
            txt = "Object tracked at [{}, {}]".format(pt1, pt2)
            cv2.putText(img, "RaspberryPi", loc, cv2.FONT_HERSHEY_SIMPLEX, .7,
                        (255, 0, 255), 2)

            cv2.line(img, pt1, (cx, cy), (255, 0, 0), 5)
            cv2.circle(img, pt1, 5, (0, 255, 0), 2)
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
        # Continue until the user presses ESC key
        if cv2.waitKey(1) == 27:
            break

    # Relase the VideoCapture object
    cam.release()
Пример #12
0
def run(source=0, dispLoc=True):
    # Create the VideoCapture object
    print(source)
    cam = cv2.VideoCapture(source)

    # If Camera Device is not opened, exit the program
    if not cam.isOpened():
        print "Video device or file couldn't be opened"
        exit()

    print "Press key `p` to pause the video to start tracking"
    while True:
        # Retrieve an image and Display it.
        retval, img = cam.read()
        if not retval:
            print "Cannot capture frame device"
            exit()
        if (cv2.waitKey(10) == ord('p')):
            break
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
    cv2.destroyWindow("Image")

    # Co-ordinates of objects to be tracked
    # will be stored in a list named `points`
    points = get_points.run(img)
    #points = ([3, 10, 200, 200], [3, 10, 200, 200])
    if not points:
        print "ERROR: No object to be tracked."
        exit()

    cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
    cv2.imshow("Image", img)

    # Initial co-ordinates of the object to be tracked
    # Create the tracker object
    tracker = dlib.correlation_tracker()
    # Provide the tracker the initial position of the object
    tracker.start_track(img, dlib.rectangle(*points[0]))

    #i=1
    txtt = ""
    while True:
        # Read frame from device or file
        retval, img = cam.read()
        if not retval:
            print "Cannot capture frame device | CODE TERMINATING :("
            exit()
        # Update the tracker
        tracker.update(img)
        # Get the position of the object, draw a
        # bounding box around it and display it.
        rect = tracker.get_position()
        pt1 = (int(rect.left()), int(rect.top()))
        pt2 = (int(rect.right()), int(rect.bottom()))
        cv2.rectangle(img, pt1, pt2, (0, 0, 255), 3)
        print "Object tracked at [{}, {}] \r".format(pt1, pt2),

        if not dispLoc:
            loc = (int(rect.left()), int(rect.top() - 20))
            txt = "Object tracked at [{}, {}]".format(pt1, pt2)
            type = sys.getfilesystemencoding()
            print(type)
            #txt = "   蔷薇目 蔷薇科 玫瑰".decode('UTF-8').encode(type)
            cv2.putText(
                img,
                txt,
                loc,
                cv2.FONT_HERSHEY_SIMPLEX,
                1,
                (0, 0, 255),
                3,
            )

        #txtt= txtt + "{},{}\n".format(pt1, pt2)
        txtt = txtt + "\n" + str(int(
            rect.left())) + " " + str(int(rect.top())) + " " + str(
                int(rect.right() - rect.left())) + " " + str(
                    int(rect.bottom() - rect.top()))
        file_object = open('position.txt', 'w+')
        file_object.writelines(txtt)
        file_object.close()
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
        #imgName = "/home/ubuntu1/LJ/after/fream"+str(i)+".jpg"
        #i=i+1
        #cv2.imwrite(imgName,img)
        # Continue until the user presses ESC key
        if cv2.waitKey(1) == 27:
            break

    # Relase the VideoCapture object
    cam.release()
Пример #13
0
import cv2
import get_points
import TLD

cap = cv2.VideoCapture(0)

while (1):
    ret, frame = cap.read()
    if ret:
        cv2.imshow('frame', frame)
        if cv2.waitKey(1) == ord('p'):
            cv2.destroyWindow('frame')
            bounding_box = get_points.run(frame)
            break

multiple = 0.5
print bounding_box
bounding_box[0] = bounding_box[0] * multiple
bounding_box[1] = bounding_box[1] * multiple
bounding_box[2] = bounding_box[2] * multiple
bounding_box[3] = bounding_box[3] * multiple

while (1):
    ret, frame = cap.read()
    if not ret:
        break
    last_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    last_gray = cv2.resize(last_gray, (320, 240), multiple, multiple)

    tld = TLD.TLD()
Пример #14
0
#index_params= dict(algorithm = FLANN_INDEX_LSH,table_number = 6,key_size = 12,multi_probe_level = 1) #for orb
search_params = dict(checks=50)

#Object of Fast Approximate Nearest Neighbor Search Library
flann = cv2.FlannBasedMatcher(index_params, search_params)

cap = cv2.VideoCapture(0)

while (1):
    ret, frame = cap.read()
    if ret:
        cv2.imshow('frame', frame)
        #press 'p' to give ROI
        if cv2.waitKey(FRAME_TIME) == ord('p'):
            cv2.destroyWindow('frame')
            points = get_points.run(frame)
            break

roi = frame[points[0][1]:points[0][3], points[0][0]:points[0][2]]
gray_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)

h, w = gray_roi.shape
pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1,
                                                       0]]).reshape(-1, 1, 2)

#Feature detection and description using SIFT
kp1, des1 = sift.detectAndCompute(gray_roi, None)

#Feature detection and description using SURF
kp2, des2 = surf.detectAndCompute(gray_roi, None)
print kp1
Пример #15
0
def cam_with_grid(kwargs):
    source = int(kwargs['device'])
    # Create the VideoCapture object
    cam = cv2.VideoCapture(source)
    # cam.set(cv2.CV_CAP_PROP_FRAME_WIDTH, 640)
    # cam.set(cv2.CV_CAP_PROP_FRAME_HEIGHT,480)
    #
    # If Camera Device is not opened, exit the program
    if not cam.isOpened():
        print("Video device or file couldn't be opened")
        exit()

    print("Press key `p` to pause the video to start tracking")
    while True:
        # Retrieve an image and Display it.
        retval, img = cam.read()
        img = cv2.flip(img, 1)  # flip horizontally for correct orientation
        if not retval:
            print("Cannot capture frame device")
            exit()
        key = cv2.waitKey(1)
        if (key == ord('p')):
            break
        elif key == 27:
            print('exiting now!')
            exit(0)
        # cv2.namedWindow("Cam Feed", cv2.WINDOW_NORMAL)
        cv2.namedWindow('Cam Feed')
        cv2.imshow("Cam Feed", img)
        # Co-ordinates of objects to be tracked
        # will be stored in a list named `points`
        # points = get_points.run(img)

    cv2.destroyWindow("Cam Feed")

    # Co-ordinates of objects to be tracked
    # will be stored in a list named `points`
    points = get_points.run(img)

    if not points:
        print("ERROR: No object to be tracked.")
        exit()

    # cv2.namedWindow("Cam Feed", cv2.WINDOW_NORMAL)
    cv2.namedWindow('Cam Feed')
    cv2.imshow("Cam Feed", img)

    # Initial co-ordinates of the object to be tracked
    # Create the tracker object
    tracker = dlib.correlation_tracker()
    # Provide the tracker the initial position of the object
    tracker.start_track(img, dlib.rectangle(*points[0]))
    ###### by this time we have the position of the object to be tracked

    # now initialize pygame and grid
    scr_width = 650
    scr_height = 600
    grid_spacing_vertical = scr_height // 2
    grid_spacing_horizontal = scr_width // 2
    line_width = 2
    delay = 0.8
    pygame.init()
    pygame.font.init()
    screen = pygame.display.set_mode([scr_width, scr_height])
    pygame.display.set_caption('Localization Grid')
    colors = {
        'white': (255, 255, 255),
        'black': (0, 0, 0),
        'red': (255, 0, 0),
        'green': (0, 255, 0),
        'blue': (0, 0, 255)
    }
    screen.fill(colors['white'])

    # get all the vertices for our localization grid
    localization_grid = []
    for col in range(1, scr_width // grid_spacing_horizontal):
        for row in range(1, scr_height // grid_spacing_vertical):
            # the first two points are for the column and the other two are for the row
            localization_grid.append([
                (col * grid_spacing_horizontal, 0),
                (col * grid_spacing_horizontal, scr_height),
                (0, row * grid_spacing_vertical),
                (scr_width, row * grid_spacing_vertical)
            ])
    localization_grid = np.asarray(localization_grid)

    # draw it only once
    for points in localization_grid[:, ]:
        pygame.draw.lines(screen, colors['black'], False,
                          [points[0], points[1]], line_width)
        pygame.draw.lines(screen, colors['black'], False,
                          [points[2], points[3]], line_width)

    ###################################################################
    # now open the camera and start the grid
    over = False
    counter = 0
    while not over:
        # Read frame from device or file
        retval, img = cam.read()
        img = cv2.flip(img, 1)  # flip horizontally for correct orientation
        if not retval:
            print("Cannot capture frame device | CODE TERMINATING :(")
            exit()
        # Update the tracker
        tracker.update(img)
        # Get the position of the object, draw a
        # bounding box around it and display it.
        rect = tracker.get_position()
        pt1 = (int(rect.left()), int(rect.top()))
        pt2 = (int(rect.right()), int(rect.bottom()))
        cv2.rectangle(img, pt1, pt2, (255, 255, 255), 3)
        # print("********************************************")
        # print("Object tracked at [{}, {}]".format(pt1, pt2))
        if False:  #dispLoc:
            loc = (int(rect.left()), int(rect.top() - 20))
            txt = "Object tracked at [{}, {}]".format(pt1, pt2)
            cv2.putText(img, txt, loc, cv2.FONT_HERSHEY_SIMPLEX, .5,
                        (255, 255, 255), 1)
        # cv2.namedWindow("Cam Feed", cv2.WINDOW_NORMAL)
        # cv2.namedWindow('Cam Feed')
        cv2.imshow("Cam Feed", img)

        # and here's the grid
        # get some events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                over = True
            if event.type == pygame.KEYDOWN:
                # let's see which key was pressed
                key_pressed = event.key
                if key_pressed == pygame.K_q or key_pressed == pygame.K_ESCAPE:
                    over = True
        # print("you sent: {}".format(message))

        # now draw the grid, with the center of the chair or target as well
        center = ((pt1[0] + pt2[0]) / 2, (pt1[1] + pt2[1]) / 2)
        # scale them
        center = map(
            int, (center[0] / 640 * scr_width, center[1] / 480 * scr_height))
        # print("position on grid at {}".format(center))
        # print("********************************************")
        # the following is the center
        pygame.draw.circle(screen, colors['blue'], center, 4)
        pygame.display.update()
        pygame.display.flip()
        # time.sleep(delay)

        # Continue until the user presses ESC key
        key_pressed = cv2.waitKey(1)
        if key_pressed == 27 or key_pressed == ord('q'):
            over = True
            # pass
    # Relase the VideoCapture object
    cam.release()
    pass
def run(source=0, dispLoc=False):
    # Create the VideoCapture object
    cam = cv2.VideoCapture(source)

    # If Camera Device is not opened, exit the program
    if not cam.isOpened():
        print "Video device or file couldn't be opened"
        exit()

    #video starts
    print "Press key `p` to pause the video to start tracking"
    while True:
        # Retrieve an image and Display it.
        retval, img = cam.read()
        if not retval:
            print "Cannot capture frame device"
            exit()
        if (cv2.waitKey(10) == ord('p')):  #stop playing video if "p" pressed
            break
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
    cv2.destroyWindow("Image")  # video stopped

    threshld = 40
    height = len(img) - threshld
    width = len(img[0]) - threshld

    # Co-ordinates of objects to be tracked
    # will be stored in a list named `points`
    points = get_points.run(img, multi=True)  #new video to take tracking data

    if not points:
        print "ERROR: No object to be tracked initially."
        # exit()
    cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
    cv2.imshow("Image", img)

    # Initial co-ordinates of the object to be tracked
    # Create the tracker object
    tracker = [dlib.correlation_tracker() for _ in xrange(len(points))]
    # Provide the tracker the initial position of the object
    # [tracker[i].start_track(img, dlib.rectangle(*rect)) for i, rect in enumerate(points)]
    for k, rect in enumerate(points):
        tracker[k].start_track(img, dlib.rectangle(*rect))

    temp_tracker = list(
        tracker
    )  #to remove the deleted trackers from tracker without affecting for loop

    while True:
        # print tracker
        # print temp_tracker
        tracker = list(temp_tracker)
        # Read frame from device or file
        retval, img = cam.read()
        if not retval:
            print "Cannot capture frame device | CODE TERMINATION....."
            exit()
        if (cv2.waitKey(10) == ord('p')):  #stop playing video if "p" pressed
            new_points = get_points.run(
                img, multi=True)  #new video to take tracking data

            if not new_points:
                print "ERROR: No new object added at last stop."
            else:
                # points.extend(new_points)
                # Create the tracker object
                new_tracker = [
                    dlib.correlation_tracker() for _ in xrange(len(new_points))
                ]
                # Provide the tracker the new positions of the object
                for j, rect2 in enumerate(new_points):
                    new_tracker[j].start_track(img, dlib.rectangle(*rect2))
                # [tracker[j].start_track(img, dlib.rectangle(*rect)) for j, rect in enumerate(points)]
                tracker.extend(new_tracker)
                print "Success: Tracking started for the newly entered vehicle."

        temp_tracker = list(tracker)
        # Update the tracker
        for i in xrange(len(tracker)):  #for number of objects to track
            # print len(tracker)
            # print i
            confdnc = tracker[i].update(img)
            # print type(confdnc)
            print confdnc
            # try:
            #   tracker[i].update(img)
            # except IndexError:
            #     del tracker[i]
            #     print "Object removed: couldn't identify, list index out of range."
            #     continue

            # Get the position of th object, draw a
            # bounding box around it and display it.
            rect = tracker[i].get_position()
            pt1 = (int(rect.left()), int(rect.top()))
            pt2 = (int(rect.right()), int(rect.bottom()))

            # print type(pt1)
            # print type(temp_tracker)

            if (pt1[0] < threshld or pt2[0] >= width or pt1[1] < threshld
                    or pt2[1] >= height):
                # del temp_tracker[i]
                print "Object removed: out of the frame"
                # continue
            if (confdnc < 3.5):
                print "Low Confidence"
                # continue

            cv2.rectangle(img, pt1, pt2, (255, 255, 255), 3)
            # print "Object {} tracked at [{}, {}] \r".format(i, pt1, pt2),

            # show location of box if mentionedd
            if dispLoc:
                loc = (int(rect.left()), int(rect.top() - 20))
                txt = "Object tracked at [{}, {}]".format(pt1, pt2)
                cv2.putText(img, txt, loc, cv2.FONT_HERSHEY_SIMPLEX, .5,
                            (255, 255, 255), 1)
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
        # Continue until the user presses ESC key
        if cv2.waitKey(1) == 27:
            break

    # Relase the VideoCapture object
    cam.release()
Пример #17
0
def optical_flow(kwargs):
    source = int(kwargs['device'])
    bluetooth_use = int(kwargs['bluetooth'])

    # set bluetooth connection
    hc06_new = '20:15:12:04:25:19'  # our device's mac address
    port = 1  # this port number has been fixed now, must delete the device to change it!!!

    if bluetooth_use:
        sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
        print('log: Trying to connect to {}'.format(hc06_new))
        sock.connect((hc06_new, port))
        sock.settimeout(100.0)
        print('log: Connected!')
    else:
        print('going without bluetooth')

    # Create the VideoCapture object
    cam = cv2.VideoCapture(source)

    # If Camera Device is not opened, exit the program
    if not cam.isOpened():
        print("Video device or file couldn't be opened")
        exit()

    print("Press key `p` to pause the video to start tracking")
    while True:
        # Retrieve an image and Display it.
        retval, img = cam.read()
        # img = cv2.flip(img,1) # flip horizontally for correct orientation
        if not retval:
            print("Cannot capture frame device")
            exit()
        key = cv2.waitKey(1)
        if (key == ord('p')):
            break
        elif key == 27:
            print('exiting now!')
            exit(0)
        # cv2.namedWindow("Cam Feed", cv2.WINDOW_NORMAL)
        cv2.namedWindow('Cam Feed')
        cv2.imshow("Cam Feed", img)

    # Co-ordinates of objects to be tracked
    # will be stored in a list named `points`

    cv2.destroyWindow("Cam Feed")

    # Co-ordinates of objects to be tracked
    # will be stored in a list named `points`
    points = get_points.run(img)

    if not points:
        print("ERROR: No object to be tracked.")
        exit()

    # cv2.namedWindow("Cam Feed", cv2.WINDOW_NORMAL)
    cv2.namedWindow('Cam Feed')
    cv2.imshow("Cam Feed", img)

    # Initial co-ordinates of the object to be tracked
    # Create the tracker object
    tracker = dlib.correlation_tracker()
    # Provide the tracker the initial position of the object
    tracker.start_track(img, dlib.rectangle(*points[0]))
    ###### by this time we have the position of the object to be tracked

    # now initialize pygame and grid
    scr_width = 650
    scr_height = 600
    grid_spacing_vertical = scr_height // 2
    grid_spacing_horizontal = scr_width // 2
    line_width = 2
    delay = 0.5
    pygame.init()
    # pygame.font.init()
    screen = pygame.display.set_mode([scr_width, scr_height])
    pygame.display.set_caption('Localization Grid')
    colors = {
        'white': (255, 255, 255),
        'black': (0, 0, 0),
        'red': (255, 0, 0),
        'green': (0, 255, 0),
        'blue': (0, 0, 255)
    }
    screen.fill(colors['white'])

    # get all the vertices for our localization grid
    localization_grid = []
    for col in range(1, scr_width // grid_spacing_horizontal):
        for row in range(1, scr_height // grid_spacing_vertical):
            # the first two points are for the column and the other two are for the row
            localization_grid.append([
                (col * grid_spacing_horizontal, 0),
                (col * grid_spacing_horizontal, scr_height),
                (0, row * grid_spacing_vertical),
                (scr_width, row * grid_spacing_vertical)
            ])
    localization_grid = np.asarray(localization_grid)

    # draw it only once
    for points in localization_grid[:, ]:
        pygame.draw.lines(screen, colors['black'], False,
                          [points[0], points[1]], line_width)
        pygame.draw.lines(screen, colors['black'], False,
                          [points[2], points[3]], line_width)

    ###################################################################
    # now open the camera and start the grid
    over = False
    counter = 0
    state = "idle"
    while not over:
        # Read frame from device or file
        retval, img = cam.read()
        # img = cv2.flip(img,1) # flip horizontally for correct orientation
        if not retval:
            print("Cannot capture frame device | CODE TERMINATING :(")
            exit()
        # Update the tracker
        tracker.update(img)
        # Get the position of the object, draw a
        # bounding box around it and display it.
        rect = tracker.get_position()
        pt1 = (int(rect.left()), int(rect.top()))
        pt2 = (int(rect.right()), int(rect.bottom()))
        cv2.rectangle(img, pt1, pt2, (255, 255, 255), 3)
        # print("********************************************")
        # print("Object tracked at [{}, {}]".format(pt1, pt2))
        if False:  #dispLoc:
            loc = (int(rect.left()), int(rect.top() - 20))
            txt = "Object tracked at [{}, {}]".format(pt1, pt2)
            cv2.putText(img, txt, loc, cv2.FONT_HERSHEY_SIMPLEX, .5,
                        (255, 255, 255), 1)
        # cv2.namedWindow("Cam Feed", cv2.WINDOW_NORMAL)
        # cv2.namedWindow('Cam Feed')
        cv2.imshow("Cam Feed", img)

        # get some events
        # message = 'idle'
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                over = True
            if event.type == pygame.KEYDOWN:
                # let's see which key was pressed
                key_pressed = event.key
                if key_pressed == pygame.K_q or key_pressed == pygame.K_ESCAPE:
                    over = True
                # bluetooth events
                if bluetooth_use:
                    if key_pressed == pygame.K_UP:
                        state = 'up'
                    elif key_pressed == pygame.K_DOWN:
                        state = 'down'
                    elif key_pressed == pygame.K_RIGHT:
                        state = 'right'
                    elif key_pressed == pygame.K_LEFT:
                        state = 'left'
                    elif key_pressed == pygame.K_SPACE:
                        state = 'idle'
        # send or not?
        # print(counter)
        if bluetooth_use:
            counter += 1
            if counter >= 2:
                sock.send(state)
                counter = 0
            # print(counter, state)
            # print("you sent: {}".format(message))

        # now draw the grid, with the center of the chair or target as well
        cart_pos = ((pt1[0] + pt2[0]) / 2, (pt1[1] + pt2[1]) / 2)
        cart_pos = map(
            int,
            (cart_pos[0] / 640 * scr_width, cart_pos[1] / 480 * scr_height))
        pygame.draw.circle(screen, colors['blue'], cart_pos, 4)

        ########################################################################################33
        # drive here
        normalized_position = (cart_pos[0] - scr_width / 2,
                               cart_pos[1] - scr_height / 2)
        position_in_quadrant = get_quadrant(normalized_position)
        log = "position on grid at {} >> {}".format(normalized_position,
                                                    position_in_quadrant)
        print(log)

        ########################################################################################33

        # print("********************************************")
        # the following is the center
        pygame.display.update()
        pygame.display.flip()
        # time.sleep(delay)

        # Continue until the user presses ESC key
        key_pressed = cv2.waitKey(1)
        if key_pressed == 27 or key_pressed == ord('q'):
            over = True
            # pass
    # Relase the VideoCapture object
    cam.release()
    if bluetooth_use: sock.close()
    pass
Пример #18
0
 def select_points(self, image):
     self.points = get_points.run(image)
     self.tracker = dlib.correlation_tracker()
     points = self.points
     self.tracker.start_track(image, dlib.rectangle(*points[0]))
     return points
Пример #19
0
def run(source, mode=False, length=500, dist=100):

	if mode:
		print "runtime of the video in seconds is - "+str(length)+" seconds"

	#list of touples containing coordinates of the rectangle
	#bounding the object of interest

	points_ped = []
	points_veh = []

	# Variable so that the trajectories are  dynamically increased and decreased. Trajectory length is constant
	frame_var_ped = 1000
	frame_var_veh = 1000

	#Store trajectory coordinates
	coord_ped = []
	coord_veh = []

	# list of pedestrian id's whose path vehicle has encroached
	collided_objects = []

	## For auto deletion of pedestrians and vehicles
	##if they wander in extremeties of the screen
	to_b_deleted_ped = []
	to_b_deleted_veh = []

	##To store all coordinates of points of conflict
	#to show later
	conflict_coord = []

	##Keep a list of every trajectory to display later
	trajectory_ped = []
	trajectory_veh = []

	# If first frame or not
	# In mode=True, we will have to do stuff in the first frame for analysis purpose
	first_frame = True

	if mode:


		# Distance between two reference lines that we will provide in first frame
		#The reference lines are two lines whose length we know in real-world
		#These lines will help in analysis accurately with real word distacnces/dimensions
		distance = dist

		#initialize no of conflict between trajectories
		noOfConflicts = -1

		#set up excel sheet for storing data
		wb = xlwt.Workbook()
		ws = wb.add_sheet("My Sheet")

		#Get fps of the source video.
		fps = get_fps(source, length)

		frame_var_ped = 20 * fps
		frame_var_veh = 10 * fps

		#Initialize the columns of the sheet
		ws.write(0, 0, "PET")
		ws.write(0, 1, "Sex")
		ws.write(0,2,"Speed")
		ws.write(0,3,"Time")
		ws.write(0,4, "Coordinates")
		### <-- Lists/data to be stored with respect to vehicle --> ###

		#list to keep velocities of the vehicles
		velocity = []

		#bool to see if it's between the two reference lines
		vehicle_vel_bool = []

		#counter of frames which the vehicle spends between the reference lines
		vehicle_frame_counter = []

		#at which noOfConflict in the sheet, the desired vehicle has conflicts so that speed can be put
		#in the database in the same row
		which_conflict = []

		##Which assigned reference line the vehicle crosses first
		which_intersect = []

		###<--- Lists to be stored with respect to pedestrian -->###
		#Store sex of the pedestrian
		pedestrian_sex = []

	cap = cv2.VideoCapture(source)
	if not cap.isOpened():
		print "Video device or file couldn't be opened"
		exit()

	#variable to track how many frames has been processed
	time_counter = 0

	while(cap.isOpened()):
		# Capture frame-by-frame

		ret, frame = cap.read()
		if not ret:
			print "Unable to capture device or video ended.\nQuitting . . ."
			break

		#Resize window
		frame = cv2.resize(frame, (450, 350))
		height, width = frame.shape[:2]

		#Make frame size fixed
		cv2.namedWindow('frame', cv2.WINDOW_AUTOSIZE)
		cv2.moveWindow('frame', 916, 418)


		#############################
		#If first frame, add two reference lines
		#Whose real tine distance is known
		###########################

		if first_frame:
			if mode:
				cv2.imshow('frame', frame)
				points = get_line.run(frame)



				l1 = np.empty((2, 2), np.int32)
				l1[0] = (points[0][0][0], points[0][0][1])
				l1[1] = (points[0][1][0], points[0][1][1])

				l2 = np.empty((2, 2), np.int32)
				l2[0] = (points[1][0][0], points[1][0][1])
				l2[1] = (points[1][1][0], points[1][1][1])

				lines = [l1, l2]

				print " press 'p' while the video plays to pause\n        and add objects to track  "
				print " press 'd' while the video plays to delete\n        an object  "
				print " press 'q' to quit \n "

				cv2.destroyWindow("Draw line here.")
			first_frame = False

		# Delete pedestrians and vehicles that go to extremes of frame automatically
##############################################################################################################################
		if to_b_deleted_ped:
			for x in to_b_deleted_ped:
				if mode:
					# try:
					del pedestrian_sex[x]
					##Delete list of collided vehicles mapped with vehicles
					for i in collided_objects:
						try:
							i.remove(x)
						except ValueError:
							pass

					trajectory_ped.append(coord_ped[x])

				del points_ped[x]
				del tracker_ped[x]
				del coord_ped[x]

			# Clear the record so that delete list can be refreshed
			to_b_deleted_ped = []

		if to_b_deleted_veh:
			for z in to_b_deleted_veh:
				if mode:
					for x in which_conflict[z]:
						ws.write(x, 2, velocity[z])

					del velocity[z]
					del vehicle_vel_bool[z]
					del vehicle_frame_counter[z]
					del which_conflict[z]
					del which_intersect[z]
					del collided_objects[z]

					trajectory_veh.append(coord_veh[z])

				del points_veh[z]
				del tracker_veh[z]
				del coord_veh[z]

			# Clear the record so that the delete list can be refreshed
			to_b_deleted_veh = []



		################################################################################################################################



		if points_ped or points_veh:
			key = cv2.waitKey(1) & 0xFF
		else:
			key = cv2.waitKey(50) & 0xFF

		#To delete already selected objects
		if key == ord('d'):
			if mode:
				print "\n press 'a' to delete a pedestrian "
				print " press 'b' to delete a vehicle "
				print " press 'r' to resume with tracking \n"
			else:
				print "\n press 'a' to delete an object of type-A "
				print " press 'b' to delete an object of type-B  "
				print " press 'r' to resume with tracking \n"


			while True:
				#input = raw_input(" \n Press appropriate key: ")
				k = cv2.waitKey(10) & 0xFF

				if k == ord('a'):
					input_a = -2

					while True:

						if mode:
							input_a = int(raw_input(" Enter id(on the terminal)  of the pedestrian to delete , -1 to move to other options :"))
						else:
							input_a = int(raw_input(
								" Enter id(on the terminal)  of the type-A object to delete , -1 to move to other options :"))

						if input_a == -1:

							if mode:
								print "\n press 'a' to delete a pedestrian "
								print " press 'b' to delete a vehicle "
								print " press 'r' to resume with tracking "
								break
							else:
								print "\n press 'a' to delete an object of type-A "
								print " press 'b' to delete an object of type-B "
								print " press 'r' to resume with tracking "
								break

						elif input_a < len(points_ped):
							if mode:
								del pedestrian_sex[input_a]
								for i in collided_objects:
									try:
										i.remove(input_a)
									except ValueError:
										pass

								trajectory_ped.append(coord_ped[input_a])
							del points_ped[input_a]
							del tracker_ped[input_a]
							del coord_ped[input_a]
							print" Object deleted successfully"
						else:
							print " Enter a valid id! "
						pass


				elif k == ord('b'):
					while True:

						if mode:
							input_b = int(raw_input(" Enter id(on the terminal)  of the vehicle to delete , -1 to move to other :"))
						else:
							input_b = int(raw_input(
								" Enter id(on the terminal)  of type-B object to delete , -1 to move to other :"))

						if input_b < 0:

							if mode:
								print "\n press 'a' to delete a pedestrian "
								print " press 'b' to delete a vehicle "
								print " press 'r' to resume with tracking "
								break
							else:
								print "\n press 'a' to delete an object of type-A "
								print " press 'b' to delete an object of type-B "
								print " press 'r' to resume with tracking "
								break

						elif input_b < len(points_veh):
							if mode:
								for x in which_conflict[input_b]:
									ws.write(x, 2, velocity[input_b])

								del velocity[input_b]
								del vehicle_vel_bool[input_b]
								del vehicle_frame_counter[input_b]
								del which_conflict[input_b]
								del which_intersect[input_b]
								del collided_objects[input_b]

								trajectory_veh.append(coord_veh[input_b])

							del points_veh[input_b]
							del tracker_veh[input_b]
							del coord_veh[input_b]
							print" Object deleted successfully"

						else:
							print " Enter a valid id! "
						pass



				elif k == ord('r'):

					break




		if key == ord('q'):
			print"\nQuitting . . \n"
			break

		#Pause to add objects to track
		if key == ord('p'):

			#Update the list of coordinates
			if points_ped:

				points_ped = []
				for i in xrange(len(tracker_ped)):

					rect = tracker_ped[i].get_position()
					points_ped.append((int(rect.left()),int(rect.top()),int(rect.right()),int(rect.bottom())))

			if points_veh:

				points_veh = []
				for i in xrange(len(tracker_veh)):
					rect = tracker_veh[i].get_position()
					points_veh.append((int(rect.left()),int(rect.top()),int(rect.right()),int(rect.bottom())))


			while True:

				#Add Pedestrians to track
				if mode:
					print "\nAdd Pedestrians . . \nDrag rectangles across the frame to assign objects\n"
				else:
					print "\nAdd type-A objects . . \nDrag rectangles across the frame to assign objects\n"

				if mode:
					temp_ped, temp_sex = get_points.run(frame,mode,for_pedestrian=True)

					if temp_ped == "QUIT":
						cv2.destroyWindow("Select objects to be tracked here.")
						cv2.destroyWindow("Objects to be tracked.")
						break
					else:
						for x in temp_ped:
							points_ped.append(x)
						for y in temp_sex:
							pedestrian_sex.append(y)
				else:
					temp_ped = get_points.run(frame, mode, for_pedestrian=True)
					if temp_ped=="QUIT":
							cv2.destroyWindow("Select objects to be tracked here.")
							cv2.destroyWindow("Objects to be tracked.")
							break
					else:
						for x in temp_ped:
							points_ped.append(x)

				#Add vehicles to track
				if mode:
					print "\nAdd vehicles, if any\nDrag rectangles across the frame to assign objects\n"
				else:
					print "\nAdd type-B objects if any . . \nDrag rectangles across the frame to assign objects\n"

				temp_veh=get_points.run(frame,mode,for_pedestrian=False)

				'''Can be made more efficient '''
				if temp_veh == "QUIT":
					cv2.destroyWindow("Select objects to be tracked here.")
					cv2.destroyWindow("Objects to be tracked.")
					break
				else:
					for x in temp_veh:
						points_veh.append(x)
						collided_objects.append([])
						if mode:
							velocity.append(0)
							vehicle_vel_bool.append(0)
							vehicle_frame_counter.append(0)
							which_conflict.append([])
							which_intersect.append([-1])

				if points_ped:
					#initiate tracker
					tracker_ped = [dlib.correlation_tracker() for _ in xrange(len(points_ped))]
					# Provide the tracker the initial position of the object
					[tracker_ped[i].start_track(frame, dlib.rectangle(*rect)) for i, rect in enumerate(points_ped)]

				if points_veh:
					#initiate tracker
					tracker_veh = [dlib.correlation_tracker() for _ in xrange(len(points_veh))]
					# Provide the tracker the initial position of the object
					[tracker_veh[i].start_track(frame, dlib.rectangle(*rect)) for i, rect in enumerate(points_veh)]

				print "press 'r' to see output "
				print "press 'q' to quit "


				if cv2.waitKey(-1) & 0xFF == ord('r'):
					cv2.destroyWindow("Select objects to be tracked here.")
					cv2.destroyWindow("Objects to be tracked.")
					print "\nResumed\n"
					break
				if cv2.waitKey(-1) & 0xFF == ord('q'):
					exit()

		if points_ped or points_veh:

			if points_ped:
				for i in xrange(len(tracker_ped)):

					tracker_ped[i].update(frame)
					# Get the position of th object, draw a
					# bounding box around it and display it.
					rect = tracker_ped[i].get_position()
					pt1 = (int(rect.left()), int(rect.top()))
					pt2 = (int(rect.right()), int(rect.bottom()))
					cv2.rectangle(frame, pt1, pt2, (255, 0, 0), 2)

					if mode:
						cv2.putText(frame, "ped"+str(i) , (int((pt1[0]+pt2[0])/2),int(pt1[1]+2)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1)
					else:
						cv2.putText(frame, "A" + str(i), (int((pt1[0] + pt2[0]) / 2), int(pt1[1] + 2)),
									cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1)
					# Add trajectory of new objects
					if len(coord_ped)<(i+1):
						coord_ped.append(np.empty((0,2),np.uint32))

					#update trajectory
					coord_ped[i] = np.append(coord_ped[i],np.array([[(pt1[0]+pt2[0])/2,pt2[1]]]),axis = 0)
					#Keep the length of trajectory constant
					if len(coord_ped[i])>frame_var_ped:
						coord_ped[i] = np.delete(coord_ped[i], (0), axis=0)

					##Autodelete if the object goes to some extreme of the image
					if (not (width*.05<coord_ped[i][-1][0]< width*.95)) or (not (height*.05<coord_ped[i][-1][1]< height*.95)):
						to_b_deleted_ped.append(i)
					#draw trajectory
					cv2.polylines(frame, [coord_ped[i]], False, (255, 0, 0),2)


			if points_veh:
				for i in xrange(len(tracker_veh)):

					tracker_veh[i].update(frame)
					# Get the position of th object, draw a
					# bounding box around it and display it.
					rect = tracker_veh[i].get_position()
					pt1 = (int(rect.left()), int(rect.top()))
					pt2 = (int(rect.right()), int(rect.bottom()))
					cv2.rectangle(frame, pt1, pt2, (0, 0, 255), 2)
					if mode:
						cv2.putText(frame, "veh"+str(i), (int((pt1[0] + pt2[0]) / 2), int(pt1[1] - 2)), cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0, 0, 255), 1)
					else:
						cv2.putText(frame, "B" + str(i), (int((pt1[0] + pt2[0]) / 2), int(pt1[1] - 2)),
									cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)


					# Add trajectory of new objects
					if len(coord_veh) < (i + 1):
						coord_veh.append(np.empty((0, 2), np.uint32))

					##update trajectory
					coord_veh[i] = np.append(coord_veh[i], np.array([[(pt1[0] + pt2[0]) / 2, pt2[1]]]), axis=0)
					if len(coord_veh[i]) > frame_var_veh:
						coord_veh[i] = np.delete(coord_veh[i], (0), axis=0)

					# draw trajectory
					cv2.polylines(frame, [coord_veh[i]], False, (0, 0, 255),2)

					##Autodelete if the object goes to some extreme of the image
					if (not (width*.05<coord_veh[i][-1][0]< width*.95)) or (not (height*.05<coord_veh[i][-1][1]< height*.95)):
						to_b_deleted_veh.append(i)


###########################################################################################################
					##Condition for finding velocity

					if mode:
						if len(coord_veh[i])>2:
							if vehicle_vel_bool[i] == 0:


								if not check_intersection(l1, coord_veh[i][-1], coord_veh[i][-2]) is None:
									which_intersect[i][0] = 0
									vehicle_vel_bool[i] = 1

								if not check_intersection(l2, coord_veh[i][-1], coord_veh[i][-2]) is None:
									which_intersect[i][0] = 1
									vehicle_vel_bool[i] = 1
									print "Second Line"

							elif vehicle_vel_bool[i] == 1:

								vehicle_frame_counter[i] += 1

								if not check_intersection(lines[(which_intersect[i][0] + 1) % 2], coord_veh[i][-1], coord_veh[i][-2]) is None:
									vehicle_vel_bool[i] = 3
									velocity[i] = (distance*fps)/vehicle_frame_counter[i]

################################################################################################################
					##Check for conflict
					if len(coord_veh[i])>2:
						for x in coord_ped:
							index = 0

							#See if already conflict occured
							if not index in collided_objects[i]:

								#Check for conflict
								if not check_intersection(x, coord_veh[i][-1], coord_veh[i][-2]) is None:
									print "Path conflict detected"

									##Add the coordinates of the collision region to database
									conflict_coord.append(coord_veh[i][-1])

									#update collidion in collided_objects
									collided_objects[i].append(index)
									if mode:
										#Also keep a track record
										noOfConflicts += 1

										#Store the no of conflict to later put velocity in databe
										#while deleting the vehicle
										which_conflict[i].append(noOfConflicts+1)

										#Find how many frames, behind the conflict occurs
										required_value =  check_intersection(x, coord_veh[i][-1], coord_veh[i][-2])
										#Store in database
										ws.write(noOfConflicts+1, 0, str(required_value/fps))
										ws.write(noOfConflicts+1, 1, str(pedestrian_sex[index]))
									 	ws.write(noOfConflicts+1, 3, str(time_counter/fps))
										ws.write(noOfConflicts+1, 4, str(coord_veh[i][-1]))
								index+=1


		if mode:
			#Draw reference lines
			cv2.polylines(frame, np.int32([l1]), False, (255, 0, 0))
			cv2.polylines(frame, np.int32([l2]), False, (0, 255, 0))

			#Update Counter for getting time
			time_counter += 1
			cv2.putText(frame, str(time_counter / fps), (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 3)


		cv2.imshow('frame', frame)
	# When everything done, release the capture

	cap.release()

	if mode:
		#print conflict_coord
		###Save the sheet
		wb.save("PET_Values.xls")

		###Show all the conflict points in a new window consisting of the first frame of the object
		cap = cv2.VideoCapture('docs/video/traffic2')

		if (cap.isOpened()):
			ret, frame = cap.read()

		frame = cv2.resize(frame, (450, 350))

		im_veh = frame.copy()
		im_ped = frame.copy()
		#Draw trajectories

		for trajectories in trajectory_veh:
			cv2.polylines(im_veh, [trajectories], False, (0, 0, 255), 1)

		for trajectories in trajectory_ped:
			cv2.polylines(im_ped, [trajectories],False, (255,0,255), 1)

		##Draw circles
		for x in conflict_coord:
			cv2.circle(frame, (x[0], x[1]), 6, (255, 255, 255), -1)
			cv2.circle(frame, (x[0], x[1]), 6, (0, 0, 255), 1)


		while (1):
			cv2.imshow('Conflict Points', frame)
			cv2.imshow('Vehicle trajectories', im_veh)
			cv2.imshow('Pedestrian trajectories' , im_ped)


			if cv2.waitKey(1) & 0xFF == ord('q'):
				break

		cap.release()
		cv2.destroyAllWindows()
def run(source=0, dispLoc=True):
    # Create the VideoCapture object
    url = 'http://10.9.0.8:8080/video/video.jpg'
    cva = cv2.VideoCapture(url)
    # Use urllib to get the image and convert into a cv2 usable format
    imgResp = urllib.urlopen(url)
    imgNp = np.array(bytearray(imgResp.read()), dtype=np.uint8)
    img = cv2.imdecode(imgNp, -1)
    #cam = cv2.VideoCapture(source)

    # If Camera Device is not opened, exit the program
    #if not cam.isOpened():
    #    print "Video device or file couldn't be opened"
    #    exit()
    print "Press key `p` to pause the video to start tracking"
    while True:
        # Retrieve an image and Display it.
        #	imga=cv2.imread(imgNp,-1)
        #retval, img = cam.read()
        #if not retval:
        #    print "Cannot capture frame device"
        #    exit()
        imgResp = urllib.urlopen(url)
        imgNp = np.array(bytearray(imgResp.read()), dtype=np.uint8)
        img = cv2.imdecode(imgNp, -1)

        if (cv2.waitKey(10) == ord('p')):
            break
#        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
#    cv2.destroyWindow("Image")
    print("TEST")
    # Co-ordinates of objects to be tracked
    # will be stored in a list named `points`
    points = get_points.run(img)

    if not points:
        print "ERROR: No object to be tracked."
        exit()

#    cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
    imgResp = urllib.urlopen(url)
    imgNp = np.array(bytearray(imgResp.read()), dtype=np.uint8)
    img = cv2.imdecode(imgNp, -1)

    cv2.imshow("Image", img)

    # Initial co-ordinates of the object to be tracked
    # Create the tracker object
    tracker = dlib.correlation_tracker()
    # Provide the tracker the initial position of the object
    tracker.start_track(img, dlib.rectangle(*points[0]))

    while True:
        # Read frame from device or file
        # Update the tracker
        imgResp = urllib.urlopen(url)
        imgNp = np.array(bytearray(imgResp.read()), dtype=np.uint8)
        img = cv2.imdecode(imgNp, -1)

        tracker.update(img)
        # Get the position of the object, draw a
        # bounding box around it and display it.
        rect = tracker.get_position()
        pt1 = (int(rect.left()), int(rect.top()))
        pt2 = (int(rect.right()), int(rect.bottom()))
        cv2.rectangle(img, pt1, pt2, (255, 255, 255), 3)
        print "Object tracked at [{}, {}] \r".format(pt1, pt2),
        if dispLoc:
            loc = (int(rect.left()), int(rect.top() - 20))
            txt = "Object tracked at [{}, {}]".format(pt1, pt2)
            cv2.putText(img, txt, loc, cv2.FONT_HERSHEY_SIMPLEX, .5,
                        (255, 255, 255), 1)

#       cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
        # Continue until the user presses ESC key
        if cv2.waitKey(1) == 27:
            break
Пример #21
0
cap = cv2.VideoCapture(0)
first_frame = True
print cap.isOpened()

while (cap.isOpened()):
    ret, frame = cap.read()
    if ret:
        cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
        cv2.imshow('frame', frame)
        if first_frame:
            print 'press p to give roi'
            print 'press ESC to exit'
            first_frame = False
        k = cv2.waitKey(FRAME_TIME)
        if k == ord('p'):
            tl, br = get_points.run(frame)
            break
        elif k == 27:
            cap.release()
            cv2.destroyWindow('frame')
            exit(0)

frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

CMT.initialise(frame_gray, tl, br)

while (1):
    ret, frame = cap.read()
    if not ret:
        break
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
Пример #22
0
def run(source=0, dispLoc=False):

    # Create the VideoCapture object
    cam = cv2.VideoCapture(source)
    # success,image = cam.read()
    cam.set(3, 640)
    cam.set(4, 480)
    # If Camera Device is not opened, exit the program
    if not cam.isOpened():
        print("Video device or file couldn't be opened")
        exit()

    print("Press key `p` to pause the video to start tracking")
    while True:
        # Retrieve an image and Display it.
        retval, img = cam.read()
        if not retval:
            print("Cannot capture frame device")
            exit()
        if (cv2.waitKey(10) == ord('p')):
            break
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
    cv2.destroyWindow("Image")

    # Co-ordinates of objects to be tracked
    # will be stored in a list named `points`
    points = get_points.run(img)

    if not points:
        print("ERROR: No object to be tracked.")
        exit()

    cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
    cv2.imshow("Image", img)

    # Initial co-ordinates of the object to be tracked
    # Create the tracker object
    tracker = dlib.correlation_tracker()
    # Provide the tracker the initial position of the object
    tracker.start_track(img, dlib.rectangle(*points[0]))

    count = 0

    while True:
        # Read frame from device or file
        retval, img = cam.read()
        if not retval:
            print("Cannot capture frame device | CODE TERMINATING :(")
            exit()
        # Update the tracker
        tracker.update(img)
        # Get the position of the object, draw a
        # bounding box around it and display it.
        rect = tracker.get_position()
        pt1 = (int(rect.left()), int(rect.top()))
        pt2 = (int(rect.right()), int(rect.bottom()))
        # pt1new = (int(pt1[0]) + int(pt1[1]))/2
        # pt2new = (int(pt2[0]) + int(pt2[1]))/2
        # new1 = pt1new
        # new2 = pt2new + 20
        # pttupleleel = (int(new1),int(new2))
        cv2.rectangle(img, pt1, pt2, (255, 255, 255), 3)

        crop = img[pt1[1]:pt2[1], pt1[0]:pt2[0]]
        height, width, channels = crop.shape
        print(height / 2, width / 2)
        # cv2.circle(img, (int(height/2),  int(width/2)),50, (255, 255, 255), 3)

        # todo txt file
        cv2.imwrite("extracted/frame%d.jpg" % count,
                    crop)  # save frame as JPEG file
        count += 1

        cv2.imshow("croped", crop)

        # cv2.circle(img, (pttupleleel), 100, (255,255,255), 3)
        # print("Object tracked at [{}, {}] \r".format(pt1, pt2)),
        if dispLoc:
            loc = (int(rect.left()), int(rect.top() - 20))
            txt = "Object tracked at [{}, {}]".format(pt1, pt2)
            cv2.putText(img, txt, loc, cv2.FONT_HERSHEY_SIMPLEX, .5,
                        (255, 255, 255), 1)

        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
        # Continue until the user presses ESC key

        if cv2.waitKey(1) == 27:
            break
    # Relase the VideoCapture object
    cam.release()
Пример #23
0
    (grabbed, frame) = camera.read()
    text = "Unoccupied"
    cv2.bitwise_not(frame, frame)
    # if the frame could not be grabbed, then we have reached the end
    # of the video
    if not grabbed:
        break

    # resize the frame, convert it to grayscale, and blur it
    #frame = imutils.resize(frame, width=1280,height=720)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    #gray = cv2.GaussianBlur(gray, (21, 21), 0)
    gray = denoise(gray)
    # draw region of interst
    if flag == 0:
        points = get_points.run(frame, multi=True)
        flag = 1
    # if the first frame is None, initialize it
    if firstFrame is None:
        firstFrame = gray
        continue
    # compute the absolute difference between the current frame and
    # first frame
    frameDelta = cv2.absdiff(firstFrame, gray)
    thresh = cv2.threshold(frameDelta, 20, 255, cv2.THRESH_BINARY)[1]

    # dilate the thresholded image to fill in holes, then find contours
    # on thresholded image
    thresh = cv2.dilate(thresh, None, iterations=2)
    (cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                                 cv2.CHAIN_APPROX_SIMPLE)
Пример #24
0
def run(source=0, dispLoc=False):
    # Create the VideoCapture object
    cam = cv2.VideoCapture()
    cam.open(source)
    # If Camera Device is not opened, exit the program
    if not cam.isOpened():
        print "Video device or file couldn't be opened"
        exit()
    


    print "Press key `p` to pause the video to start tracking"
    while True:
        # Retrieve an image and Display it.
        retval, img = cam.read()
        if not retval:
            print "Cannot capture frame device"
            exit()
        if(cv2.waitKey(10)==ord('p')):
            break
        #cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.namedWindow("Image")
        cv2.imshow("Image", img)
    cv2.destroyWindow("Image")

    # Co-ordinates of objects to be tracked 
    # will be stored in a list named `points`
    points = get_points.run(img) 

    deadzone_height = numpy.size(img, 0)/2
    deadzone_width = numpy.size(img, 1)/2

    print ('width =  ',deadzone_height ,'height =  ',deadzone_width)

    if not points:
        print "ERROR: No object to be tracked."
        exit()
    
    cv2.namedWindow("Image")
    cv2.imshow("Image", img)

    # Initial co-ordinates of the object to be tracked 
    # Create the tracker object
    tracker = dlib.correlation_tracker()
    # Provide the tracker the initial position of the object
    tracker.start_track(img, dlib.rectangle(*points[0]))

    while True:
        # Read frame from device or file
        retval, img = cam.read()
        if not retval:
            print "Cannot capture frame device | CODE TERMINATING :("
            exit()
        # Update the tracker  
        tracker.update(img)
        # Get the position of the object, draw a 
        # bounding box around it and display it.
        rect = tracker.get_position()
        pt1 = (int(rect.left()), int(rect.top()))
        pt2 = (int(rect.right()), int(rect.bottom()))
        center = ((pt1[0]+pt2[0])/2 , (pt1[1]+pt2[1])/2)
        print (center)

        if (deadzone_width - center[0]) > deadzone_width/2:
        	print('move left')
        	#send_ned_velocity(-1,0,0,5)
        	#time.sleep(5)

        if (deadzone_width - center[0]) < -deadzone_width/2:
        	print('move right')
        	#send_ned_velocity(1,0,0,5)
        	#time.sleep(5)

        if (deadzone_height - center[1]) > deadzone_height/2:
        	print('move forward')
        	#send_ned_velocity(0,1,0,5)
        	#time.sleep(5)
        if (deadzone_height - center[1]) < -deadzone_height/2:
        	print('move back')
        	#send_ned_velocity(0,-1,0,5)
        	#time.sleep(5)


        cv2.rectangle(img, pt1, pt2, (255, 255, 255), 3)
        #print "Object tracked at [{}, {}] \r".format(pt1, pt2),
        if dispLoc:
            loc = (int(rect.left()), int(rect.top()-20))
            txt = "Object tracked at [{}, {}]".format(pt1, pt2)
            cv2.putText(img, txt, loc , cv2.FONT_HERSHEY_SIMPLEX, .5, (255,255,255), 1)
        cv2.namedWindow("Image")
        cv2.imshow("Image", img)
        
        # Continue until the user presses ESC key
        if cv2.waitKey(1) == 27:
        	print("Setting LAND mode...")
        	vehicle.mode = VehicleMode("LAND")
        	#Close vehicle object before exiting script
        	print "Close vehicle object"
        	vehicle.close()
        	print("Completed")
        	break

    # Relase the VideoCapture object
    cam.release()
Пример #25
0
                (grabbed, frame) = camera.read()
                text = "Unoccupied"
                cv2.bitwise_not(frame,frame)
                # if the frame could not be grabbed, then we have reached the end
                # of the video
                if not grabbed:
                                break

                # resize the frame, convert it to grayscale, and blur it
                #frame = imutils.resize(frame, width=1280,height=720)
                gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                #gray = cv2.GaussianBlur(gray, (21, 21), 0)
                gray = denoise(gray)
                # draw region of interst
                if flag==0 :
                    points = get_points.run(frame, multi=True)
                    flag=1
                # if the first frame is None, initialize it
                if firstFrame is None:
                                firstFrame = gray
                                continue
                # compute the absolute difference between the current frame and
                # first frame
                frameDelta = cv2.absdiff(firstFrame, gray)
                thresh = cv2.threshold(frameDelta, 20, 255, cv2.THRESH_BINARY)[1]

                # dilate the thresholded image to fill in holes, then find contours
                # on thresholded image
                thresh = cv2.dilate(thresh, None, iterations=2)
                (cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)
Пример #26
0
def run(source=0, dispLoc=False):
    # Create the VideoCapture object
    cam = cv2.VideoCapture()
    cam.open(source)
    # If Camera Device is not opened, exit the program
    if not cam.isOpened():
        print "Video device or file couldn't be opened"
        exit()
    


    print "Press key `p` to pause the video to start tracking"
    while True:
        # Retrieve an image and Display it.
        retval, img = cam.read()
        if not retval:
            print "Cannot capture frame device"
            exit()
        if(cv2.waitKey(10)==ord('p')):
            break
        #cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.namedWindow("Image")
        cv2.imshow("Image", img)
    cv2.destroyWindow("Image")

    # Co-ordinates of objects to be tracked 
    # will be stored in a list named `points`
    points = get_points.run(img) 

    img_height = numpy.size(img,0)
    img_width = numpy.size(img,1)

    deadzone_height = numpy.size(img, 0)/4
    deadzone_width = numpy.size(img, 1)/4

    print ('width =  ',deadzone_height ,'height =  ',deadzone_width)

    if not points:
        print "ERROR: No object to be tracked."
        exit()
    
    cv2.namedWindow("Image")
    cv2.imshow("Image", img)

    # Initial co-ordinates of the object to be tracked 
    # Create the tracker object
    tracker = dlib.correlation_tracker()
    # Provide the tracker the initial position of the object
    tracker.start_track(img, dlib.rectangle(*points[0]))

    old_error_x = 0
    old_error_y = 0
    kI = 0
    kP = 0
    while True:
        # Read frame from device or file
        retval, img = cam.read()
        if not retval:
            print "Cannot capture frame device | CODE TERMINATING :("
            exit()
        # Update the tracker  
        tracker.update(img)
        # Get the position of the object, draw a 
        # bounding box around it and display it.
        rect = tracker.get_position()
        pt1 = (int(rect.left()), int(rect.top()))
        pt2 = (int(rect.right()), int(rect.bottom()))
        center = ((pt1[0]+pt2[0])/2 , (pt1[1]+pt2[1])/2)
Пример #27
0
import cv2
import get_points
import TLD

cap = cv2.VideoCapture(0)

while(1):
    ret,frame = cap.read()
    if ret:
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) == ord('p'):
            cv2.destroyWindow('frame')
            bounding_box = get_points.run(frame)
            break
        
multiple = 0.5
print bounding_box
bounding_box[0] = bounding_box[0]*multiple
bounding_box[1] = bounding_box[1]*multiple
bounding_box[2] = bounding_box[2]*multiple
bounding_box[3] = bounding_box[3]*multiple

while(1):
    ret,frame = cap.read()
    if not ret:
        break
    last_gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

    last_gray = cv2.resize(last_gray,(320,240),multiple,multiple)

    tld = TLD.TLD()
Пример #28
0
def run(source=0, dispLoc=False):
    # Create the VideoCapture object
    cam = cv2.VideoCapture(source)

    # If Camera Device is not opened, exit the program
    if not cam.isOpened():
        print "Video device or file couldn't be opened"
        exit()
    bs = cv2.createBackgroundSubtractorKNN(detectShadows=False)
    print "Press key `p` to pause the video to start tracking"
    while True:
        # Retrieve an image and Display it.
        retval, img = cam.read()
        if not retval:
            print "Cannot capture frame device"
            exit()
        if (cv2.waitKey(10) == ord('p')):
            break
        fg_mask = bs.apply(img)
        th = cv2.threshold(fg_mask.copy(), 244, 255, cv2.THRESH_BINARY)[1]
        th = cv2.erode(th,
                       cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)),
                       iterations=2)
        dilated = cv2.dilate(th,
                             cv2.getStructuringElement(cv2.MORPH_ELLIPSE,
                                                       (8, 3)),
                             iterations=2)
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)

    cv2.destroyWindow("Image")

    # Co-ordinates of objects to be tracked
    # will be stored in a list named `points`

    points = get_points.run(dilated)

    if not points:
        print "ERROR: No object to be tracked."
        exit()

    cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
    cv2.imshow("Image", img)

    # Initial co-ordinates of the object to be tracked
    # Create the tracker object
    tracker = dlib.correlation_tracker()
    # Provide the tracker the initial position of the object
    tracker.start_track(dilated, dlib.rectangle(*points[0]))

    while True:
        # Read frame from device or file
        retval, img = cam.read()
        if not retval:
            print "Cannot capture frame device | CODE TERMINATING :("
            exit()
        # Update the tracker
        tracker.update(img)
        # Get the position of the object, draw a
        # bounding box around it and display it.
        rect = tracker.get_position()
        pt1 = (int(rect.left()), int(rect.top()))
        pt2 = (int(rect.right()), int(rect.bottom()))
        cv2.rectangle(img, pt1, pt2, (255, 255, 255), 3)
        print "Object tracked at [{}, {}] \r".format(pt1, pt2),
        if dispLoc:
            loc = (int(rect.left()), int(rect.top() - 20))
            txt = "Object tracked at [{}, {}]".format(pt1, pt2)
            cv2.putText(img, txt, loc, cv2.FONT_HERSHEY_SIMPLEX, .5,
                        (255, 255, 255), 1)
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
        # Continue until the user presses ESC key
        if cv2.waitKey(1) == 27:
            break

    # Relase the VideoCapture object
    cam.release()
Пример #29
0
def run(source=0, dispLoc=False):
    # Create the VideoCapture object
    cam = cv2.VideoCapture(source)

    # If Camera Device is not opened, exit the program
    if not cam.isOpened():
        print "Video device or file couldn't be opened"
        exit()

    print "Press key `p` to pause the video to start tracking"
    while True:
        # Retrieve an image and Display it.
        retval, img = cam.read()
        if not retval:
            print "Cannot capture frame device"
            exit()
        if(cv2.waitKey(10) == ord('p')):
            break
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
    cv2.destroyWindow("Image")

    # Co-ordinates of objects to be tracked
    # will be stored in a list named `points`
    points = get_points.run(img, multi=True)

    if not points:
        print "ERROR: No object to be tracked."
        exit()

    cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
    cv2.imshow("Image", img)

    # Initial co-ordinates of the object to be tracked
    # Create the tracker object
    tracker = [dlib.correlation_tracker() for _ in xrange(len(points))]
    # Provide the tracker the initial position of the object
    [tracker[i].start_track(img, dlib.rectangle(*rect))
     for i, rect in enumerate(points)]

    while True:
        # Read frame from device or file
        retval, img = cam.read()
        if not retval:
            print "Cannot capture frame device | CODE TERMINATION :( "
            exit()
        # Update the tracker

        xpoint = []
        for i in xrange(len(tracker)):
            tracker[i].update(img)
            # Get the position of th object, draw a
            # bounding box around it and display it.
            rect = tracker[i].get_position()
            pt1 = (int(rect.left()), int(rect.top()))
            pt2 = (int(rect.right()), int(rect.bottom()))
            xpoint.append((int(rect.left() + ((rect.right() - rect.left()) / 2)),
                           int(rect.top() + ((rect.bottom() - rect.top()) / 2))))
            cv2.rectangle(img, pt1, pt2, (255, 255, 255), 3)
            print "Object {} tracked at [{}, {}] \r".format(i, pt1, pt2)
            if dispLoc:
                loc = (int(rect.left()), int(rect.top() - 20))
                txt = "Object tracked at [{}, {}]".format(pt1, pt2)
                cv2.putText(img, txt, loc,
                            cv2.FONT_HERSHEY_SIMPLEX, .5, (255, 255, 255), 1)
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
        turnDedecation(xpoint)
        # Continue until the user presses ESC key
        if cv2.waitKey(1) == 27:
            break

    # Relase the VideoCapture object
    cam.release()
Пример #30
0
def run(source=0, dispLoc=False):
    # Create the VideoCapture object
    cam = cv2.VideoCapture()
    cam.open(source)
    # If Camera Device is not opened, exit the program
    if not cam.isOpened():
        print "Video device or file couldn't be opened"
        exit()
    
    print "Press key `p` to pause the video to start tracking"
    while True:
        # Retrieve an image and Display it.
        retval, img = cam.read()
        if not retval:
            print "Cannot capture frame device"
            exit()
        if(cv2.waitKey(10)==ord('p')):
            break
        #cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.namedWindow("Image")
        cv2.imshow("Image", img)
    cv2.destroyWindow("Image")

    # Co-ordinates of objects to be tracked 
    # will be stored in a list named `points`
    points = get_points.run(img) 

    if not points:
        print "ERROR: No object to be tracked."
        exit()
    
    cv2.namedWindow("Image")
    cv2.imshow("Image", img)

    # Initial co-ordinates of the object to be tracked 
    # Create the tracker object
    tracker = dlib.correlation_tracker()
    # Provide the tracker the initial position of the object
    tracker.start_track(img, dlib.rectangle(*points[0]))

    while True:
        # Read frame from device or file
        retval, img = cam.read()
        if not retval:
            print "Cannot capture frame device | CODE TERMINATING :("
            exit()
        # Update the tracker  
        tracker.update(img)
        # Get the position of the object, draw a 
        # bounding box around it and display it.
        rect = tracker.get_position()
        pt1 = (int(rect.left()), int(rect.top()))
        pt2 = (int(rect.right()), int(rect.bottom()))
        center = ((pt1[0]+pt2[0])/2 , (pt1[1]+pt2[1])/2)
        print (center)
        cv2.rectangle(img, pt1, pt2, (255, 255, 255), 3)
        #print "Object tracked at [{}, {}] \r".format(pt1, pt2),
        if dispLoc:
            loc = (int(rect.left()), int(rect.top()-20))
            txt = "Object tracked at [{}, {}]".format(pt1, pt2)
            cv2.putText(img, txt, loc , cv2.FONT_HERSHEY_SIMPLEX, .5, (255,255,255), 1)
        cv2.namedWindow("Image")
        cv2.imshow("Image", img)
        # Continue until the user presses ESC key
        if cv2.waitKey(1) == 27:
            break

    # Relase the VideoCapture object
    cam.release()
Пример #31
0
def run(source=0, dispLoc=False):
    # Create the VideoCapture object

    # flash_threshold = 482286005 # video 3 mid
    # flash_threshold = 256239190 # video 3 left
    # flash_threshold = 378792000 # video 3 right

    # flash_threshold = 232000000 # video 4 left
    # flash_threshold = 536700000 # video 4 mid
    flash_threshold = 240205771  # video 4 top
    cam = cv2.VideoCapture(source)

    # If Camera Device is not opened, exit the program
    if not cam.isOpened():
        print("Video device or file couldn't be opened")
        exit()

    print("Press key `p` to pause the video to start tracking")
    while True:
        # Retrieve an image and Display it.
        retval, img = cam.read()
        if not retval:
            print("Cannot capture frame device")
            exit()
        if (cv2.waitKey(10) == ord('p')):
            break
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)

        if img.sum() > flash_threshold:
            print("flash", img.sum())

        # for the unseen video, print the sum of RGB value to set the flash threshold
        # print(img.sum())
    cv2.destroyWindow("Image")

    # Co-ordinates of objects to be tracked
    # will be stored in a list named `points`
    points = get_points.run(img)

    if not points:
        print("ERROR: No object to be tracked.")
        exit()

    cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
    cv2.imshow("Image", img)

    # Initial co-ordinates of the object to be tracked
    # Create the tracker object
    tracker = dlib.correlation_tracker()
    # Provide the tracker the initial position of the object
    tracker.start_track(img, dlib.rectangle(*points[0]))

    position_tuple_list = []
    record = False
    while True:
        # Read frame from device or file
        retval, img = cam.read()
        if not retval:
            print("Cannot capture frame device | CODE TERMINATING :(")
            break
        # Update the tracker
        tracker.update(img)
        # Get the position of the object, draw a
        # bounding box around it and display it.
        rect = tracker.get_position()
        if img.sum() > flash_threshold:
            record = True
            print("flash")
        if record == True:
            x_y_pair = ((int(rect.right() + rect.left()) / 2),
                        int((rect.top() + rect.bottom()) / 2))
            print(x_y_pair)
            position_tuple_list.append(x_y_pair)
        pt1 = (int(rect.left()), int(rect.top()))  # left-top point
        pt2 = (int(rect.right()), int(rect.bottom()))  # right-buttom point
        cv2.rectangle(img, pt1, pt2, (255, 255, 255), 3)
        # print ("Object tracked at [{}, {}] \r".format(pt1, pt2),)
        if dispLoc:
            loc = (int(rect.left()), int(rect.top() - 20))
            txt = "Object tracked at [{}, {}]".format(pt1, pt2)
            cv2.putText(img, txt, loc, cv2.FONT_HERSHEY_SIMPLEX, .5,
                        (255, 255, 255), 1)
        cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
        cv2.imshow("Image", img)
        # Continue until the user presses ESC key
        if cv2.waitKey(1) == 27:
            break

    # Relase the VideoCapture object
    cam.release()
    position_tuple_list = np.array(position_tuple_list)
    np.save(str(source) + ".npy", position_tuple_list)