예제 #1
0
def main(yolo):

    source = 'gst.jpg'  # 0 or youtube or jpg
    URL = 'https://youtu.be/OI802VvUN38'

    FLAGScsv = 1

    if FLAGScsv:
        csv_obj = save_csv()
        num_a2b, num_b2a = csv_obj.startday()  #read old count from csv file

    else:
        num_a2b = 0
        #start from zero
        num_b2a = 0

    ina_old = set()
    ina_now = set()
    inb_old = set()
    inb_now = set()
    num_a2b_old = 0
    num_b2a_old = 0
    a2b_old = set()
    b2a_old = set()
    i = 0

    points = []
    tpro = 0.
    # Definition of the parameters
    max_cosine_distance = 0.1
    nn_budget = None
    nms_max_overlap = 1.0

    # deep_sort
    model_filename = 'model_data/mars-small128.pb'
    encoder = gdet.create_box_encoder(model_filename, batch_size=1)

    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    tracker = Tracker(metric)

    if source == 'youtube':

        width = 854
        height = 480
        sp.call(["youtube-dl", "--list-format", URL])
        run = sp.Popen(["youtube-dl", "-f", "94", "-g", URL], stdout=sp.PIPE)
        VIDM3U8, _ = run.communicate()

        VIDM3U8 = str(VIDM3U8, 'utf-8')
        VIDM3U8 = "".join(("hls://", str(VIDM3U8)))

        p1 = sp.Popen([
            'streamlink', '--hls-segment-threads', '10', VIDM3U8, 'best', '-o',
            '-'
        ],
                      stdout=sp.PIPE)
        p2 = sp.Popen([
            'ffmpeg', '-i', '-', '-f', 'image2pipe', "-loglevel", "quiet",
            "-pix_fmt", "bgr24", "-vcodec", "rawvideo", '-r', '10', "-"
        ],
                      stdin=p1.stdout,
                      stdout=sp.PIPE)

    else:
        video_capture = cv2.VideoCapture(source)

    print('video source : ', source)

    out = cv2.VideoWriter('outpy.avi',
                          cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10,
                          (608, 608))

    #  ___________________________________________________________________________________________________________________________________________MAIN LOOP
    while True:

        # get 1 frame

        if source == 'youtube':

            raw_frame = p2.stdout.read(width * height * 3)
            frame = np.fromstring(raw_frame, dtype='uint8').reshape(
                (height, width, 3))

        elif source == 'gst.jpg':
            try:
                img_bin = open('gst.jpg', 'rb')
                buff = io.BytesIO()
                buff.write(img_bin.read())
                buff.seek(0)
                temp_img = numpy.array(Image.open(buff), dtype=numpy.uint8)
                frame = cv2.cvtColor(temp_img, cv2.COLOR_RGB2BGR)
            except OSError:
                continue
            except TypeError:
                continue

        else:
            ret, frame = video_capture.read()

            if ret != True:
                break

        image = Image.fromarray(frame)

        # ______________________________________________________________________________________________________________________________DETECT WITH YOLO
        t1 = time.time()

        boxs = yolo.detect_image(image)

        # print("box_num",len(boxs))
        features = encoder(frame, boxs)

        # score to 1.0 here).
        detections = [
            Detection(bbox, 1.0, feature)
            for bbox, feature in zip(boxs, features)
        ]

        # Run non-max suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(
            boxes, nms_max_overlap, scores)  #index that filtered

        detections = [detections[i] for i in indices]

        # ______________________________________________________________________________________________________________________________DRAW DETECT BOX

        for det in detections:
            bbox = det.to_tlbr()
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                          (int(bbox[2]), int(bbox[3])), (0, 0, 255), 1)

        # ___________________________________________________________________________Call the tracker
        tracker.predict()
        tracker.update(detections)

        # __________________________________________________________________________________________________________________________DRAW TRACK RECTANGLE
        ina_now = set()
        inb_now = set()
        for track in tracker.tracks:
            if track.is_confirmed() and track.time_since_update > 1:
                continue

            bbox = track.to_tlbr()

            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                          (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2)
            cv2.putText(frame, str(track.track_id),
                        (int(bbox[0]), int(bbox[1]) + 30),
                        cv2.FONT_HERSHEY_SIMPLEX, 5e-3 * 200, (0, 255, 0), 3)

            dot = int(int(bbox[0]) +
                      ((int(bbox[2]) - int(bbox[0])) / 2)), int(bbox[3] - 5)

            cv2.circle(frame, dot, 10, (0, 0, 255), -1)

            if points:
                dotc = Point(dot)

                ina_now.add(track.track_id) if (
                    polygon_a.contains(dotc)
                    and track.track_id not in ina_now) else None

                inb_now.add(track.track_id) if (
                    polygon_b.contains(dotc)
                    and track.track_id not in inb_now) else None

        # print('ina_now : ',ina_now,'ina_old : ',ina_old)

        for item in a2b:  #check who pass a->b is already exist in a2b_cus
            a2b_cus.add(item) if item not in a2b_cus else None

        a2b = inb_now.intersection(ina_old)
        num_a2b += len(a2b - a2b_old)

        b2a = ina_now.intersection(inb_old)
        num_b2a += len(b2a - b2a_old)
        a2b_old = a2b
        b2a_old = b2a

        ina_old = ina_old.union(ina_now)

        inb_old = inb_old.union(inb_now)

        i += 1
        if i > 10:  #slow down backup old
            ina_old.pop() if len(ina_old) != 0 else None
            inb_old.pop() if len(inb_old) != 0 else None
            i = 0

        # print('num_a2b : ',num_a2b,'num_b2a : ',num_b2a)

        # __________________________________________________________________________________________________________________________________________CSV

        if FLAGScsv and ((num_a2b_old != num_a2b) or (num_b2a_old != num_b2a)):
            record = [
                time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()), num_a2b,
                num_b2a, num_a2b - num_b2a
            ]
            csv_obj.save_this(record)

        num_a2b_old = num_a2b
        num_b2a_old = num_b2a

        # _________________________________________________________________________________________________________________________GET POINTS From click

        if (cv2.waitKey(1) == ord('p')):
            points = get_lines.run(frame, multi=True)
            print(points)

            #region
            if len(points) % 3 == 0 and len(points) / 3 == 1:  #1 door
                print('1 door mode')
                polygon_a = Polygon([
                    points[0][0:2], points[0][2:4], points[1][0:2],
                    points[1][2:4]
                ])
                polygon_b = Polygon([
                    points[1][0:2], points[1][2:4], points[2][0:2],
                    points[2][2:4]
                ])

            elif len(points) % 3 == 0 and len(points) / 3 == 2:  #2 door
                print('2 doors mode')
                polygon_a1 = Polygon([
                    points[0][0:2], points[0][2:4], points[1][0:2],
                    points[1][2:4]
                ])
                polygon_a2 = Polygon([
                    points[3][0:2], points[3][2:4], points[4][0:2],
                    points[4][2:4]
                ])
                polygon_a = [polygon_a1, polygon_a2]
                polygon_a = cascaded_union(polygon_a)

                polygon_b1 = Polygon([
                    points[1][0:2], points[1][2:4], points[2][0:2],
                    points[2][2:4]
                ])
                polygon_b2 = Polygon([
                    points[4][0:2], points[4][2:4], points[5][0:2],
                    points[5][2:4]
                ])
                polygon_b = [polygon_b1, polygon_b2]
                polygon_b = cascaded_union(polygon_b)

            else:
                print('Please draw 3 or 6 lines')
                break

        if points:
            for line in points:

                cv2.line(frame, line[0:2], line[2:4], (0, 255, 255),
                         2)  # draw line

        cv2.putText(frame, 'in : ' + str(num_a2b), (10, 100),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2, cv2.LINE_AA)
        cv2.putText(frame, 'out : ' + str(num_b2a), (10, 150),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2, cv2.LINE_AA)

        out.write(frame)
        cv2.imshow('', frame)

        print('process time : ', time.time() - tpro)
        tpro = time.time()

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

    video_capture.release()
    out.release()
    cv2.destroyAllWindows()
예제 #2
0
def main(yolo):

   # Definition of the parameters
    max_cosine_distance = 0.3
    nn_budget = None
    nms_max_overlap = 1.0
    
   # deep_sort 
    model_filename = 'model_data/mars-small128.pb'
    encoder = gdet.create_box_encoder(model_filename,batch_size=1)
    
    metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget)
    tracker = Tracker(metric)

    writeVideo_flag = True 
    
    if len(sys.argv) > 1:
        video_capture = cv2.VideoCapture(sys.argv[1])
    else:
        video_capture = cv2.VideoCapture(0)

    if writeVideo_flag:
    # Define the codec and create VideoWriter object
        w = int(video_capture.get(3))
        h = int(video_capture.get(4))
        fourcc = cv2.VideoWriter_fourcc(*'MJPG')
        if len(sys.argv) > 1:
            file_name = re.split("\.|/", sys.argv[1])[-2]
        else:
            file_name = "camera"
        out = cv2.VideoWriter(file_name + '_output.avi', fourcc, 30, (w, h))
        list_file = open(file_name + '_detection.txt', 'w')
        frame_index = -1 
        
    fps = 0.0
    while True:
        ret, frame = video_capture.read()  # frame shape 640*480*3
        if ret != True:
            break
        t1 = time.time()

       # image = Image.fromarray(frame)
        image = Image.fromarray(frame[...,::-1]) #bgr to rgb
        boxs, scores_ = yolo.detect_image(image)
       # print("box_num",len(boxs))
        features = encoder(frame,boxs)
        
        # score to 1.0 here).
        detections = [Detection(bbox, 1.0, feature) for bbox, feature in zip(boxs, features)]
        
        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap, scores)
        detections = [detections[i] for i in indices]
        
        # Call the tracker
        tracker.predict()
        tracker.update(detections)
        
        track_str = ""
        timestamp = time.time()
        localTime = time.localtime(timestamp)
        strTime = time.strftime("%Y-%m-%d %H:%M:%S", localTime)
        track_num = 0
        
        for track in tracker.tracks:

            if not track.is_confirmed() or track.time_since_update > 1:
                continue 
            bbox = track.to_tlbr()
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])),(255,255,255), 2)
            
            # 2019-10-21
            if len(scores_) > 0:
                if track_num >= len(scores_):
                     continue
                cv2.putText(frame, "id: " + str(track.track_id) + " score: " + str(scores_[track_num])[:6] ,(int(bbox[0]), int(bbox[1])),0, 5e-3 * 200, (0,255,0),2)
                # 2019/10/21 add track_str
                if writeVideo_flag:                                  
                  track_str = track_str + str(strTime) + ";" + str(frame_index + 1) + ";" + str(track.track_id) + ";" + str(scores_[track_num])[:6] + ";" + str(boxs[track_num][0]) + ' ' + str(boxs[track_num][1]) + ' ' + str(boxs[track_num][2]) + ' ' + str(boxs[track_num][3]) + ' ' + "\n"
                
                track_num += 1  
        for det in detections:
            bbox = det.to_tlbr()
            cv2.rectangle(frame,(int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])),(255,0,0), 2)
            
        cv2.imshow('', frame)
        
        if writeVideo_flag:
            # save a frame
            out.write(frame)
            frame_index = frame_index + 1

            list_file.write(track_str) # 2019/10/21
            
            """
            2019/10/21
            if len(boxs) != 0:
                for i in range(0,len(boxs)):
                    list_file.write(str(boxs[i][0]) + ' '+str(boxs[i][1]) + ' '+str(boxs[i][2]) + ' '+str(boxs[i][3]) + ' ')
            list_file.write('\n')
            """
            
        fps  = ( fps + (1./(time.time()-t1)) ) / 2
        print("fps= %f"%(fps))
        
        # Press Q to stop!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    video_capture.release()
    if writeVideo_flag:
        out.release()
        list_file.close()
    cv2.destroyAllWindows()
예제 #3
0
def main(yolo):

    source = 0  # 0 for webcam or youtube or jpg
    FLAGScsv = 1

    if FLAGScsv:
        csv_obj = save_csv()
        num_a2b_start, num_b2a_start = csv_obj.startday(
        )  #read old count from csv file

    else:
        num_a2b_start = 0
        #start from zero
        num_b2a_start = 0

    ina_old = set()
    ina_now = set()
    inb_old = set()
    inb_now = set()
    num_a2b_old = 0
    num_b2a_old = 0
    a2b_old = set()
    b2a_old = set()
    i = 0
    a2b_cus = set()
    b2a_cus = set()

    #points=[(462, 259, 608, 608), (439, 608, 387, 403), (279, 456, 285, 608), (182, 70, 249, 168), (218, 278, 116, 95), (60, 166, 235, 331)]
    with open('linefile', 'rb') as fp:
        points = pickle.load(fp)

    print('Load lines :', points)

    if points:

        if len(points) % 3 == 0 and len(points) / 3 == 1:  #1 door
            print('1 door mode')
            polygon_a = Polygon([
                points[0][0:2], points[0][2:4], points[1][0:2], points[1][2:4]
            ])
            polygon_b = Polygon([
                points[1][0:2], points[1][2:4], points[2][0:2], points[2][2:4]
            ])

        elif len(points) % 3 == 0 and len(points) / 3 == 2:  #2 door
            print('2 doors mode')
            polygon_a1 = Polygon([
                points[0][0:2], points[0][2:4], points[1][0:2], points[1][2:4]
            ])
            polygon_a2 = Polygon([
                points[3][0:2], points[3][2:4], points[4][0:2], points[4][2:4]
            ])
            polygon_a = [polygon_a1, polygon_a2]
            polygon_a = cascaded_union(polygon_a)

            polygon_b1 = Polygon([
                points[1][0:2], points[1][2:4], points[2][0:2], points[2][2:4]
            ])
            polygon_b2 = Polygon([
                points[4][0:2], points[4][2:4], points[5][0:2], points[5][2:4]
            ])
            polygon_b = [polygon_b1, polygon_b2]
            polygon_b = cascaded_union(polygon_b)

    tpro = 0.
    # Definition of the parameters
    max_cosine_distance = 0.7
    nn_budget = None
    nms_max_overlap = 1.0

    # deep_sort
    model_filename = 'model_data/mars-small128.pb'
    encoder = gdet.create_box_encoder(model_filename, batch_size=1)

    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    tracker = Tracker(metric)

    video_capture = cv2.VideoCapture(source)

    print('video source : ', source)

    #out = cv2.VideoWriter('output.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (608,608))
    #  ___________________________________________________________________________________________________________________________________________MAIN LOOP
    while True:

        # get 1 frame

        if source == 'youtube':

            raw_frame = p2.stdout.read(width * height * 3)
            frame = np.fromstring(raw_frame, dtype='uint8').reshape(
                (width, height, 3))

        elif source == 'gst.jpg':
            try:
                img_bin = open('gst.jpg', 'rb')
                buff = io.BytesIO()
                buff.write(img_bin.read())
                buff.seek(0)
                frame = numpy.array(Image.open(buff), dtype=numpy.uint8)  #RGB
                #frame=adjust_gamma(frame,gamma=1.6)

                frame = cv2.resize(frame, (608, 608))
            except OSError:
                continue
            except TypeError:
                continue

        else:
            ret, frame = video_capture.read()
            frame = cv2.resize(
                frame,
                (608, 608))  # maybe your webcam is not in the right size
            frame = cv2.cvtColor(
                frame, cv2.COLOR_RGB2BGR)  # because opencv read as BGR

            if ret != True:
                break

        image = Image.fromarray(frame)

        # ______________________________________________________________________________________________________________________________DETECT WITH YOLO
        t1 = time.time()

        boxs = yolo.detect_image(image)

        # print("box_num",len(boxs))
        features = encoder(frame, boxs)

        # score to 1.0 here).
        detections = [
            Detection(bbox, 1.0, feature)
            for bbox, feature in zip(boxs, features)
        ]

        # Run non-max suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(
            boxes, nms_max_overlap, scores)  #index that filtered

        detections = [detections[i] for i in indices]

        # ______________________________________________________________________________________________________________________________DRAW DETECT BOX

        for det in detections:
            bbox = det.to_tlbr()
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                          (int(bbox[2]), int(bbox[3])), (0, 0, 255), 1)

        # ___________________________________________________________________________Call the tracker
        tracker.predict()
        tracker.update(detections)

        frame = cv2.cvtColor(frame,
                             cv2.COLOR_RGB2BGR)  #change to BGR for show only

        # __________________________________________________________________________________________________________________________DRAW TRACK RECTANGLE
        ina_now = set()
        inb_now = set()
        for track in tracker.tracks:
            if track.is_confirmed() and track.time_since_update > 1:
                continue

            bbox = track.to_tlbr()

            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                          (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2)
            cv2.putText(frame, str(track.track_id),
                        (int(bbox[0]), int(bbox[1]) + 30),
                        cv2.FONT_HERSHEY_SIMPLEX, 5e-3 * 200, (0, 255, 0), 3)

            dot = int(int(bbox[0]) +
                      ((int(bbox[2]) - int(bbox[0])) / 2)), int(bbox[3] - 15)

            cv2.circle(frame, dot, 10, (0, 0, 255), -1)

            if points:
                dotc = Point(dot)

                ina_now.add(track.track_id) if (
                    polygon_a.contains(dotc)
                    and track.track_id not in ina_now) else None

                inb_now.add(track.track_id) if (
                    polygon_b.contains(dotc)
                    and track.track_id not in inb_now) else None

        # print('ina_now : ',ina_now,'ina_old : ',ina_old)
        # print('inb_now : ',inb_now,'inb_old : ',inb_old)

        a2b = inb_now.intersection(ina_old)
        for item in a2b:  #check who pass a->b is already exist in a2b_cus
            a2b_cus.add(item) if item not in a2b_cus else None
        num_a2b = num_a2b_start + len(a2b_cus)

        b2a = ina_now.intersection(inb_old)
        for item in b2a:  #check who pass a->b is already exist in a2b_cus
            b2a_cus.add(item) if item not in b2a_cus else None
        num_b2a = num_b2a_start + len(b2a_cus)

        a2b_old = a2b
        b2a_old = b2a

        ina_old = ina_now

        inb_old = inb_now

        # i+=1
        # if i > 30 : #slow down backup old
        #     ina_old =set()
        #     inb_old =set()
        #     i=0

        # __________________________________________________________________________________________________________________CSV

        if FLAGScsv and ((num_a2b_old != num_a2b) or (num_b2a_old != num_b2a)):
            record = [
                time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()), num_a2b,
                num_b2a, num_a2b - num_b2a
            ]
            csv_obj.save_this(record)

        num_a2b_old = num_a2b
        num_b2a_old = num_b2a

        # _____________________________________________________________________________________________________GET POINTS From click

        if (cv2.waitKey(1) == ord('p')):
            points = get_lines.run(frame, multi=True)
            print(points)

            #region
            if len(points) % 3 == 0 and len(points) / 3 == 1:  #1 door
                print('1 door mode')
                polygon_a = Polygon([
                    points[0][0:2], points[0][2:4], points[1][0:2],
                    points[1][2:4]
                ])
                polygon_b = Polygon([
                    points[1][0:2], points[1][2:4], points[2][0:2],
                    points[2][2:4]
                ])

                #save to file
                with open('linefile', 'wb') as fp:
                    pickle.dump(points, fp)

            elif len(points) % 3 == 0 and len(points) / 3 == 2:  #2 door
                print('2 doors mode')
                polygon_a1 = Polygon([
                    points[0][0:2], points[0][2:4], points[1][0:2],
                    points[1][2:4]
                ])
                polygon_a2 = Polygon([
                    points[3][0:2], points[3][2:4], points[4][0:2],
                    points[4][2:4]
                ])
                polygon_a = [polygon_a1, polygon_a2]
                polygon_a = cascaded_union(polygon_a)

                polygon_b1 = Polygon([
                    points[1][0:2], points[1][2:4], points[2][0:2],
                    points[2][2:4]
                ])
                polygon_b2 = Polygon([
                    points[4][0:2], points[4][2:4], points[5][0:2],
                    points[5][2:4]
                ])
                polygon_b = [polygon_b1, polygon_b2]
                polygon_b = cascaded_union(polygon_b)
                with open('linefile', 'wb') as fp:
                    pickle.dump(points, fp)

            else:
                print('Please draw 3 or 6 lines')
                break

        if points:
            for line in points:

                cv2.line(frame, line[0:2], line[2:4], (0, 255, 255),
                         2)  # draw line

        cv2.putText(frame, 'in : ' + str(num_a2b), (10, 100),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2, cv2.LINE_AA)
        cv2.putText(frame, 'out : ' + str(num_b2a), (10, 150),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2, cv2.LINE_AA)

        #out.write(frame)
        #

        cv2.imshow('', frame)

        print('process time : ', time.time() - tpro)
        tpro = time.time()

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

    video_capture.release()
    #out.release()
    cv2.destroyAllWindows()
예제 #4
0
def main(yolo, url, CreateBoxEncoder, q):
    producer = None
    if KAFKA_ON:
        ip_port = '{}:{}'.format(KAFKA_IP, KAFKA_PORT)
        producer = KafkaProducer(bootstrap_servers=ip_port)
        logger.debug('open kafka')
    # Definition of the parameters
    max_cosine_distance = 0.3
    nn_budget = None
    nms_max_overlap = 1.0
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    tracker = Tracker(metric)
    door = get_door(url)
    #    init   var
    center_mass = {}
    miss_ids = []
    disappear_box = {}
    person_list = []
    in_house = {}
    in_out_door = {"out_door_per": 0, "into_door_per": 0}
    only_id = str(uuid.uuid4())
    logger.debug('rtmp: {} load finish'.format(url))
    last_person_num = 0
    last_monitor_people = 0
    while True:
        t1 = time.time()
        if q.empty():
            continue
        frame = q.get()
        image = Image.fromarray(frame[..., ::-1])  # bgr to rgb
        boxs, scores_ = yolo.detect_image(image)
        t2 = time.time()
        # print('5====={}======{}'.format(os.getpid(), round(t2 - t1, 4)))
        now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        logger.debug("box_num: {}".format(len(boxs)))
        features = CreateBoxEncoder.encoder(frame, boxs)
        # score to 1.0 here).
        # detections = [Detection(bbox, 1.0, feature) for bbox, feature in zip(boxs, features)]
        detections = [
            Detection(bbox, scores_, feature)
            for bbox, scores_, feature in zip(boxs, scores_, features)
        ]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]

        # Call the tracker
        tracker.predict()
        tracker.update(detections)
        # 实时人员ID保存
        track_id_list = []

        cv2.rectangle(frame, (door[0], door[1]), (door[2], door[3]),
                      (0, 0, 255), 2)
        door_half_h = int(int((door[1] + door[3]) / 2) * DOOR_HIGH)
        cv2.line(frame, (0, door_half_h), (111111, door_half_h), (0, 255, 0),
                 1, 1)
        high_score_ids = {}
        for track in tracker.tracks:
            # 当跟踪的目标在未来的20帧未出现,则判断丢失,保存至消失的id中间区
            if track.time_since_update == MAX_AGE:
                miss_id = str(track.track_id)
                miss_ids.append(miss_id)
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            # 如果人id存在,就把人id的矩形框坐标放进center_mass 否则 创建一个key(人id),value(矩形框坐标)放进center_mass
            track_id = str(track.track_id)
            bbox = track.to_tlbr()
            near_door = is_near_door({track_id: bbox}, door)
            if track.score >= 0.92 and not near_door:
                high_score_ids[track_id] = [[
                    int(bbox[0]),
                    int(bbox[1]),
                    int(bbox[2]),
                    int(bbox[3])
                ]]

            track_id_list.append(track_id)

            if track_id in center_mass:
                center_ = center_mass.get(track_id)
                if len(center_) > 49:
                    center_.pop(0)
                center_.append(
                    [int(bbox[0]),
                     int(bbox[1]),
                     int(bbox[2]),
                     int(bbox[3])])
            else:
                center_mass[track_id] = [[
                    int(bbox[0]),
                    int(bbox[1]),
                    int(bbox[2]),
                    int(bbox[3])
                ]]

            # # --------------------------------------------
            # # logger.debug('box1:{}'.format([int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])]))
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                          (int(bbox[2]), int(bbox[3])), (255, 0, 0), 2)
            cv2.putText(frame, str(track.track_id),
                        (int(bbox[0]), int(bbox[1])), 0, 5e-3 * 200,
                        (0, 255, 0), 2)
            x0, y0 = int((bbox[0] + bbox[2]) / 2), int((bbox[1] + bbox[3]) / 2)
            cv2.putText(frame, str(round(track.score, 3)), (x0, y0), 0, 0.6,
                        (0, 255, 0), 2)
            # cv2.circle(frame, (x0, y0), 2, (0, 255, 255), thickness=2, lineType=1, shift=0)
            # # --------------------------------------------

            # x0, y0 = int((bbox[0] + bbox[2]) / 2), int((bbox[1] + bbox[3]) / 2)
            # w = abs(int(bbox[3]) - int(bbox[1]))
            # h = abs(int(bbox[2]) - int(bbox[0]))
            logger.info('id:{}, score:{}'.format(track_id, track.score))

        for id in miss_ids:
            if id in center_mass.keys():
                disappear_box[id] = center_mass[id]
                del center_mass[id]
        miss_ids.clear()

        # # 进出门判断
        out_or_in(center_mass, door, in_house, disappear_box, in_out_door)
        # near_door = is_near_door(center_mass, door, disappear_id)

        # 相对精准识别人 用来实时传递当前人数
        box_score_person = [scores for scores in scores_ if scores > 0.72]
        person_sum = in_out_door['into_door_per'] - in_out_door['out_door_per']
        # if person_sum <= len(high_score_ids) and not near_door:
        if person_sum <= len(high_score_ids):
            # 当时精准人数大于进出门之差时 来纠正进门人数 并把出门人数置为0
            if person_sum == len(high_score_ids) == 1:
                pass
                # print('person_sum == len(high_score_ids) == 1')
            else:
                logger.warning('reset in_out_door person')
                in_out_door['out_door_per'] = 0
                in_out_door['into_door_per'] = len(high_score_ids)
                in_house.update(high_score_ids)
                # print('high score:{}'.format(high_score_ids))
                logger.warning('22222222-id: {} after into of door: {}'.format(
                    in_house.keys(), in_out_door['into_door_per']))
                person_sum = len(high_score_ids)
        if in_out_door['into_door_per'] == in_out_door['out_door_per'] > 0:
            in_out_door['into_door_per'] = in_out_door['out_door_per'] = 0
        if len(person_list) > 100:
            person_list.pop(0)
        person_list.append(person_sum)
        # 从url提取摄像头编号
        pattern = str(url)[7:].split(r"/")
        logger.debug('pattern {}'.format(pattern[VIDEO_CONDE]))
        video_id = pattern[VIDEO_CONDE]
        logger.info('object tracking cost {}'.format(time.time() - t1))
        # 当列表中都是0的时候 重置进出门人数和所有字典参数变量
        if person_list.count(0) == len(person_list) == 101:
            logger.debug('long time person is 0')
            in_out_door['into_door_per'] = 0
            in_out_door['out_door_per'] = 0
            in_house.clear()
            logger.warning('All Clear')
        cv2.putText(frame, "person: " + str(person_sum), (40, 40), 0,
                    5e-3 * 200, (0, 255, 0), 2)
        cv2.putText(frame, "now_per: " + str(len(box_score_person)), (280, 40),
                    0, 5e-3 * 200, (0, 255, 0), 2)

        # 当满足条件时候 往前端模块发送人员的信息
        if (last_person_num != person_sum
                or last_monitor_people != len(box_score_person)) and producer:
            monitor_people_num = len(box_score_person)
            logger.debug("person-sum:{} monitor-people_num:{}".format(
                person_sum, monitor_people_num))
            # if int(time.time()) - last_time >= 1:
            cv2.imwrite(
                "/opt/code/deep_sort_yolov3/image/{}.jpg".format(
                    str(uuid.uuid4())), frame)
            # print('save img success')
            save_to_kafka(TOPIC_SHOW, now, person_sum, url, producer, video_id,
                          monitor_people_num, only_id)
            if last_person_num > 0 and person_sum == 0:
                only_id = str(uuid.uuid4())

            if last_person_num == 0 and person_sum > 0:
                save_to_kafka(TOPIC_NVR, now, person_sum, url, producer,
                              video_id, len(box_score_person), only_id)

            # last_time = int(time.time())
            last_person_num = person_sum
            last_monitor_people = len(box_score_person)
        # 当满足条件时候 往NVR模块发送信息

        logger.info('url:{} into_door_per: {}'.format(
            url, in_out_door['into_door_per']))
        logger.info('url:{} out_door_per: {}'.format(
            url, in_out_door['out_door_per']))
        logger.info('url:{} in_house: {}'.format(url, in_house))
        logger.info('url:{} monitor_people_num: {}'.format(
            url, len(box_score_person)))
        logger.info('url:{} person_sum: {}'.format(url, person_sum))
        logger.info('GPU image load cost {}'.format(time.time() - t1))
        t3 = time.time()
        fps = round(1 / (round(t3 - t1, 4)), 3)
        # print('pid:{}===fps:{}===time:{}'.format(os.getpid(), fps, round(t3 - t1, 4)))
        # print('*' * 30)
        fps = ((1 / (time.time() - t1)))
        logger.debug("fps= %f" % (fps))
        cv2.imshow('', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
예제 #5
0
                               pady=5,
                               ipadx=0,
                               ipady=0,
                               sticky=W + E)
Label(settingFrame, text=" Đường dẫn tập tin").grid(row=3, sticky=W)
directoryText = StringVar()
directory = Entry(settingFrame, width=32, textvariable=directoryText)
directory.grid(row=4, padx=5, pady=5, ipadx=0, ipady=0, sticky=W)
Label(settingFrame, text=" Trạng thái phân tích").grid(row=6, sticky=W)
statusText = StringVar()
status = Entry(settingFrame, width=32, textvariable=statusText)
status.grid(row=7, padx=5, pady=5, ipadx=0, ipady=0, sticky=W)
statusText.set(" Đang chờ tập tin")
Label(settingFrame, text=" Ngày giờ hệ thống").grid(row=9, sticky=W)
curTime = Entry(settingFrame, width=32)
curTime.insert(15, time.strftime(" %m/%d/%Y, %H:%M:%S %p"))
curTime.grid(row=10, padx=5, pady=5, ipadx=0, ipady=0, sticky=W)
progress = Progressbar(settingFrame, orient=HORIZONTAL, mode="determinate")
progress.grid(row=11, padx=5, pady=5, ipadx=0, ipady=0, sticky=N + W + E + S)
progress["value"] = 0
app8 = Frame(infoFrame)
app8.pack(side=TOP, fill="both")
lmain8 = Label(app8)
lmain8.grid(padx=5, pady=5, ipadx=0, ipady=0, sticky=E + S)
cv2image8 = cv2.imread("info.png")
cv2image8 = cv2.cvtColor(cv2image8, cv2.COLOR_BGR2RGBA)
cv2image8 = cv2.resize(cv2image8, (195, 200))
img8 = Image.fromarray(cv2image8)
imgtk8 = ImageTk.PhotoImage(image=img8)
lmain8.imgtk8 = imgtk8
lmain8.configure(image=imgtk8)