Exemplo n.º 1
0
 def camera_connect(self):
     ### connect camera
     self.capture = VideoCaptureAsync(
         self.project_data["Setup"]["CameraPort"],
         self.project_data["Setup"]["CameraResX"],
         self.project_data["Setup"]["CameraResY"],
     )
     self.capture.start()
Exemplo n.º 2
0
def init_cameras():
    #capture thermic frame
    cap_thermal = VideoCaptureAsync(thermal=True)
    cap_thermal.start()
    #capture normal frame
    cap_normal = VideoCaptureAsync(thermal=False)
    cap_normal.start()
    return cap_normal, cap_thermal
Exemplo n.º 3
0
  def __init__(self, cameraSource, height, output_file=None, startFrame=0,
               async_read=False, outputToServer=False, capture_size=None):
    if async_read:
      self.camera = VideoCaptureAsync(cameraSource)
    else:
      self.camera = cv2.VideoCapture(cameraSource)

    if capture_size is not None:
      print(capture_size)
      self.camera.set(cv2.CAP_PROP_FRAME_WIDTH, capture_size[0])
      self.camera.set(cv2.CAP_PROP_FRAME_HEIGHT, capture_size[1])

    if async_read:
      self.ORIGINAL_WIDTH = self.camera.width
      self.ORIGINAL_HEIGHT = self.camera.height
    else:
      self.ORIGINAL_WIDTH = int(self.camera.get(cv2.CAP_PROP_FRAME_WIDTH))
      self.ORIGINAL_HEIGHT = int(self.camera.get(cv2.CAP_PROP_FRAME_HEIGHT))

    print('CameraSource')
    print('Requested capture size', capture_size)
    print('Actual capture size', self.ORIGINAL_WIDTH, self.ORIGINAL_HEIGHT)

    self.HEIGHT = height
    self.WIDTH = self.ORIGINAL_WIDTH * self.HEIGHT // self.ORIGINAL_HEIGHT
    self.WIDTH = self.WIDTH + self.WIDTH % 2  # Make it even.

    self.startFrame = startFrame
    self.nFrames = 0
    self.writer = VideoWriter(output_file)

    if async_read:
      self.camera.start()

    self.outputToServer = outputToServer
    if outputToServer:
      # https://robotpy.readthedocs.io/en/stable/vision/code.html
      pass
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)

    tracking = True
    writeVideo_flag = True
    asyncVideo_flag = False

    file_path = 'video.webm'
    if asyncVideo_flag:
        video_capture = VideoCaptureAsync(file_path)
    else:
        video_capture = cv2.VideoCapture(file_path)

    if asyncVideo_flag:
        video_capture.start()

    if writeVideo_flag:
        if asyncVideo_flag:
            w = int(video_capture.cap.get(3))
            h = int(video_capture.cap.get(4))
        else:
            w = int(video_capture.get(3))
            h = int(video_capture.get(4))
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter('output_yolov4.avi', fourcc, 30, (w, h))
        frame_index = -1

    fps = 0.0
    fps_imutils = imutils.video.FPS().start()
    model_par, valid_transform = model_init_par()
    while True:
        ret, frame = video_capture.read()  # frame shape 640*480*3
        if ret != True:
            break

        t1 = time.time()

        image = Image.fromarray(frame[..., ::-1])  # bgr to rgb
        boxes, confidence, classes = yolo.detect_image(image)

        if tracking:
            features = encoder(frame, boxes)

            detections = [
                Detection(bbox, confidence, cls, feature)
                for bbox, confidence, cls, feature in zip(
                    boxes, confidence, classes, features)
            ]
        else:
            detections = [
                Detection_YOLO(bbox, confidence, cls)
                for bbox, confidence, cls in zip(boxes, confidence, classes)
            ]

        # 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]

        for det in detections:
            bbox = det.to_tlbr()
            score = "%.2f" % round(det.confidence * 100, 2) + "%"
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                          (int(bbox[2]), int(bbox[3])), (255, 0, 0), 2)
            if len(classes) > 0:
                cls = det.cls
                cv2.putText(frame,
                            str(cls) + " " + score,
                            (int(bbox[0]), int(bbox[3])), 0,
                            1e-3 * frame.shape[0], (0, 255, 0), 1)

        if tracking:
            # Call the tracker
            tracker.predict()
            tracker.update(detections)

            for track in tracker.tracks:
                if not track.is_confirmed() or track.time_since_update > 1:
                    continue
                bbox = track.to_tlbr()
                #crop_img = frame[int(bbox[1]):int(bbox[3]),int(bbox[0]):int(bbox[2])]
                crop_img = image.crop(
                    [int(bbox[0]),
                     int(bbox[1]),
                     int(bbox[2]),
                     int(bbox[3])])
                #res_txt = demo_par(model_par, valid_transform, crop_img)

                #draw.rectangle(xy=person_bbox[:-1], outline='red', width=1)

                cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                              (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2)
                cv2.putText(frame, "ID: " + str(track.track_id),
                            (int(bbox[0]), int(bbox[1])), 0,
                            1e-3 * frame.shape[0], (0, 255, 0), 1)
                font = ImageFont.truetype(
                    '/home/sohaibrabbani/PycharmProjects/Strong_Baseline_of_Pedestrian_Attribute_Recognition/arial.ttf',
                    size=10)
                # positive_cnt = 1
                # for txt in res_txt:
                #     if 'personal' in txt:
                #         #draw.text((x1, y1 + 20 * positive_cnt), txt, (255, 0, 0), font=font)
                #         cv2.putText(frame, txt, (int(bbox[0]), int(bbox[1]) + 20 * positive_cnt), 0,
                #                     1e-3 * frame.shape[0], (0, 255, 0), 1)
                #         positive_cnt += 1

        cv2.imshow('', frame)

        if writeVideo_flag:  # and not asyncVideo_flag:
            # save a frame
            out.write(frame)
            frame_index = frame_index + 1

        fps_imutils.update()

        if not asyncVideo_flag:
            fps = (fps + (1. / (time.time() - t1))) / 2
            print("FPS = %f" % (fps))

        # Press Q to stop!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    fps_imutils.stop()
    print('imutils FPS: {}'.format(fps_imutils.fps()))

    if asyncVideo_flag:
        video_capture.stop()
    else:
        video_capture.release()

    if writeVideo_flag:
        out.release()

    cv2.destroyAllWindows()
"""signal detection"""
import logging as log
import os

import cv2 as cv
import numpy as np
from imutils import grab_contours, resize

import prediction
from skimage import measure
from videocaptureasync import VideoCaptureAsync

VID = cv.VideoCapture('C:/Users/tbolz/Desktop/videos/video_40_ss_auto.h264')
CAP = VideoCaptureAsync('C:/Users/tbolz/Desktop/videos/video_40_ss_auto.h264')


def predict_number(sub_img):
    """calls prediction class to predict the number"""
    prediction.start(sub_img)


def detect_shape(cont, orig):
    """detects the square and crops it"""
    # compute the bounding box of the contour and use the
    # bounding box to compute the aspect ratio
    peri = cv.arcLength(cont, True)
    approx = cv.approxPolyDP(cont, 0.02 * peri, True)
    (x, y, w, h) = cv.boundingRect(approx)
    ar = w / float(h)
    if len(approx) == 4:
        # a square will have an aspect ratio that is approximately
Exemplo n.º 6
0
def test(width, height):

    cap1 = VideoCaptureAsync(
        "rtsp://*****:*****@192.168.15.200:554/moxa-cgi/udpstream_ch1")
    cap2 = VideoCaptureAsync(
        "rtsp://*****:*****@192.168.15.200:554/moxa-cgi/udpstream_ch2")
    cap3 = VideoCaptureAsync(
        "rtsp://*****:*****@192.168.15.200:554/moxa-cgi/udpstream_ch3")
    cap4 = VideoCaptureAsync(
        "rtsp://*****:*****@192.168.15.200:554/moxa-cgi/udpstream_ch4")
    cap1.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap1.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    cap2.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap2.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    cap3.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap3.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    cap4.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap4.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

    alpha = 90
    beta = 90
    gamma = 90
    focalLength = 500
    dist = 500
    move_upper = 0
    move_left = 0
    move_right = 0
    move_lower = 0
    move_together = 0
    cv2.namedWindow('tune_upper')
    cv2.namedWindow('tune_left')
    cv2.namedWindow('tune_right')
    cv2.namedWindow('tune_lower')
    cv2.namedWindow('move')

    cv2.createTrackbar('Alpha', 'tune_upper', 60, 180, nothing)
    cv2.createTrackbar('Beta', 'tune_upper', 90, 180, nothing)
    cv2.createTrackbar('Gamma', 'tune_upper', 90, 180, nothing)
    cv2.createTrackbar('f', 'tune_upper', 300, 2000, nothing)
    cv2.createTrackbar('Distance', 'tune_upper', 500, 2000, nothing)

    cv2.createTrackbar('Alpha', 'tune_left', 90, 180, nothing)
    cv2.createTrackbar('Beta', 'tune_left', 70, 180, nothing)
    cv2.createTrackbar('Gamma', 'tune_left', 90, 180, nothing)
    cv2.createTrackbar('f', 'tune_left', 300, 2000, nothing)
    cv2.createTrackbar('Distance', 'tune_left', 500, 2000, nothing)

    cv2.createTrackbar('Alpha', 'tune_right', 90, 180, nothing)
    cv2.createTrackbar('Beta', 'tune_right', 110, 180, nothing)
    cv2.createTrackbar('Gamma', 'tune_right', 90, 180, nothing)
    cv2.createTrackbar('f', 'tune_right', 300, 2000, nothing)
    cv2.createTrackbar('Distance', 'tune_right', 500, 2000, nothing)

    cv2.createTrackbar('Alpha', 'tune_lower', 60, 180, nothing)
    cv2.createTrackbar('Beta', 'tune_lower', 90, 180, nothing)
    cv2.createTrackbar('Gamma', 'tune_lower', 90, 180, nothing)
    cv2.createTrackbar('f', 'tune_lower', 300, 2000, nothing)
    cv2.createTrackbar('Distance', 'tune_lower', 500, 2000, nothing)

    cv2.createTrackbar('Move upper', 'move', 0, 400, nothing)
    cv2.createTrackbar('Move left', 'move', 0, 400, nothing)
    cv2.createTrackbar('Move right', 'move', 0, 400, nothing)
    cv2.createTrackbar('Move lower', 'move', 0, 400, nothing)
    cv2.createTrackbar('Move together', 'move', 200, 400, nothing)

    cap1.start()
    cap2.start()
    cap3.start()
    cap4.start()
    h = 480
    w = 720
    vis = np.zeros(((w + h * 2), (w + h * 2), 3), np.uint8)
    vis_center = np.zeros(((w + h * 2), h, 3), np.uint8)
    vis_left = np.zeros(((w + h * 2), h, 3), np.uint8)
    vis_right = np.zeros(((w + h * 2), h, 3), np.uint8)
    void = np.zeros((w, w, 3), np.uint8)
    void_side = np.zeros((h, h, 3), np.uint8)

    while 1:
        _, frame1 = cap1.read()
        _, frame2 = cap2.read()
        _, frame3 = cap3.read()
        _, frame4 = cap4.read()

        frame11 = undistort(frame1)
        frame22 = undistort(frame2)
        frame33 = undistort(frame3)
        frame44 = undistort(frame4)

        alpha_upper = cv2.getTrackbarPos('Alpha', 'tune_upper')
        beta_upper = cv2.getTrackbarPos('Beta', 'tune_upper')
        gamma_upper = cv2.getTrackbarPos('Gamma', 'tune_upper')
        focalLength_upper = cv2.getTrackbarPos('f', 'tune_upper')
        dist_upper = cv2.getTrackbarPos('Distance', 'tune_upper')

        alpha_left = cv2.getTrackbarPos('Alpha', 'tune_left')
        beta_left = cv2.getTrackbarPos('Beta', 'tune_left')
        gamma_left = cv2.getTrackbarPos('Gamma', 'tune_left')
        focalLength_left = cv2.getTrackbarPos('f', 'tune_left')
        dist_left = cv2.getTrackbarPos('Distance', 'tune_left')

        alpha_right = cv2.getTrackbarPos('Alpha', 'tune_right')
        beta_right = cv2.getTrackbarPos('Beta', 'tune_right')
        gamma_right = cv2.getTrackbarPos('Gamma', 'tune_right')
        focalLength_right = cv2.getTrackbarPos('f', 'tune_right')
        dist_right = cv2.getTrackbarPos('Distance', 'tune_right')

        alpha_lower = cv2.getTrackbarPos('Alpha', 'tune_lower')
        beta_lower = cv2.getTrackbarPos('Beta', 'tune_lower')
        gamma_lower = cv2.getTrackbarPos('Gamma', 'tune_lower')
        focalLength_lower = cv2.getTrackbarPos('f', 'tune_lower')
        dist_lower = cv2.getTrackbarPos('Distance', 'tune_lower')

        move_upper = cv2.getTrackbarPos('Move upper', 'move')
        move_left = cv2.getTrackbarPos('Move left', 'move')
        move_right = cv2.getTrackbarPos('Move right', 'move')
        move_lower = cv2.getTrackbarPos('Move lower', 'move')
        move_together = cv2.getTrackbarPos('Move together', 'move')

        alpha_upper = (alpha_upper - 90) * math.pi / 180
        beta_upper = (beta_upper - 90) * math.pi / 180
        gamma_upper = (gamma_upper - 90) * math.pi / 180

        alpha_left = (alpha_left - 90) * math.pi / 180
        beta_left = (beta_left - 90) * math.pi / 180
        gamma_left = (gamma_left - 90) * math.pi / 180

        alpha_right = (alpha_right - 90) * math.pi / 180
        beta_right = (beta_right - 90) * math.pi / 180
        gamma_right = (gamma_right - 90) * math.pi / 180

        alpha_lower = (alpha_lower - 90) * math.pi / 180
        beta_lower = (beta_lower - 90) * math.pi / 180
        gamma_lower = (gamma_lower - 90) * math.pi / 180

        #rotation_mat = cv2.getRotationMatrix2D((360,240), 180, 1)
        #print("Transformation: ", Transformation.shape)
        #print("Rotation: ", rotation_mat.shape)

        transformation_upper = get_transformation(w, h, alpha_upper,
                                                  beta_upper, gamma_upper,
                                                  dist_upper,
                                                  focalLength_upper)
        transformation_left = get_transformation(h, w, alpha_left, beta_left,
                                                 gamma_left, dist_left,
                                                 focalLength_left)
        transformation_right = get_transformation(h, w, alpha_right,
                                                  beta_right, gamma_right,
                                                  dist_right,
                                                  focalLength_right)
        transformation_lower = get_transformation(w, h, alpha_lower,
                                                  beta_lower, gamma_lower,
                                                  dist_lower,
                                                  focalLength_lower)

        left = rotate_bound(frame33, 270)
        right = rotate_bound(frame22, 90)

        result_upper = cv2.warpPerspective(frame11, transformation_upper,
                                           (720, 480))
        result_left = cv2.warpPerspective(left, transformation_left,
                                          (480, 720))
        result_right = cv2.warpPerspective(right, transformation_right,
                                           (480, 720))
        result_lower = cv2.warpPerspective(frame44, transformation_lower,
                                           (720, 480))

        #cv2.imshow('Frame1', frame1)
        #cv2.imshow('Frame11', frame11)
        #cv2.imshow('Frame2', frame2)
        #cv2.imshow('Frame22', frame22)
        #cv2.imshow('Frame3', frame3)
        #cv2.imshow('Frame33', frame33)
        #cv2.imshow('Frame4', frame4)
        #cv2.imshow('Frame44', frame44)
        #left = rotate_bound(result_left, 270)
        #right = rotate_bound(result_right, 90)
        result_lower = cv2.flip(result_lower, 0)
        #cv2.imshow('left', left)
        #cv2.imshow('right',right)
        #vis_center = np.concatenate((result,void,frame44),axis=0)
        #vis_right = np.concatenate((void_side,right,void_side),axis=0)
        #vis_left = np.concatenate((void_side,left,void_side),axis=0)
        #vis = np.concatenate((vis_left,vis_center,vis_right),axis=1)
        vis[move_upper + move_together:result_upper.shape[0] + move_upper +
            move_together,
            h - (result_upper.shape[1] - 720) / 2:result_upper.shape[1] + h -
            (result_upper.shape[1] - 720) / 2, :] = result_upper
        vis[h:result_left.shape[0] + h,
            move_left + move_together:result_left.shape[1] + move_left +
            move_together:] += result_left
        vis[h:result_right.shape[0] + h,
            h + w - move_right - move_together:result_right.shape[1] -
            move_right - move_together + w + h, :] += result_right
        vis[h + w - move_lower - move_together:result_lower.shape[0] -
            move_lower - move_together + w + h,
            h:result_lower.shape[1] + h, :] += result_lower
        height, width = vis.shape[:2]
        res = cv2.resize(vis, (width * 4 / 7, height * 4 / 7),
                         interpolation=cv2.INTER_CUBIC)
        #cv2.imshow('combined', vis_center)
        #cv2.imshow('combined_left', vis_left)
        #cv2.imshow('combined_right', vis_right)
        cv2.imshow('vis', res)
        vis = np.zeros(((w + h * 2), (w + h * 2), 3), np.uint8)
        if cv2.waitKey(1) == 27:
            cv2.destroyAllWindows()
            break

    cap1.stop()
    cap2.stop()
    cap3.stop()
    cap4.stop()
    cv2.destroyAllWindows()
Exemplo n.º 7
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)

    np.random.seed(42)
    COLORS = np.random.randint(0, 255, size=(200, 3),
                               dtype="uint8")

    writeVideo_flag = True
    asyncVideo_flag = False

    file_path = 'testvideo2.avi'

    # load the COCO class labels our YOLO model was trained on    加载我们的YOLO模型经过培训的COCO类标签
    labelsPath = os.path.sep.join(["model_data", "coco_classes.txt"])
    LABELS = open(labelsPath).read().strip().split("\n")
    # print(str(len(LABELS))+"load successfully")
    # print(LABELS)
    class_nums = np.zeros(80)
    counter = np.zeros(80)
    track_id_max = -1
    if asyncVideo_flag:
        video_capture = VideoCaptureAsync(file_path)
    else:
        video_capture = cv2.VideoCapture(file_path)

    if asyncVideo_flag:
        video_capture.start()

    if writeVideo_flag:
        if asyncVideo_flag:
            w = int(video_capture.cap.get(3))
            h = int(video_capture.cap.get(4))
        else:
            w = int(video_capture.get(3))
            h = int(video_capture.get(4))
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter('testvideo2_out.avi', fourcc, 30, (w, h))
        frame_index = -1

    fps = 0.0
    fps_imutils = imutils.video.FPS().start()

    img_num = 0
    frame_cnt = 0
    while True:
        ret, frame = video_capture.read()  # frame shape 640*480*3
        frame_copy = frame.copy()
        if ret != True or frame_cnt > 30:
            break

        t1 = time.time()

        image = Image.fromarray(frame[..., ::-1])  # bgr to rgb

        boxs, confidence, class_names = yolo.detect_image(image)

        features = encoder(frame, boxs)

        detections = [Detection(bbox, confidence, feature) for bbox, confidence, feature in
                      zip(boxs, confidence, 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)
        # print("print indices!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
        # print(indices)

        # for i in indices:
        #    print(str(i)+class_names[i][0])

        # print(indices.shape)

        detections = [detections[i] for i in indices]
        class_names = [class_names[i] for i in indices]
        print("class_name:" + str(class_names))

        class_IDs = []
        current_nums = np.zeros(80)
        # class_IDs=[]
        for class_name in class_names:
            for i, LABEL in enumerate(LABELS):
                if class_name[0] == LABEL:
                    current_nums[i] += 1
                    class_IDs.append(i)
        # print("person:"+str(current_nums[0]))

        cv2.putText(frame, 'Current', (20, 70), cv2.FONT_HERSHEY_DUPLEX, 0.75, (255, 255, 255), 2)
        cv2.putText(frame, 'Total', (180, 70), cv2.FONT_HERSHEY_DUPLEX, 0.75, (0, 255, 255), 2)

        x1 = 20
        y1 = 100
        for i, cl in enumerate(current_nums):
            if cl > 0:
                cv2.putText(frame, LABELS[i] + "=" + str(cl), (x1, y1), cv2.FONT_HERSHEY_DUPLEX, 0.6, (255, 255, 255),
                            1)  # 当前帧各类别数量
                y1 = y1 + 20

        for i, det in enumerate(detections):
            bbox = det.to_tlbr()
            score = "%.2f" % round(det.confidence * 100, 2)
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 255, 255), 1)
            cv2.putText(frame, score + '%', (int(bbox[0]), int(bbox[1]) + 10), cv2.FONT_HERSHEY_DUPLEX, 0.3,
                        (255, 255, 255), 1)
            # cv2.putText(frame, class_names[i],(int(bbox[0]), int(bbox[1])-5), 0, 5e-3 * 130, (0, 255, 0), 2)
            # cv2.putText(frame, class_names[i], (int(bbox[0]), int(bbox[1])), 0, 5e-3 * 130,(0, 255, 255), 2)

        print("Total of detections:" + str(len(detections)))
        # Call the tracker
        tracker.predict()
        tracker.update(detections, class_IDs)

        # for i, cl in enumerate(class_nums):
        #     if cl > 0:
        #         print("add: " + LABELS[i] + str(cl - class_last_nums[i]))

        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                print("not track.is_confirmed() or track.time_since_update > 1: " + str(track.track_id))
                continue
            bbox = track.to_tlbr()
            color = [int(c) for c in COLORS[track.class_id % len(COLORS)]]
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), color, 2)

            cv2.putText(frame, str(track.track_id) + ' ' + LABELS[track.class_id], (int(bbox[0]), int(bbox[1]) - 5),
                        cv2.FONT_HERSHEY_DUPLEX, 0.4, color, 1)

            if track.track_id > track_id_max:
                counter[track.class_id] = counter[track.class_id] + 1
                track_id_max = track.track_id
                dest = frame_copy[int(bbox[1]):int(bbox[3]), int(bbox[0]):int(bbox[2])]
                pdest = "./output_image/" + str(LABELS[track.class_id]) + str(int(counter[track.class_id])) + ".png"
                cv2.imwrite(pdest, dest)
                img_num += 1
            # class_nums[track.class_id].append(track.track_id)
            # print(str(LABELS[track.class_id])+":"+class_nums[track.class_id])
            # print("track.id: " + str(track.track_id))
            # print("track.class_name: " + str(LABELS[track.class_id]))

        print(str(counter))
        print("--------------------------该帧输出完毕!--------------------------------------")

        # cv2.putText(frame, 'Total', (200, 60), cv2.FONT_HERSHEY_DUPLEX, 0.75, (255, 0, 0), 2)
        # x2 = 200
        # y2 = 100
        # for i, cl in enumerate(class_nums):
        #     if cl > 0:
        #         cv2.putText(frame, LABELS[i] + "=" + str(cl), (x2, y2), cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 0, 0), 2)
        #         y2 = y2 + 20

        cv2.putText(frame, "FPS: %f" % (fps), (int(20), int(40)), 0, 5e-3 * 200, (0, 255, 0), 3)  # !!!!!!!!!输出FPS
        x2 = 180
        y2 = 100
        for i, cl in enumerate(counter):
            if cl > 0:
                cv2.putText(frame, LABELS[i] + "=" + str(cl), (x2, y2), cv2.FONT_HERSHEY_DUPLEX, 0.6, (0, 255, 255), 1)
                y2 = y2 + 20
        # cv2.imshow('', frame)

        if writeVideo_flag:  # and not asyncVideo_flag:
            # save a frame
            out.write(frame)
            frame_index = frame_index + 1

        fps_imutils.update()

        fps = (fps + (1. / (time.time() - t1))) / 2

        # print("FPS = %f"%(fps))

        frame_cnt += 1
        # Press Q to stop!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    fps_imutils.stop()
    print('imutils FPS: {}'.format(fps_imutils.fps()))

    if asyncVideo_flag:
        video_capture.stop()
    else:
        video_capture.release()

    if writeVideo_flag:
        out.release()

    cv2.destroyAllWindows()
Exemplo n.º 8
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)

    tracking = True
    writeVideo_flag = False
    asyncVideo_flag = False
    webcamera_flag = True
    ipcamera_flag = False
    udp_flag = True

    file_path = '/workspace/data/C0133_v4.mp4'
    if asyncVideo_flag :
        video_capture = VideoCaptureAsync(file_path)
    elif ipcamera_flag :
        video_capture = cv2.VideoCapture('rtsp://*****:*****@192.168.2.201/ONVIF/MediaInput?profile=def_profile1')
    elif webcamera_flag :
        video_capture = cv2.VideoCapture(0)
    else:
        video_capture = cv2.VideoCapture(file_path)
        
    if asyncVideo_flag:
        video_capture.start()

    if writeVideo_flag:
        if asyncVideo_flag:
            w = int(video_capture.cap.get(3))
            h = int(video_capture.cap.get(4))
        else:
            w = int(video_capture.get(3))
            h = int(video_capture.get(4))
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter('output_yolov4.avi', fourcc, 30, (w, h))
        frame_index = -1

    if udp_flag:
        HOST = ''
        PORT = 5000
        address = '192.168.2.255'
        sock =socket(AF_INET, SOCK_DGRAM)
        sock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
        sock.bind((HOST, PORT))


    fps = 0.0
    fps_imutils = imutils.video.FPS().start()
    
    savetime = 0

    while True:
        nowtime = datetime.datetime.now().isoformat()
        ret, frame = video_capture.read()  # frame shape 640*480*3
        t1 = time.time()

        if time.time() - savetime >= 30: 
            print('save data') 
            cv2.imwrite("/workspace/images/image.png", frame)
            savetime = time.time()
        image = Image.fromarray(frame[...,::-1])  # bgr to rgb
        boxes, confidence, classes = yolo.detect_image(image)

        if tracking:
            features = encoder(frame, boxes)

            detections = [Detection(bbox, confidence, cls, feature) for bbox, confidence, cls, feature in
                        zip(boxes, confidence, classes, features)]
        else:
            detections = [Detection_YOLO(bbox, confidence, cls) for bbox, confidence, cls in
                        zip(boxes, confidence, classes)]

        # 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]

        if tracking:
            # Call the tracker
            tracker.predict()
            tracker.update(detections)

            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)
                cv2.putText(frame, "ID: " + str(track.track_id), (int(bbox[0]), int(bbox[1])), 0,
                            1.5e-3 * frame.shape[0], (0, 255, 0), 1)
                # socket
                message = str(nowtime + "," + str(track.track_id) + "," + str(int(bbox[0])) + "," + str(int(bbox[1])) + "," + str(int(bbox[2])) + "," + str(int(bbox[3])))
                bmessage = message.encode('utf-8')
                print(type(bmessage))
                if udp_flag:
                    sock.sendto(message.encode('utf-8'), (address, PORT))


        for det in detections:
            bbox = det.to_tlbr()
            score = "%.2f" % round(det.confidence * 100, 2) + "%"
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 0, 0), 2)
            if len(classes) > 0:
                cls = det.cls
                cv2.putText(frame, str(cls) + " " + score, (int(bbox[0]), int(bbox[3])), 0,
                            1.5e-3 * frame.shape[0], (0, 255, 0), 1)

        #cv2.imshow('', frame)

        if writeVideo_flag: # and not asyncVideo_flag:
            # save a frame
            out.write(frame)
            frame_index = frame_index + 1

        fps_imutils.update()

        if not asyncVideo_flag:
            fps = (fps + (1./(time.time()-t1))) / 2
            print("FPS = %f"%(fps))
        
        # Press Q to stop!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    fps_imutils.stop()
    print('imutils FPS: {}'.format(fps_imutils.fps()))

    if asyncVideo_flag:
        video_capture.stop()
    else:
        video_capture.release()

    if writeVideo_flag:
        out.release()

    cv2.destroyAllWindows()
Exemplo n.º 9
0
    freq = cv2.getTickFrequency()
    font = cv2.FONT_HERSHEY_SIMPLEX

    detector = ObjectDetectorLite()

    os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;udp"

    # initialize the input queue (frames), output queue (detections),
    # and the list of actual detections returned by the child process
    inputQueue = Queue(maxsize=1)
    outputQueue = Queue(maxsize=1)
    result = None
    print('Queue started')

    source = "rtmp://192.168.0.115/live"
    cap = VideoCaptureAsync(src=source)
    cap.start()
    time.sleep(2)

    p = threading.Thread(target=process_frame,
                         args=(
                             detector,
                             inputQueue,
                             outputQueue,
                         ))
    p.daemon = True
    p.start()

    while True:
        t1 = cv2.getTickCount()
Exemplo n.º 10
0
from videocaptureasync import VideoCaptureAsync
import pyautogui
import pyttsx3
from gui import Interface

if __name__ == '__main__':

    eyeTrack = EyeTrack()
    int2Text = ['zero', 'um', 'dois', 'tres', 'quatro', 'cinco']
    contadorOlhosFechados = 0

    gui = Interface()
    gui.speech_assistant(
        'Olá estou aqui para ajudar você a expressar algumas necessidades.  Por favor, olhe em direção ao cartão que expressa a sua necessidade'
    )
    video = VideoCaptureAsync(0)
    video.start()

    pyautogui.press('tab')

    while True:
        (grabed, frame) = video.read()
        if not grabed:
            break

        coords, frame = eyeTrack.getCoordenada(frame, debug=False)

        if coords:
            direcao = eyeTrack.getEyeDirection(coords)

            if direcao == Direction.DIREITA:
Exemplo n.º 11
0
def main(yolo):
    # Definition of the parameters
    max_cosine_distance = 0.2
    nn_budget = None
    nms_max_overlap = 1.0

    output_format = 'mp4'

    initialize_door_by_yourself = False
    door_array = None
    # 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)

    show_detections = True
    writeVideo_flag = True
    asyncVideo_flag = False

    error_values = []
    check_gpu()
    files = sorted(os.listdir('data_files/videos'))

    for video_name in files:
        print("opening video: {}".format(video_name))
        file_path = join('data_files/videos', video_name)
        output_name = 'save_data/out_' + video_name[0:-3] + output_format
        counter = Counter(counter_in=0, counter_out=0, track_id=0)
        truth = get_truth(video_name)

        if asyncVideo_flag:
            video_capture = VideoCaptureAsync(file_path)
        else:
            video_capture = cv2.VideoCapture(file_path)

        if asyncVideo_flag:
            video_capture.start()

        if writeVideo_flag:
            if asyncVideo_flag:
                w = int(video_capture.cap.get(3))
                h = int(video_capture.cap.get(4))
            else:
                w = int(video_capture.get(3))
                h = int(video_capture.get(4))
            fourcc = cv2.VideoWriter_fourcc(*'XVID')
            out = cv2.VideoWriter(output_name, fourcc, 15, (w, h))
            frame_index = -1

        fps = 0.0
        fps_imutils = imutils.video.FPS().start()

        all_doors = read_door_info('data_files/doors_info_links.json')
        door_array = all_doors[video_name]
        rect_door = Rectangle(door_array[0], door_array[1], door_array[2],
                              door_array[3])

        border_door = door_array[3]
        while True:
            ret, frame = video_capture.read()  # frame shape 640*480*3
            if not ret:
                y1 = (counter.counter_in - truth.inside)**2
                y2 = (counter.counter_out - truth.outside)**2
                total_count = counter.return_total_count()
                true_total = truth.inside + truth.outside
                if true_total != 0:
                    err = abs(total_count - true_total) / true_total
                else:
                    err = abs(total_count - true_total)
                mse = (y1 + y2) / 2
                log_res = "in video: {}\n predicted / true\n counter in: {} / {}\n counter out: {} / {}\n" \
                          " total: {} / {}\n error: {}\n mse error: {}\n______________\n".format(video_name,
                                                                                                 counter.counter_in,
                                                                                                 truth.inside,
                                                                                                 counter.counter_out,
                                                                                                 truth.outside,
                                                                                                 total_count,
                                                                                                 true_total, err, mse)
                with open('../log_results.txt', 'a') as log:
                    log.write(log_res)
                print(log_res)
                error_values.append(err)
                break

            t1 = time.time()

            image = Image.fromarray(frame[..., ::-1])  # bgr to rgb
            boxes, confidence, classes = yolo.detect_image(image)

            features = encoder(frame, boxes)
            detections = [
                Detection(bbox, confidence, cls, feature)
                for bbox, confidence, cls, feature in zip(
                    boxes, confidence, classes, features)
            ]

            # Run non-maxima suppression.
            boxes = np.array([d.tlwh for d in detections])
            scores = np.array([d.confidence for d in detections])
            classes = np.array([d.cls 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)

            cv2.rectangle(frame, (int(door_array[0]), int(door_array[1])),
                          (int(door_array[2]), int(door_array[3])),
                          (23, 158, 21), 3)
            for det in detections:
                bbox = det.to_tlbr()
                if show_detections and len(classes) > 0:
                    score = "%.2f" % (det.confidence * 100) + "%"
                    # rect_head = Rectangle(bbox[0], bbox[1], bbox[2], bbox[3])
                    # rect_door = Rectangle( int(door_array[0]), int(door_array[1]), int(door_array[2]), int(door_array[3]) )
                    # intersection = rect_head & rect_door
                    #
                    # if intersection:
                    #     squares_coeff = rect_square(*intersection)/ rect_square(*rect_head)
                    #     cv2.putText(frame, score + " inter: " + str(round(squares_coeff, 3)), (int(bbox[0]), int(bbox[3])), 0,
                    #             1e-3 * frame.shape[0], (0, 100, 255), 5)
                    cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                                  (int(bbox[2]), int(bbox[3])), (255, 0, 0), 3)

            for track in tracker.tracks:
                if not track.is_confirmed() or track.time_since_update > 1:
                    continue
                bbox = track.to_tlbr()
                # first appearence of object with id=track.id

                if track.track_id not in counter.people_init or counter.people_init[
                        track.track_id] == 0:
                    counter.obj_initialized(track.track_id)
                    rect_head = Rectangle(bbox[0], bbox[1], bbox[2], bbox[3])

                    intersection = rect_head & rect_door
                    if intersection:

                        intersection_square = rect_square(*intersection)
                        head_square = rect_square(*rect_head)
                        rat = intersection_square / head_square

                        #             1e-3 * frame.shape[0], (0, 100, 255), 5)

                        #     was initialized in door, probably going in
                        if rat >= 0.7:
                            counter.people_init[track.track_id] = 2
                            #     initialized in the bus, mb going out
                        elif rat <= 0.4 or bbox[3] > border_door:
                            counter.people_init[track.track_id] = 1
                        #     initialized between the exit and bus, not obvious state
                        elif rat > 0.4 and rat < 0.7:
                            counter.people_init[track.track_id] = 3
                            counter.rat_init[track.track_id] = rat
                    # res is None, means that object is not in door contour
                    else:
                        counter.people_init[track.track_id] = 1
                    counter.people_bbox[track.track_id] = bbox
                counter.cur_bbox[track.track_id] = bbox

                adc = "%.2f" % (track.adc *
                                100) + "%"  # Average detection confidence
                cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                              (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2)
                cv2.putText(frame, "ID: " + str(track.track_id),
                            (int(bbox[0]), int(bbox[1])), 0,
                            1e-3 * frame.shape[0], (0, 255, 0), 5)

                if not show_detections:
                    track_cls = track.cls
                    cv2.putText(frame, str(track_cls),
                                (int(bbox[0]), int(bbox[3])), 0,
                                1e-3 * frame.shape[0], (0, 255, 0), 1)
                    cv2.putText(
                        frame, 'ADC: ' + adc,
                        (int(bbox[0]), int(bbox[3] + 2e-2 * frame.shape[1])),
                        0, 1e-3 * frame.shape[0], (0, 255, 0), 1)

            id_get_lost = [
                track.track_id for track in tracker.tracks
                if track.time_since_update >= 35
            ]
            # and track.age >= 29]
            # id_inside_tracked = [track.track_id for track in tracker.tracks if track.age > 60]
            for val in counter.people_init.keys():
                # check bbox also
                inter_square = 0
                cur_square = 0
                ratio = 0
                cur_c = find_centroid(counter.cur_bbox[val])
                init_c = find_centroid(counter.people_bbox[val])
                vector_person = (cur_c[0] - init_c[0], cur_c[1] - init_c[1])

                if val in id_get_lost and counter.people_init[val] != -1:
                    rect_сur = Rectangle(counter.cur_bbox[val][0],
                                         counter.cur_bbox[val][1],
                                         counter.cur_bbox[val][2],
                                         counter.cur_bbox[val][3])
                    inter = rect_сur & rect_door
                    if inter:

                        inter_square = rect_square(*inter)
                        cur_square = rect_square(*rect_сur)
                        try:
                            ratio = inter_square / cur_square
                        except ZeroDivisionError:
                            ratio = 0

                    # if vector_person < 0 then current coord is less than initialized, it means that man is going
                    # in the exit direction
                    if vector_person[1] > 70 and counter.people_init[val] == 2 \
                            and ratio < 0.9:  # and counter.people_bbox[val][3] > border_door \
                        counter.get_in()

                    elif vector_person[1] < -70 and counter.people_init[val] == 1 \
                            and ratio >= 0.6:
                        counter.get_out()

                    elif vector_person[1] < -70 and counter.people_init[val] == 3 \
                            and ratio > counter.rat_init[val] and ratio >= 0.6:
                        counter.get_out()
                    elif vector_person[1] > 70 and counter.people_init[val] == 3 \
                            and ratio < counter.rat_init[val] and ratio < 0.6:
                        counter.get_in()

                    counter.people_init[val] = -1
                    del val

            ins, outs = counter.show_counter()
            cv2.rectangle(frame, (0, 0), (250, 50), (0, 0, 0), -1, 8)
            cv2.putText(frame, "in: {}, out: {} ".format(ins, outs), (10, 35),
                        0, 1e-3 * frame.shape[0], (255, 255, 255), 3)

            cv2.namedWindow('video', cv2.WINDOW_NORMAL)
            cv2.resizeWindow('video', 1422, 800)
            cv2.imshow('video', frame)

            if writeVideo_flag:
                # save a frame
                out.write(frame)
                frame_index = frame_index + 1

            fps_imutils.update()

            if not asyncVideo_flag:
                fps = (fps + (1. / (time.time() - t1))) / 2
                # print("FPS = %f" % fps)

            # Press Q to stop!
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        fps_imutils.stop()
        print('imutils FPS: {}'.format(fps_imutils.fps()))

        if asyncVideo_flag:
            video_capture.stop()
        else:
            video_capture.release()

        if writeVideo_flag:
            out.release()

        cv2.destroyAllWindows()
        del door_array

    mean_error = np.mean(error_values)
    print("mean error for {} videos: {}".format(len(files), mean_error))
Exemplo n.º 12
0
    def run(self):
        asyncVideo_flag = self.args.asyncVideo_flag
        writeVideo_flag = self.args.writeVideo_flag

        if asyncVideo_flag:
            video_capture = VideoCaptureAsync(self.video_path)
        else:
            video_capture = cv2.VideoCapture(self.video_path)

        if asyncVideo_flag:
            video_capture.start()

        if writeVideo_flag:
            if asyncVideo_flag:
                w = int(video_capture.cap.get(3))
                h = int(video_capture.cap.get(4))
            else:
                w = int(video_capture.get(3))
                h = int(video_capture.get(4))
            fourcc = cv2.VideoWriter_fourcc(*'XVID')
            out = cv2.VideoWriter('output_yolov4.avi', fourcc, 30, (w, h))
            frame_index = -1

        fps = 0.0
        fps_imutils = imutils.video.FPS().start()

        count_frame = 0
        while True:
            count_frame += 1
            t1 = time.time()
            ret, frame = video_capture.read()
            if ret != True:
                break

            _frame = frame
            # process
            frame = self.process(frame, _frame, count_frame)

            # visualize
            if self.args.visualize:
                frame = imutils.resize(frame, width=1000)
                cv2.imshow("Final result", frame)

            if writeVideo_flag:  # and not asyncVideo_flag:
                # save a frame
                out.write(frame)
                frame_index = frame_index + 1

            fps_imutils.update()

            if not asyncVideo_flag:
                fps = (fps + (1. / (time.time() - t1))) / 2
                print("FPS = %f" % (fps))

            # Press Q to stop!
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        fps_imutils.stop()
        print('imutils FPS: {}'.format(fps_imutils.fps()))

        if asyncVideo_flag:
            video_capture.stop()
        else:
            video_capture.release()

        if writeVideo_flag:
            out.release()

        cv2.destroyAllWindows()
Exemplo n.º 13
0
def main(width=640, height=360, k=False):
    last_detected = datetime(1990, 1, 1)
    if k:
        cap = VideoCaptureAsync(0)
    else:
        cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    if k:
        cap.start()
    t0 = time.time()
    i = 0
    net = Detector(bytes("cfg/yolo-lite.cfg", encoding="utf-8"),
                   bytes("moirai.weights", encoding="utf-8"), 0,
                   bytes("obj.data", encoding="utf-8"))
    while True:
        r, frame = cap.read()
        if r:
            dark_frame = Image(frame)
            results = net.detect(dark_frame)
            del dark_frame

            for cat, score, bounds in results:
                x, y, w, h = bounds
                cv2.rectangle(frame, (int(x - w / 2), int(y - h / 2)),
                              (int(x + w / 2), int(y + h / 2)), (255, 0, 255))
            if len(results) > 0:
                if datetime.now() > last_detected + timedelta(seconds=6):
                    last_detected = datetime.now()
                    prob = results[0][1]
                    requests.post('http://192.168.6.219:8080/area/alert',
                                  data={
                                      "cam_id": 1,
                                      "prob": prob
                                  })
        cv2.imshow('Frame', frame)
        cv2.waitKey(1) & 0xFF
    if k:
        cap.stop()
    cv2.destroyAllWindows()
Exemplo n.º 14
0
def main(yolo):
    # Definition of the parameters
    max_cosine_distance = 0.2
    nn_budget = None
    nms_max_overlap = 1.0
    output_format = 'mp4'

    # 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)

    show_detections = True
    writeVideo_flag = True
    asyncVideo_flag = False

    fpeses = []

    check_gpu()
    video_name = 'test1.mp4'

    print("opening video: {}".format(video_name))
    file_path = join('data_files/videos', video_name)
    output_name = 'save_data/out_' + video_name[0:-3] + output_format
    counter = Counter(counter_in=0, counter_out=0, track_id=0)

    if asyncVideo_flag:
        video_capture = VideoCaptureAsync(file_path)
    else:
        video_capture = cv2.VideoCapture(file_path)

    if asyncVideo_flag:
        video_capture.start()
        w = int(video_capture.cap.get(3))
        h = int(video_capture.cap.get(4))
    else:
        w = int(video_capture.get(3))
        h = int(video_capture.get(4))

    if writeVideo_flag:
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter(output_name, fourcc, 15, (w, h))

    left_array = [0, 0, w / 2, h]
    fps = 0.0
    fps_imutils = imutils.video.FPS().start()

    rect_left = Rectangle(left_array[0], left_array[1], left_array[2],
                          left_array[3])

    border_door = left_array[3]
    while True:
        ret, frame = video_capture.read()  # frame shape 640*480*3
        if not ret:
            with open('log_results.txt', 'a') as log:
                log.write('1')
            break

        t1 = time.time()

        image = Image.fromarray(frame[..., ::-1])  # bgr to rgb
        boxes, confidence, classes = yolo.detect_image(image)

        features = encoder(frame, boxes)
        detections = [
            Detection(bbox, confidence, cls,
                      feature) for bbox, confidence, cls, feature in zip(
                          boxes, confidence, classes, features)
        ]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        classes = np.array([d.cls 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)

        cv2.rectangle(frame, (int(left_array[0]), int(left_array[1])),
                      (int(left_array[2]), int(left_array[3])), (23, 158, 21),
                      3)
        if len(detections) != 0:
            counter.someone_inframe()
            for det in detections:
                bbox = det.to_tlbr()
                if show_detections and len(classes) > 0:
                    score = "%.2f" % (det.confidence * 100) + "%"
                    cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                                  (int(bbox[2]), int(bbox[3])), (255, 0, 0), 3)
        else:
            if counter.need_to_clear():
                counter.clear_all()

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

            if track.track_id not in counter.people_init or counter.people_init[
                    track.track_id] == 0:
                counter.obj_initialized(track.track_id)
                ratio_init = find_ratio_ofbboxes(bbox=bbox,
                                                 rect_compare=rect_left)

                if ratio_init > 0:
                    if ratio_init >= 0.8 and bbox[3] < left_array[3]:
                        counter.people_init[
                            track.track_id] = 2  # man in left side
                    elif ratio_init < 0.8 and bbox[3] > left_array[
                            3]:  # initialized in the bus, mb going out
                        counter.people_init[track.track_id] = 1
                else:
                    counter.people_init[track.track_id] = 1
                counter.people_bbox[track.track_id] = bbox
            counter.cur_bbox[track.track_id] = bbox

            adc = "%.2f" % (track.adc *
                            100) + "%"  # Average detection confidence
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                          (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2)
            cv2.putText(frame, "ID: " + str(track.track_id),
                        (int(bbox[0]), int(bbox[1]) + 50), 0,
                        1e-3 * frame.shape[0], (0, 255, 0), 5)

            if not show_detections:
                track_cls = track.cls
                cv2.putText(frame, str(track_cls),
                            (int(bbox[0]), int(bbox[3])), 0,
                            1e-3 * frame.shape[0], (0, 255, 0), 1)
                cv2.putText(
                    frame, 'ADC: ' + adc,
                    (int(bbox[0]), int(bbox[3] + 2e-2 * frame.shape[1])), 0,
                    1e-3 * frame.shape[0], (0, 255, 0), 1)

        id_get_lost = [
            track.track_id for track in tracker.tracks
            if track.time_since_update >= 5
        ]

        # TODO clear people_init and other dicts
        for val in counter.people_init.keys():
            ratio = 0
            cur_c = find_centroid(counter.cur_bbox[val])
            init_c = find_centroid(counter.people_bbox[val])
            vector_person = (cur_c[0] - init_c[0], cur_c[1] - init_c[1])

            if val in id_get_lost and counter.people_init[val] != -1:
                ratio = find_ratio_ofbboxes(bbox=counter.cur_bbox[val],
                                            rect_compare=rect_left)

                if vector_person[0] > 200 and counter.people_init[val] == 2 \
                        and ratio < 0.7:  # and counter.people_bbox[val][3] > border_door \
                    counter.get_out()
                    print(vector_person[0], counter.people_init[val], ratio)

                elif vector_person[0] < -100 and counter.people_init[val] == 1 \
                        and ratio >= 0.7:
                    counter.get_in()
                    print(vector_person[0], counter.people_init[val], ratio)

                counter.people_init[val] = -1
                del val

        ins, outs = counter.show_counter()
        cv2.rectangle(frame, (700, 0), (950, 50), (0, 0, 0), -1, 8)
        cv2.putText(frame, "in: {}, out: {} ".format(ins, outs), (710, 35), 0,
                    1e-3 * frame.shape[0], (255, 255, 255), 3)

        cv2.namedWindow('video', cv2.WINDOW_NORMAL)
        # cv2.resizeWindow('video', 1422, 800)
        cv2.imshow('video', frame)

        if writeVideo_flag:
            out.write(frame)

        fps_imutils.update()

        if not asyncVideo_flag:
            fps = (fps + (1. / (time.time() - t1))) / 2
            print("FPS = %f" % fps)

            if len(fpeses) < 15:
                fpeses.append(round(fps, 2))

            elif len(fpeses) == 15:
                # fps = round(np.median(np.array(fpeses)))
                median_fps = float(np.median(np.array(fpeses)))
                fps = round(median_fps, 1)
                print('max fps: ', fps)
                fps = 20
                counter.fps = fps
                fpeses.append(fps)

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

    if asyncVideo_flag:
        video_capture.stop()
    else:
        video_capture.release()

    if writeVideo_flag:
        out.release()

    cv2.destroyAllWindows()
Exemplo n.º 15
0
                        nargs='+',
                        default=[300, 300],
                        help="Image dimensions. ex. -D 224 224")

    parser.add_argument(
        '-c',
        '--colormode',
        type=str,
        default="bgr",
        help="RGB vs BGR color sequence. This is network dependent.")

    ARGS = parser.parse_args()

    # Create a VideoCapture object
    # camera = cv2.VideoCapture( ARGS.video )
    camera_1 = VideoCaptureAsync("rtsp://192.168.1.10/1")
    camera_2 = VideoCaptureAsync("rtsp://192.168.1.10/2")

    # Set camera resolution
    # camera.set( cv2.CAP_PROP_FRAME_WIDTH, 352 )
    # camera.set( cv2.CAP_PROP_FRAME_HEIGHT, 288 )
    camera_1.set(cv2.CAP_PROP_FRAME_WIDTH, 352)
    camera_1.set(cv2.CAP_PROP_FRAME_HEIGHT, 288)
    camera_2.set(cv2.CAP_PROP_FRAME_WIDTH, 352)
    camera_2.set(cv2.CAP_PROP_FRAME_HEIGHT, 288)

    # Load the labels file
    labels = [
        line.rstrip('\n') for line in open(ARGS.labels) if line != 'classes\n'
    ]
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)

    tracking = True
    writeVideo_flag = True
    asyncVideo_flag = False

    file_path = 'IMG_3326.MOV'
    dfObj = pd.DataFrame(
        columns=['frame_num', 'track', 'cx', 'cy', 'w', 'h', 'track_temp'])
    dfObjDTP = pd.DataFrame(columns=[
        'filename', 'frame_num', 'bb1', 'bb2', 'bb3', 'bb4', 'track',
        'track_temp', 'Height'
    ])

    if asyncVideo_flag:
        video_capture = VideoCaptureAsync(file_path)
    else:
        video_capture = cv2.VideoCapture(file_path)

    if asyncVideo_flag:
        video_capture.start()

    if writeVideo_flag:
        if asyncVideo_flag:
            w = int(video_capture.cap.get(3))
            h = int(video_capture.cap.get(4))
        else:
            w = int(video_capture.get(3))
            h = int(video_capture.get(4))
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter('output_yolov4.avi', fourcc, 30, (w, h))
        frame_index = -1

    fps = 0.0
    fps_imutils = imutils.video.FPS().start()

    while True:
        ret, frame = video_capture.read()  # frame shape 640*480*3
        if ret != True:
            break

        t1 = time.time()

        image = Image.fromarray(frame[..., ::-1])  # bgr to rgb
        boxes, confidence, classes = yolo.detect_image(image)

        if tracking:
            features = encoder(frame, boxes)

            detections = [
                Detection(bbox, confidence, cls, feature)
                for bbox, confidence, cls, feature in zip(
                    boxes, confidence, classes, features)
            ]
        else:
            detections = [
                Detection_YOLO(bbox, confidence, cls)
                for bbox, confidence, cls in zip(boxes, confidence, classes)
            ]

        # 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]

        if tracking:
            # Call the tracker

            tracker.predict()
            tracker.update(detections)

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

                #Ini buat cropping gambar per frame

                #cropped_image = frame[int(bbox[1]):int(bbox[1])+(int(bbox[3])-int(bbox[1])),int(bbox[0]):int(bbox[0])+(int(bbox[2])-int(bbox[0]))]
                cropped_image = frame[int(bbox[1]):int(bbox[1]) + 256,
                                      int(bbox[0]):int(bbox[0]) + 128]
                # cropped_image = frame[2:5,6:10]

                # Matiin atau comment biar ga ada box putih
                # cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2)
                #
                # cv2.putText(frame, "ID: " + str(track.track_id), (int(bbox[0]), int(bbox[1])), 0,
                #             1.5e-3 * frame.shape[0], (0, 255, 0), 1)

                # print(cropped_image)
                dirname = "output_crop/{}".format(track.track_id)
                if not os.path.exists(dirname):
                    os.makedirs(dirname)

                if (cropped_image.size == 0):
                    continue
                else:
                    writeStatus = cv2.imwrite(
                        "output_crop/{}/frame_{}.png".format(
                            track.track_id, frame_index), cropped_image)
                    print("output_crop/{}/frame_{}.png".format(
                        track.track_id, frame_index))

                # Write CSV
                dfObj = dfObj.append(pd.Series([
                    frame_index, track.track_id,
                    int(bbox[0]),
                    int(bbox[1]),
                    int(bbox[2]) - int(bbox[0]),
                    int(bbox[3]) - int(bbox[1]), track.time_since_update
                ],
                                               index=dfObj.columns),
                                     ignore_index=True)

                dfObjDTP = dfObjDTP.append(pd.Series([
                    file_path, frame_index,
                    int(bbox[0]),
                    int(bbox[1]),
                    int(bbox[2]),
                    int(bbox[3]), track.track_id, track.time_since_update,
                    int(bbox[3]) - int(bbox[1])
                ],
                                                     index=dfObjDTP.columns),
                                           ignore_index=True)

        for det in detections:
            bbox = det.to_tlbr()
            score = "%.2f" % round(det.confidence * 100, 2) + "%"

            #Matiin atau comment biar ga ada box putih di crop image
            # cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 0, 0), 2)

            # if len(classes) > 0:
            #     cls = det.cls
            #     cv2.putText(frame, str(cls) + " " + score, (int(bbox[0]), int(bbox[3])), 0,
            #                 1.5e-3 * frame.shape[0], (0, 255, 0), 1)

        cv2.imshow('', frame)

        if writeVideo_flag:  # and not asyncVideo_flag:
            # save a frame
            out.write(frame)
            frame_index = frame_index + 1

        fps_imutils.update()

        if not asyncVideo_flag:
            fps = (fps + (1. / (time.time() - t1))) / 2
            print("FPS = %f" % (fps))

        # Press Q to stop!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    fps_imutils.stop()
    print('imutils FPS: {}'.format(fps_imutils.fps()))

    if asyncVideo_flag:
        video_capture.stop()
    else:
        video_capture.release()

    if writeVideo_flag:
        out.release()

    dfObj = dfObj.sort_values(["track", "frame_num"], ascending=(True, True))
    dfObj.to_csv(r'result_temp.csv', index=False)
    dfObjDTP = dfObjDTP.sort_values(["track", "frame_num"],
                                    ascending=(True, True))
    dfObjDTP.to_csv(r'result_temp_dtp.csv', index=False)
    convert_to_final()
    cv2.destroyAllWindows()
Exemplo n.º 17
0
def test(n_frames=500, width=1280, height=720):

    cap1 = VideoCaptureAsync(
        "rtsp://*****:*****@192.168.15.200:554/moxa-cgi/udpstream_ch1")
    cap2 = VideoCaptureAsync(
        "rtsp://*****:*****@192.168.15.200:554/moxa-cgi/udpstream_ch2")
    cap3 = VideoCaptureAsync(
        "rtsp://*****:*****@192.168.15.200:554/moxa-cgi/udpstream_ch3")
    cap4 = VideoCaptureAsync(
        "rtsp://*****:*****@192.168.15.200:554/moxa-cgi/udpstream_ch4")
    cap1.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap1.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    cap2.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap2.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    cap3.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap3.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    cap4.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap4.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

    cap1.start()
    cap2.start()
    cap3.start()
    cap4.start()
    while 1:
        _, frame1 = cap1.read()
        _, frame2 = cap2.read()
        _, frame3 = cap3.read()
        _, frame4 = cap4.read()

        cv2.imshow('Frame1', frame1)
        cv2.imshow('Frame2', frame2)
        cv2.imshow('Frame3', frame3)
        cv2.imshow('Frame4', frame4)

        cv2.waitKey(1) & 0xFF

    cap1.stop()
    cap2.stop()
    cap3.stop()
    cap4.stop()
    cv2.destroyAllWindows()
Exemplo n.º 18
0
class ListScreen(Screen):
    def __init__(self, **kwargs):
        super(ListScreen, self).__init__(**kwargs)
        # run clock
        Clock.schedule_interval(self.init_gui, 0.2)
        Clock.schedule_interval(self.show_status, 0.03)

    def init_gui(self, dt):
        self.new_file()
        Clock.unschedule(self.init_gui)
        Clock.schedule_interval(self.cam_update, 0.03)

    def cam_update(self, dt):
        try:
            _, frame = self.capture.read()
            buf1 = cv2.flip(frame, 0)
            buf = buf1.tostring()
            texture1 = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='rgb')
            texture1.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
            self.ids['img_cam'].texture = texture1
        except Exception as e:
            pass

    #### File menu
    def exit_app(self):
        self.camera_disconnect()
        self.printer_disconnect()
        App.get_running_app().stop()
    def new_file(self):
        ## File menu / New Project
        self.init_project()

    def init_project(self):
        # init data
        self.project_file_path = ""
        self.project_data=data.init_project_data()
        self.project_data['CADMode']="None"
        self.ids["img_cad_origin"].set_cad_view(self.project_data)
        self.ids["img_cad_origin"].redraw_cad_view()
        self.capture = None
        self.print = None
        self.paneldisselection=[]
        try:
            self.camera_disconnect()
            self.camera_connect()
            self.printer_disconnect()
            self.printer_connect()
        except Exception as e:
            print(e, "cam or printer start problem")
            pass

    def load_file(self):
        ### File Menu / Load project
        content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
        self._popup = Popup(title="Load file", content=content,
                            size_hint=(0.9, 0.9))
        self._popup.open()
        self.project_data['CADMode']="None"

    def load(self, path, filename):
        ### click load button of Loading Dialog
        ### load all info from pre saved project file
        try:
            ### if proper project file
            self.project_file_path =  os.path.expanduser(filename[0])
            self.project_data=data.read_project_data(self.project_file_path)
            self.paneldisselection=[]
            try:
                self.camera_disconnect()
                self.camera_connect()
                self.printer_disconnect()
                self.printer_connect()
            except Exception as e:
                print(e, "cam or printer start problem")
                pass

            self.ids["img_cad_origin"].set_cad_view(self.project_data)
            self.ids["img_cad_origin"].redraw_cad_view()
        except:
            ### if not proper project file
            print("Problem loading file")
            self.dismiss_popup()
            return
        self.dismiss_popup()

    def save_file(self):
        ### File Menu / Save Project
        if self.project_file_path == "":
            self.save_as_file()
        else:
            data.write_project_data(self.project_file_path, self.project_data)

    def save_as_file(self):
        ### File Menu / Save as Project
        content = SaveDialog(save=self.save, cancel=self.dismiss_popup)
        self._popup = Popup(title="Save file", content=content,
                            size_hint=(0.9, 0.9))
        self._popup.open()
        self.project_data['CADMode']="None"

    def save(self, path, filename):
        ### after click Save button of Save Dialog
        self.project_file_path = os.path.expanduser(os.path.join(path, filename))
        print(path, filename, self.project_file_path)
        data.write_project_data(self.project_file_path, self.project_data)
        self.dismiss_popup()


    #### program menu ####
    def import_file(self):
        ### Program menu / import NC drills
        content = ImportDialog(load=self.import_ncdrill, cancel=self.dismiss_popup)
        self._popup = Popup(title="Import file", content=content,
                            size_hint=(0.9, 0.9))
        self._popup.open()
        self.project_data['CADMode']="None"

    def import_ncdrill(self, path, filename):

        ### after click load button of Loading button
        try:
            ### if proper project file
            nc_file_path  = os.path.expanduser(filename[0])
            ncdata=excellon.load_nc_drill(nc_file_path)
            print("ncdata", ncdata)
            # convert tool list for selection
            self.project_data['NCTool']=excellon.convert_to_tools(ncdata)
            # convert soldering tool path
            self.project_data['SolderToolpath']=excellon.convert_to_json(ncdata)
            # redraw
            self.ids["img_cad_origin"].redraw_cad_view()

        except:
            ### if not proper project file
            self.dismiss_popup()
            return

        self.dismiss_popup()


    def select_side(self):
        ### Program menu / Select Soldering Side
        side = [  { "text" : "Top", "is_selected" : self.project_data['SolderSide']=="Top" },
                { "text" : "Bottom", "is_selected" : self.project_data['SolderSide']=="Bottom" }  ]
        self.ignore_first=not side[0]['is_selected']

        content = ListPopup()
        args_converter = lambda row_index, rec: {'text': rec['text'], 'is_selected': rec['is_selected'], 'size_hint_y': None, 'height': 50}
        list_adapter = ListAdapter(data=side, args_converter=args_converter, propagate_selection_to_data=True, cls=ListItemButton, selection_mode='single', allow_empty_selection=False)
        list_view = ListView(adapter=list_adapter)

        content.ids.profile_list.add_widget(list_view)

        list_view.adapter.bind(on_selection_change=self.selected_side)

        self._popup = Popup(title="Select Soldering Side", content=content, size_hint=(0.5, 0.6))
        self._popup.open()
        self.project_data['CADMode']="None"

    def selected_side(self, adapter):
        if self.ignore_first:
            self.ignore_first=False
            return
        self.project_data['SolderSide']=adapter.selection[0].text
        self.dismiss_popup()
        self.ids["img_cad_origin"].redraw_cad_view()

    def select_profile(self):
        ### Program menu / Select Soldering Profile
        profile = excellon.convert_to_solderingprofile(self.project_data)
        if len(profile) < 1:
            return
        self.ignore_first=not profile[0]['is_selected']
        content = ListPopup()
        args_converter = lambda row_index, rec: {'text': rec['text'], 'is_selected': rec['is_selected'], 'size_hint_y': None, 'height': 50}
        list_adapter = ListAdapter(data=profile, args_converter=args_converter, propagate_selection_to_data=True, cls=ListItemButton, selection_mode='single', allow_empty_selection=False)
        list_view = ListView(adapter=list_adapter)

        content.ids.profile_list.add_widget(list_view)

        list_view.adapter.bind(on_selection_change=self.selected_profile)

        self._popup = Popup(title="Select Soldering Profile", content=content, size_hint=(0.5, 0.6))
        self._popup.open()
        self.project_data['CADMode']="None"

    def selected_profile(self, adapter):
        ### select profile on soldering profile list
        if self.ignore_first:
            self.ignore_first=False
            return
        num=excellon.get_solderingprofile_index_by_id(self.project_data['SolderingProfile']['SolderingProfile'], adapter.selection[0].text)
        self.project_data['SelectedSolderingProfile']=num
        self.dismiss_popup()
        self.ids["img_cad_origin"].redraw_cad_view()
        self.project_data['CADMode']="Select"

    def select_by_dia(self):
        ### Program Menu / Select soldering pad by diameter
        tools=self.project_data['NCTool']
        if len(tools) < 1:
            return
        self.ignore_first=not tools[0]['is_selected']

        content = ListPopup()
        args_converter = lambda row_index, rec: {'text': rec['text'], 'is_selected': rec['is_selected'], 'size_hint_y': None, 'height': 40}
        list_adapter = ListAdapter(data=tools, args_converter=args_converter, propagate_selection_to_data=True, cls=ListItemButton, selection_mode='single', allow_empty_selection=False)
        list_view = ListView(adapter=list_adapter)

        content.ids.profile_list.add_widget(list_view)
        list_view.adapter.bind(on_selection_change=self.selected_tools)

        self._popup = Popup(title="Select Soldering Pad by Tools", content=content, size_hint=(0.5, 0.7))
        self._popup.open()
        self.project_data['CADMode']="None"

    def selected_tools(self, adapter):
        ### select tool on tools' list
        if self.ignore_first:
            self.ignore_first=False
            return
        soldertoolpath=self.project_data['SolderToolpath']
        num=int(adapter.selection[0].text.split(":")[0])
        excellon.select_by_tool(soldertoolpath, num, self.project_data['SelectedSolderingProfile'])
        # redraw
        self.dismiss_popup()
        self.ids["img_cad_origin"].redraw_cad_view()

    def select_by_view(self):
        ### Program menu / Select Soldering pads by View
        self.project_data['CADMode']="Select"

    def deselect_by_view(self):
        ### Program Menu / Deselect by view
        self.project_data['CADMode']="Deselect"

    def set_reference1(self):
        ### Program Menu / Set Reference point 1
        self.project_data['CADMode']="Ref1"

    def set_reference2(self):
        ### Program Menu /  Set Reference Point 2
        self.project_data['CADMode']="Ref2"

    def optmize_nc(self):
        ### Program Menu / Optmize NC drills
        soldertoolpath=self.project_data['SolderToolpath']
        excellon.optimize_soldertoolpath(soldertoolpath)

    ##### panel menu
    def set_num_panel(self):
        num=excellon.get_num_panel(self.project_data['Panel'])
        content = EditPopup(save=self.save_panel_num, cancel=self.dismiss_popup)
        content.ids["btn_connect"].text = "Save"
        content.ids["text_port"].text = str(num)
        self._popup = Popup(title="Select panel num", content=content,
                            size_hint=(0.5, 0.4))
        self._popup.open()
        self.project_data['CADMode']="None"

    def save_panel_num(self, txt_port):
        # set num of panels
        num  = int(txt_port)
        num = max(1, num)
        num = min(self.project_data['Setup']['MaxPanel'], num)
        excellon.set_num_panel(self.project_data['Panel'], num)
        self.dismiss_popup()

    def set_reference_panel(self):
        #  show dialpad
        print("ref")
        self.ids["tab_panel"].switch_to(self.ids["tab_panel"].tab_list[0])
        self.content = ControlPopup(controlXYZ=self.control_XYZ, set_panel_ref1=self.set_panel_ref1, set_panel_ref2=self.set_panel_ref2, get_panel_ref1=self.get_panel_ref1, get_panel_ref2=self.get_panel_ref2, cancel=self.dismiss_popup)
        self.content.ids["cur_X"].text = format(self.project_data['Setup']['TravelX'],".2f")
        self.content.ids["cur_Y"].text = format(self.project_data['Setup']['TravelY'],".2f")
        self.content.ids["cur_Z"].text = format(self.project_data['Setup']['TravelZ'],".2f")
        self.content.ids["cur_panel"].text = "1"
        self._popup = Popup(title="Set reference point", content=self.content,
                            size_hint=(0.4, 0.4), background_color=[0, 0, 0, 0.0])
        self._popup.pos_hint={"center_x": .8, "center_y": .8}
        self._popup.open()
        self.project_data['CADMode']="None"

        # home printer
        gcode=robotcontrol.go_home(self.project_data)
        self.queue_printer_command(gcode)

    def control_XYZ(self, axis, value):
        ### click any button on dialpad, calculate new position
        index=int(self.content.ids["cur_panel"].text)
        x=float(self.content.ids["cur_X"].text)
        y=float(self.content.ids["cur_Y"].text)
        z=float(self.content.ids["cur_Z"].text)

        if axis == "X":
            x += float(value)
        elif axis == "Y":
            y += float(value)
        elif axis == "Z":
            z += float(value)
        elif axis == "HomeXY":
            x=self.project_data['Setup']['TravelX']
            y=self.project_data['Setup']['TravelY']
        elif axis == "HomeZ":
            z=self.project_data['Setup']['TravelZ']

        index = max(1, index)
        index = min(self.project_data['Setup']['MaxPanel'], index)
        x=max(self.project_data['Setup']['MinX'],x)
        x=min(self.project_data['Setup']['MaxX'],x)
        y=max(self.project_data['Setup']['MinY'],y)
        y=min(self.project_data['Setup']['MaxY'],y)
        z=max(self.project_data['Setup']['MinZ'],z)
        z=min(self.project_data['Setup']['MaxZ'],z)

        self.content.ids["cur_panel"].text = str(index)
        self.content.ids["cur_X"].text = format(x,".2f")
        self.content.ids["cur_Y"].text = format(y,".2f")
        self.content.ids["cur_Z"].text = format(z,".2f")

        # go xyz printer
        gcode=robotcontrol.go_xyz(self.project_data,x,y,z)
        self.queue_printer_command(gcode)

    def set_panel_ref1(self):
        ### click set1 button on dialpad
        index=int(self.content.ids["cur_panel"].text)
        index=min(index,excellon.get_num_panel(self.project_data['Panel']))
        index=max(index,1)

        x=float(self.content.ids["cur_X"].text)
        y=float(self.content.ids["cur_Y"].text)
        z=float(self.content.ids["cur_Z"].text)
        excellon.set_panel_reference_1(self.project_data['Panel'], index-1, x, y, z)

    def set_panel_ref2(self):
        ### click set2 button on dialpad
        index=int(self.content.ids["cur_panel"].text)
        x=float(self.content.ids["cur_X"].text)
        y=float(self.content.ids["cur_Y"].text)
        z=float(self.content.ids["cur_Z"].text)
        excellon.set_panel_reference_2(self.project_data['Panel'], index-1, x, y, z)

    def get_panel_ref1(self):
        ### click on get1 button on dialpad
        index=int(self.content.ids["cur_panel"].text)
        x,y,z = excellon.get_panel_reference_1(self.project_data['Panel'], index-1)
        if x==-1 and y==-1 and z==-1:
            x=self.project_data['Setup']['TravelX']
            y=self.project_data['Setup']['TravelY']
            z=self.project_data['Setup']['TravelZ']
        self.content.ids["cur_X"].text = format(x,".2f")
        self.content.ids["cur_Y"].text = format(y,".2f")
        self.content.ids["cur_Z"].text = format(z,".2f")
        # go xyz printer
        gcode=robotcontrol.go_xyz(self.project_data,x,y,z)
        self.queue_printer_command(gcode)

    def get_panel_ref2(self):
        ### click on get2 button on dialpad
        index=int(self.content.ids["cur_panel"].text)
        x,y,z = excellon.get_panel_reference_2(self.project_data['Panel'], index-1)
        if x==-1 and y==-1 and z==-1:
            x=self.project_data['Setup']['TravelX']
            y=self.project_data['Setup']['TravelY']
            z=self.project_data['Setup']['TravelZ']
        self.content.ids["cur_X"].text = format(x,".2f")
        self.content.ids["cur_Y"].text = format(y,".2f")
        self.content.ids["cur_Z"].text = format(z,".2f")
        # go xyz printer
        gcode=robotcontrol.go_xyz(self.project_data,x,y,z)
        self.queue_printer_command(gcode)

    def select_pcb_in_panel(self):
        num=excellon.get_num_panel(self.project_data['Panel'])
        content = EditPopup(save=self.save_pcb_in_panel, cancel=self.dismiss_popup)
        content.ids["btn_connect"].text = "Save"
        content.ids["text_port"].text = ""
        self._popup = Popup(title="Select Panels to exclude from Soldering example \"1,2\"", content=content,
                            size_hint=(0.5, 0.4))
        self._popup.open()
        self.project_data['CADMode']="None"

    def save_pcb_in_panel(self, txt_port):
        # set array of panels
        excludepanels=txt_port.split(",")
        panel=[]
        for p in range(excellon.get_num_panel(self.project_data['Panel'])):
            if str(p+1) in excludepanels:
                panel.append(p)
        self.paneldisselection=panel
        self.dismiss_popup()

    def start_soldering(self):
        ### toolbar start soldering button
        # prepare panel
        panel=[]
        for p in range(excellon.get_num_panel(self.project_data['Panel'])):
            if p not in self.paneldisselection:
                panel.append(p)
        # print
        print("panel", panel)
        gcode=robotcontrol.panel_soldering(self.project_data, panel, False)
        self.queue_printer_command(gcode)

    def test_soldering(self):
        ### toolbar test soldering button
        # prepare panel
        panel=[]
        for p in range(excellon.get_num_panel(self.project_data['Panel'])):
            if p not in self.paneldisselection:
                panel.append(p)
        # print
        gcode=robotcontrol.panel_soldering(self.project_data, panel, True)
        self.queue_printer_command(gcode)

    def pause_soldering(self):
        ### toolbar pause soldering button
        if self.print.printing:
            self.print.pause()

    def resume_soldering(self):
        ### toolbar resume soldering button
        if self.print.printing:
            self.print.resume()

    def stop_soldering(self):
        ### toolbar stop soldering button
        if self.print.printing:
            self.print.cancelprint()

    def queue_printer_command(self, gcode):
        garray=robotcontrol.make_array(gcode)
        #print("gcode raw", gcode, garray)

        gcoded = gcoder.LightGCode(garray)
        #print("gcoded", gcoded)
        if hasattr(self,'print') and self.print is not None:
            if not self.print.online or not self.print.printer:
                print("Problem with printer", self.print.online, self.print.printer)
            if self.print.printing:
                self.print.send(gcoded)
            else:
                self.print.startprint(gcoded)
        else:
            print("Problem with printer interface")
    #### Connect menu
    def set_printer(self):
        ### Connect Menu /  Connect Printer
        content = EditPopup(save=self.save_printer_port, cancel=self.dismiss_popup)
        content.ids["text_port"].text = self.project_data['Setup']['RobotPort']
        self._popup = Popup(title="Select Printer port", content=content,
                            size_hint=(0.5, 0.4))
        self._popup.open()
        self.project_data['CADMode']="None"

    def save_printer_port(self, txt_port):
        self.project_data['Setup']['RobotPort'] = txt_port
        try:
            self.printer_disconnect()
            self.printer_connect()
        except  Exception as e:
            print(e,"exception save robot port")
            pass
        self.dismiss_popup()

    def set_camera(self):
        # set camera device
        content = EditPopup(save=self.save_camera_port, cancel=self.dismiss_popup)
        content.ids["text_port"].text = self.project_data['Setup']['CameraPort']
        self._popup = Popup(title="Select Camera port", content=content,
                            size_hint=(0.5, 0.4))
        self._popup.open()
        self.project_data['CADMode']="None"

    def save_camera_port(self, txt_port):
        self.project_data['Setup']['CameraPort'] = txt_port
        try:
            self.camera_disconnect()
            self.camera_connect()
        except  Exception as e:
            print(e,"exception save cam port")
            pass
        self.dismiss_popup()

    def dismiss_popup(self):
        self._popup.dismiss()

    def camera_connect(self):
        ### connect camera
        self.capture = VideoCaptureAsync(self.project_data['Setup']['CameraPort'])
        self.capture.start()

    def camera_disconnect(self):
        if self.capture is not None:
            self.capture.stop()
            self.capture = None

    def printer_connect(self):
        if self.print is None:
            self.print = printcore(self.project_data['Setup']['RobotPort'], 115200)

    def printer_disconnect(self):
        if self.print is not None:
            self.print.disconnect()
            self.print = None

    def show_status(self, dt):
        self.ids["lbl_layer_status"].text="Layer: "+self.project_data['SolderSide']
        self.ids["lbl_cad_status"].text="CADMode: "+self.project_data['CADMode']

        profile=excellon.get_list_soldering_profile(self.project_data['SolderingProfile'])
        self.ids["lbl_profile_status"].text="Profile: "+profile[self.project_data['SelectedSolderingProfile']]
        if hasattr(self,'capture') and self.capture is not None:
            self.ids["lbl_cam_status"].text="Camera: Connected"
        else:
            self.ids["lbl_cam_status"].text="Camera: Not Found"
        #printer
        if hasattr(self,'print') and self.print is not None:
            if self.print.printer is None:
                self.ids["lbl_printer_status"].text="Robot: No 3d printer found"
            elif self.print.printing:
                if len(self.print.mainqueue)>0:
                    self.ids["lbl_printer_status"].text="Robot: Soldering "+ str(round(float(self.print.queueindex) / len(self.print.mainqueue)*100,2))+"%"
                else:
                    self.ids["lbl_printer_status"].text="Robot: Soldering"
            elif self.print.online:
                self.ids["lbl_printer_status"].text="Robot: Idle"
            else:
                self.ids["lbl_printer_status"].text="Robot: Connected"
        else:
            self.ids["lbl_printer_status"].text="Robot: Not Found"
Exemplo n.º 19
0
class CameraSource(object):
  def __init__(self, cameraSource, height, output_file=None, startFrame=0,
               async_read=False, outputToServer=False, capture_size=None):
    if async_read:
      self.camera = VideoCaptureAsync(cameraSource)
    else:
      self.camera = cv2.VideoCapture(cameraSource)

    if capture_size is not None:
      print(capture_size)
      self.camera.set(cv2.CAP_PROP_FRAME_WIDTH, capture_size[0])
      self.camera.set(cv2.CAP_PROP_FRAME_HEIGHT, capture_size[1])

    if async_read:
      self.ORIGINAL_WIDTH = self.camera.width
      self.ORIGINAL_HEIGHT = self.camera.height
    else:
      self.ORIGINAL_WIDTH = int(self.camera.get(cv2.CAP_PROP_FRAME_WIDTH))
      self.ORIGINAL_HEIGHT = int(self.camera.get(cv2.CAP_PROP_FRAME_HEIGHT))

    print('CameraSource')
    print('Requested capture size', capture_size)
    print('Actual capture size', self.ORIGINAL_WIDTH, self.ORIGINAL_HEIGHT)

    self.HEIGHT = height
    self.WIDTH = self.ORIGINAL_WIDTH * self.HEIGHT // self.ORIGINAL_HEIGHT
    self.WIDTH = self.WIDTH + self.WIDTH % 2  # Make it even.

    self.startFrame = startFrame
    self.nFrames = 0
    self.writer = VideoWriter(output_file)

    if async_read:
      self.camera.start()

    self.outputToServer = outputToServer
    if outputToServer:
      # https://robotpy.readthedocs.io/en/stable/vision/code.html
      pass
      #self.outputStream = CameraServer.getInstance().putVideo(
      #    'ProcessedVisionFrame', self.WIDTH, self.HEIGHT)


  def GetFrame(self):  
    # Processing on first call. 
    if self.nFrames == 0:
      # Skip some frames if requested.
      if self.startFrame > 0:
        skippedFrames = 0
        while True:
          ret, frame = self.camera.read()
          if not ret or frame is None:
            print('No more frames')
            return None
          skippedFrames += 1
          if skippedFrames >= self.startFrame:
            break
      # Start timer for first frame.
      self.startTime = time.time()

    # Get frame.
    frame = None
    frameTime = time.time() 
    if self.nFrames > 0 and self.nFrames % 50 == 0:
      print('FPS: ', self.nFrames / (frameTime - self.startTime))
    self.nFrames += 1
    ret, frame = self.camera.read()
    if ret and frame is not None:
      if frame.shape[0] != self.HEIGHT:
        frame = cv2.resize(frame, (self.WIDTH, self.HEIGHT))
    return frame


  def ImageSize(self):
    return (self.WIDTH, self.HEIGHT)


  def OutputFrameAndTestContinue(self, message, frame, height=None):
    self.writer.OutputFrame(frame)
    if self.outputToServer:
      self.outputStream.putFrame(frame)
    return ShowFrameAndTestContinue(message, frame, height)


  def __del__(self):
    self.camera.release()
    cv2.destroyAllWindows()
Exemplo n.º 20
0
 def camera_connect(self):
     ### connect camera
     self.capture = VideoCaptureAsync(self.project_data['Setup']['CameraPort'])
     self.capture.start()
Exemplo n.º 21
0
def main(yolo):
    # Definition of the parameters
    max_cosine_distance = 0.2
    nn_budget = None
    nms_max_overlap = 1.0

    output_format = 'mp4'
    video_name = 'bus4_2in_4out.mp4'
    file_path = join('data_files/videos', video_name)
    output_name = 'save_data/out_' + video_name[0:-3] + output_format
    initialize_door_by_yourself = False
    door_array = None
    # 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)

    show_detections = True
    writeVideo_flag = True
    asyncVideo_flag = False

    counter = Counter(counter_in=0, counter_out=0, track_id=0)

    if asyncVideo_flag:
        video_capture = VideoCaptureAsync(file_path)
    else:
        video_capture = cv2.VideoCapture(file_path)

    if asyncVideo_flag:
        video_capture.start()

    if writeVideo_flag:
        if asyncVideo_flag:
            w = int(video_capture.cap.get(3))
            h = int(video_capture.cap.get(4))
        else:
            w = int(video_capture.get(3))
            h = int(video_capture.get(4))
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter(output_name, fourcc, 15, (w, h))
        frame_index = -1

    fps = 0.0
    fps_imutils = imutils.video.FPS().start()

    ret, first_frame = video_capture.read()

    if door_array is None:
        if initialize_door_by_yourself:
            door_array = select_object(first_frame)[0]
            print(door_array)
        else:
            all_doors = read_door_info('data_files/doors_info.csv')
            door_array = all_doors[video_name]

    border_door = door_array[3]
    error_values = []
    truth = get_truth(video_name)
    while True:
        ret, frame = video_capture.read()  # frame shape 640*480*3
        if not ret:
            total_count = counter.return_total_count()
            true_total = truth.inside + truth.outside
            err = abs(total_count - true_total) / true_total
            log_res = "in video: {}\n predicted / true\n counter in: {} / {}\n counter out: {} / {}\n" \
                      " total: {} / {}\n error: {}\n______________\n".format(video_name, counter.counter_in,
                                                                             truth.inside,
                                                                             counter.counter_out, truth.outside,
                                                                             total_count, true_total, err)
            with open('log_results.txt', 'w') as file:
                file.write(log_res)
            print(log_res)
            error_values.append(err)
            break

        t1 = time.time()

        image = Image.fromarray(frame[..., ::-1])  # bgr to rgb
        boxes, confidence, classes = yolo.detect_image(image)

        features = encoder(frame, boxes)
        detections = [
            Detection(bbox, confidence, cls,
                      feature) for bbox, confidence, cls, feature in zip(
                          boxes, confidence, classes, features)
        ]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        classes = np.array([d.cls 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)

        cv2.rectangle(frame, (int(door_array[0]), int(door_array[1])),
                      (int(door_array[2]), int(door_array[3])), (23, 158, 21),
                      2)

        for det in detections:
            bbox = det.to_tlbr()
            if show_detections and len(classes) > 0:
                score = "%.2f" % (det.confidence * 100) + "%"
                rect_head = Rectangle(bbox[0], bbox[1], bbox[2], bbox[3])
                rect_door = Rectangle(int(door_array[0]), int(door_array[1]),
                                      int(door_array[2]), int(door_array[3]))
                intersection = rect_head & rect_door

                if intersection:
                    squares_coeff = rect_square(*intersection) / rect_square(
                        *rect_head)
                    cv2.putText(
                        frame,
                        score + " inter: " + str(round(squares_coeff, 3)),
                        (int(bbox[0]), int(bbox[3])), 0, 1e-3 * frame.shape[0],
                        (0, 100, 255), 5)
                    cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                                  (int(bbox[2]), int(bbox[3])), (255, 0, 0), 3)

        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            bbox = track.to_tlbr()
            # first appearence of object with id=track.id

            if track.track_id not in counter.people_init or counter.people_init[
                    track.track_id] == 0:
                counter.obj_initialized(track.track_id)
                rect_head = Rectangle(bbox[0], bbox[1], bbox[2], bbox[3])
                rect_door = Rectangle(door_array[0], door_array[1],
                                      door_array[2], door_array[3])
                res = rect_head & rect_door
                if res:

                    inter_square = rect_square(*res)
                    head_square = rect_square(*rect_head)
                    #     was initialized in door, probably going in
                    if (inter_square / head_square) >= 0.8:
                        counter.people_init[track.track_id] = 2
                        #     initialized in the bus, mb going out
                    elif (inter_square /
                          head_square) <= 0.4 or bbox[3] > border_door:
                        counter.people_init[track.track_id] = 1
                # res is None, means that object is not in door contour
                else:
                    counter.people_init[track.track_id] = 1

                counter.people_bbox[track.track_id] = bbox
            counter.cur_bbox[track.track_id] = bbox

            adc = "%.2f" % (track.adc *
                            100) + "%"  # Average detection confidence
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                          (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2)
            cv2.putText(frame, "ID: " + str(track.track_id),
                        (int(bbox[0]), int(bbox[1])), 0, 1e-3 * frame.shape[0],
                        (0, 255, 0), 5)

            if not show_detections:
                track_cls = track.cls
                cv2.putText(frame, str(track_cls),
                            (int(bbox[0]), int(bbox[3])), 0,
                            1e-3 * frame.shape[0], (0, 255, 0), 1)
                cv2.putText(
                    frame, 'ADC: ' + adc,
                    (int(bbox[0]), int(bbox[3] + 2e-2 * frame.shape[1])), 0,
                    1e-3 * frame.shape[0], (0, 255, 0), 1)

        id_get_lost = [
            track.track_id for track in tracker.tracks
            if track.time_since_update >= 25 and track.age >= 29
        ]
        id_inside_tracked = [
            track.track_id for track in tracker.tracks if track.age > 60
        ]
        for val in counter.people_init.keys():
            # check bbox also
            cur_c = find_centroid(counter.cur_bbox[val])
            init_c = find_centroid(counter.people_bbox[val])
            vector_person = (cur_c[0] - init_c[0], cur_c[1] - init_c[1])

            if val in id_get_lost and counter.people_init[val] != -1:
                # if vector_person < 0 then current coord is less than initialized, it means that man is going
                # in the exit direction
                if vector_person[1] > 70 and counter.people_init[
                        val] == 2:  # and counter.people_bbox[val][3] > border_door \
                    counter.get_in()

                elif vector_person[1] < -70 and counter.people_init[val] == 1:
                    counter.get_out()

                counter.people_init[val] = -1
                print(f"person left frame")
                print(f"current centroid - init : {cur_c} - {init_c}\n")
                print(f"vector: {vector_person}\n")

                del val
            # elif val in id_inside_tracked and val not in id_get_lost and counter.people_init[val] == 1 \
            #         and bb_intersection_over_union(counter.cur_bbox[val], door_array) <= 0.3 \
            #         and vector_person[1] > 0:  # and \
            #     # counter.people_bbox[val][3] > border_door:
            #     counter.get_in()
            #
            #     counter.people_init[val] = -1
            #     print(f"person is tracked for a long time")
            #     print(f"current centroid - init : {cur_c} - {init_c}\n")
            #     print(f"vector: {vector_person}\n")
            #     imaggg = cv2.line(frame, find_centroid(counter.cur_bbox[val]),
            #                       find_centroid(counter.people_bbox[val]),
            #                       (0, 0, 255), 7)

            # cv2.imshow('frame', imaggg)
            # cv2.waitKey(0)

        ins, outs = counter.show_counter()
        cv2.putText(frame, "in: {}, out: {} ".format(ins, outs), (10, 30), 0,
                    1e-3 * frame.shape[0], (255, 0, 0), 5)

        cv2.namedWindow('image', cv2.WINDOW_NORMAL)
        cv2.resizeWindow('image', 1400, 800)
        cv2.imshow('image', frame)

        if writeVideo_flag:
            # save a frame
            out.write(frame)
            frame_index = frame_index + 1

        fps_imutils.update()

        if not asyncVideo_flag:
            fps = (fps + (1. / (time.time() - t1))) / 2
            # print("FPS = %f" % (fps))

        # Press Q to stop!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    fps_imutils.stop()
    print('imutils FPS: {}'.format(fps_imutils.fps()))

    if asyncVideo_flag:
        video_capture.stop()
    else:
        video_capture.release()

    if writeVideo_flag:
        out.release()

    cv2.destroyAllWindows()

    mean_error = np.mean(error_values)
    print("mean error for {} video: {}".format(video_name, mean_error))
Exemplo n.º 22
0
    def __init__(self):
        self.pastUnion = 2
        self.first = True
        self.path = "knowFaces/reddy.png"
        self.db_img = cv2.imread(self.path)
        self.db_img = cv2.resize(self.db_img, (150, 150),
                                 interpolation=cv2.INTER_CUBIC)

        self.cap = VideoCaptureAsync()
        self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
        self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
        self.cap.start()

        frame_width = 1280
        frame_height = 720

        # Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.
        self.out = cv2.VideoWriter('outpy.avi',
                                   cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'),
                                   10, (1280, 720))

        self.face_record1 = "nadies"
        self.nombres = {}
        self.first = True
        self.accuracy = 2

        self.know_persons = getKnowPersonsFromDB()
        self.faces_encoding = []
        self.face_names = []
        self.known_face_encodings, self.known_face_names = self.getEncodingFaces(
            self.know_persons)
        self.face_locations = []
        self.face_encodings = []
        self.face_names = []
        self.process_this_frame = True

        while True:
            ret, frame = self.cap.read()
            small_frame = cv2.resize(frame, (0, 0), fx=1, fy=1)
            #mitad de la calidad
            rgb_small_frame = small_frame[:, :, ::-1]
            if self.process_this_frame:
                self.face_locations = face_recognition.face_locations(
                    rgb_small_frame, model="cnn")  #, model ="cnn")
                self.face_encodings = self.face_enc(rgb_small_frame,
                                                    self.face_locations,
                                                    num_jitters=100)
                self.face_names = []
                self.face_values = []

                for face_encoding in self.face_encodings:
                    self.matches = face_recognition.compare_faces(
                        self.known_face_encodings,
                        face_encoding,
                        tolerance=0.30)
                    self.name = "Unknown"
                    self.values = np.linalg.norm(self.known_face_encodings -
                                                 face_encoding,
                                                 axis=1)

                    if True in self.matches:
                        first_match_index = self.matches.index(True)
                        self.accuracy = self.values[
                            first_match_index]  #get the accuracy
                        self.name = self.known_face_names[first_match_index]
                    self.face_names.append(self.name)
                    self.face_values.append(self.accuracy)  #gui

            self.process_this_frame = not self.process_this_frame

            tratar = False
            for (top, right, bottom,
                 left), name, acc in zip(self.face_locations, self.face_names,
                                         self.face_values):
                """top *= 2
                right *= 2
                bottom *= 2
                left *= 2"""
                collight = (123, 123, 123)
                actual_img = cv2.resize(frame[top:bottom, left:right],
                                        (150, 150),
                                        interpolation=cv2.INTER_CUBIC)  #gui
                cv2.rectangle(frame, (left, top), (right, bottom), collight,
                              1)  #face bordes
                ##calcular el tamaño entre top y left
                vertical = bottom - top
                horizontal = right - left
                #draw contorns
                r = right
                l = left

                t = top
                b = bottom

                alpha = vertical / 4
                alpha = int(alpha)
                betha = 2 * alpha
                if (name == "Unknown"):
                    col = (255, 255, 255)

                else:
                    col = (241, 175, 14)
                cv2.line(frame, (l, t), (l, t + alpha), col, 2)
                cv2.line(frame, (l, t), (l + alpha, t), col, 2)

                cv2.line(frame, (r, t), (r - alpha, t), col, 2)
                cv2.line(frame, (r, t), (r, t + alpha), col, 2)

                cv2.line(frame, (l, b), (l + alpha, b), col, 2)
                cv2.line(frame, (l, b), (l, b - alpha), col, 2)

                cv2.line(frame, (r, b), (r - alpha, b), col, 2)
                cv2.line(frame, (r, b), (r, b - alpha), col, 2)

                alpha = 10
                cv2.line(frame, (l - alpha, t + betha), (l + alpha, t + betha),
                         collight, 1)
                cv2.line(frame, (r - alpha, t + betha), (r + alpha, t + betha),
                         collight, 1)
                cv2.line(frame, (l + betha, t - alpha), (l + betha, t + alpha),
                         collight, 1)
                cv2.line(frame, (l + betha, b - alpha), (l + betha, b + alpha),
                         collight, 1)

                #print("vertical: ", vertical)
                #print("horizontal: ", horizontal)

                #cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (123, 123, 123), cv2.FILLED) #space for name
                cv2.putText(frame, name, (left + 6, bottom - 6),
                            cv2.FONT_HERSHEY_DUPLEX, 0.70, (255, 255, 0), 1)
                print("nombres: ", self.nombres)

                try:
                    if (self.nombres[self.name] >= 1):
                        self.nombres[self.name] += 1
                    else:
                        self.nombres[self.name] = 1

                except:
                    print("entrando excepcion")
                    self.nombres[self.name] = 1

                if (name != "Unknown" and self.nombres[self.name] == 7):
                    if (self.face_record1 != self.name):
                        self.dahua(
                            self.name, actual_img,
                            acc)  #causa 50fps con 0.02 y el mas bajo 0.001
                        self.face_record1 = name
                    self.nombres[self.name] = 1
            #self.out.write(frame)
            cv2.imshow('Video', frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        self.cap.stop()
        print(self.out.release())
        #out.release()
        cv2.destroyAllWindows()
Exemplo n.º 23
0
def main(yolo):

    # Definition of the parameters
    max_cosine_distance = 0.3
    nn_budget = None
    nms_max_overlap = 1.0

    width = 1280
    height = 720
    rfps = 10

    # 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)

    tracking = args.tracking
    writeVideo_flag = args.writeVideo_flag
    asyncVideo_flag = args.asyncVideo_flag
    webcamera_flag = args.webcamera_flag
    ipcamera_flag = args.ipcamera_flag
    udp_flag = args.udp_flag

    full_cam_addr, key = sd.set_address(args.ipaddress, args.cam_ip,
                                        args.cam_cmd, args.key)
    cam_ip = full_cam_addr.replace(args.cam_cmd,
                                   "").replace("rtsp://*****:*****@", "")
    if args.jpegmode:
        full_cam_addr = full_cam_addr.replace("rtsp", "http")

    print(full_cam_addr)
    print(key)

    if asyncVideo_flag:
        print("load videofile")
        video_capture = VideoCaptureAsync(args.videofile)
    elif ipcamera_flag or args.jpegmode:
        print("load ipcamera")
        video_capture = cv2.VideoCapture(full_cam_addr)
        # width = video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)
        # height = video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
        # rfps = video_capture.get(cv2.CAP_PROP_FPS)
        print("fps:{}width:{}height:{}".format(rfps, width, height))
    elif webcamera_flag:
        print("load webcamera")
        video_capture = cv2.VideoCapture(0)
    else:
        print("load videofile")
        video_capture = cv2.VideoCapture(args.videofile)

    # video_capture.start()

    if writeVideo_flag:
        if asyncVideo_flag:
            w = int(video_capture.cap.get(3))
            h = int(video_capture.cap.get(4))
        else:
            w = int(video_capture.get(3))
            h = int(video_capture.get(4))
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter('output_yolov4.avi', fourcc, 30, (w, h))
        frame_index = -1

    if udp_flag:
        HOST = ''
        PORT = 5000
        address = '192.168.2.255'
        sock = socket(AF_INET, SOCK_DGRAM)
        sock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
        sock.bind((HOST, PORT))

    fps = 0.0

    i = 0

    if not args.maskoff:
        maskbgi = Image.new('RGB', (int(width), int(height)), (0, 0, 0))
        mask = Image.open(args.maskdir + 'mask' + args.ipaddress[-1] +
                          '.png').convert("L").resize(size=(int(width),
                                                            int(height)),
                                                      resample=Image.NEAREST)

    while True:
        nowtime = datetime.datetime.now(
            timezone('Asia/Tokyo')).strftime('%Y-%m-%d %H:%M:%S.%f%z')
        ret, frame = video_capture.read()  # frame shape 640*480*3
        if not ret:
            print('cant read')
            video_capture = cv2.VideoCapture(full_cam_addr)
            continue

        t1 = time.time()

        try:
            image = Image.fromarray(frame[..., ::-1])  # bgr to rgb
        except TypeError:
            video_capture = cv2.VideoCapture(full_cam_addr)
            continue
        image = Image.composite(maskbgi, image, mask)
        boxes, confidence, classes = yolo.detect_image(image)

        if tracking:
            features = encoder(frame, boxes)

            detections = [
                Detection(bbox, confidence, cls, feature)
                for bbox, confidence, cls, feature in zip(
                    boxes, confidence, classes, features)
            ]
        else:
            detections = [
                Detection_YOLO(bbox, confidence, cls)
                for bbox, confidence, cls in zip(boxes, confidence, classes)
            ]

        # 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]
        car_data = {}
        if tracking:
            # Call the tracker
            tracker.predict()
            tracker.update(detections)

            for track in tracker.tracks:
                if not track.is_confirmed() or track.time_since_update > 1:
                    continue
                bbox = track.to_tlbr()
                car_data[str(track.track_id)] = [
                    int(bbox[0]),
                    int(bbox[1]),
                    int(bbox[2]),
                    int(bbox[3])
                ]
            sd.send_amqp(
                sd.create_jsondata(cam_ip, nowtime,
                                   time.time() - t1, car_data, args.jsonfile,
                                   args.json_path, i), key, args.AMQPHost)
            i += 1

        if not asyncVideo_flag:
            fps = (fps + (1. / (time.time() - t1))) / 2
            print("FPS = %f" % (fps))

        ### 読み飛ばし処理を追加 ###
        if not args.jsonfile and args.skip:
            if fps <= 10:
                for _i in range(int(math.ceil(rfps / fps)) - 1):
                    ret, frame = video_capture.read()

    if asyncVideo_flag:
        video_capture.stop()
    else:
        video_capture.release()

    if writeVideo_flag:
        out.release()

    cv2.destroyAllWindows()
Exemplo n.º 24
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
    asyncVideo_flag = False

    file_path = 'video.webm'
    if asyncVideo_flag :
        video_capture = VideoCaptureAsync(file_path)
    else:
        video_capture = cv2.VideoCapture(file_path)

    if asyncVideo_flag:
        video_capture.start()

    if writeVideo_flag:
        if asyncVideo_flag:
            w = int(video_capture.cap.get(3))
            h = int(video_capture.cap.get(4))
        else:
            w = int(video_capture.get(3))
            h = int(video_capture.get(4))
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter('output_yolov4.avi', fourcc, 30, (w, h))
        frame_index = -1

    fps = 0.0
    fps_imutils = imutils.video.FPS().start()

    while True:
        ret, frame = video_capture.read()  # frame shape 640*480*3
        if ret != True:
             break

        t1 = time.time()

        image = Image.fromarray(frame[...,::-1])  # bgr to rgb
        boxs = yolo.detect_image(image)[0]
        confidence = yolo.detect_image(image)[1]

        features = encoder(frame,boxs)

        detections = [Detection(bbox, confidence, feature) for bbox, confidence, feature in zip(boxs, confidence, 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)
        
        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)
            cv2.putText(frame, str(track.track_id),(int(bbox[0]), int(bbox[1])),0, 5e-3 * 200, (0,255,0),2)

        for det in detections:
            bbox = det.to_tlbr()
            score = "%.2f" % round(det.confidence * 100, 2)
            cv2.rectangle(frame,(int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])),(255,0,0), 2)
            cv2.putText(frame, score + '%', (int(bbox[0]), int(bbox[3])), 0, 5e-3 * 130, (0,255,0),2)
            
        cv2.imshow('', frame)

        if writeVideo_flag: # and not asyncVideo_flag:
            # save a frame
            out.write(frame)
            frame_index = frame_index + 1

        fps_imutils.update()

        fps = (fps + (1./(time.time()-t1))) / 2
        print("FPS = %f"%(fps))
        
        # Press Q to stop!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    fps_imutils.stop()
    print('imutils FPS: {}'.format(fps_imutils.fps()))

    if asyncVideo_flag:
        video_capture.stop()
    else:
        video_capture.release()

    if writeVideo_flag:
        out.release()

    cv2.destroyAllWindows()
Exemplo n.º 25
0
            if img.ndim == 2:
                img = np.tile(img[..., None], [1, 1, 3])
            img = resize(img, (256, 256))[..., :3]
            avatars.append(img)

    log('load checkpoints..')

    generator, kp_detector = load_checkpoints(config_path=opt.config,
                                              checkpoint_path=opt.checkpoint,
                                              device=device)

    fa = face_alignment.FaceAlignment(face_alignment.LandmarksType._2D,
                                      flip_input=True,
                                      device=device)

    cap = VideoCaptureAsync(opt.cam)
    cap.start()

    if _streaming:
        ret, frame = cap.read()
        stream = pyfakewebcam.FakeWebcam(f'/dev/video{opt.virt_cam}',
                                         frame.shape[1], frame.shape[0])

    cur_ava = 0
    avatar = None
    change_avatar(fa, avatars[cur_ava])
    passthrough = False

    cv2.namedWindow('cam', cv2.WINDOW_GUI_NORMAL)
    cv2.namedWindow('avatarify', cv2.WINDOW_GUI_NORMAL)
    cv2.moveWindow('cam', 0, 0)
Exemplo n.º 26
0
def main():

    PATH_TO_CKPT_PERSON = 'models/faster_rcnn_restnet50.pb'

    # Definition of the parameters
    max_cosine_distance = 0.3
    nn_budget = 200
    nms_max_overlap = 1.0
    yolo = YOLO()
    reid = PERSON_REID()
    frozen_person = FROZEN_GRAPH_INFERENCE(PATH_TO_CKPT_PERSON)

    # # 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)

    # file_path = 0
    if VIDEO_CAPTURE == 0 and asyncVideo_flag == True:
        video_capture = VideoCaptureAsync(file_path)
    elif VIDEO_CAPTURE == 1 and asyncVideo_flag == True:
        video_capture = myVideoCapture(file_path)
    else:
        video_capture = cv2.VideoCapture(file_path)
        im_width = int(video_capture.get(3))
        im_height = int(video_capture.get(4))

    if asyncVideo_flag:
        video_capture.start()

    if writeVideo_flag:
        if asyncVideo_flag:
            w = int(video_capture.cap.get(3))
            h = int(video_capture.cap.get(4))
        else:
            w = int(video_capture.get(3))
            h = int(video_capture.get(4))
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter(output_filename, fourcc, 30, (w, h))
        frame_index = -1

    fps = 0.0
    fps_imutils = imutils.video.FPS().start()
    boxs = list()
    confidence = list()
    persons = list()
    frame_count = 0
    track_count = 0
    num_files = 0

    while True:
        t1 = time.time()
        ret, frame = video_capture.read()  # frame shape 640*480*3
        frame_org = frame.copy()
        if ret != True:
            break
        frame_count += 1
        # print('Frame count: {}'.format(frame_count))

        # Person detection using Frozen Graph
        persons = frozen_person.run_frozen_graph(frame, im_width, im_height)
        boxs = [[
            person['left'], person['top'], person['width'], person['height']
        ] for person in persons]
        confidence = [person['confidence'] for person in persons]
        cropped_persons = list(person['cropped'] for person in persons)

        # # Person detection using YOLO - Keras-converted model
        # image = Image.fromarray(frame[...,::-1])  # bgr to rgb
        # boxs = yolo.detect_image(image)[0]
        # confidence = yolo.detect_image(image)[1]
        # cropped_persons = [np.array(frame[box[1]:box[1]+box[3], box[0]:box[0]+box[2]]) for box in boxs] #[x,y,w,h]

        # features = encoder(frame, boxs)
        if len(cropped_persons) > 0:
            features = reid.extract_feature_imgTensor(cropped_persons)
            # print(features.shape)
            detections = [
                Detection(bbox, confidence,
                          feature) for bbox, confidence, feature in zip(
                              boxs, confidence, 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)

            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])), (0, 0, 255), 2)
                cv2.putText(frame, str(track.track_id),
                            (int(bbox[0]), int(bbox[1])), 0, 5e-3 * 200,
                            (0, 255, 0), 2)

                directory = os.path.join('output', str(track.track_id))
                if not os.path.exists(directory):
                    os.makedirs(directory)
                # file_count = len([name for name in os.listdir(directory+'/') if os.path.isfile(name)])
                file_count = sum(
                    [len(files) for root, dirs, files in os.walk(directory)])
                # print(file_count)

                if file_count == 0:
                    cv2.imwrite(
                        directory + '/' + str(file_count + 1) + '.jpg',
                        frame_org[int(bbox[1]):int(bbox[3]),
                                  int(bbox[0]):int(bbox[2])])
                elif file_count > 0 and track_count % 10 == 0:
                    cv2.imwrite(
                        directory + '/' + str(file_count + 1) + '.jpg',
                        frame_org[int(bbox[1]):int(bbox[3]),
                                  int(bbox[0]):int(bbox[2])])

                track_count += 1

            for det in detections:
                bbox = det.to_tlbr()
                score = "%.2f" % round(det.confidence * 100, 2)
                cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                              (int(bbox[2]), int(bbox[3])), (255, 0, 0), 2)
                cv2.putText(frame, score + '%', (int(bbox[0]), int(bbox[3])),
                            0, 5e-3 * 130, (0, 255, 0), 2)

        cv2.imshow('YOLO DeepSort', frame)

        if writeVideo_flag:  # and not asyncVideo_flag:
            # save a frame
            out.write(frame)
            frame_index = frame_index + 1

        fps_imutils.update()

        fps = (fps + (1. / (time.time() - t1))) / 2
        # print("FPS = %f"%(fps))

        # Press Q to stop!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    fps_imutils.stop()
    # print('imutils FPS: {}'.format(fps_imutils.fps()))

    if asyncVideo_flag:
        video_capture.stop()
    else:
        video_capture.release()

    if writeVideo_flag:
        out.release()

    cv2.destroyAllWindows()
Exemplo n.º 27
0
class Recognition():

    #parametros globales para la segunda ventana
    def distance(self, accuracy, name):
        #pasar dos encodings procesa el nivel de accuracy de cada uno y devuelve un loading bar
        load = accuracy * 270
        color = (0, 0, 255)
        image = np.zeros((40, 300, 3), np.uint8)
        cv2.rectangle(image, (0, 0), (300, 50), (255, 255, 255), cv2.FILLED)
        cv2.putText(image, name, (10, 15), cv2.FONT_HERSHEY_DUPLEX, 0.5,
                    (125, 125, 0), 1)
        cv2.rectangle(image, (10, 20), (int(load) + 15, 30), color, cv2.FILLED)
        return image

    def record_date_hour(self, name):
        #insert where name date
        date = strftime("%Y/%m/%d", gmtime())
        hour = strftime("%H:%M:%S", gmtime())
        try:
            connection = psycopg2.connect(
                "dbname=registros user=reddy password=123456 port=5432 host=localhost port=5432"
            )
        except:
            print("conexion exito")
        cursor = connection.cursor()
        postgres_insert_query = """ INSERT INTO deteccion (name, fecha, hora) VALUES (%s, %s, %s)"""

        fecha = "'" + date + "'"
        hora = "'" + hour + "'"
        record_to_insert = (name, fecha, hora)
        cursor.execute(postgres_insert_query, record_to_insert)
        connection.commit()
        cursor.close()
        connection.close()

    def dahua(self, name, actual_img, accuracy):
        path = "knowFaces/" + name.lower() + ".png"
        db_img = cv2.imread(path)
        db_img = cv2.resize(db_img, (150, 150), interpolation=cv2.INTER_CUBIC)
        un_img = np.concatenate((db_img, actual_img), axis=1)
        un_img = np.concatenate((un_img, self.distance(accuracy, name)),
                                axis=0)
        self.record_date_hour(name)
        if (self.first):
            self.first = False
            cv2.imshow("Board", un_img)
        else:
            final = np.concatenate((un_img, self.pastUnion), axis=0)
            cv2.imshow("Board", final)
        self.pastUnion = un_img

        return

    def face_enc(self, face_image, known_face_locations=None, num_jitters=1):
        """
        Given an image, return the 128-dimension face encoding for each face in the image.
        :param face_image: The image that contains one or more faces
        :param known_face_locations: Optional - the bounding boxes of each face if you already know them.
        :param num_jitters: How many times to re-sample the face when calculating encoding. Higher is more accurate, but slower (i.e. 100 is 100x slower)
        :return: A list of 128-dimensional face encodings (one for each face in the image)
        """
        raw_landmarks = face_recognition.api._raw_face_landmarks(
            face_image, known_face_locations)
        return [
            np.array(
                face_encoder.compute_face_descriptor(face_image,
                                                     raw_landmark_set,
                                                     num_jitters))
            for raw_landmark_set in raw_landmarks
        ]

    def getEncodingFaces(self, know_persons):
        i = 1
        for imag in know_persons:
            image = face_recognition.load_image_file(imag.getImgSrc())
            #self.faces_encoding.append(face_recognition.face_encodings(image, num_jitters=100)[0])
            self.faces_encoding.append(
                self.face_enc(image, num_jitters=100)[0])

            self.face_names.append(imag.getNombre())
            i = i + 1
        return self.faces_encoding, self.face_names

    def __init__(self):
        self.pastUnion = 2
        self.first = True
        self.path = "knowFaces/reddy.png"
        self.db_img = cv2.imread(self.path)
        self.db_img = cv2.resize(self.db_img, (150, 150),
                                 interpolation=cv2.INTER_CUBIC)

        self.cap = VideoCaptureAsync()
        self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
        self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
        self.cap.start()

        frame_width = 1280
        frame_height = 720

        # Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.
        self.out = cv2.VideoWriter('outpy.avi',
                                   cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'),
                                   10, (1280, 720))

        self.face_record1 = "nadies"
        self.nombres = {}
        self.first = True
        self.accuracy = 2

        self.know_persons = getKnowPersonsFromDB()
        self.faces_encoding = []
        self.face_names = []
        self.known_face_encodings, self.known_face_names = self.getEncodingFaces(
            self.know_persons)
        self.face_locations = []
        self.face_encodings = []
        self.face_names = []
        self.process_this_frame = True

        while True:
            ret, frame = self.cap.read()
            small_frame = cv2.resize(frame, (0, 0), fx=1, fy=1)
            #mitad de la calidad
            rgb_small_frame = small_frame[:, :, ::-1]
            if self.process_this_frame:
                self.face_locations = face_recognition.face_locations(
                    rgb_small_frame, model="cnn")  #, model ="cnn")
                self.face_encodings = self.face_enc(rgb_small_frame,
                                                    self.face_locations,
                                                    num_jitters=100)
                self.face_names = []
                self.face_values = []

                for face_encoding in self.face_encodings:
                    self.matches = face_recognition.compare_faces(
                        self.known_face_encodings,
                        face_encoding,
                        tolerance=0.30)
                    self.name = "Unknown"
                    self.values = np.linalg.norm(self.known_face_encodings -
                                                 face_encoding,
                                                 axis=1)

                    if True in self.matches:
                        first_match_index = self.matches.index(True)
                        self.accuracy = self.values[
                            first_match_index]  #get the accuracy
                        self.name = self.known_face_names[first_match_index]
                    self.face_names.append(self.name)
                    self.face_values.append(self.accuracy)  #gui

            self.process_this_frame = not self.process_this_frame

            tratar = False
            for (top, right, bottom,
                 left), name, acc in zip(self.face_locations, self.face_names,
                                         self.face_values):
                """top *= 2
                right *= 2
                bottom *= 2
                left *= 2"""
                collight = (123, 123, 123)
                actual_img = cv2.resize(frame[top:bottom, left:right],
                                        (150, 150),
                                        interpolation=cv2.INTER_CUBIC)  #gui
                cv2.rectangle(frame, (left, top), (right, bottom), collight,
                              1)  #face bordes
                ##calcular el tamaño entre top y left
                vertical = bottom - top
                horizontal = right - left
                #draw contorns
                r = right
                l = left

                t = top
                b = bottom

                alpha = vertical / 4
                alpha = int(alpha)
                betha = 2 * alpha
                if (name == "Unknown"):
                    col = (255, 255, 255)

                else:
                    col = (241, 175, 14)
                cv2.line(frame, (l, t), (l, t + alpha), col, 2)
                cv2.line(frame, (l, t), (l + alpha, t), col, 2)

                cv2.line(frame, (r, t), (r - alpha, t), col, 2)
                cv2.line(frame, (r, t), (r, t + alpha), col, 2)

                cv2.line(frame, (l, b), (l + alpha, b), col, 2)
                cv2.line(frame, (l, b), (l, b - alpha), col, 2)

                cv2.line(frame, (r, b), (r - alpha, b), col, 2)
                cv2.line(frame, (r, b), (r, b - alpha), col, 2)

                alpha = 10
                cv2.line(frame, (l - alpha, t + betha), (l + alpha, t + betha),
                         collight, 1)
                cv2.line(frame, (r - alpha, t + betha), (r + alpha, t + betha),
                         collight, 1)
                cv2.line(frame, (l + betha, t - alpha), (l + betha, t + alpha),
                         collight, 1)
                cv2.line(frame, (l + betha, b - alpha), (l + betha, b + alpha),
                         collight, 1)

                #print("vertical: ", vertical)
                #print("horizontal: ", horizontal)

                #cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (123, 123, 123), cv2.FILLED) #space for name
                cv2.putText(frame, name, (left + 6, bottom - 6),
                            cv2.FONT_HERSHEY_DUPLEX, 0.70, (255, 255, 0), 1)
                print("nombres: ", self.nombres)

                try:
                    if (self.nombres[self.name] >= 1):
                        self.nombres[self.name] += 1
                    else:
                        self.nombres[self.name] = 1

                except:
                    print("entrando excepcion")
                    self.nombres[self.name] = 1

                if (name != "Unknown" and self.nombres[self.name] == 7):
                    if (self.face_record1 != self.name):
                        self.dahua(
                            self.name, actual_img,
                            acc)  #causa 50fps con 0.02 y el mas bajo 0.001
                        self.face_record1 = name
                    self.nombres[self.name] = 1
            #self.out.write(frame)
            cv2.imshow('Video', frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        self.cap.stop()
        print(self.out.release())
        #out.release()
        cv2.destroyAllWindows()
Exemplo n.º 28
0
def main(yolo):
    # Definition of the parameters
    with open("cfg/detection_tracker_cfg.json") as detection_config:
        detect_config = json.load(detection_config)
    with open("cfg/doors_info.json") as doors_config:
        doors_config = json.load(doors_config)
    with open("cfg/around_doors_info.json") as around_doors_config:
        around_doors_config = json.load(around_doors_config)
    model_filename = detect_config["tracking_model"]
    input_folder, output_folder = detect_config["input_folder"], detect_config[
        "output_folder"]
    meta_folder = detect_config["meta_folder"]
    output_format = detect_config["output_format"]

    # Deep SORT
    max_cosine_distance = 0.3
    nn_budget = None
    nms_max_overlap = 1.0
    encoder = gdet.create_box_encoder(model_filename, batch_size=1)
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    show_detections = True
    asyncVideo_flag = False

    check_gpu()

    # from here should start loop to process videos from folder
    # for video_name in os.listdir(input_folder):

    HOST = "localhost"
    PORT = 8075
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.bind((HOST, PORT))
        sock.listen()
        conn, addr = sock.accept()
        with conn:
            print('Connected by', addr)
            #  loop over all videos
            while True:
                data = conn.recv(1000)
                video_motion_list = data.decode("utf-8").split(';')
                videos_que = deque()
                for video_motion in video_motion_list:
                    videos_que.append(video_motion)
                video_name = videos_que.popleft()

                if not video_name.endswith(output_format):
                    continue

                print('elements in que', len(videos_que))
                print("opening video: {}".format(video_name))
                full_video_path = join(input_folder, video_name)
                # full_video_path = "rtsp://*****:*****@192.168.1.52:554/1/h264major"

                meta_name = meta_folder + video_name[:-4] + ".json"
                with open(meta_name) as meta_config_json:
                    meta_config = json.load(meta_config_json)
                camera_id = meta_config["camera_id"]
                if not os.path.exists(output_folder + str(camera_id)):
                    os.mkdir(output_folder + str(camera_id))

                output_name = output_folder + camera_id + '/out_' + video_name
                counter = Counter(counter_in=0, counter_out=0, track_id=0)
                tracker = Tracker(metric)

                if asyncVideo_flag:
                    video_capture = VideoCaptureAsync(full_video_path)
                    video_capture.start()
                    w = int(video_capture.cap.get(3))
                    h = int(video_capture.cap.get(4))
                else:
                    video_capture = cv2.VideoCapture(full_video_path)
                    w = int(video_capture.get(3))
                    h = int(video_capture.get(4))

                fourcc = cv2.VideoWriter_fourcc(*'XVID')
                out = cv2.VideoWriter(output_name, fourcc, 25, (w, h))

                door_array = doors_config["{}".format(camera_id)]
                around_door_array = tuple(
                    around_doors_config["{}".format(camera_id)])
                rect_door = Rectangle(door_array[0], door_array[1],
                                      door_array[2], door_array[3])
                border_door = door_array[3]
                #  loop over video
                save_video_flag = False
                while True:
                    fps_imutils = imutils.video.FPS().start()
                    ret, frame = video_capture.read()
                    if not ret:
                        with open('videos_saved/log_results.txt', 'a') as log:
                            log.write(
                                'processed (ret). Time: {}, camera id: {}\n'.
                                format(video_name, camera_id))
                        break
                    t1 = time.time()
                    # lost_ids = counter.return_lost_ids()
                    image = Image.fromarray(frame[..., ::-1])  # bgr to rgb
                    # image = image.crop(around_door_array)
                    boxes, confidence, classes = yolo.detect_image(image)

                    features = encoder(frame, boxes)
                    detections = [
                        Detection(bbox, confidence, cls, feature)
                        for bbox, confidence, cls, feature in zip(
                            boxes, confidence, classes, features)
                    ]

                    # Run non-maxima suppression.
                    boxes = np.array([d.tlwh for d in detections])
                    scores = np.array([d.confidence for d in detections])
                    classes = np.array([d.cls 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)

                    cv2.rectangle(frame,
                                  (int(door_array[0]), int(door_array[1])),
                                  (int(door_array[2]), int(door_array[3])),
                                  (23, 158, 21), 3)
                    if len(detections) != 0:
                        counter.someone_inframe()
                        for det in detections:
                            bbox = det.to_tlbr()
                            if show_detections and len(classes) > 0:
                                score = "%.2f" % (det.confidence * 100) + "%"
                                cv2.rectangle(frame,
                                              (int(bbox[0]), int(bbox[1])),
                                              (int(bbox[2]), int(bbox[3])),
                                              (255, 0, 0), 3)
                    else:
                        if counter.need_to_clear():
                            counter.clear_all()
                    # identities = [track.track_id for track in tracker.tracks]
                    # counter.update_identities(identities)

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

                        if track.track_id not in counter.people_init or counter.people_init[
                                track.track_id] == 0:
                            # counter.obj_initialized(track.track_id)
                            ratio_init = find_ratio_ofbboxes(
                                bbox=bbox, rect_compare=rect_door)

                            if ratio_init > 0:
                                if ratio_init >= 0.5:  # and bbox[3] < door_array[3]:
                                    counter.people_init[
                                        track.track_id] = 2  # man in the door
                                elif ratio_init < 0.5:  # and bbox[3] > door_array[3]:  # initialized in the outside
                                    counter.people_init[track.track_id] = 1
                            else:
                                counter.people_init[track.track_id] = 1
                            counter.people_bbox[track.track_id] = bbox
                        counter.cur_bbox[track.track_id] = bbox

                        adc = "%.2f" % (track.adc * 100
                                        ) + "%"  # Average detection confidence
                        cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                                      (int(bbox[2]), int(bbox[3])),
                                      (255, 255, 255), 2)
                        cv2.putText(frame, "ID: " + str(track.track_id),
                                    (int(bbox[0]), int(bbox[1]) + 50), 0,
                                    1e-3 * frame.shape[0], (0, 255, 0), 3)

                        if not show_detections:
                            track_cls = track.cls
                            cv2.putText(frame, str(track_cls),
                                        (int(bbox[0]), int(bbox[3])), 0,
                                        1e-3 * frame.shape[0], (0, 255, 0), 3)
                            cv2.putText(frame, 'ADC: ' + adc,
                                        (int(bbox[0]),
                                         int(bbox[3] + 2e-2 * frame.shape[1])),
                                        0, 1e-3 * frame.shape[0], (0, 255, 0),
                                        3)
                        # if track.time_since_update >= 15:
                        #     id_get_lost.append(track.track_id)
                    id_get_lost = [
                        track.track_id for track in tracker.tracks
                        if track.time_since_update >= 15
                    ]

                    for val in counter.people_init.keys():
                        ratio = 0
                        cur_c = find_centroid(counter.cur_bbox[val])
                        init_c = find_centroid(counter.people_bbox[val])
                        if val in id_get_lost and counter.people_init[
                                val] != -1:
                            ratio = find_ratio_ofbboxes(
                                bbox=counter.cur_bbox[val],
                                rect_compare=rect_door)
                            if counter.people_init[val] == 2 \
                                    and ratio < 0.6:  # and counter.people_bbox[val][3] > border_door \
                                counter.get_out()
                                save_video_flag = True
                                print(counter.people_init[val], ratio)
                            elif counter.people_init[val] == 1 \
                                    and ratio >= 0.6:
                                counter.get_in()
                                save_video_flag = True
                                print(counter.people_init[val], ratio)
                            counter.people_init[val] = -1

                    ins, outs = counter.return_counter()
                    cv2.rectangle(frame, (frame.shape[1] - 150, 0),
                                  (frame.shape[1], 50), (0, 0, 0), -1, 8)
                    cv2.putText(frame, "in: {}, out: {} ".format(ins, outs),
                                (frame.shape[1] - 140, 20), 0,
                                1e-3 * frame.shape[0], (255, 255, 255), 3)
                    out.write(frame)
                    fps_imutils.update()
                    if not asyncVideo_flag:
                        pass
                        # fps = (1. / (time.time() - t1))
                        # print("FPS = %f" % fps)

                        # if len(fpeses) < 15:
                        #     fpeses.append(round(fps, 2))
                        #
                        # elif len(fpeses) == 15:
                        #     # fps = round(np.median(np.array(fpeses)))
                        #     median_fps = float(np.median(np.array(fpeses)))
                        #     fps = round(median_fps, 1)
                        #     print('max fps: ', fps)
                        #     # fps = 20
                        #     counter.fps = fps
                        #     fpeses.append(fps)

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

                if asyncVideo_flag:
                    video_capture.stop()
                    del video_capture
                else:
                    video_capture.release()

                if save_video_flag:
                    with open('videos_saved/log_results.txt', 'a') as log:
                        log.write(
                            'detected!!! time: {}, camera id: {}, detected move in: {}, out: {}\n'
                            .format(video_name, camera_id, ins, outs))
                        log.write('video written {}\n\n'.format(output_name))
                    out.release()
                else:
                    if out.isOpened():
                        out.release()
                        if os.path.isfile(output_name):
                            os.remove(output_name)

                if os.path.isfile(full_video_path):
                    os.remove(full_video_path)
                if os.path.isfile(meta_name):
                    os.remove(meta_name)
                save_video_flag = False
                cv2.destroyAllWindows()
Exemplo n.º 29
0
    def run_video(self):
        # init for classify module
        clf_model = None
        clf_labels = None

        encoder = gdet.create_box_encoder(self.cfg.DEEPSORT.MODEL,
                                          batch_size=4)
        metric = nn_matching.NearestNeighborDistanceMetric(
            "cosine", self.cfg.DEEPSORT.MAX_COSINE_DISTANCE,
            self.cfg.DEEPSORT.NN_BUDGET)
        tracker = Tracker(self.cfg, metric)

        tracking = True
        writeVideo_flag = self.args.out_video
        asyncVideo_flag = False

        list_classes = ['loai_1', 'loai_2', 'loai_3', 'loai_4']
        arr_cnt_class = np.zeros((len(list_classes), self.number_MOI),
                                 dtype=int)

        fps = 0.0
        fps_imutils = imutils.video.FPS().start()
        counted_obj = []
        count_frame = 0
        objs_dict = {}

        if asyncVideo_flag:
            video_capture = VideoCaptureAsync(self.video_path)
        else:
            video_capture = cv2.VideoCapture(self.video_path)

        if asyncVideo_flag:
            video_capture.start()

        if writeVideo_flag:
            if asyncVideo_flag:
                w = int(video_capture.cap.get(3))
                h = int(video_capture.cap.get(4))
            else:
                w = int(video_capture.get(3))
                h = int(video_capture.get(4))
            fourcc = cv2.VideoWriter_fourcc(*'XVID')
            output_camname = 'output' + '_' + self.video_name + '.avi'
            out = cv2.VideoWriter(output_camname, fourcc, 10, (1280, 720))
            frame_index = -1

        while True:
            count_frame += 1
            print('frame processing .......: ', count_frame)
            ret, frame = video_capture.read()
            if ret != True:
                break

            frame_info = self.video_name + "_" + str(count_frame - 1)

            t1 = time.time()
            # frame = cv2.flip(frame, -1)

            _frame = self.process(frame, count_frame, frame_info, encoder,
                                  tracking, tracker, objs_dict, counted_obj,
                                  arr_cnt_class, clf_model, clf_labels)

            if writeVideo_flag:  # and not asyncVideo_flag:
                # save a frame
                out.write(_frame)
                frame_index = frame_index + 1

            # visualize
            if self.args.visualize:
                _frame = imutils.resize(_frame, width=1000)
                cv2.imshow("Final result", _frame)

            fps_imutils.update()

            if not asyncVideo_flag:
                fps = (fps + (1. / (time.time() - t1))) / 2
                print("FPS = %f" % (fps))

            # Press Q to stop!
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        fps_imutils.stop()
        print('imutils FPS: {}'.format(fps_imutils.fps()))

        if asyncVideo_flag:
            video_capture.stop()
        else:
            video_capture.release()

        if writeVideo_flag:
            out.release()

        cv2.destroyAllWindows()
Exemplo n.º 30
0
                img = np.tile(img[..., None], [1, 1, 3])
            img = resize(img, (256, 256))[..., :3]
            avatars.append(img)

    log('load checkpoints..')

    generator, kp_detector = load_checkpoints(config_path=opt.config,
                                              checkpoint_path=opt.checkpoint,
                                              device=device)

    fa = face_alignment.FaceAlignment(face_alignment.LandmarksType._2D,
                                      flip_input=True,
                                      device=device)

    # cap = cv2.VideoCapture(opt.cam)
    cap = VideoCaptureAsync(opt.cam)
    if not cap.isOpened():
        log("Cannot open camera. Try to choose other CAMID in './scripts/settings.sh'"
            )
        exit()
    cap.start()

    ret, frame = cap.read()
    if not ret:
        log("Cannot read from camera")
        exit()

    if _streaming:
        stream = pyfakewebcam.FakeWebcam(f'/dev/video{opt.virt_cam}',
                                         frame.shape[1], frame.shape[0])