Ejemplo n.º 1
0
    def learn_face(self, nb_image, user):
        d = Detection(self.proto, self.model)
        face_images = d.get_face(nb_image, 0.8)
        # place where images to train the recognizer on are stored
        path_to_images = "temp/dataset/%s" % user
        if not os.path.exists(path_to_images):
            os.makedirs(path_to_images)
        i = 0
        for image in face_images:
            image_name = path_to_images + "/" + str(i) + ".jpg"
            i += 1
            # cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            cv2.imwrite(image_name, image)
        ExtractEmbeddings.main(self.dataset, self.embeddings, self.proto,
                               self.model, self.embedding_model)
        TrainModel.main(self.embeddings, self.recognizer, self.le)

        path_to_user_images = self.dataset + "/" + user
        shutil.rmtree(path_to_user_images)
        # zip output so it can be sent easily
        zipname = "%s_frdata.zip" % user
        output_zip = zipfile.ZipFile(zipname, 'w')
        # for folder, subfolders, files in os.walk("output"):
        #     for file in files:
        #         output_zip.write(os.path.join(folder, file), os.path.relpath(os.path.join(folder, file), "output"),
        #                          compress_type=zipfile.ZIP_DEFLATED)
        output_zip.write("output/le.pickle", "le.pickle")
        output_zip.write("output/recognizer.pickle", "recognizer.pickle")

        output_zip.close()
        return zipname
def get_costs_matrix(actual_blobs, detections, threshold):
    # the costs matrix width has to be larger or equal than height
    rows_count = len(actual_blobs)

    if rows_count > len(detections):
        columns_count = rows_count
        for i in range(0, len(actual_blobs) - len(detections)):
            detections = np.append(detections, Detection())
    else:
        columns_count = len(detections)

    costs_matrix = np.zeros(shape=(rows_count, columns_count), dtype=float)

    for i, blob in enumerate(actual_blobs):
        for j, detection in enumerate(detections):
            if detection.position is None:
                costs_matrix[i][j] = INFINITE
            else:
                distance = euclidean_distance(
                    blob_center(blob), blob_center(detection.position)
                )

                costs_matrix[i][j] = \
                    distance if distance <= threshold else INFINITE

    return costs_matrix, detections
Ejemplo n.º 3
0
    def update(self, bbox_xcycwh, confidences, ori_img,update_fg=True):
        self.height, self.width = ori_img.shape[:2]
        # generate detections
        if update_fg or not(len(bbox_xcycwh)==len(self.feature_tmp)):
            features = self._get_features(bbox_xcycwh, ori_img)
            self.feature_tmp = features
        else:
            features = self.feature_tmp
        detections = [Detection(bbox_xcycwh[i], conf, features[i]) for i,conf in enumerate(confidences) if conf>self.min_confidence]

        # run on non-maximum supression
        # boxes = np.array([d.tlwh for d in detections])
        # scores = np.array([d.confidence for d in detections])
        # indices = non_max_suppression( boxes, self.nms_max_overlap, scores)
        # detections = [detections[i] for i in indices]

        # update tracker
        self.tracker.predict()
        self.tracker.update(detections)
        # output bbox identities
        outputs = []
        for track in self.tracker.tracks:
            #print('time_s',track.time_since_update)
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            box = track.to_tlwh()
            x1,y1,x2,y2 = self._tlwh_to_xyxy(box)
            track_id = track.track_id
            fg = self.check_in_out(track)
            outputs.append(np.array([x1,y1,x2,y2,track_id,fg], dtype=np.int))
        if len(outputs) > 0:
            outputs = np.stack(outputs,axis=0)
        return outputs
Ejemplo n.º 4
0
 def end_ship_event(self):
     time.sleep(0.05)
     tempDetection = Detection(self.args)
     tempDetection.serialId, tempDetection.x, tempDetection.y = 0, [], []
     self.fire_and_forget()
     if not self.args["TEST"]:
         string_to_send = """
            mutation{
                endShipEvent(eventId: 
                  %d
                ){ 
                id,
                  }
                }
            """ % (self.id)
         request = requests.post(self.args["URL"],
                                 json={'query': string_to_send})
         if self.args["INFO"]:
             print(
                 f"close event {self.id} type {self.eventType} in camera {self.cameraId} in time {datetime.datetime.now()}"
             )
         if request.status_code != 200 and self.args["ERRORS"]:
             if self.args["WARNINGS"]:
                 logging.warning(
                     f"Query failed to run by returning code of {request.status_code} in end_ship_event"
                 )
     else:
         if self.args["INFO"]:
             print(
                 f"close event {self.id} type {self.eventType} in camera {self.cameraId} in time {datetime.datetime.now()}"
             )
     self.open = False
     self.published = False
     self.closedTime = time.time()
Ejemplo n.º 5
0
def main():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(('localhost', 51000))

    detecter = Detection()
    dp = Display()
    ad = Audio()
    thread = Thread(target=dp.start)
    thread.setDaemon(True)
    thread.start()

    state_before = 0
    while True:
        motor_power, target_dist = detecter.detect()

        # 走行プログラムと送信
        sock.sendall(motor_power.to_bytes(2, 'big') + target_dist.to_bytes(2, 'big'))
        byte_data = sock.recv(4)
        #print(byte_data)
        state = int.from_bytes(byte_data[:2], 'big')
        ad_flag = int.from_bytes(byte_data[2:], 'big')

        #print(state)
        if state != state_before:
            dp.changeImage(state)
            if ad_flag == 1:
                if state_before == 0:
                    ad.play(0)
                elif state == 5:
                    ad.play(1)
                elif state_before == 5:
                    ad.play(2)
        state_before = state
Ejemplo n.º 6
0
 def __init__(self):
     self.c = Communicate()
     self.d = Detection()
     self.m = Measure()
     self.msg_list = MSG_LIST  # 提示消息字典
     self.cmd_dict = {}  # 命令字典
     self._func_dict()  # 填充cmd_dict,将类方法名中后缀为'_(\d+)'的方法添加进命令字典
Ejemplo n.º 7
0
    def test_metrics_bad_confidence(self):
        # [2 2 10 20; 80 80 30 40]
        true1 = BoundingBox(1, 'A', (2, 2), (12, 22))
        true2 = BoundingBox(1, 'A', (80, 80), (110, 120))

        labels = [true1, true2]

        # [4 4 10 20; 50 50 30 10; 90 90 40 50];
        pred1 = BoundingBox(1, 'A', (4, 4), (14, 24), confidence=0.1)
        pred2 = BoundingBox(1, 'A', (50, 50), (80, 60), confidence=0.1)
        pred3 = BoundingBox(1, 'A', (80, 80), (110, 120), confidence=0.1)

        predictions = [pred1, pred2, pred3]

        det = Detection(labels=labels, predictions=predictions)

        pr, re, _ = det.metrics()

        self.assertEqual(pr, 0.0)
        self.assertEqual(re, 0.0)

        pr, re, _ = det.metrics(confidence_threshold=0.1)

        self.assertEqual(pr, 2.0 / 3.0)
        self.assertEqual(re, 1.000)
Ejemplo n.º 8
0
    def get_objects(self, frame):
        """Converts bytes into Detection objects.
        
        Arguments:
            frame {Bytes} -- Raw data to parse
        
        Returns:
            [Detection], int -- list of Detections, frame taken time
        """
        time = int.from_bytes(frame[0:4], byteorder='little')
        size = len(frame)
        detected_objects = []
        for i in range(int(size/(5*4))):
            start_idx = 4 + 24 * i
            class_id = int.from_bytes(
                frame[start_idx:start_idx+4],  byteorder='little')
            x = int.from_bytes(
                frame[start_idx+4:start_idx+7],  byteorder='little')
            y = int.from_bytes(
                frame[start_idx+8:start_idx+11], byteorder='little')
            w = int.from_bytes(
                frame[start_idx+12:start_idx+15], byteorder='little')
            h = int.from_bytes(
                frame[start_idx+16:start_idx+19], byteorder='little')
            frame_id = int.from_bytes(
                frame[start_idx+20:start_idx+23], byteorder='little')

            class_name = self._dict.id_to_class_name(class_id)
            new_obj = Detection(x, y, w, h, time, frame_id, \
                class_name=class_name, class_id=class_id)
            detected_objects.append(new_obj)

        return detected_objects, time
Ejemplo n.º 9
0
def main():

    img = parseInput()
    d = Detection()
    link_coord = d.run(img)
    laser_coord = getLaserCoords(img)
    print("Link Coordinates", link_coord)
    print("Laser Coordinates", laser_coord)
Ejemplo n.º 10
0
    def __init__(self):
        self.webcam = Webcam()
        self.webcam.start()

        self.detection = Detection()

        self.x_axis = 0.0
        self.y_axis = 0.0
        self.z_axis = 0.0
        self.z_pos = -7.0
Ejemplo n.º 11
0
 def __init__(self, text_to_speech, speech_to_text):
     Feature.__init__(self)
     Speaking.__init__(self, text_to_speech)
     Emotion.__init__(self)
     self.speech_to_text = speech_to_text
     self.background_image = np.array([])
     self.detection_image = np.array([])
     self.detection = Detection()
     self.reels = [None, None, None]
     self.holds = [None, None]
     self.coins = 100
Ejemplo n.º 12
0
    def __init__(self):
        # self.state = 'start'
        self.state = 'intent'
        self.exit = False
        self.curr_msg = None
        self.menu = {}
        self.create_menu_graph()
        self.browser = 'chrome'
        # print(self.menu)
        self.spell_check_model = SpellCorrectionModel(language='en')
        self.domain_spell_check_model = SpellCorrectionModel(language='en')
        self.spell_check_model.load('en.pkl')
        self.domain_spell_check_model.load('custom_model.pkl')
        with open('data.json', "r") as f:
            self.db = json.load(f)
        f.close()
        # with open('words.json') as f:
        #     self.all_words = json.load(f)
        # f.close()

        pk_file = open('final.pkl', 'rb')
        answers = pickle.load(pk_file)
        self.answer_detector = Detection(answers=answers,
                                         detection_type='answer')

        intent_answers = []
        for each in INTENT_DATA:
            for i in INTENT_DATA[each]['data']:
                intent_answers.append(
                    (clean_text(i), INTENT_DATA[each]['number']))
        self.intent_detector = Detection(answers=intent_answers,
                                         detection_type='intent')

        port = 465  # For SSL
        smtp_server = "smtp.gmail.com"
        self.sender_email = "*****@*****.**"
        self.receiver_email = "*****@*****.**"
        password = "******"
        context = ssl.create_default_context()
        self.server = smtplib.SMTP_SSL(smtp_server, port, context=context)
        self.server.login(self.sender_email, password)
Ejemplo n.º 13
0
def convert_detections(detections, features, appearance_features,
                       detections_3d):
    detection_list = []
    if detections_3d is None:
        detections_3d = [None] * len(detections)
    for detection, feature, appearance_feature, detection_3d in zip(
            detections, features, appearance_features, detections_3d):
        x1, y1, x2, y2, conf, _, _ = detection
        box_2d = [x1, y1, x2 - x1, y2 - y1]
        if detection_3d is not None:
            x, y, z, l, w, h, theta = detection_3d
            box_3d = [x, y, z, l, w, h, theta]
        else:
            box_3d = None
        if feature is None:
            detection_list.append(
                Detection(box_2d, None, conf, appearance_feature, feature))
        else:
            detection_list.append(
                Detection(box_2d, box_3d, conf, appearance_feature, feature))

    return detection_list
Ejemplo n.º 14
0
 def detect(self, image, return_image=False):
     data = []
     if self.detector == None:
         ids, corners, centers, Hs = pyAprilTag.find(image)
         for i in range(len(ids)):
             # reversing corners since that's the order they were in for the older library
             corners_list = corners[i].tolist()
             corners_list.reverse()
             data.append(
                 Detection(ids[i], centers[i], corners_list,
                           util.compute_angle(corners_list)))
     else:
         data = [
             Detection(detection.tag_id, detection.center,
                       detection.corners,
                       util.compute_angle(detection.corners))
             for detection in self.detector.detect(image, False)
         ]
     data.sort(key=lambda entry: entry.tag_id)
     if return_image:
         return data, image
     else:
         return data
Ejemplo n.º 15
0
def main():

    # Define class
    detection = Detection()
    user_interface = UserInterface()
    # Define var
    layout_crowd_detection = user_interface.layout_crowd_detection()
    layout_define_threshold = user_interface.layout_define_threshold()
    # Define threshold
    threshold = define_threshold(layout_define_threshold)
    # Crowd detection
    if not threshold == 'exit':
        detection.crowd_detection(threshold, layout_crowd_detection)

    print("Exit system")
Ejemplo n.º 16
0
def detections_from_csv(csv_filepath: str) -> List[Detection]:
    """
    reads a detection list from csv file
    :param csv_filepath: csv file path
    :return: detections list
    """
    detections = list()
    with open(csv_filepath, newline='') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            top_right_coord = Coordinate(int(row[X_TOP_RIGHT_KEY]),
                                         int(row[Y_TOP_RIGHT_KEY]))
            bb = BoundingBox(top_right_coord, int(row[BB_W]), int(row[BB_H]))
            detection = Detection(bb, float(row[SCORE]))
            detections.append(detection)
    return detections
Ejemplo n.º 17
0
    def predicted_position_as_real(self, time):
        """When no coordinates has been linked appends
        predicted position to history.

        Arguments:
            time {int} -- image download time
        """

        previous_box = self._boxes_history[-1]
        prediced_state = self._filter.x
        time = time % 0xffffffff
        box = Detection(prediced_state[0], prediced_state[2], int(prediced_state[4]),
                        int(prediced_state[5]), time, previous_box.frame_id + 1, \
                        class_name=self._class_name)
        self._boxes_history.append(box)
        self.time_since_update += 1
Ejemplo n.º 18
0
    def run(self):

        detect = Detection(
            graph=PATH_TO_GRAPH,
            labels=PATH_TO_LABELS,
            classes=NUM_CLASSES
        )
        try:
            while True:
                connection, addr = self.socket.accept()
                client_thread = threading.Thread(
                    target=self.process,
                    args=(connection, detect)
                )  # 有新的连接建立时,创建新线程
                client_thread.start()
        except:
            detect.terminate()
            print('Exceptions Occurred !')
Ejemplo n.º 19
0
def runCAMprocess():
    d = Detection()

    while True:

        img_str = ""
        while True:
            data = camera_conn.recv(1024)
            img_str += data
            if "breakbreakbreak" in str(data):
                break
        print("Received Image")

        received_file = 'received_image.jpg'
        resized_file = 'resized_image.jpg'

        decoded_img = base64.b64decode(
            img_str.replace("breakbreakbreak", "") + "===")

        with open(received_file, 'wb') as f:
            f.write(decoded_img)
        img = cv2.imread(received_file)

        #resize_img = cv2.resize(img, (1280, 960))
        resize_img = cv2.resize(img, (960, 720))
        cv2.imwrite(resized_file, resize_img)

        b64_str = ""
        with open(resized_file, "rb") as imageFile:
            b64_str = base64.b64encode(imageFile.read())
        b64_str = b64_str + "breakbreakbreak"
        num_chunks = len(b64_str) / 1024
        for i in range(0, num_chunks - 1):
            web_conn.send(b64_str[1024 * i:1024 * (i + 1)])
        web_conn.send(b64_str[1024 * (num_chunks - 1):])
        print("Sent Image to Dashboard")

        location_data1, location_data2 = getLocations(d, img)
        controls_conn.send("clean" + location_data1)
        print(location_data1)
        camera_conn.send(location_data1)
        web_conn.send(location_data2)
        print("Sent Position Data to Pi 3 and Dashboard")
        print(" ")
def get_box_from_object_detection(yolo, image):
    (h, w) = image.shape[:2]
    center_point = (int(w / 2), int(h / 2))
    final_detections = []

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

    detections = [
        Detection(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])
    classes = np.array([d.cls for d in detections])
    indices = non_max_suppression(boxes, 1.0, scores)
    detections = [detections[i] for i in indices]

    # cv2.imshow("Bounding Box", cv2.resize(after_registration_object, (800, 500)))
    # cv2.waitKey(1)

    if len(detections) == 0:
        print("No human object detection!")
        return None

    for detection in detections:
        start_x, _, end_x, _ = detection.to_tlbr()

        # Registration only on the center area of image
        if end_x < center_point[0] or start_x > center_point[0]:
            continue
        final_detections.append(detection)

    # get biggest bounding boxes (closest object)
    if len(final_detections) >= 1:
        final_detections = sorted(final_detections,
                                  key=sort_by_bb_area,
                                  reverse=True)
    else:
        return None

    return final_detections[0].tlwh
Ejemplo n.º 21
0
def load_detections(dataset, detector, boat_class, min_conf):
    text_file_path = "detections_no_desc/%s/%s.txt" % (dataset, detector)
    f = open(text_file_path, "r")
    line = f.readline()
    detections = {}
    comps = []
    while (line):

        line = line.replace("\n", "")
        comps = line.split(",")

        if (int(comps[2]) == boat_class and float(comps[3]) > min_conf):

            if (not comps[0] in detections):
                detections[comps[0]] = []
            if (not (int(comps[4]) > 270 and int(comps[4]) < 740
                     and int(comps[5]) > 540 and int(comps[6]) > 580)):

                detections[comps[0]].append(
                    Detection(comps[3], comps[4:8], comps[8:]))

        line = f.readline()

    f.close()
    detections_after = {}
    for k in detections.keys():
        cur = detections[k]
        detections_after[k] = []
        #print('there was ',len(cur),' detections')
        for item in cur:
            contained = False
            for item2 in cur:
                overlap = get_overlap_to_self(item, item2)

                if (overlap > 0.5):
                    #print('overlap is ',overlap,' discarding...')
                    contained = True
                    break
            if (contained == False):
                detections_after[k].append(item.copy())
        #print('now there are ', len(detections_after[k]),' detections')
    return detections_after
Ejemplo n.º 22
0
    def test_precision_bad_predicts_stream(self):
        det = Detection()

        det.add_label(BoundingBox(1, 'A', (2, 2), (12, 22)))
        det.add_label(1, 'A', 80, 80, 110, 120)
        det.add_label(BoundingBox(1, 'A', (20, 20), (30, 30)))

        # [4 4 10 20; 50 50 30 10; 90 90 40 50];
        pred1 = BoundingBox(1, 'A', (4, 4), (14, 24))
        pred2 = BoundingBox(1, 'A', (50, 50), (80, 60))
        pred3 = BoundingBox(1, 'B', (80, 80), (110, 120))

        det.add_prediction(BoundingBox(1, 'A', (4, 4), (14, 24)))
        det.add_prediction(1, 'A', 50, 50, 80, 60)
        det.add_prediction(1, 'B', 80, 80, 110, 120)

        pr, re, _ = det.metrics()

        self.assertEqual(pr, 1.0 / 3.0)
        self.assertEqual(re, 1.0 / 3.0)
Ejemplo n.º 23
0
    def test_precision_one_class(self):
        # [2 2 10 20; 80 80 30 40]
        true1 = BoundingBox(1, 'A', (2, 2), (12, 22))
        true2 = BoundingBox(1, 'A', (80, 80), (110, 120))

        labels = [true1, true2]

        # [4 4 10 20; 50 50 30 10; 90 90 40 50];
        pred1 = BoundingBox(1, 'A', (4, 4), (14, 24))
        pred2 = BoundingBox(1, 'A', (50, 50), (80, 60))
        pred3 = BoundingBox(1, 'A', (80, 80), (110, 120))

        predictions = [pred1, pred2, pred3]

        det = Detection(labels=labels, predictions=predictions)

        pr, re, _ = det.metrics()

        self.assertEqual(pr, 2.0 / 3.0)
        self.assertEqual(re, 1.000)
    def __init__(self):
        super(MainGUI, self).__init__()

        self.initiate_shared_variables()

        # Create detection and load model
        self.detection = Detection(shared_variables=self.shared_variables)

        self.threadpool = QThreadPool()

        print("Multithreading with maximum %d threads" %
              self.threadpool.maxThreadCount())
        """
        self.timer = QTimer()
        self.timer.setInterval(10)
        self.timer.timeout.connect(self.print_output)
        self.timer.start()
        """

        # Start Detection thread
        self.start_worker()
    def convert_single(self, detected_object, frame_millis):
        """Converts single DetectedObject object into Detection.
        
        Arguments:
            detected_object {DetectedObject} -- Object to convert.
            frame_millis {int} -- Analyzed picture taken time.
        
        Returns:
            Detection -- Target object.
        """
        frame_millis = frame_millis % 0xffffffff

        return Detection(      \
            detected_object.x, \
            detected_object.y, \
            detected_object.w, \
            detected_object.h, \
            frame_millis,      \
            detected_object.frame_id, \
            class_name=detected_object.class_name, \
            class_id = detected_object.class_id)
Ejemplo n.º 26
0
def setup():

    global camera_conn, camera_addr, controls_conn, controls_addr, web_conn, web_addr

    camera_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    controls_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    web_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print('Sockets created.')

    camera_socket.bind((HOST, CAMERA_PORT))
    controls_socket.bind((HOST, CONTROLS_PORT))
    web_socket.bind((HOST, WEB_PORT))
    print('Socket binds complete.')

    camera_socket.listen(10)
    controls_socket.listen(10)
    web_socket.listen(10)
    print('Sockets now listening.')

    camera_conn, camera_addr = camera_socket.accept()
    controls_conn, controls_addr = controls_socket.accept()
    web_conn, web_addr = web_socket.accept()

    d = Detection()
Ejemplo n.º 27
0
from imutils.video import FPS
import numpy as np
import argparse
import imutils
import cv2
from detection import Detection

sign_detect = Detection()
stream = cv2.VideoCapture(0)
fps = FPS().start()

while True:
	
	(grabbed, frame) = stream.read()
	if not grabbed:
		break
	sign_detect.signDetected(frame)
	cv2.imshow("Frame", frame)
	if cv2.waitKey(10) == 27:
                break
	fps.update()

fps.stop()
stream.release()
cv2.destroyAllWindows()
Ejemplo n.º 28
0
    def create_network(self,
                       net_info,
                       input_width=None,
                       input_height=None,
                       input_channels=None):
        models = OrderedDict()
        blob_channels = dict()
        blob_width = dict()
        blob_height = dict()

        layers = net_info['layers']
        props = net_info['props']
        layer_num = len(layers)

        blob_channels['data'] = 3
        if input_channels != None:
            blob_channels['data'] = input_channels
        blob_height['data'] = 1
        if input_height != None:
            blob_height['data'] = input_height
        blob_width['data'] = 1
        if input_width != None:
            blob_width['data'] = input_width
        if props.has_key('input_shape'):
            blob_channels['data'] = int(props['input_shape']['dim'][1])
            blob_height['data'] = int(props['input_shape']['dim'][2])
            blob_width['data'] = int(props['input_shape']['dim'][3])

            self.width = int(props['input_shape']['dim'][3])
            self.height = int(props['input_shape']['dim'][2])
        elif props.has_key('input_dim'):
            blob_channels['data'] = int(props['input_dim'][1])
            blob_height['data'] = int(props['input_dim'][2])
            blob_width['data'] = int(props['input_dim'][3])

            self.width = int(props['input_dim'][3])
            self.height = int(props['input_dim'][2])

        if input_width != None and input_height != None:
            blob_width['data'] = input_width
            blob_height['data'] = input_height
            self.width = input_width
            self.height = input_height

        i = 0
        while i < layer_num:
            layer = layers[i]
            lname = layer['name']
            if layer.has_key('include') and layer['include'].has_key('phase'):
                phase = layer['include']['phase']
                lname = lname + '.' + phase
            ltype = layer['type']
            tname = layer['top']
            if ltype in ['Data', 'AnnotatedData']:
                if not self.omit_data_layer:
                    models[lname] = CaffeData(layer.copy())
                    data, label = models[lname].forward()
                    data_name = tname[0] if type(tname) == list else tname
                    blob_channels[data_name] = data.size(
                        1)  # len(layer['transform_param']['mean_value'])
                    blob_height[data_name] = data.size(
                        2
                    )  #int(layer['transform_param']['resize_param']['height'])
                    blob_width[data_name] = data.size(
                        3
                    )  #int(layer['transform_param']['resize_param']['width'])
                    self.height = blob_height[data_name]
                    self.width = blob_width[data_name]
                i = i + 1
                continue
            bname = layer['bottom']
            if ltype == 'Convolution':
                convolution_param = layer['convolution_param']
                channels = blob_channels[bname]
                out_filters = int(convolution_param['num_output'])
                kernel_size = int(convolution_param['kernel_size'])
                stride = int(convolution_param['stride']
                             ) if convolution_param.has_key('stride') else 1
                pad = int(convolution_param['pad']
                          ) if convolution_param.has_key('pad') else 0
                group = int(convolution_param['group']
                            ) if convolution_param.has_key('group') else 1
                dilation = 1
                if convolution_param.has_key('dilation'):
                    dilation = int(convolution_param['dilation'])
                bias = True
                if convolution_param.has_key(
                        'bias_term'
                ) and convolution_param['bias_term'] == 'false':
                    bias = False
                models[lname] = nn.Conv2d(channels,
                                          out_filters,
                                          kernel_size=kernel_size,
                                          stride=stride,
                                          padding=pad,
                                          dilation=dilation,
                                          groups=group,
                                          bias=bias)
                blob_channels[tname] = out_filters
                blob_width[tname] = (blob_width[bname] + 2 * pad -
                                     kernel_size) / stride + 1
                blob_height[tname] = (blob_height[bname] + 2 * pad -
                                      kernel_size) / stride + 1
                i = i + 1
            elif ltype == 'BatchNorm':
                momentum = 0.9
                if layer.has_key('batch_norm_param') and layer[
                        'batch_norm_param'].has_key('moving_average_fraction'):
                    momentum = float(
                        layer['batch_norm_param']['moving_average_fraction'])
                channels = blob_channels[bname]
                models[lname] = nn.BatchNorm2d(channels,
                                               momentum=momentum,
                                               affine=False)
                blob_channels[tname] = channels
                blob_width[tname] = blob_width[bname]
                blob_height[tname] = blob_height[bname]
                i = i + 1
            elif ltype == 'Scale':
                channels = blob_channels[bname]
                models[lname] = Scale(channels)
                blob_channels[tname] = channels
                blob_width[tname] = blob_width[bname]
                blob_height[tname] = blob_height[bname]
                i = i + 1
            elif ltype == 'ReLU':
                inplace = (bname == tname)
                if layer.has_key('relu_param') and layer['relu_param'].has_key(
                        'negative_slope'):
                    negative_slope = float(
                        layer['relu_param']['negative_slope'])
                    models[lname] = nn.LeakyReLU(negative_slope=negative_slope,
                                                 inplace=inplace)
                else:
                    models[lname] = nn.ReLU(inplace=inplace)
                blob_channels[tname] = blob_channels[bname]
                blob_width[tname] = blob_width[bname]
                blob_height[tname] = blob_height[bname]
                i = i + 1
            elif ltype == 'Pooling':
                kernel_size = int(layer['pooling_param']['kernel_size'])
                stride = int(layer['pooling_param']['stride'])
                padding = 0
                if layer['pooling_param'].has_key('pad'):
                    padding = int(layer['pooling_param']['pad'])
                pool_type = layer['pooling_param']['pool']
                if pool_type == 'MAX':
                    models[lname] = nn.MaxPool2d(kernel_size,
                                                 stride,
                                                 padding=padding,
                                                 ceil_mode=True)
                elif pool_type == 'AVE':
                    models[lname] = nn.AvgPool2d(kernel_size,
                                                 stride,
                                                 padding=padding,
                                                 ceil_mode=True)

                blob_width[tname] = int(
                    math.ceil((blob_width[bname] + 2 * padding - kernel_size) /
                              float(stride))) + 1
                blob_height[tname] = int(
                    math.ceil(
                        (blob_height[bname] + 2 * padding - kernel_size) /
                        float(stride))) + 1
                blob_channels[tname] = blob_channels[bname]
                i = i + 1
            elif ltype == 'Eltwise':
                operation = 'SUM'
                if layer.has_key('eltwise_param') and layer[
                        'eltwise_param'].has_key('operation'):
                    operation = layer['eltwise_param']['operation']
                bname0 = bname[0]
                bname1 = bname[1]
                models[lname] = Eltwise(operation)
                blob_channels[tname] = blob_channels[bname0]
                blob_width[tname] = blob_width[bname0]
                blob_height[tname] = blob_height[bname0]
                i = i + 1
            elif ltype == 'InnerProduct':
                filters = int(layer['inner_product_param']['num_output'])
                if blob_width[bname] != -1 or blob_height[bname] != -1:
                    channels = blob_channels[bname] * blob_width[
                        bname] * blob_height[bname]
                    models[lname] = nn.Sequential(FCView(),
                                                  nn.Linear(channels, filters))
                else:
                    channels = blob_channels[bname]
                    models[lname] = nn.Linear(channels, filters)
                blob_channels[tname] = filters
                blob_width[tname] = 1
                blob_height[tname] = 1
                i = i + 1
            elif ltype == 'Dropout':
                channels = blob_channels[bname]
                dropout_ratio = float(layer['dropout_param']['dropout_ratio'])
                models[lname] = nn.Dropout(dropout_ratio, inplace=True)
                blob_channels[tname] = blob_channels[bname]
                blob_width[tname] = blob_width[bname]
                blob_height[tname] = blob_height[bname]
                i = i + 1
            elif ltype == 'Normalize':
                channels = blob_channels[bname]
                scale = float(layer['norm_param']['scale_filler']['value'])
                models[lname] = Normalize(channels, scale)
                blob_channels[tname] = blob_channels[bname]
                blob_width[tname] = blob_width[bname]
                blob_height[tname] = blob_height[bname]
                i = i + 1
            elif ltype == 'LRN':
                local_size = int(layer['lrn_param']['local_size'])
                alpha = float(layer['lrn_param']['alpha'])
                beta = float(layer['lrn_param']['beta'])
                models[lname] = LRN(local_size, alpha, beta)
                blob_channels[tname] = blob_channels[bname]
                blob_width[tname] = blob_width[bname]
                blob_height[tname] = blob_height[bname]
                i = i + 1
            elif ltype == 'Permute':
                orders = layer['permute_param']['order']
                order0 = int(orders[0])
                order1 = int(orders[1])
                order2 = int(orders[2])
                order3 = int(orders[3])
                models[lname] = Permute(order0, order1, order2, order3)
                shape = [
                    1, blob_channels[bname], blob_height[bname],
                    blob_width[bname]
                ]
                blob_channels[tname] = shape[order1]
                blob_height[tname] = shape[order2]
                blob_width[tname] = shape[order3]
                i = i + 1
            elif ltype == 'Flatten':
                axis = int(layer['flatten_param']['axis'])
                models[lname] = Flatten(axis)
                blob_channels[tname] = blob_channels[bname] * blob_width[
                    bname] * blob_height[bname]
                blob_width[tname] = 1
                blob_height[tname] = 1
                i = i + 1
            elif ltype == 'Slice':
                axis = int(layer['slice_param']['axis'])
                assert (axis == 1)
                assert (type(tname) == list)
                slice_points = layer['slice_param']['slice_point']
                assert (type(slice_points) == list)
                assert (len(slice_points) == len(tname) - 1)
                slice_points = [int(s) for s in slice_points]
                shape = [
                    1, blob_channels[bname], blob_height[bname],
                    blob_width[bname]
                ]
                slice_points.append(shape[axis])
                models[lname] = Slice(axis, slice_points)
                prev = 0
                for idx, tn in enumerate(tname):
                    blob_channels[tn] = slice_points[idx] - prev
                    blob_width[tn] = blob_width[bname]
                    blob_height[tn] = blob_height[bname]
                    prev = slice_points[idx]
                i = i + 1
            elif ltype == 'Concat':
                axis = 1
                if layer.has_key('concat_param'
                                 ) and layer['concat_param'].has_key('axis'):
                    axis = int(layer['concat_param']['axis'])
                models[lname] = Concat(axis)
                if axis == 1:
                    blob_channels[tname] = 0
                    for bn in bname:
                        blob_channels[tname] += blob_channels[bn]
                        blob_width[tname] = blob_width[bn]
                        blob_height[tname] = blob_height[bn]
                elif axis == 2:
                    blob_channels[tname] = blob_channels[bname[0]]
                    blob_width[tname] = 1
                    blob_height[tname] = 0
                    for bn in bname:
                        blob_height[tname] += blob_height[bn]
                i = i + 1
            elif ltype == 'PriorBox':
                min_size = float(layer['prior_box_param']['min_size'])
                max_size = -1
                if layer['prior_box_param'].has_key('max_size'):
                    max_size = float(layer['prior_box_param']['max_size'])
                aspects = []
                if layer['prior_box_param'].has_key('aspect_ratio'):
                    print(layer['prior_box_param']['aspect_ratio'])
                    aspects = layer['prior_box_param']['aspect_ratio']
                    aspects = [float(aspect) for aspect in aspects]
                clip = (layer['prior_box_param']['clip'] == 'true')
                flip = False
                if layer['prior_box_param'].has_key('flip'):
                    flip = (layer['prior_box_param']['flip'] == 'true')
                step = int(layer['prior_box_param']['step'])
                offset = float(layer['prior_box_param']['offset'])
                variances = layer['prior_box_param']['variance']
                variances = [float(v) for v in variances]
                models[lname] = PriorBox(min_size, max_size, aspects, clip,
                                         flip, step, offset, variances)
                blob_channels[tname] = 1
                blob_width[tname] = 1
                blob_height[tname] = 1
                i = i + 1
            elif ltype == 'DetectionOutput':
                num_classes = int(
                    layer['detection_output_param']['num_classes'])
                bkg_label = int(
                    layer['detection_output_param']['background_label_id'])
                top_k = int(
                    layer['detection_output_param']['nms_param']['top_k'])
                keep_top_k = int(layer['detection_output_param']['keep_top_k'])
                conf_thresh = float(
                    layer['detection_output_param']['confidence_threshold'])
                nms_thresh = float(layer['detection_output_param']['nms_param']
                                   ['nms_threshold'])
                models[lname] = Detection(num_classes, bkg_label, top_k,
                                          conf_thresh, nms_thresh, keep_top_k)
                blob_channels[tname] = 1
                blob_width[tname] = 1
                blob_height[tname] = 1
                i = i + 1
            elif ltype == 'MultiBoxLoss':
                num_classes = int(layer['multibox_loss_param']['num_classes'])
                overlap_threshold = float(
                    layer['multibox_loss_param']['overlap_threshold'])
                prior_for_matching = layer['multibox_loss_param'][
                    'use_prior_for_matching'] == 'true'
                bkg_label = int(
                    layer['multibox_loss_param']['background_label_id'])
                neg_mining = True
                neg_pos = float(layer['multibox_loss_param']['neg_pos_ratio'])
                neg_overlap = float(
                    layer['multibox_loss_param']['neg_overlap'])
                models[lname] = MultiBoxLoss(num_classes,
                                             overlap_threshold,
                                             prior_for_matching,
                                             bkg_label,
                                             neg_mining,
                                             neg_pos,
                                             neg_overlap,
                                             use_gpu=True)
                blob_channels[tname] = 1
                blob_width[tname] = 1
                blob_height[tname] = 1
                i = i + 1
            elif ltype == 'Crop':
                axis = int(layer['crop_param']['axis'])
                offset = int(layer['crop_param']['offset'])
                models[lname] = Crop(axis, offset)
                blob_channels[tname] = blob_channels[bname[0]]
                blob_width[tname] = blob_width[bname[0]]
                blob_height[tname] = blob_height[bname[0]]
                i = i + 1
            elif ltype == 'Deconvolution':
                #models[lname] = nn.UpsamplingBilinear2d(scale_factor=2)
                #models[lname] = nn.Upsample(scale_factor=2, mode='bilinear')
                in_channels = blob_channels[bname]
                out_channels = int(layer['convolution_param']['num_output'])
                group = int(layer['convolution_param']['group'])
                kernel_w = int(layer['convolution_param']['kernel_w'])
                kernel_h = int(layer['convolution_param']['kernel_h'])
                stride_w = int(layer['convolution_param']['stride_w'])
                stride_h = int(layer['convolution_param']['stride_h'])
                pad_w = int(layer['convolution_param']['pad_w'])
                pad_h = int(layer['convolution_param']['pad_h'])
                kernel_size = (kernel_h, kernel_w)
                stride = (stride_h, stride_w)
                padding = (pad_h, pad_w)
                bias_term = layer['convolution_param']['bias_term'] != 'false'
                models[lname] = nn.ConvTranspose2d(in_channels,
                                                   out_channels,
                                                   kernel_size=kernel_size,
                                                   stride=stride,
                                                   padding=padding,
                                                   groups=group,
                                                   bias=bias_term)
                blob_channels[tname] = out_channels
                blob_width[tname] = 2 * blob_width[bname]
                blob_height[tname] = 2 * blob_height[bname]
                i = i + 1
            elif ltype == 'Reshape':
                reshape_dims = layer['reshape_param']['shape']['dim']
                reshape_dims = [int(item) for item in reshape_dims]
                models[lname] = Reshape(reshape_dims)
                blob_channels[tname] = 1
                blob_width[tname] = 1
                blob_height[tname] = 1
                i = i + 1
            elif ltype == 'Softmax':
                axis = 1
                if layer.has_key('softmax_param'
                                 ) and layer['softmax_param'].has_key('axis'):
                    axis = int(layer['softmax_param']['axis'])
                models[lname] = Softmax(axis)
                blob_channels[tname] = blob_channels[bname]
                blob_width[tname] = 1
                blob_height[tname] = 1
                i = i + 1
            elif ltype == 'Accuracy':
                models[lname] = Accuracy()
                blob_channels[tname] = 1
                blob_width[tname] = 1
                blob_height[tname] = 1
                i = i + 1
            elif ltype == 'SoftmaxWithLoss':
                models[lname] = SoftmaxWithLoss()
                blob_channels[tname] = 1
                blob_width[tname] = 1
                blob_height[tname] = 1
                i = i + 1
            else:
                print('create_network: unknown type #%s#' % ltype)
                i = i + 1
            input_width = blob_width[bname] if type(
                bname) != list else blob_width[bname[0]]
            input_height = blob_height[bname] if type(
                bname) != list else blob_height[bname[0]]
            input_channels = blob_channels[bname] if type(
                bname) != list else blob_channels[bname[0]]
            output_width = blob_width[tname] if type(
                tname) != list else blob_width[tname[0]]
            output_height = blob_height[tname] if type(
                tname) != list else blob_height[tname[0]]
            output_channels = blob_channels[tname] if type(
                tname) != list else blob_channels[tname[0]]
            print('create %-20s (%4d x %4d x %4d) -> (%4d x %4d x %4d)' %
                  (lname, input_channels, input_height, input_width,
                   output_channels, output_height, output_width))

        return models
Ejemplo n.º 29
0
IOU_THRESHOLD = config["EVAL"]['IOU_THRESHOLD']

# DETECTION PARAMETERS

lasot_dir = lasot_dir + "/*/"
folders = glob.glob(lasot_dir)
# print(folders[0].split('\\')[-2])

tracker = Tracking(
    config='tracking/experiments/siamrpn_r50_l234_dwxcorr/config.yaml',
    snapshot='tracking/experiments/siamrpn_r50_l234_dwxcorr/model.pth')

detector = Detection(
    config="./detectron2/configs/COCO-InstanceSegmentation/small.yaml",
    model=
    "detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl"
)

reid_module = REID(model=REID_BACKBONE)

tracklet = Tracklet(TRACKLET_SIZE)

running_stats = RunningStats()


def reid_rescore(reid_module, frame, template_features, bboxes, scores):

    #  rescore detection and tracking results with REID module and sort results.
    batch = []
    for bbox in bboxes:
Ejemplo n.º 30
0
        # Calculate real-world measurements
        real_x, real_y = detector_state.find_blob_distance(x, y)
        real_size = detector_state.find_blob_size(size, real_y)

        # Calculate the area of holes in the contour
        #contour_hole_area = sum([cv2.contourArea(cont)
        #                         for j, cont in enumerate(contours)
        #if hierarchy[0][j][3] == i])
        contour_hole_area = None

        blobs.append(
            Blob(x, y, contour, contour_hole_area, size, area, real_x, real_y,
                 real_size))

    detection = Detection(blobs)

    # Run Filter Heuristics
    heuristic_total = heuristics.get_weighted_result(detection, detector_state)

    # Distill heuristics into a detection
    # detection.chosen_blob = chosen
    for heuristic_weight, blob in zip(heuristic_total, detection):
        if heuristic_weight >= heuristics.threshold:
            detection.chosen_blobs.append(blob)

    # Check Failure Cases
    for failure_case in failure_cases:
        must_fail = failure_case.test(detection, detector_state)
        if must_fail:
            failure_text = failure_case.get_failure_text()