Exemplo n.º 1
0
        sys.stdout = open(logfile, "w")

    stream = NetVideoStream(args.server_ip, args.server_port, args.image_width,
                            args.image_height, args.image_channels)
    if not stream.start():
        print("[ERROR] could not connect to Server...")
        exit(2)

    signal.signal(signal.SIGUSR1, signal_handler)

    result_video_writer = None
    result_video_queue = None
    if args.create_result_video:
        result_video_queue = Queue(maxsize=args.video_queue_size)
        result_video_writer = VideoWriter(args.image_width, args.image_height,
                                          args.video_folder,
                                          result_video_queue)

    result_q = Queue(maxsize=args.result_queue_size)

    result_server = ResultServer(args.client_port, result_q)
    result_server.start()

    config = tools.load_config(args.config)
    od = ObjectDetection(config, args.image_width, args.image_height,
                         args.gpu_usage, args.threshold).start_up()

    dt = t.time()
    fps_dt = t.time()
    fps = FPS().start()
Exemplo n.º 2
0
            cv2.imshow("retail_out", image)
            cv2.waitKey(1)


t = Thread(target=read)
t.start()

thread1 = Thread(target=infer_yolo, args=(
    timestamp,
    margin,
    points,
))
thread1.start()

video_writer = VideoWriter(
    out_path.get() +
    "/t_mobile_demo_out_{}.avi".format(time.strftime('%Y_%m_%d_%H_%M')),
    image.shape[1], image.shape[0], 25)

while True:
    # print(detector_op.is_closed())
    trk_op.wait()
    if trk_ip.is_closed():
        # print("Here")
        video_writer.finish()
        break
    ret, inference = trk_op.pull()
    if ret:
        trackers = inference.get_result()
        frame = inference.get_input().get_image()
        patches = inference.get_data()
        # trails = inference.get_meta_dict()['trails']
    def start():
        try:

            # ssdmobilenet v2 labels
            LABELS = config.LABELS
            # face recognition labels
            le = pickle.loads(open(config.LABEL_PATH, 'rb').read())
            #classes to be detected
            LIST_CLASSES = 'person'.split(',')
            # recognizer label (svm model)
            recognizer = pickle.loads(open(config.MODEL_PATH, 'rb').read())
            # load face features exractor from disk
            embedder = FeatureExtractor(config.EMBEDDING_MODEL_PATH)
            # load face recognizer from disk
            faceDetector = FaceDetector(config.FACE_DECTOR_PATH)
            # load pretrained ssdmobilenet from disk
            pedestrianDetector = PedestrianDetector(
                config.SSD_MOBILENET_V2_PATH,
                faceDetector.detector.device_path())

            # trackers
            #load deepsort model from disk
            encoder = gdet.create_box_encoder(config.TRACKER_MODEL_PATH,
                                              batch_size=1)
            #instatiate metrics
            metric = nn_matching.NearestNeighborDistanceMetric(
                'cosine', args['cosine_distance'], None)
            # instatiate tracker
            tracker = Tracker(metric)

            if not args.get('input', False):
                cap = cv2.VideoCapture(1)
            else:
                cap = cv2.VideoCapture(args['input'])

            # instatiate video writer
            videoWriter = VideoWriter(args['output'],
                                      cap.get(cv2.CAP_PROP_FPS))

            # let's declare some variable
            H, W = None, None
            # objects dict holds different ids and their corresponding class name
            objects = {}
            # holds the tracked objects and the number of consecutive frames they have been marked as disappeared
            disappearedIds = {}
            # total frame counts will be used to compute average fps
            frameCounts = 0
            # frames determine number of frame  allows for an object to be disappeared
            #frames = 0
            # track object id
            trackedOjects = []
            # video writer process
            process = None
            # inference start time
            startTime = datetime.datetime.now()

            while cap.isOpened():
                grabbed, frame = cap.read()
                # stop while loop if is there is no frame available
                if not grabbed:
                    break
                #stop while loop if q is pressed
                key = cv2.waitKey(1)
                if key == ord('q'):
                    break

                if H is None or W is None:
                    H, W = frame.shape[:2]

                # start video writer process
                if args['output'] and process is None:
                    writeVideo = Value('i', 1)
                    frameQueue = Queue()
                    process = Process(target=videoWriter.start,
                                      args=(writeVideo, frameQueue, H, W))
                    process.start()
                if frameCounts % 3 == 0:
                    results = pedestrianDetector.detect(
                        frame, args['confidence'])
                bboxes = []
                scores = []
                #frames += 1
                #loop over detection results
                for r in results:
                    # label index
                    label_id = int(r.label_id + 1)
                    # check if detected object is in the list of the object we are interrested in.
                    if label_id in LABELS.keys(
                    ) and LABELS[label_id] in LIST_CLASSES:
                        # scale bounding box
                        bbox = r.bounding_box.flatten() * np.array(
                            [W, H, W, H])
                        bbox = bbox.astype('int')
                        # add object bounding box to bboxes
                        bboxes.append(bbox)
                        # add detection confidence to scores list
                        scores.append(r.score)

                bboxes = np.array(bboxes)
                scores = np.array(scores)
                if frameCounts % 3 == 0:
                    #grab feartures from frame
                    features = encoder(frame, bboxes)
                    # perform deep sort detection
                    detections = [
                        Detection(bbox, score, feature)
                        for bbox, score, feature in zip(
                            bboxes, scores, features)
                    ]
                    # grab boxes, scores, and classes_name from deep sort detections
                    pedestrianBoxes = np.array([d.tlwh for d in detections])
                    pedestrianScores = np.array(
                        [d.confidence for d in detections])
                    #perform non-maxima suppression on deep sort detections
                    indexes = preprocessing.non_max_suppression(
                        pedestrianBoxes, args['nms_threshold'],
                        pedestrianScores)
                    detections = [detections[i] for i in indexes]
                    # update tracker
                    tracker.predict()
                    tracker.update(detections)
                # loop over tracked objects

                for track in tracker.tracks:
                    if not track.is_confirmed() or track.time_since_update > 1:
                        continue

                    pedestrianBox = track.to_tlbr()
                    startX, startY, endX, endY = int(pedestrianBox[0]), int(
                        pedestrianBox[1]), int(pedestrianBox[2]), int(
                            pedestrianBox[3])
                    trackedOjects.append(track.track_id)

                    # Perfrom face detection only if there is no object in objects dict or current track id is not objects keys
                    if len(objects) == 0 or track.track_id not in objects.keys(
                    ):
                        faces = faceDetector.detect(frame, .3)
                        # loop over detected faces
                        for face in faces:
                            bbox = face.bounding_box.flatten() * np.array(
                                [W, H, W, H])
                            xmin, ymin, xmax, ymax = bbox.astype('int')
                            # check if detexcted face bboxe is inside current pedestrian bbox
                            isOverlap = helper.overlap(
                                (startX, startY, endX, endY),
                                (xmin, ymin, xmax, ymax))
                            #
                            if isOverlap:
                                # grab ROI (face in our case) and then pass it to face feature extractor
                                roi = frame[ymin:ymax, xmin:xmax]
                                vector = embedder.run(roi)
                                # preprocess the feature vector extracted
                                vector = np.expand_dims(vector, axis=0)
                                # pass the preprocessed vector to perform prediction us trained ML model (SVM in our case)
                                proba = recognizer.predict_proba(vector)
                                ind = np.argmax(proba[0])
                                # filter out weak face detection
                                if proba[0][ind] > .6:
                                    label = le.classes_[ind]
                                    # perform list comprehension to avoid two different ids using same name.
                                    if label in objects.values():
                                        #and track.track_id not in objects.keys():
                                        #del objects[track.track_id]
                                        objects = {
                                            k: v
                                            for k, v in objects.items()
                                            if v != label
                                        }
                                        continue

                                    if label in objects.values(
                                    ) and track.track_id in objects.keys():
                                        helper.drawBox(
                                            frame,
                                            (startX, startY, endX, endY),
                                            label, (0, 255, 0))

                                    else:
                                        cv2.rectangle(frame, (xmin, ymin),
                                                      (xmax, ymax),
                                                      (0, 255, 0), 2)
                                        helper.drawBox(
                                            frame,
                                            (startX, startY, endX, endY),
                                            label, (0, 255, 0))

                                    # add ids to objects
                                    objects[track.track_id] = label
                                    # set number of disappear for current track id  to 0
                                    disappearedIds[track.track_id] = 0

                    else:
                        # tracked already recognized people
                        for key, name in objects.items():
                            if key == track.track_id:
                                disappearedIds[track.track_id] = 0
                                helper.drawBox(frame,
                                               (startX, startY, endX, endY),
                                               name, (0, 255, 0))
                # copy  disappearedIds dict and increase the number of disappeared for a specific tracked object
                idsCopy = disappearedIds.copy()
                for i in idsCopy:
                    if i not in trackedOjects:
                        disappearedIds[i] += 1
                    #delete a given object if its number of dissapeared reached the maximum.
                    # 300 is the number of maximum consecutive frame a given tracked id (object) is allowed to be marked as disappeared
                    #until it is deleted from tracking.
                    if disappearedIds[i] == 300:
                        del disappearedIds[i]
                        if i in objects.keys():
                            del objects[i]
                        print(objects)

                trackedOjects = []

                font = cv2.FONT_HERSHEY_SIMPLEX

                elips = (datetime.datetime.now() - startTime).total_seconds()
                frameCounts += 1
                fps = frameCounts / elips
                text = 'Average FPS: {:.2f}'.format(fps)
                cv2.putText(frame, text, (10, 20), font, 0.5, (0, 255, 0), 2)
                # put frame in queue
                if process is not None:
                    frameQueue.put(frame)

                cv2.imshow('Face Recognition and Pedestrian Tracking', frame)
            # stop writting process
            if process is not None:
                writeVideo.value = 0
                process.join()

            cap.release()
            cv2.destroyAllWindows()

        except Exception as e:
            raise e
Exemplo n.º 4
0
            continue
        detector_ip.push(Inference(image.copy()))
        # print('waiting')
        detector_op.pull_wait()
        # print('done')
        ret, inference = detector_op.pull()
        if ret:
            i_dets = inference.get_result()
            trk_ip.push(Inference(i_dets))
        # sleep(0.1)


t = Thread(target=read)
t.start()

video_writer = VideoWriter(out_path.get() + "/t_mobile_demo_out_4.avi",
                           image.shape[1], image.shape[0], 25)

while True:
    # print(detector_op.is_closed())
    trk_op.pull_wait()
    if trk_ip.is_closed():
        # print("Here")
        video_writer.finish()
        break
    ret, inference = trk_op.pull()
    if ret:
        trackers = inference.get_result()
        frame = inference.get_input().get_image()

        for trk in trackers:
            if not trk.is_confident():