Example #1
0
class VideoSource (object):
    def __init__ (self, video_file, log, use_pi_camera = True, resolution=(320, 200), framerate = 30, night = False):
        self.filename = video_file
        self.log = log
        self.use_pi_camera  = use_pi_camera
        self.resolution = resolution
        self.framerate = framerate
        self.night = night
        self.fvs = None
        self.stream = None
        self._done = False

    def start (self):
        if self.filename is not None:
            self.log.debug('Video file: %s', self.filename)
            self.fvs = FileVideoStream(self.filename).start()
            self.stream = self.fvs.stream
            time.sleep(1.0)
        else: 
            if self.use_pi_camera:
                self.log.debug('Pi Camera (%d %d)', self.resolution[0], self.resolution[1])
                self.stream = VideoStream(src=0,
                    usePiCamera=True,                    
                    resolution=self.resolution,
                    framerate=self.framerate,
                    sensor_mode=5
                    ).start()

            else:
                self.log.debug('Web Camera')
                self.stream = cv2.VideoCapture(0)
                self.stream.set(cv2.CAP_PROP_BUFFERSIZE, 2)
                self.resolution = (
                    self.stream.get(cv2.CAP_PROP_FRAME_WIDTH),
                    self.stream.get(cv2.CAP_PROP_FRAME_HEIGHT)
                )
                self.framerate = self.stream.get(cv2.CAP_PROP_FPS)

        return self.resolution, self.framerate

    def read(self):
        if self.filename:
            frame = self.fvs.read()
        else:
            frame = self.stream.read()
        return frame

    def stop (self):
        if self.filename:
            self.fvs.stop()
        else:
            self.stream.stop()
        self._done = True

    def done(self):
        # check to see if video is still running
        running = (self.filename and self.fvs.running()) or True
        self._done = not running
        return self._done
def loadStream(res: tuple=(640,480), fps: int=30, loadVideo: Optional[str]=None, startFrame: Optional[int]=None) -> GameViewSource:
    # Load from video or from webcam
    if not loadVideo:
        stream = VideoStream(src=0, resolution=res, framerate=fps).start()
        time.sleep(2)
        fps = stream.stream.get(cv2.CAP_PROP_FPS)
        res = (int(stream.stream.get(cv2.CAP_PROP_FRAME_WIDTH)), int(stream.stream.get(cv2.CAP_PROP_FRAME_HEIGHT)))
        gameViewSource = GameViewSource(stream, False, res)
    else:
        stream = cv2.VideoCapture(loadVideo)
        fps = stream.get(cv2.CAP_PROP_FPS)
        if startFrame:
            stream.set(cv2.CAP_PROP_POS_FRAMES, startFrame)
        res = (int(stream.get(cv2.CAP_PROP_FRAME_WIDTH)), int(stream.get(cv2.CAP_PROP_FRAME_HEIGHT)))
        gameViewSource = GameViewSource(stream, True, res)
    print("Stream:\n\tFPS - %d\n\tResolution: (%d, %d)" % (fps, res[0], res[1]))

    # Setup net
    netX = userSetupScene(gameViewSource)
    gameViewSource.setNetPos(netX)
    print("Got net x: %d" % netX)

    # View has been setup
    return gameViewSource
Example #3
0
def detector(args):
    # CLASSIFIER command line switches
    print(args)
    if (args[1] == "haar" or args[1] == "Haar"):
        classifier = cv2.CascadeClassifier(
            "/home/pi/Desktop/project_master/input_files/classifiers/haarcascade_fullbody.xml"
        )
    elif (args[1] == "hog" or args[1] == "Hog"):
        classifier = cv2.HOGDescriptor()
        classifier.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
    elif (args[1] == "yolo" or args[1] == "Yolo"):
        # ToDo set up yolo detector
        classifier = ''
    else:
        print(
            "Invalid command: Please enter a command in the form of either\n"
            "detector-notPi.py detectionMethod cam gt PATHtoGroundTruthFile noshow\n"
            "detector-notPi.py detectionMethod vid PATHtoInputVid gt PATHtoGroundTruthFile noshow\n"
            "gt switch can be ommitted and noshow can be ommitted to display the detections on the monitor"
        )
        exit(1)

    stats = open("/home/pi/Desktop/project_master/output_files/stats.txt", "w")

    class groundTruth:
        def __init__(self, type, height, width, x, y, frameN):
            self.type = type
            self.height = height
            self.width = width
            self.x = x
            self.y = y
            self.frameN = frameN
            self.true = 0

    groundT = {}
    cur = 1
    tokens = ''
    words = {}
    detection = []
    file = None
    # GROUNTRUTH command line switches
    if (len(args) > 3 and args[3] == "gt"):
        if (args[4] is None):
            file = None
        else:
            file = args[4]
    elif (len(args) > 5 and args[4] == "gt"):
        if (len(args) < 6):
            file = open(
                '/home/pi/Desktop/project_master/input_files/ground_truth/GroundTruth.csv',
                'r')
        else:
            file = open(args[5])
    if (file is not None):
        file = file.readlines()
        for lines in file[0:]:
            lines = lines.strip("\r\n")
            lines = lines.strip('"')
            words = lines.split(',')
            groundT[cur] = groundTruth(words[0], words[1], words[2], words[3],
                                       words[4], words[5])
            cur += 1
        print(groundT[1].x)

    # VIDEO INPUT command line switches
    # Apply detector on the devices default camera
    if (args[2] == 'cam' or args[2] == 'Cam'):
        if (args[1] == 'hog' or args[1] == 'Hog'):
            camera = PiCamera()
            rawCapture = PiRGBArray(camera, )
            camera.capture(rawCapture, format="bgr")
        else:
            cam = VideoStream(usePiCamera=True).start()
    # Apply detector to a specified input video
    elif (args[2] == 'vid' or args[2] == 'Vid'):
        cam = cv2.VideoCapture(args[3])
    else:
        print(
            "Input error: must specify cam to use default device or vid PATHtoVid to apply on a video file"
        )
    # set height and width (Input width and height must match output width and height)
    if (args[2] == 'cam' or args[2] == 'Cam'):
        print()
    else:
        cam.set(3, 640)
        cam.set(4, 480)

    # Create the output writer with the correct codec
    # !!!!!MINA BOTROS THIS IS WHERE THE OUTPUT VIDEO GETS WRITTEN CHANGE NAME IF YOU WANT TO SAVE THE VIDEO!!!!!!
    fourcc = cv2.VideoWriter_fourcc(*'X264')
    out = cv2.VideoWriter(
        '/home/pi/Desktop/project_master/output_files/output_vids/Haar_Vid2.mp4',
        fourcc, 2, (480, 640))

    # Frame count variable for keeping track of people in each frame
    # will be used for tracking
    framecount = 1
    start_time = time.time()
    last_upload_time = start_time
    upload_timer = start_time
    while (True):
        # computer reads the image
        if (args[1] == 'Haar' or args[1] == 'haar'):
            frame = cam.read()
        elif (args[1] == 'Hog' or args[1] == 'hog'):
            frame = rawCapture.array
        elif (args[1] == 'Yolo' or args[1] == 'Yolo'):
            #ToDo: Enter yolo frame intialization
            print()
        i = 1

        key = cv2.waitKey(1)

        #Draw the ground truth boxes if supplied
        if (len(args) > 4 and (args[4] == "gt" or args[3] == "gt")):
            while i < cur:
                if str(framecount) == groundT[i].frameN and groundT[i].y != '':
                    cv2.rectangle(
                        frame,
                        (int(float(groundT[i].x)), int(float(groundT[i].y))),
                        (int(float(groundT[i].x) + float(groundT[i].width)),
                         int(float(groundT[i].y) + float(groundT[i].height))),
                        (255, 255, 255), 2)
                i += 1

        # Initialize the detector with the correct classifier model
        if (args[1] == "hog" or args[1] == "Hog"):
            (pedestrian, weights) = classifier.detectMultiScale(frame)
        elif (args[1] == "haar" or args[1] == "Haar"):
            pedestrian = classifier.detectMultiScale(frame)
        elif (args[1] == "yolo" or args[1] == "Yolo"):
            # ToDo add yolo classifier initilization
            pedestrian = ""

        # Counts the number of people in each frame
        count = 0

        # Apply the classifier to each frame and draws a rectangle around the detected people
        # and formats the detection into a transmittable packet
        max = 0
        for (x, y, w, h) in pedestrian:
            k = 2
            # count the number of people
            count += 1

            # draw the rectangle
            cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
            global t
            t = 1
            # store the center of each person
            he = int(x + w / 2)
            wi = int(y + h / 2)

            # JSON format for transmission
            detection.append("{Human: frame: " + str(framecount) +
                             ", num_in_frame: " + str(count) + ", x: " +
                             str(he) + ", y: " + str(wi) + "}")
            #individual detections to be stored locally as a local log for debugging purposes
            location = str(framecount) + ' ' + str(count) + ' ' + str(
                x) + ' ' + str(y) + ' ' + str(x + w) + ' ' + str(y + h)

            # draw a circle at the center of the person with their centers
            cv2.circle(frame, (he, wi), 2, (255, 0, 0), 2)
            cv2.putText(frame, "(" + str(x) + "," + str(y) + ")", (he, wi),
                        cv2.FONT_HERSHEY_SIMPLEX, .30, (255, 255, 255))

            # write each person's location to the output file
            stats.write(location + "\n")

        max = 0

        # if escape is hit exit
        if key in [27]:
            break

        #framerate (FPS) calculation
        end_time = time.time()
        elapsed_time = end_time - start_time
        fps = (framecount / elapsed_time)
        print(str(framecount) + " processed at " + str(fps) + "fps")

        #Packet Transmission timer
        elapsed_time_from_last_upload = end_time - last_upload_time

        # output a json packet to send to be picked up by the transmitter
        #currently set to transmit in 10 second intervals
        if (elapsed_time_from_last_upload >= 1):
            # dump to a json
            last_upload_time = time.time()
            outfile = open(
                '/home/pi/Desktop/project_master/output_files/data.json', 'w')
            json.dump(detection, outfile)
            outfile.close()
            detection.clear()
            print(
                "------------------PACKET WRITTEN FOR TRANSMISSION-----------------------"
            )

        if (args[len(args) - 1] != "noshow"):
            # UI text show user how to quit, number of people and fps
            cv2.putText(frame, "Number of people:" + str(count), (10, 70),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255))
            cv2.putText(frame, "Press ESC to quit.", (10, 30),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255))
            cv2.putText(frame, "FPS: " + str(fps), (10, 100),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255))
            cv2.putText(frame, "FRAME:" + str(framecount), (10, 700),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255))

            # If noshow is not specified then display the detections to the screen
            cv2.imshow("Real Time Facial Recognition", frame)
        out.write(frame)
        framecount += 1

    cam.release()
    out.release()
    stats.close()
    cv2.destroyAllWindows()
Example #4
0
import os
import sys
import tensorflow as tf

stopEvent = threading.Event()
pause_ = False
stop_ = False
outputPath = "/home/pi/Documents/Pi_Pro/tf_files"
root = tk.Tk()
root.overrideredirect(True)
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(),
                                   root.winfo_screenheight()))
print root.winfo_screenwidth(), root.winfo_screenheight()

video = VideoStream(usePiCamera=True, resolution=(640, 480)).start()
video.set(3, 1024)
video.set(4, 720)
time.sleep(0.2)


def videoLoop():
    global panel
    global frame
    while (not stop_):
        try:
            # keep looping over frames until we are instructed to stop
            while not stopEvent.is_set() and not stop_:
                # grab the frame from the video stream
                frame = video.read()

                #if not ret:continue
# lbpcascade_frontalface: 1.1
# haarcascade_frontalface_alt2: 1.3
SCALE_FACTOR = 1.1
MIN_NEIGHBORS = 5
#MIN_SIZE = 30
MIN_SIZE = 80

cascPath = sys.argv[1]
faceCascade = cv2.CascadeClassifier(cascPath)

if ENABLE_VIDEO_STREAM:
    video_capture = VideoStream(usePiCamera=False).start()

else:
    video_capture = cv2.VideoCapture(0)
    video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, FRAME_WIDTH)
    video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT)

time.sleep(1)
t = ticket()

def faceDetect(gray):
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=SCALE_FACTOR,
        minNeighbors=MIN_NEIGHBORS,
        minSize=(MIN_SIZE, MIN_SIZE),
        flags=cv2.CASCADE_SCALE_IMAGE
    )

    if len(faces)>0:
Example #6
0
class Vision:
    # Construtor:
    def __init__(self, cam):
        self.posx = 0
        self.posy = 0

        # construct the argument parse and parse the arguments
        self.ap = argparse.ArgumentParser()
        self.ap.add_argument("-v",
                             "--video",
                             help="path to the (optional) video file")
        self.ap.add_argument("-b",
                             "--buffer",
                             type=int,
                             default=64,
                             help="max buffer size")
        self.args = vars(self.ap.parse_args())

        # define the lower and upper boundaries of the "green"
        # ball in the HSV color space, then initialize the
        # list of tracked points
        self.greenLower = (0, 0, 0)
        self.greenUpper = (200, 105, 105)

        # define camera
        self.cam = cam  # it could be 0, 1 in this case

        self.pts = deque(maxlen=self.args["buffer"])

        self.key = cv2.waitKey(1) & 0xFF

        # if a video path was not supplied, grab the reference
        # to the webcam
        if not self.args.get("video", False):
            self.vs = VideoStream(src=cam).start()

        # otherwise, grab a reference to the video file
        else:
            self.vs = cv2.VideoCapture(self.args["video"])
            self.vs.set(3, 320)
            self.vs.set(4, 240)

        self.frame = self.vs.read()

    # Metodo que captura e corta o frame
    def Frame_Capture(self):
        # grab the current frame
        self.frame = self.vs.read()
        self.frame = cv2.flip(self.frame, -1)
        # create a cropped frame
        self.crop_frame = self.frame[
            10:480, 30:535]  # Crop from {x, y, w, h } => {0, 0, 300, 400}

        # handle the frame from VideoCapture or VideoStream
        self.frame = self.crop_frame[1] if self.args.get(
            "video", False) else self.crop_frame

        # redimensiona a imagem, borra, transforma em HSV
        self.frame = imutils.resize(self.frame, width=300)
        blurred = cv2.GaussianBlur(self.frame, (11, 11), 0)
        hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)

        # mascara de cor para dilatar e remover ruidos do objetos
        mask = cv2.inRange(hsv, self.greenLower, self.greenUpper)
        mask = cv2.erode(mask, None, iterations=0)
        mask = cv2.dilate(mask, None, iterations=4)

        cv2.imshow("masked_main", mask)
        # find contours in the mask and initialize the current
        # (x, y) center of the ball
        cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)
        cnts = cnts[0] if imutils.is_cv2() else cnts[1]
        center = None

        # desenhar set points
        cv2.circle(self.frame, (33, 260),
                   20, (255, 0, 0),
                   thickness=5,
                   lineType=8,
                   shift=0)
        cv2.circle(self.frame, (169, 260),
                   10, (0, 0, 0),
                   thickness=5,
                   lineType=8,
                   shift=0)
        cv2.circle(self.frame, (225, 185),
                   10, (0, 0, 0),
                   thickness=5,
                   lineType=8,
                   shift=0)
        cv2.circle(self.frame, (280, 255),
                   10, (0, 0, 0),
                   thickness=5,
                   lineType=8,
                   shift=0)
        cv2.circle(self.frame, (280, 129),
                   10, (0, 0, 0),
                   thickness=5,
                   lineType=8,
                   shift=0)
        cv2.circle(self.frame, (105, 85),
                   10, (0, 0, 0),
                   thickness=5,
                   lineType=8,
                   shift=0)
        cv2.circle(self.frame, (25, 15),
                   10, (0, 0, 0),
                   thickness=5,
                   lineType=8,
                   shift=0)
        cv2.circle(self.frame, (285, 90),
                   10, (0, 0, 0),
                   thickness=5,
                   lineType=8,
                   shift=0)
        cv2.circle(self.frame, (275, 15),
                   20, (0, 0, 255),
                   thickness=5,
                   lineType=8,
                   shift=0)

        # only proceed if at least one contour was found
        if len(cnts) > 0:
            # find the largest contour in the mask, then use
            # it to compute the minimum enclosing circle and
            # centroid
            c = max(cnts, key=cv2.contourArea)
            ((x, y), radius) = cv2.minEnclosingCircle(c)
            M = cv2.moments(c)
            center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

            ballX = int(M["m10"] / M["m00"])
            ballY = int(M["m01"] / M["m00"])

            # X_string = str(int(M["m10"] / M["m00"]))
            # Y_string = str(int(M["m01"] / M["m00"]))
            # previous_errorY_str = str(int(previous_error_y))
            # previous_errorX_str = str(int(previous_error_x))

            # print(
            #    "posicao x:" + X_string + " " + "posicao y:" + Y_string + " " + "erroY: " + previous_errorY_str + " " + "erroX: " + previous_errorX_str)

            # mask2 = cv2.inRange(hsv, gL, gU)

            # only proceed if the radius meets a minimum size
            if radius > 10:
                # draw the circle and centroid on the frame,
                # then update the list of tracked points
                cv2.circle(self.frame, (int(x), int(y)), int(radius),
                           (0, 255, 255), 2)
                cv2.circle(self.frame, center, 5, (0, 0, 255), -1)

        # update the points queue
        self.pts.appendleft(center)

        # loop over the set of tracked points
        for i in range(1, len(self.pts)):
            # if either of the tracked points are None, ignore
            # them
            if self.pts[i - 1] is None or self.pts[i] is None:
                continue

            # otherwise, compute the thickness of the line and
            # draw the connecting lines
            thickness = int(np.sqrt(self.args["buffer"] / float(i + 1)) * 2.5)
            cv2.line(self.frame, self.pts[i - 1], self.pts[i], (0, 0, 255),
                     thickness)

        # show the frame to our screen
        cv2.imshow("Frame", self.frame)
        self.key = cv2.waitKey(1) & 0xFF
        self.posx = self.pixel2cm(ballX)
        self.posy = self.pixel2cm(ballY)

    def getPosx(self):
        return self.posx

    def getPosy(self):
        return self.posy

    # Conversao de pixel para cm
    def pixel2cm(self, pos):
        return pos / 100
Example #7
0
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help="path to the (optional) video file")
ap.add_argument("-b", "--buffer", type=int, default=64, help="max buffer size")
args = vars(ap.parse_args())
pts = deque(maxlen=args["buffer"])

cap = None

if not args.get("video", False):
    cap = VideoStream(src=0).start()
# otherwise, grab a reference to the video file
else:
    cap = cv2.VideoCapture(args["video"])
    cap.set(cv2.CAP_PROP_AUTOFOCUS, 0)  # turn the autofocus off
# allow the camera or video file to warm up
time.sleep(2.0)

# establish blob detector
params = cv2.SimpleBlobDetector_Params()
params2 = cv2.SimpleBlobDetector_Params()
params.filterByColor = 1
params.blobColor = 255
params2.filterByColor = 1
params2.blobColor = 255

params.filterByCircularity = 1
params.minCircularity = 0.1
params.maxCircularity = 1.0
Example #8
0
    def detect(self):
        # define the two output layer names for the EAST detector model that
        # we are interested in -- the first is the output probabilities and the
        # second can be used to derive the bounding box coordinates of text
        layerNames = [
            "feature_fusion/Conv_7/Sigmoid", "feature_fusion/concat_3"
        ]

        # load the pre-trained EAST text detector
        net = cv2.dnn.readNet(self.east)

        # if a video path was not supplied, grab the reference to the web cam
        if not self.videoFile:
            print("[INFO] starting video stream...")
            vs = VideoStream(src=0).start()
            time.sleep(1.0)
        # otherwise, grab a reference to the video file
        else:
            vs = cv2.VideoCapture(self.videoFile)

        w = vs.get(cv2.CAP_PROP_FRAME_WIDTH)
        h = vs.get(cv2.CAP_PROP_FRAME_HEIGHT)

        reshape_ratio = np.array(
            (w / float(self.reshapeW), h / float(self.reshapeH)))

        self.fps = vs.get(cv2.CAP_PROP_FPS)
        self.frames = int(vs.get(cv2.CAP_PROP_FRAME_COUNT))
        self.spf = 1.0 / self.fps

        print(
            f"[INFO] fps={self.fps}, frames={self.frames}, spf={self.spf}(seconds per frame)"
        )

        fps = FPS().start()

        while True:
            starttime = time.time()
            ret, frame = vs.read()
            reshaped_frame = cv2.resize(frame, (self.reshapeW, self.reshapeH))

            if not ret:
                break

            # construct a blob from the image and then perform a forward pass of
            # the model to obtain the two output layer sets
            blob = cv2.dnn.blobFromImage(reshaped_frame,
                                         1.0, (self.reshapeW, self.reshapeH),
                                         (123.68, 116.78, 103.94),
                                         swapRB=True,
                                         crop=False)
            net.setInput(blob)
            (scores, geometry) = net.forward(layerNames)

            # decode the predictions, then  apply non-maxima suppression to
            # suppress weak, overlapping bounding boxes
            (polys, confidences) = self.decode_predictions(scores, geometry)

            indexes = nms.nms.polygons(polys,
                                       confidences,
                                       nms_threshold=self.nms_threshold)

            if indexes:
                self.nmspolys = np.array(
                    np.array(polys)[indexes] * reshape_ratio, np.int32)
                self.nmsscores = np.array(confidences)[indexes]

                cv2.polylines(frame, self.nmspolys, True, (0, 0, 255), 1)
                for i in range(len(self.nmsscores)):
                    poly = self.nmspolys[i]
                    cv2.putText(frame, f"{self.nmsscores[i]*100:.2f}%",
                                (poly[0, 0], poly[0, 1] + 18),
                                cv2.FONT_HERSHEY_DUPLEX, 0.5, (0, 0, 255), 1)

            cv2.imshow(self.winName, frame)
            key = cv2.waitKey(1) & 0xFF
            fps.update()
            elapsed = time.time() - starttime
            if self.frameskip:
                vs.set(cv2.CAP_PROP_POS_FRAMES,
                       vs.get(cv2.CAP_PROP_POS_FRAMES) + elapsed // self.spf)
            # if 'q' key pressed, break from the loop
            if key == ord("q"):
                break

        fps.stop()
        print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
        print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
        # if we are using a webcam, release the pointer
        if not self.videoFile:
            vs.stop()

        # otherwise, release the file pointer
        else:
            vs.release()

        # close all windows
        cv2.destroyAllWindows()
    if event == cv2.EVENT_LBUTTONDOWN:
        pixel = image_hsv[y, x]
        upper = np.array([pixel[0] + 10, pixel[1] + 10, pixel[2] + 40])
        lower = np.array([pixel[0] - 10, pixel[1] - 10, pixel[2] - 40])
        print(pixel, lower, upper)


# if a video path was not supplied, grab the reference
# to the webcam
if not args.get("video", False):
    vs = VideoStream(src=0).start()

# otherwise, grab a reference to the video file
else:
    vs = cv2.VideoCapture(args["video"])
    vs.set(cv2.CAP_PROP_AUTO_WB, 0)
    vs.set(cv2.CAP_PROP_WB_TEMPERATURE, 2800)
    vs.set(cv2.CAP_PROP_BRIGHTNESS, 0)
    vs.set(cv2.CAP_PROP_CONTRAST, 32)
    vs.set(cv2.CAP_PROP_HUE, 0)
    vs.set(cv2.CAP_PROP_SATURATION, 150)
    vs.set(cv2.CAP_PROP_SHARPNESS, 2)
    vs.set(cv2.CAP_PROP_GAMMA, 100)
    vs.set(cv2.CAP_PROP_BACKLIGHT, 1)
    vs.set(cv2.CAP_PROP_GAIN, 0)
    vs.set(cv2.CAP_PROP_AUTO_EXPOSURE, .75)
    vs.set(cv2.CAP_PROP_EXPOSURE, 37)
    #print(vs.get(cv2.CAP_PROP_FRAME_WIDTH))
    #print(vs.get(cv2.CAP_PROP_FRAME_HEIGHT))

# allow the camera or video file to warm up
def trackObject(object, args):
    result = {}

    # extract the OpenCV version info
    (major, minor) = cv2.__version__.split(".")[:2]

    # if we are using OpenCV 3.2 OR BEFORE, we can use a special factory
    # function to create our object tracker
    if int(major) == 3 and int(minor) < 3:
        tracker = cv2.Tracker_create(args["tracker"].upper())

    # otherwise, for OpenCV 3.3 OR NEWER, we need to explicity call the
    # approrpiate object tracker constructor:
    else:
        # initialize a dictionary that maps strings to their corresponding
        # OpenCV object tracker implementations
        OPENCV_OBJECT_TRACKERS = {
            "csrt": cv2.TrackerCSRT_create,
            "kcf": cv2.TrackerKCF_create,
            "boosting": cv2.TrackerBoosting_create,
            "mil": cv2.TrackerMIL_create,
            "tld": cv2.TrackerTLD_create,
            "medianflow": cv2.TrackerMedianFlow_create,
            "mosse": cv2.TrackerMOSSE_create
        }

        # grab the appropriate object tracker using our dictionary of
        # OpenCV object tracker objects
        tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]()
    # tracker = cv2.Tracker_create(args["tracker"].upper())
    # initialize the bounding box coordinates of the object we are going
    # to track

    initBB = None

    # if a video path was not supplied, grab the reference to the web cam
    if not args.get("video", False):
        print("[INFO] starting video stream...")
        vs = VideoStream(src=0).start()
        time.sleep(1.0)

    # otherwise, grab a reference to the video file
    else:
        vs = cv2.VideoCapture(args["video"])

    fps = vs.get(cv2.CAP_PROP_FPS)
    # print("FPS: " + str(fps))

    vs.set(cv2.CAP_PROP_POS_FRAMES, object['time'] * fps)
    # print("Pos: ", object['time'] * fps)

    result['id'] = object['id']
    result['fps'] = fps
    result['start'] = int(object['time'] * fps)
    result['track'] = []
    # exit(0)

    # width of processing video
    pWidth = 200

    # initialize the FPS throughput estimator
    # fps = None

    # loop over frames from the video stream

    initBB = (object['location']['left'], object['location']['top'],
              object['location']['width'], object['location']['height'])

    width = vs.get(cv2.CAP_PROP_FRAME_WIDTH)
    height = vs.get(cv2.CAP_PROP_FRAME_HEIGHT)

    initBB = scaleRect(initBB, pWidth / width)

    frame = vs.read()
    oFrame = frame[1] if args.get("video", False) else frame
    frame = imutils.resize(oFrame, width=pWidth)
    tracker.init(frame, initBB)
    count = int(fps)
    # fps = FPS().start()
    while True:
        count = count + 1
        # grab the current frame, then handle if we are using a
        # VideoStream or VideoCapture object
        frame = vs.read()

        oFrame = frame[1] if args.get("video", False) else frame
        # resize the frame (so we can process it faster) and grab the
        # frame dimensions
        # check to see if we have reached the end of the stream
        if oFrame is None:
            break
        frame = imutils.resize(oFrame, width=pWidth)
        (H, W) = frame.shape[:2]

        # check to see if we are currently tracking an object

        if initBB is not None:  # grab the new bounding box coordinates of the object
            (success, box) = tracker.update(frame)

            # check to see if the tracking was a success
            if success:
                (x, y, w, h) = [int(v) for v in box]
                # print(str(count) + ": [" + str(x) + ", " + str(y) + ", " + str(w) + ", " + str(h) + "]")
                rect = scaleRect((x, y, w, h), width / pWidth)
                # print(str(count) + ": [" + str(rect[0]) + ", " + str(rect[1]) + ", " + str(rect[2]) + ", " + str(rect[3]) + "]")
                # cv2.rectangle(oFrame, (rect[0], rect[1]), (rect[0]+ rect[2], rect[1] + rect[3]), (0, 255, 0), 2)
                cv2.rectangle(
                    oFrame, (int(rect[0]), int(rect[1])),
                    (int(rect[0]) + int(rect[2]), int(rect[1]) + int(rect[3])),
                    (0, 255, 0), 2)
                result['track'].append([rect[1], rect[0], rect[2], rect[3]])
            else:
                break
            # update the FPS counters
            # fps.update()
            # fps.stop()

            # initialize the set of information we'll be displaying on
            # the frame
            info = [
                ("Tracker", args["tracker"]),
                ("Success", "Yes" if success else "No"),
                # ("FPS", "{:.2f}".format(fps.fps())),
            ]

            # loop over the info tuples and draw them on our frame
            for (i, (k, v)) in enumerate(info):
                text = "{}: {}".format(k, v)
                cv2.putText(oFrame, text, (10, H - ((i * 20) + 20)),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
        #
        # show the output frame
        cv2.imshow("Frame", oFrame)
        key = cv2.waitKey(1) & 0xFF  # do not remove this line

        # if the 's' key is selected, we are going to "select" a bounding
        # box to track
        # if key == ord("s"):
        # select the bounding box of the object we want to track (make
        # sure you press ENTER or SPACE after selecting the ROI)
        # initBB = cv2.selectROI("Frame", frame, fromCenter=False,
        #                        showCrosshair=True)

        # start OpenCV object tracker using the supplied bounding box
        # coordinates, then start the FPS throughput estimator as well
        # tracker.init(frame, initBB)
        # fps = FPS().start()

        # if the `q` key was pressed, break from the loop
        # elif key == ord("q"):
        #     break
    # print(count)
    # if we are using a webcam, release the pointer
    if not args.get("video", False):
        vs.stop()

    # otherwise, release the file pointer
    else:
        vs.release()

    # close all windows

    # print(result)
    totalResult.append(result)
Example #11
0
def mot_detect(args):
    def nothing(emp):
        pass

    print("----------> create the trackers")
    # extract the OpenCV version info
    (major, minor) = cv2.__version__.split(".")[:2]
    # if we are using OpenCV 3.2 OR BEFORE, we can use a special factory
    # function to create our object tracker
    if int(major) == 3 and int(minor) < 3:
        tracker = cv2.Tracker_create(args["tracker"].upper())
    # otherwise, for OpenCV 3.3 OR NEWER, we need to explicity call the
    # approrpiate object tracker constructor:
    else:
        # initialize a dictionary that maps strings to their corresponding
        # OpenCV object tracker implementations
        OPENCV_OBJECT_TRACKERS = {
            "csrt": cv2.TrackerCSRT_create,
            "kcf": cv2.TrackerKCF_create,
            "boosting": cv2.TrackerBoosting_create,
            "mil": cv2.TrackerMIL_create,
            "tld": cv2.TrackerTLD_create,
            "medianflow": cv2.TrackerMedianFlow_create,
            "mosse": cv2.TrackerMOSSE_create
        }
        # grab the appropriate object tracker using our dictionary of
        # OpenCV object tracker objects
        trackers = cv2.MultiTracker_create()
    # tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]()
    # initialize the bounding box coordinates of the object we are going
    # to track
    initBB = None
    tclass = []
    start_time = time.time()
    failure_flag = False

    # if a video path was not supplied, grab the reference to the web cam
    if not args.get("video", False):
        print("[INFO] starting video stream...")
        vs = VideoStream(src=0).start()
        time.sleep(1.0)
    # otherwise, grab a reference to the video file
    else:
        vs = cv2.VideoCapture(args["video"])
    # initialize the FPS throughput estimator
    fps = None

    frames = int(vs.get(cv2.CAP_PROP_FRAME_COUNT))
    loop_flag = 0
    pos = 0

    print("----------> read the frames")
    cv2.namedWindow('Frame')
    cv2.createTrackbar('time', 'Frame', 0, frames, nothing)

    # loop over frames from the video stream
    while True:
        # process-bar setting
        if loop_flag == pos:
            loop_flag = loop_flag + 1
            cv2.setTrackbarPos('time', 'Frame', loop_flag)
        else:
            pos = cv2.getTrackbarPos('time', 'Frame')
            loop_flag = pos
            vs.set(cv2.CAP_PROP_POS_FRAMES, pos)

        # grab the current frame, then handle if we are using a
        # VideoStream or VideoCapture object
        frame = vs.read()
        frame = frame[1] if args.get("video", False) else frame
        # check to see if we have reached the end of the stream
        if frame is None:
            break
        # resize the frame (so we can process it faster) and grab the
        # frame dimensions
        frame = imutils.resize(frame,
                               width=int(args["set_width"]),
                               height=int(args["set_height"]))
        frame0 = frame.copy()
        (H, W) = frame.shape[:2]

        # check to see if we are currently tracking an object
        if initBB is not None:
            print("----------> update the trackers' roi area")
            # grab the new bounding box coordinates of the object
            (success, boxes) = trackers.update(frame)
            print("[INFO] success / box num", success, len(boxes))

            # check to see if the tracking was a success
            if success:
                tpos = []
                for num, box in enumerate(boxes):
                    (x, y, w, h) = [int(v) for v in box]
                    new_pos = [x, y, x + w, y + h]
                    tpos.append(new_pos)
                    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0),
                                  2)
                    cv2.putText(frame, tclass[num], (x, y - 10),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
                now_time = time.time()
                if now_time - start_time > float(
                        args["interval"]) and len(boxes) > 0:
                    start_time = now_time
                    pic_name = str(time.time()) + ".jpg"
                    pic_fullpath = Path(args["pic_dir"]).joinpath(pic_name)
                    print("[INFO] save new pic:", pic_fullpath)
                    r = Image.fromarray(frame0[:, :, 2]).convert('L')
                    g = Image.fromarray(frame0[:, :, 1]).convert('L')
                    b = Image.fromarray(frame0[:, :, 0]).convert('L')
                    img = Image.merge("RGB", (r, g, b))
                    print("----------> save the pic and annotation xml")
                    img.save(pic_fullpath)
                    annotation_single_img(args["pic_dir"], pic_name,
                                          args["xml_dir"], tclass, tpos)
                # update the FPS counter
                fps.update()
                fps.stop()
                # initialize the set of information we'll be displaying on
                # the frame
                info = [
                    ("Tracker", args["tracker"]),
                    ("Success", "Yes" if success else "No"),
                    ("FPS", "{:.2f}".format(fps.fps())),
                ]
                # loop over the info tuples and draw them on our frame
                for (i, (k, v)) in enumerate(info):
                    text = "{}: {}".format(k, v)
                    cv2.putText(frame, text, (10, H - ((i * 20) + 20)),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
            else:
                trackers.clear()
                trackers = cv2.MultiTracker_create()
                failure_flag = True
                initBB = None
                tclass = []
                cv2.putText(frame, lost_warning, (10, 100),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
        # show the output frame
        cv2.imshow("Frame", frame)
        if failure_flag:
            cv2.waitKey(5000) & 0xFF
            failure_flag = False
        else:
            key = cv2.waitKey(100) & 0xFF
        # if the 's' key is selected, we are going to "select" a bounding
        # box to track
        if key == ord("s"):
            print("----------> select roi by mouse")
            cv2.putText(frame, input_cmd, (10, 20), cv2.FONT_HERSHEY_SIMPLEX,
                        0.6, (0, 0, 255), 2)
            cv2.imshow("Frame", frame)
            # show the classes choice
            ckey = cv2.waitKey(0) & 0xFF
            if int(ckey - 48) > len(select_classes) or int(ckey - 48) <= 0:
                continue
            cname = select_classes[int(ckey - 48)]
            print("[INFO] choose type to label:", cname)
            # select the bounding box of the object we want to track (make
            # sure you press ENTER or SPACE after selecting the ROI)
            initBB = cv2.selectROIs("Frame",
                                    frame,
                                    fromCenter=False,
                                    showCrosshair=True)

            # start OpenCV object tracker using the supplied bounding box
            # coordinates, then start the FPS throughput estimator as well
            initBB = tuple(map(tuple, initBB))
            if str(initBB) == '()':
                print("[WARNING] There is no select ROIs!")
                # initBB==None
            else:
                for bb in initBB:
                    tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]()
                    trackers.add(tracker, frame, bb)
                    tclass.append(cname)

            fps = FPS().start()

        # if the `q` key was pressed, break from the loop
        elif key == ord("q"):
            print("----------> quit all process")
            break

        elif key == ord("r"):
            print("----------> clear all roi trackers")
            tclass = []
            trackers.clear()
            trackers = cv2.MultiTracker_create()
            cv2.putText(frame, input_cmd, (10, 20), cv2.FONT_HERSHEY_SIMPLEX,
                        0.6, (0, 0, 255), 2)
            cv2.imshow("Frame", frame)
            # show the classes choice
            ckey = cv2.waitKey(0) & 0xFF
            cname = select_classes[int(ckey - 48)]
            print("[INFO]You have chosen the class:%s" % cname)
            initBB = cv2.selectROIs("Frame",
                                    frame,
                                    fromCenter=False,
                                    showCrosshair=True)
            initBB = tuple(map(tuple, initBB))
            if str(initBB) == '()':
                print("[WARNING] There is no select ROIs!")
                # initBB==None
            else:
                print("---------->add new roi trackers")
                for bb in initBB:
                    tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]()
                    trackers.add(tracker, frame, bb)
                    tclass.append(cname)
        # else:
        #     continue

    # if we are using a webcam, release the pointer
    if not args.get("video", False):
        vs.stop()
    # otherwise, release the file pointer
    else:
        vs.release()
    # close all windows
    cv2.destroyAllWindows()
Example #12
0
    print("[INFO] starting video stream...")
    vs = VideoStream(src=0).start()
    time.sleep(2.0)

# otherwise, grab a reference to the video file
else:
    print("[INFO] opening video file...")
    vs = cv2.VideoCapture(args["input"])

# initialize the video writer (we'll instantiate later if need be)
writer = None

a = 800
l = 800

vs.set(cv2.CAP_PROP_POS_MSEC, 20)  # just cue to 2 sec. position
#success,image = vs.read()
#if success:
#cv2.imwrite("frame20sec.png", image)     # save frame as JPEG file
#cv2.imshow("20sec",image)
#cv2.waitKey()
# initialize the frame dimensions (we'll set them as soon as we read
# the first frame from the video)
W = None
H = None

# instantiate our centroid tracker, then initialize a list to store
# each of our dlib correlation trackers, followed by a dictionary to
# map each unique object ID to a TrackableObject
ct = CentroidTracker(maxDisappeared=40, maxDistance=50)
trackers = []
        help="max buffer size")
args = vars(ap.parse_args())

greenLower = (29, 86, 6)
greenUpper = (64, 255, 255)
yellowLower = (30, 60, 165)
yellowUpper = (40, 120, 255)
redLower = (0, 50, 50)
redUpper = (10, 255, 255)
pts = deque(maxlen=args["buffer"])

if not args.get("video", False):
        vs = VideoStream(src=0).start()
else:
        vs = cv2.VideoCapture(args["video"])
        vs.set(3, 600) // codes for pi zero
        vs.set(4, 600) // codes for pi zero
time.sleep(2.0)

while True:
  frame = vs.read()
  frame = frame[1] if args.get("video", False) else frame
  if frame is None:
    break
  #frame = imutils.resize(frame, width=600) ?????why => imutils cannot be install in pi zero
  blurred = cv2.GaussianBlur(frame, (11, 11), 0)
  hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
  
  mask = cv2.inRange(hsv, redLower, redUpper)
  mask = cv2.erode(mask, None, iterations=2)
  mask = cv2.dilate(mask, None, iterations=2)
Example #14
0
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])

# initialize the video stream, allow the cammera sensor to warmup,
# and initialize the FPS counter

#To use the webcam set this variable to 1
web_cam = 1
print("[INFO] starting video stream...")
if web_cam not in [0]:
    vs = VideoStream(src=0).start()
    time.sleep(2.0)
    fps = FPS().start()
else:
    vs = cv2.VideoCapture('video3.mp4')
    vs.set(cv2.CAP_PROP_FPS, 5)
#
# loop over the frames from the video stream
frame_count = 0
people = 0
vehicles = 0
t1 = time.clock()
while True:
    # grab the frame from the threaded video stream and resize it
    # to have a maximum width of 400 pixels
    t2 = time.clock() - t1
    if t2 >= 60:
        print("Number of pedestrians in one minute =", people)
        print(" Number of vehicles in one minute =", vehicles)
        print("Number of frames processed=", frame_count)
        accident = random.randint(0, 2)
Example #15
0
ap.add_argument("-b", "--buffer", type=int, default=64, help="max buffer size")
args = vars(ap.parse_args())

greenLower = (29, 86, 6)
greenUpper = (64, 255, 255)
yellowLower = np.array([30, 60, 165], dtype=np.uint8)
yellowUpper = np.array([40, 120, 255], dtype=np.uint8)
redLower = (0, 50, 50)
redUpper = (10, 255, 255)
pts = deque(maxlen=args["buffer"])

if not args.get("video", False):
    vs = VideoStream(src=0).start()
else:
    vs = cv2.VideoCapture(args["video"])
    vs.set(3, 600)
    vs.set(4, 600)
time.sleep(2.0)


def move_car(x, y, radius):
    if x > 400:
        period = float((x - 300) / (300 * 5))
        os.system("python motorcontrol2.py pright " + str(period))
        print period
    elif x < 200:
        period = float((300 - x) / (300 * 5))
        os.system("python motorcontrol2.py pleft " + str(period))
        print period
    elif ((200 <= x <= 400) and radius < 80):
        os.system("python motorcontrol2.py go " + str(0.3))
Example #16
0
# import matplotlib.pyplot as plt

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p",
                "--picamera",
                type=int,
                default=-1,
                help="whether or not the Raspberry Pi camera should be used")
args = vars(ap.parse_args())

# initialize the video stream and allow the cammera sensor to warmup
cap = VideoStream(usePiCamera=args["picamera"] > 0).start()
time.sleep(2.0)
'''
cap = cv2.VideoCapture(0)
cap.set(3,WIDTH)
cap.set(4,HEIGHT)
'''
last = 0

firstRun = True

time.sleep(2.0)
'''
for i in range(10):
	ret, frame = cap.read()
'''
frame = cap.read()
#frame=cv2.resize(frame, (WIDTH,int(HEIGHT*frame.shape[0]/frame.shape[1])))


      # # Load model
  clf = None

  clf = joblib.load(r"C:\Users\Navya\Desktop\major\replay-attack_ycrcb_luv_extraTreesClassifier.pkl")

  # # Open the camera

  cap = cv2.VideoCapture(0)
    

  width = 320
  height = 240
  cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
  cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)

  # # Initialize face detector
  #cascPath = "C:\Users\Navya\Desktop\major\haarcascade_frontalface_default.xml"
  faceCascade = cv2.CascadeClassifier(r"C:\Users\Navya\Desktop\major\haarcascade_frontalface_default.xml")

  sample_number = 1
  count = 0
  measures = np.zeros(sample_number, dtype=np.float)

  while True:
      ret, img_bgr = cap.read()
      if ret is False:
          print ("Error grabbing frame from camera")
          break
Example #18
0
# initialize the bounding box coordinates of the object we are going
# to track
initBB = None

# if a video path was not supplied, grab the reference to the web cam
if not args.get("video", False):
    print("[INFO] starting video stream...")
    vs = VideoStream(src=0).start()
    time.sleep(1.0)

# otherwise, grab a reference to the video file
else:
    vs = cv2.VideoCapture(args["video"])

vs.set(cv2.CAP_PROP_FPS, 30)

# initialize the FPS throughput estimator
fps = None

start = time.time()
# loop over frames from the video stream
while True:
    # grab the current frame, then handle if we are using a
    # VideoStream or VideoCapture object
    frame = vs.read()
    frame = frame[1] if args.get("video", False) else frame

    # check to see if we have reached the end of the stream
    if frame is None:
         print("Video finished. Press any key to continue.")
Example #19
0
def main(args, total=0):

    WIDTH = 300
    HEIGHT = 300

    try:
        # direction going in
        in_direction = MoveDirection(args["direction"].lower())
    except:
        raise ValueError(
            "Only 'up' or 'down', 'left', 'right' directions are supported")

    centroid_idx = get_horiz_vert(in_direction).value
    is_visual = args["visual"]

    # mask used to indicate the "in" direction
    if is_visual:
        mask = np.zeros((HEIGHT, WIDTH, 3)).astype('uint8')
        w = WIDTH // 2
        h = HEIGHT // 2

        if in_direction == MoveDirection.LEFT:
            mask[:, :w, 2] = 255
        elif in_direction == MoveDirection.RIGHT:
            mask[:, w:, 2] = 255
        elif in_direction == MoveDirection.DOWN:
            mask[h:, :, 2] = 255
        else:
            mask[:h, :, 2] = 255

    # store or ignore the count we receive in the reset request
    store_count = args['set_count']

    if args["debug"]:
        logging.info("Please attach a debugger to port 5680")
        import ptvsd
        ptvsd.enable_attach(('0.0.0.0', 5680))
        ptvsd.wait_for_attach()
        ptvsd.break_into_debugger()

    if args["id"] is None:
        raise ValueError("id must be specified")

    global running, sess

    # load our serialized model from disk
    logging.info("loading model...")

    detector_type = args["detector"]
    if detector_type == "opencv":
        detector = DetectorCV(args["model"],
                              args["prototxt"],
                              confidence_thresh=args["confidence"])
    elif detector_type == "onnx":
        detector = DetectorOnnx(args["model"])
    else:
        raise ValueError(
            f"Unkonwn detector: {args['detector']}. Use 'opencv' or 'onnx'")

    is_rtsp = args["input"] is not None and args["input"].startswith("rtsp://")
    source = "prod" if is_rtsp else "test"

    # if a video path was not supplied, grab a reference to the webcam
    logging.info("starting video stream...")
    if not args.get("input", False):
        vs = VideoStream(src=0).start()
        time.sleep(2.0)

    # otherwise, grab a reference to the video file
    else:
        if (not is_rtsp and not os.path.exists(args["input"])):
            raise FileNotFoundError(args["input"])

        vs = cv2.VideoCapture(args["input"])
        if is_rtsp:
            vs.set(cv2.CAP_PROP_BUFFERSIZE, 600)

    # initialize the video writer (we'll instantiate later if need be)
    writer = None

    # initialize the frame dimensions (we'll set them as soon as we read
    # the first frame from the video)
    W = None
    H = None

    # instantiate our centroid tracker, then initialize a list to store
    # each of our dlib correlation trackers, followed by a dictionary to
    # map each unique object ID to a TrackableObject
    centroidTracker = CentroidTracker(maxDisappeared=40, maxDistance=50)
    trackers = []
    trackableObjects = {}

    # initialize the total number of frames processed thus far, along
    # with the total number of objects that have moved either up or down
    totalFrames = 0
    totalDown = total if store_count and (
        in_direction == MoveDirection.DOWN
        or in_direction == MoveDirection.RIGHT) else 0
    totalUp = total if store_count and (
        in_direction == MoveDirection.UP
        or in_direction == MoveDirection.LEFT) else 0

    # detailed counters of foot traffic
    currentIn = 0
    currentOut = 0

    # report counts from this camera
    messageEvery = args["report_count"]

    # start the frames per second throughput estimator
    fps = FPS().start()

    # loop over frames from the video stream
    while True:
        # grab the next frame and handle if we are reading from either
        # VideoCapture or VideoStream
        frame = vs.read()
        frame = frame[1] if args.get("input", False) else frame

        # if we are viewing a video and we did not grab a frame then we
        # have reached the end of the video
        if messenger.should_reset() or (args["input"] is not None
                                        and frame is None):
            logging.debug("We are DONE!")
            break

        # resize the frame to have a maximum width of 500 pixels (the
        # less data we have, the faster we can process it), then convert
        # the frame from BGR to RGB for dlib
        frame = cv2.resize(frame, (WIDTH, HEIGHT),
                           interpolation=cv2.INTER_LINEAR)
        rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        # if the frame dimensions are empty, set them
        if W is None or H is None:
            (H, W) = frame.shape[:2]
            # count the object when it's crossing either mid-height or mid-width
            crossingDimension = get_dir_dimension(in_direction, W, H) // 2

        # if we are supposed to be writing a video to disk, initialize
        # the writer
        if args["output"] is not None and writer is None:
            fourcc = cv2.VideoWriter_fourcc(*"MJPG")
            writer = cv2.VideoWriter(args["output"], fourcc, 30, (W, H), True)

        # initialize the current status along with our list of bounding
        # box rectangles returned by either (1) our object detector or
        # (2) the correlation trackers
        status = "Waiting"
        rects = []

        # check to see if we should run a more computationally expensive
        # object detection method to aid our tracker
        if totalFrames % args["skip_frames"] == 0:
            # set the status and initialize our new set of object trackers
            status = "Detecting"
            trackers = []
            detections = detector.detect(frame)

            for startX, startY, endX, endY in detections:
                # construct a dlib rectangle object from the bounding
                # box coordinates and then start the dlib correlation
                # tracker
                cv2.rectangle(frame, (startX, startY), (endX, endY),
                              (255, 0, 0), 2)

                tracker = dlib.correlation_tracker()
                rect = dlib.rectangle(startX, startY, endX, endY)
                tracker.start_track(rgb, rect)

                # add the tracker to our list of trackers so we can
                # utilize it during skip frames
                trackers.append(tracker)

        # otherwise, we should utilize our object *trackers* rather than
        # object *detectors* to obtain a higher frame processing throughput
        else:
            # loop over the trackers
            for tracker in trackers:
                # set the status of our system to be 'tracking' rather
                # than 'waiting' or 'detecting'
                status = "Tracking"
                cv2.rectangle(frame, (startX, startY), (endX, endY),
                              (255, 0, 0), 2)

                # update the tracker and grab the updated position
                tracker.update(rgb)
                pos = tracker.get_position()

                # unpack the position object
                startX = int(pos.left())
                startY = int(pos.top())
                endX = int(pos.right())
                endY = int(pos.bottom())

                # add the bounding box coordinates to the rectangles list
                rects.append((startX, startY, endX, endY))

        # draw a horizontal line in the center of the frame -- once an
        # object crosses this line we will determine whether they were
        # moving 'up' or 'down'
        if is_visual:
            if get_horiz_vert(in_direction) == CountDirection.VERTICAL:
                cv2.line(frame, (0, H // 2), (W, H // 2), (0, 255, 255), 2)
            else:
                cv2.line(frame, (W // 2, 0), (W // 2, H), (0, 255, 255), 2)

        # use the centroid tracker to associate the (1) old object
        # centroids with (2) the newly computed object centroids
        objects = centroidTracker.update(rects)

        # loop over the tracked objects
        for (objectID, centroid) in objects.items():
            # check to see if a trackable object exists for the current
            # object ID
            trackableObject = trackableObjects.get(objectID, None)

            if trackableObject is None:
                trackableObject = TrackableObject(objectID, centroid)

            # otherwise, there is a trackable object so we can utilize it
            # to determine direction
            else:
                # where have we seen it last?
                xy = trackableObject.centroids[-1][centroid_idx]
                # see if we need to count it.
                # we count iff the centroid crossed the mid-line since its last known position
                direction = get_trigger_count(xy, centroid[centroid_idx],
                                              crossingDimension)

                trackableObject.centroids.append(centroid)

                # if the direction is negative (indicating the object
                # is moving up/left) AND the centroid is above the center
                # line, count the object
                if direction < 0:
                    totalUp += 1
                    currentOut += 1

                # if the direction is positive (indicating the object
                # is moving down/right) AND the centroid is below the
                # center line, count the object
                elif direction > 0:
                    totalDown += 1
                    currentIn += 1

            # store the trackable object in our dictionary
            trackableObjects[objectID] = trackableObject

            # draw both the ID of the object and the centroid of the
            # object on the output frame
            if is_visual:
                text = "ID {}".format(objectID)
                cv2.putText(frame, text, (centroid[0] - 10, centroid[1] - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
                cv2.circle(frame, (centroid[0], centroid[1]), 4, (0, 255, 0),
                           -1)

        # up or down counting - based on what we have parameterized
        total = totalDown - totalUp

        if in_direction == MoveDirection.UP or in_direction == MoveDirection.LEFT:
            total = -total
            # swap current "in" and "out" counters depending on direction
            tmp = currentIn
            currentIn = currentOut
            currentOut = tmp

        messenger.update_count(total)

        if totalFrames % messageEvery == 0:
            # messenger has been initialized with resettableCount
            messenger.send_count()

            # send current "in" and "out" foot traffic and update direction
            messenger.send_traffic_details(currentIn, currentOut, source)

            currentIn = currentOut = 0

        if is_visual:
            # construct a tuple of information we will be displaying on the
            # frame
            up, down = get_cur_direction_names(in_direction)

            info = [
                (up, totalUp),
                (down, totalDown),
                ("Total", total),
                ("Status", status),
            ]

            # loop over the info tuples and draw them on our frame
            for (i, (k, v)) in enumerate(info):
                text = "{}: {}".format(k, v)
                cv2.putText(frame, text, (10, H - ((i * 20) + 20)),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)

        # check to see if we should write the frame to disk
        if writer is not None:
            writer.write(frame)

        if is_visual:
            img = cv2.addWeighted(frame, 0.8, mask, 0.2, 0)
            # show the output frame
            cv2.imshow("Frame", img)
            key = cv2.waitKey(1) & 0xFF

            # if the `q` key was pressed, break from the loop
            # and completely stop running
            if key == ord("q"):
                running = False
                break

        # increment the total number of frames processed thus far and
        # then update the FPS counter
        totalFrames += 1
        fps.update()

    # stop the timer and display FPS information
    fps.stop()
    logging.info("elapsed time: {:.2f}".format(fps.elapsed()))
    logging.info("approx. FPS: {:.2f}".format(fps.fps()))

    # check to see if we need to release the video writer pointer
    if writer is not None:
        writer.release()

    # if we are not using a video file, stop the camera video stream
    if not args.get("input", False):
        vs.stop()

    # otherwise, release the video file pointer
    else:
        vs.release()