Exemple #1
0
def main(argv):
    print(video_collection.count_documents({}))
    # Per camera basis
    detector = yolo.YOLO(argv[1])
    camera = argv[2]

    # Get videos of the camera from database
    videos = list(video_collection.find({'id': camera}))
    number_of_videos = len(videos)
    print('There are ', number_of_videos, 'videos for', camera)

    counter = 0

    # Make the folder
    camera_folder = '/TfLfeeds/' + camera
    if os.path.exists(camera_folder):
        shutil.rmtree(camera_folder)
    os.mkdir(camera_folder)

    for video in videos:
        vidObj, video_folder = download_video(video)
        frames = get_frames(vidObj, video_folder)
        input_frames(frames, video, detector)
        counter += 1
        print('Handled', counter)
Exemple #2
0
def main(argv):
    global api_url
    if argv[1] == 'local':
        api_url = "http://localhost:8080/TFLcamera/"
    if argv[1] == 'bucket':
        api_url = "https://jamcam-detections-api.appspot.com/TFLcamera/"

    detector = yolo.YOLO(argv[2])
    print('Getting latest feed as of ',
          datetime.utcnow().replace(tzinfo=timezone.utc).isoformat(), 'from',
          argv[3], argv[4])
    feeds = choose_feeds(int(argv[3]), int(argv[4]))
    for camera in feeds:
        checksum = get_checksum(camera)
        if checksum:
            checksum_exists = requests.get(api_url +
                                           "checksum_detection_exists/" +
                                           checksum).json()['result']
            if checksum_exists:
                print('Video detection for video: ', checksum,
                      'already exists!')
                continue
            vidObj, video_folder = download_video(camera)
            frames = get_frames(vidObj, video_folder)
            input_frames(frames, camera, detector)
    print('Finished...')
Exemple #3
0
    def __init__(self, parent=None, QSS=None):
        super(hello_MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.ms = MySignals()
        self.ms.text_print.connect(self.info_show)
        #显示时间
        self.timer = QTimer()
        self.timer.timeout.connect(self.showTime)
        self.timer.start(1000)

        self.actionopenPicture.triggered.connect(self.openPicture)#打开图片
        self.actionopenVideo.triggered.connect(self.openVideo)  # 打开视频
        self.menu_2.triggered.connect(self.popWindow1)
        self.menu_3.triggered.connect(self.popWindow)

        self.pushButton.clicked.connect(self.pattern_fun)#图片识别

        # self.pushButton_2.clicked.connect(self.slotStart)  # 视频识别
        # self.pushButton_2.clicked.connect(self.run)
        self.pushButton_2.clicked.connect(self.camrun) #打开摄像头
        self.pushButton_3.clicked.connect(self.cutRecg)
        self.pushButton_7.clicked.connect(self.slotStop)  # 停止识别

        self.timer_camera = QTimer()
        #yolo部分初始化
        self.YOLO = yolo.YOLO()

        self.thstop = False
        self.i = 1
Exemple #4
0
    def video_fun(self):

        d1 = os.path.split(self.video_name)  # 文件名
        d2 = re.split(r"[.]", d1[1])  # 去除后缀,
        #把函数移进来
        invideo=self.video_name
        outvideo="{0}/{1}_res.mp4".format(d1[0],d2[0])
        YOLO = yolo.YOLO()
        self.vid = cv2.VideoCapture(invideo)
        if not self.vid.isOpened():
            raise IOError("Couldn't open webcam or video")
        #格式,帧速率\码率(fps), 帧的尺寸
        video_FourCC = int(self.vid.get(cv2.CAP_PROP_FOURCC))
        video_fps = self.vid.get(cv2.CAP_PROP_FPS)
        video_size = (int(self.vid.get(cv2.CAP_PROP_FRAME_WIDTH)),
                      int(self.vid.get(cv2.CAP_PROP_FRAME_HEIGHT)))
        isOutput = True if outvideo != "" else False
        if isOutput:
            print("!!! TYPE:", type(outvideo), type(video_FourCC), type(video_fps), type(video_size))
            #名字,格式,帧速率\码率(fps), 帧的尺寸
            out = cv2.VideoWriter(outvideo, video_FourCC, video_fps, video_size)
        accum_time = 0
        curr_fps = 0
        fps = "FPS: ??"
        prev_time = timer()
        while True:
            #读取
            return_value, frame = self.vid.read()
            if frame is None:
                break
            #frame原是np,变成img给YOLO处理后再变回np.
            image = Image.fromarray(frame)
            image = YOLO.detect_image(image)[0]
            self.result = np.asarray(image)
            curr_time = timer()
            #执行时间
            exec_time = curr_time - prev_time
            prev_time = curr_time
            #累计用时
            accum_time = accum_time + exec_time
            #累计fps
            curr_fps = curr_fps + 1
            #大于一秒,计算一秒的累计fps
            if accum_time > 1:
                accum_time = accum_time - 1
                fps = "FPS: " + str(curr_fps)
                curr_fps = 0
            #图片,添加的文字,左上角坐标,字体,字体大小,颜色,字体粗细
            cv2.putText(self.result, text=fps, org=(3, 15), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                        fontScale=0.50, color=(255, 0, 0), thickness=2)
            cv2.namedWindow("result", cv2.WINDOW_NORMAL)
            #显示和图像一样大,第一个是视频标题

            cv2.imshow("result", self.result)
            if isOutput:
                out.write(self.result)
            # 保持窗口.参数表示延迟多少毫秒。默认情况为0。当delay≤0,可以理解为延迟无穷大毫秒,就是暂停了。
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        YOLO.close_session()
 def __init__(self):
     # Placeholder for the eventual image publisher
     self.image_stream_sub = rospy.Subscriber('rgb_image', Image, self._process_image)
     # TODO: make custom message to hold cone coordinates and possibly detection score (confidence)
     self.cone_loc_pub = rospy.Publisher('cone_loc', Point, queue_size=10)
     self.yolo = yolo.YOLO()
     self.cones = []
Exemple #6
0
def _main_(args):
   config_path = args.conf

   with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())
        yoloclient=yolo.YOLO()
        yoloclient.calculateMap(config)
Exemple #7
0
def main(argv):
    detector = yolo.YOLO(argv[1])
    all_frame_detections = {}

    print('Begin detecting from', argv[1])
    while True:
        camera, checksum, detections = detector.get_detection()
        if not handle_detection(all_frame_detections, camera, checksum,
                                detections):
            break

    print('Finished...')
def detect_imagee(path):
    print('Begin:  detect_imagee(path)')
    yol = y.YOLO()
    #img = input('Input image filename:')
    try:
        print('open image')
        image = Image.open(path)
        print('opened image')
    except:
        print('Open Error! ')
    else:
        print('begin yol.detect_image(image)')
        r_image = yol.detect_image(image)
        r_image.show()
    yol.close_session()
def object_detection(image_name):
    yolov3 = yolo.YOLO()
    #    image_name = "4.jpg"
    dir_path = "E:/keras-yolo3-master/keras-yolo3-master/final_model"
    os.chdir(dir_path)
    image_path = dir_path + "/data/" + image_name
    image = Image.open(image_path)
    r_image, box_count, left, top, right, bottom = yolov3.detect_image(image)
    r_image.save(dir_path + "/result_whole/" + image_name)
    #    print(box_count,left, top, right, bottom)
    if box_count > 0:
        image = Image.open(image_path)
        cropped = image.crop((left, top, right, bottom))
        cropped.save(dir_path + "/result/" + image_name)
    print(box_count)
Exemple #10
0
def main(argv):
    global api_url
    if argv[1] == 'local':
        api_url = "http://localhost:8080/TFLcamera/"
    if argv[1] == 'bucket':
        api_url = "https://jamcam-detections-api.appspot.com/TFLcamera/"

    detector = yolo.YOLO(argv[2])
    all_frame_detections = {}

    print('Begin detecting from', argv[2])
    while True:
        camera, checksum, detections = detector.get_detection()
        if not handle_detection(all_frame_detections, camera, checksum,
                                detections):
            break

    print('Finished...')
Exemple #11
0
    def run(self):
        #init detectors
        self.past_detection = yolo.YOLO()
        self.text_detection = yolo_text.YOLO()
        self.char_detection = yolo_char.YOLO()

        while True:

            #############################################PART 1##########################################
            #Grab image from camera
            fullimg, img = self.cam.grabbingImage()

            #img2 = img.copy()
            #start = time.time()
            #img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            #img = cv2.imread(os.path.join("./", 'prothese.png'))

            #convert image to PIL format
            img_pil = Image.fromarray(fullimg)

            #is_detected, out_boxes, out_scores, out_classes = self.text_detection.detect_image(img_pil)
            #Init
            computeResults = image.Image(fullimg)
            #Pastille detection
            is_detected, out_boxes, out_scores, out_classes = self.past_detection.detect_image(
                img_pil)

            #############################################PART 2##########################################
            #Text Detection
            if is_detected == True:

                detect = detection_instance.DetectionInstance(fullimg)
                is_cropped, img_chip = detect.get_chip_area(out_boxes)
                computeResults.saveImage(img_chip)
                print("Image saved. Change/Turn prothesis")
                logging.info("Circle detected")
                print("Image saved")
            else:
                print("Unable to find circle. Please move the prosthesis")
                logging.info("Circle not found")

            self.ui.displayImage(img)
Exemple #12
0
def yolo_detect(path2images, model_path, anchors_path, classes_path, size,
                predicted_path):
    """
    Use pre-trained Yolo network to detect object in input images
    :param path2images: Folder with images which we want to detect
    :param model_path: Path to h5 Yolo model file
    :param anchors_path: Path to anchors file
    :param classes_path: Path to classes description file
    :param size: Yolo model image size
    :param predicted_path: Path to output folder to save results
    :return: None
    """

    params = {
        "model_path": "{0}".format(model_path),
        "anchors_path": "{0}".format(anchors_path),
        "classes_path": "{0}".format(classes_path),
        # "model_image_size": size
    }

    for f in os.listdir(path2images):
        bboxes = y.detect_image(yolo.YOLO(**params),
                                img_filename=os.path.join(path2images, f))
        if len(bboxes) > 0:
            for bbox in bboxes:
                pred_fname = os.path.join(predicted_path,
                                          "{0}.txt".format(f.split(".")[0]))
                with open(pred_fname, "w") as pred_file:
                    pred_file.write("{0} {1} {2} {3} {4}".format(
                        bbox[0], bbox[2][0], bbox[2][1], bbox[3][0],
                        bbox[3][1]))
        else:
            pred_fname = os.path.join(predicted_path,
                                      "{0}.txt".format(f.split(".")[0]))
            with open(pred_fname, "w") as pred_file:
                pred_file.write("undefined 0 0 0 0 0")
Exemple #13
0
    def evaluate_all_tasks(self, yoloclient=yolo.YOLO()):

        behavior2.location(self.client)
        while not self.client.confirmConnection():
            responses = self.load_images()
            for response in responses:
                now = datetime.datetime.now()
                date_time = now.strftime("%d-%m-%Y,%H-%M-%S-%f")
                if not os.path.exists(resources.filename):
                    os.makedirs(resources.filename)

                print("Detect objects")
                image = np.fromstring(response.image_data_uint8,
                                      dtype=np.uint8)  # get numpy array
                # reshape array to 4 channel image array H X W X 4
                img_rgba = image.reshape(response.height, response.width, 4)
                img_rgb = rgba2rgb(img_rgba)
                img_dist_topleftp = yoloclient.detect_image(img_rgb)
                image_result = img_dist_topleftp[0]
                image_result.save(resources.filename + date_time + ".png")
                behavior2.drive(self.client, airsim.CarControls(),
                                img_dist_topleftp[1], img_dist_topleftp[2])
        yoloclient.close_session()
        self.client.reset()
Exemple #14
0
        #     print('closing', conn)
        #     sel.unregister(conn)
        #     conn.close()


def do_task(path_ImageToBeDetected, conn):
    print('before detected-- path_ImageToBeDetected: ', path_ImageToBeDetected)
    path_ImageToBeDetected = path_ImageToBeDetected.strip()
    print('after detected-- path_ImageToBeDetected: ', path_ImageToBeDetected)
    path_ImageSaved = y.detect_img(yol, path_ImageToBeDetected)
    print('开始返回')
    ret = conn.send(path_ImageSaved.encode())
    print(ret)


yol = y.YOLO()
# sel = selectors.DefaultSelector()
executor = ThreadPoolExecutor(max_workers=6)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 7000))
sock.listen()
# sock.setblocking(False)
# sel.register(sock, selectors.EVENT_READ, accept)

# while True:
#     events = sel.select(timeout=None)
#     print(len(events))
#     for key, mask in events:
#         callback = key.data
#         callback(key.fileobj)
Exemple #15
0
            prYellow("RESTRICAO 3: {}".format(lblRestricao3))
            prYellow("RESTRICAO 4: {}".format(lblRestricao4))
        else:
            print(page["MensagemErro"])


def letter_probs_to_code(letter_probs):
    return "".join(common.CHARS[i] for i in numpy.argmax(letter_probs, axis=1))


if __name__ == "__main__":
    weights_cnn = 'weights_20180708_171720_feedback.npz'
    weights_yolo = 'model_data/yolo_19000.h5'
    start_time = time.time()
    prYellow('Carregando Yolov3...')
    yolo = yolo.YOLO(weights_yolo)
    prGreen('Yolov3: modelo, âncoras e classes carregadas.')
    prYellow('Carregando Rede Neural Convolucional (CNN)...')
    f = numpy.load(weights_cnn)
    param_vals = [f[n] for n in sorted(f.files, key=lambda s: int(s[4:]))]
    x, y, params = model.get_training_model()
    prGreen('CNN: Modelo e pesos carregados.')

    time.sleep(2)
    prYellow('Inicializando sessão do Tensorflow...')
    with tf.Session(config=tf.ConfigProto()) as sess:
        feed_dict = {x: numpy.zeros([1, 64, 128])}
        feed_dict.update(dict(zip(params, param_vals)))
        y_val = sess.run(y, feed_dict=feed_dict)
        out_boxes, out_probs, out_labels = yolo.get_detections(
            Image.fromarray(numpy.zeros([100, 100, 3]).astype('uint8')))
Exemple #16
0
    def run(self):
        #init detectors
        self.past_detection = yolo.YOLO()
        self.text_detection = yolo_text.YOLO()
        self.char_detection = yolo_char.YOLO()

        while True:

            #############################################PART 1##########################################
            #Grab image from camera
            ##fullimg, img = self.cam.grabbingImage()

            #img2 = img.copy()
            #start = time.time()
            #img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            img = cv2.imread(os.path.join("./", 'prothese.png'))

            #convert image to PIL format
            img_pil = Image.fromarray(img)
            
            #is_detected, out_boxes, out_scores, out_classes = self.text_detection.detect_image(img_pil)
            #Init
            computeResults = image.Image(img)
            #Pastille detection
            is_detected, out_boxes, out_scores, out_classes = self.past_detection.detect_image(img_pil)
            #print (out_boxes)
            #image = Image(image)
            #gui = GUI(image)

            #############################################PART 2##########################################
            #Text Detection
            if is_detected == True :

                detect = detection_instance.DetectionInstance(img)
                is_cropped, img_chip = detect.get_chip_area(out_boxes)
                computeResults.saveImage(img_chip)
                #print ("Pastille detectée, image enregistrées. Changez/Tournez la prothèse")
                logging.info("Circle detected")

                #Opencv to PILLOW image
                img_chip_pil = Image.fromarray(img_chip)
                img_chip_pil_rotate = img_chip_pil.copy()

                #init array results
                best_scores = np.array([0,0,0], dtype=float)
                best_boxes = np.array([[0,0,0,0],[0,0,0,0],[0,0,0,0]])
                best_deg = np.array([0,0,0], dtype=int)

                #check every image rotate, with 10 deg step
                for deg in range(0, 360, 10):
                    #inference function
                    is_detected, out_boxes, out_scores, out_classes = self.text_detection.detect_image(img_chip_pil_rotate)
                    #Check the best score detection for each text
                    if len(out_scores) == 3:
                        for i in range(len(out_scores)):
                            if out_scores[i] > best_scores[i]:
                                best_scores[i] = out_scores[i]
                                best_deg[i] = deg

                                for y in range(len(out_boxes[i])):
                                    best_boxes[i][y] = out_boxes[i][y]

                        #rotate image before saving to visualize... (For dev)
                        img_chip_pil_rotate = img_chip_pil.rotate(deg)
                        open_cv_image = np.array(img_chip_pil_rotate) 
                        computeResults.saveImage(open_cv_image)

                    else :
                        continue

                if len(best_scores) == 3:
                    #Crop texts detection
                    img_text1, img_text2, img_text3 = detect.get_text_area(best_boxes, best_deg[0], best_deg[1], best_deg[2])

                    #Save texte images
                    computeResults.saveImage(img_text1)
                    computeResults.saveImage(img_text2)
                    computeResults.saveImage(img_text3)

                    #Convert to PIL format
                    img_text1_pil = Image.fromarray(img_text1)
                    img_text2_pil = Image.fromarray(img_text2)
                    img_text3_pil = Image.fromarray(img_text3)

                    #############################################PART 3##########################################
                    #Char detection
                    #Init list of char
                    list_line1 = []
                    list_line2 = []
                    list_line3 = []

                    #inference function
                    is_detected_t1, out_boxes_t1, out_scores_t1, out_classes_t1 = self.char_detection.detect_image(img_text1_pil)
                    is_detected_t2, out_boxes_t2, out_scores_t2, out_classes_t2 = self.char_detection.detect_image(img_text2_pil)
                    is_detected_t3, out_boxes_t3, out_scores_t3, out_classes_t3 = self.char_detection.detect_image(img_text3_pil)

                    #if at least 1 detection by line...
                    if (is_detected_t1 == True and is_detected_t2 == True and is_detected_t3 == True):
                        for char in out_classes_t1:
                            list_line1.append(char)
                        for char in out_classes_t2:
                            list_line2.append(char)
                        for char in out_classes_t3:
                            list_line3.append(char)

                        #convert from list to concatenated string
                        list_line1_str = ''.join(map(str, list_line1))
                        list_line2_str = ''.join(map(str, list_line2))
                        list_line3_str = ''.join(map(str, list_line3))

                        final_list = [list_line1_str, list_line2_str, list_line3_str]

                        #Instructions for sending to Arduino and simulate keystrokes of a keyboard...
                        #Send string by string
                        self.keyboard.send(list_line1_str)
                        self.keyboard.send("  ")
                        self.keyboard.send(list_line2_str)
                        self.keyboard.send("  ")
                        self.keyboard.send(list_line3_str)

                        #add Serial Number on the final picture
                        final_img = computeResults.addSerialNumber(final_list)
                        logging.info("Serial Number written")
                        #cam.saveImage(fullimg, img)
                        computeResults.saveImage(final_img)
                        logging.info("Image saved")

                    else :
                        print ("Unable to find all char. Please move the prosthesis")
                        logging.info("All char not found")
                else :
                    print ("Unable to find all text area. Please move the prosthesis")
                    logging.info("All texts not found")
            else :
                print ("Unable to find circle. Please move the prosthesis")
                logging.info("Circle not found")
                #time.sleep(1)
            
            #self.ui.img = self.ui.loadImage(img)
            self.ui.displayImage(img)
Exemple #17
0
frames = pipeline.wait_for_frames()
# frames.get_depth_frame() is a 640x360 depth image
aligned_frames = align.process(frames)

# Get aligned frames
aligned_depth_frame = aligned_frames.get_depth_frame(
)  # aligned_depth_frame is a 640x480 depth image
print('Aligned Depth Frame')
print(aligned_depth_frame)
color_frame = aligned_frames.get_color_frame()

depth_image = np.asanyarray(aligned_depth_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())

# Init YOLO model
YOLO = yolo.YOLO()


def run(bboxes1, bboxes2):
    x11, y11, x12, y12 = np.split(bboxes1, 4, axis=1)
    x21, y21, x22, y22 = np.split(bboxes2, 4, axis=1)
    xA = np.maximum(x11, np.transpose(x21))
    yA = np.maximum(y11, np.transpose(y21))
    xB = np.minimum(x12, np.transpose(x22))
    yB = np.minimum(y12, np.transpose(y22))
    interArea = np.maximum((xB - xA + 1), 0) * np.maximum((yB - yA + 1), 0)
    boxAArea = (x12 - x11 + 1) * (y12 - y11 + 1)
    boxBArea = (x22 - x21 + 1) * (y22 - y21 + 1)
    iou = interArea / (boxAArea + np.transpose(boxBArea) - interArea)
    return iou
Exemple #18
0
def main(nn_metric,
         nn_matching_threshold,
         nn_budget,
         tracker_max_iou_distance,
         tracker_max_age,
         tracker_n_init,
         nms_max_overlap,
         camera_num,
         yolo_model_path,
         yolo_anchors_path,
         yolo_classes_path,
         deep_sort_model_path,
         seed=None,
         gpu=""):
    """
    Parameters
    ----------
    nn_metric : str
        "cosine" or "euclidean"
        Use in nn_matching.NearestNeighborDistanceMetric
    nn_matching_threshold : float
    nn_budget : int
    tracker_max_iou_distance : int
        In a final matching stage, we run intersection over union
        association as proposed in the original SORT algorithm
        on the set of unconfirmed and unmatched tracks of age n = 1
    tracker_max_age : int
        Tracks are terminated if they are not detected for `tracker_max_age` frames.
    tracker_n_init : int
        A new tracker undergoes a probationary period where the target needs to
        be eassociated with detecions to accumulate enough evidence
        in order to prevent tracking of false positive.
    nms_max_overlap : float, range 0 ~ 1
        preprocessing.non_max_suppression
    camera_num : int
        webcam device number
    yolo_model_path : str
        .h5 weight file
    yolo_anchors_path : str
        anchors file path
    yolo_classes_path : str
        classes filepath
    deep_sort_model_path : str
        weight filepath
    seed : int or None
        random seed
    gpu : str
        like "0,1"
    """
    import os
    import sys
    import pathlib
    import re
    import pprint
    import time
    import shutil

    import numpy as np
    import tensorflow as tf
    print("tensorflow.__version__ : {}".format(tf.__version__))

    import timeit
    import warnings
    from PIL import Image

    hyper_param_id = \
        "NN-metric-{}-thresh{}-budget{}" \
        .format(nn_metric, nn_matching_threshold, nn_budget)
    hyper_param_id += \
        "__TRACKER-iou{}-age{}-ninit{}" \
        .format(tracker_max_iou_distance, tracker_max_age, tracker_n_init)
    hyper_param_id += "__overlap{}".format(nms_max_overlap)

    # seed
    np.random.seed(seed=seed)
    tf.random.set_random_seed(seed=seed)
    # gpu
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"  # see issue #152
    os.environ["CUDA_VISIBLE_DEVICES"] = gpu
    # path
    check_list = [
        yolo_model_path,
        yolo_anchors_path,
        yolo_classes_path,
        deep_sort_model_path,
    ]
    for path in check_list:
        assert os.path.exists(path), "Not exists. : {}".format(path)

    #===========================================================================
    import yolo
    from deep_sort import preprocessing
    from deep_sort import nn_matching
    from deep_sort import detection
    from deep_sort import tracker
    from tools import generate_detections
    warnings.filterwarnings('ignore')

    #===========================================================================
    # deep_sort
    encoder = \
        generate_detections.create_box_encoder(
            model_filename = deep_sort_model_path,
            input_name     = 'images',
            output_name    = 'features',
            batch_size     = 1)
    metric = \
        nn_matching.NearestNeighborDistanceMetric(
            metric             = nn_metric,
            matching_threshold = nn_matching_threshold,
            budget             = nn_budget)
    tracker_instance = \
        tracker.Tracker(metric           = metric,
                        max_iou_distance = tracker_max_iou_distance,
                        max_age          = tracker_max_age,
                        n_init           = tracker_n_init)

    # Get Video
    cap = cv2.VideoCapture(camera_num)
    assert cap.isOpened(), "webcam capture failed. : {}".format(camera_num)

    # init
    fps = 0.0

    # Load YOLO
    yolov3 = yolo.YOLO(model_path=yolo_model_path,
                       anchors_path=yolo_anchors_path,
                       classes_path=yolo_classes_path)

    # Loop
    start_time = time.time()
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            print("webcam cap.read() failed.")
            break
        t1 = timeit.time.time()

        image = Image.fromarray(frame)
        # x,y,w,h
        boxs = yolov3.detect_image(image)
        # print("box_num",len(boxs))
        features = encoder(frame, boxs)
        # score to 1.0 here).
        detections = [
            detection.Detection(tlwh=bbox, confidence=1.0, feature=feature)
            for bbox, feature in zip(boxs, features)
        ]
        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]

        # Call the tracker
        tracker_instance.predict()
        tracker_instance.update(detections)
        for track in tracker_instance.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            bbox = track.to_tlbr()

            # bgr
            color = (0, 0, 255)
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                          (int(bbox[2]), int(bbox[3])), color, 2)
            #cv2.putText(frame, str(track.track_id),(int(bbox[0]), int(bbox[1])),0, 5e-3 * 200, (0,255,0),2)

            top_left = (int(bbox[0]), int(bbox[1]))

            font_face = cv2.FONT_HERSHEY_PLAIN
            label = str(track.track_id)
            text_size, _ = \
                cv2.getTextSize(text=label,
                                fontFace=font_face,
                                fontScale=2,
                                thickness=2)
            #top_left = (top_left[0], top_left[1] + text_size[1] - 1)
            bottom_right = (top_left[0] + text_size[0] - 1,
                            top_left[1] + text_size[1] - 1)
            bottom_left = (top_left[0], bottom_right[1])
            cv2.rectangle(img=frame,
                          pt1=top_left,
                          pt2=bottom_right,
                          color=color,
                          thickness=-1)
            cv2.putText(img=frame,
                        text=label,
                        org=bottom_left,
                        fontFace=font_face,
                        fontScale=1,
                        color=(255, 255, 255),
                        thickness=1)

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

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

        # show each frame result
        cv2.imshow("frame", frame)
        key = cv2.waitKey(1)
        if key & 0xFF == ord('q'):
            break

        # print log
        elapsed_time = time.time() - start_time
        print("fps : {:8.4} | elapsed time : {:8.2}".format(fps, elapsed_time))

    # END Log
    elapsed_time = time.time() - start_time
    print("fps : {:8.4} | elapsed time : {:8.2}".format(fps, elapsed_time))

    cap.release()
Exemple #19
0
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
np.random.seed(42)

import warnings
warnings.filterwarnings('ignore')


import yolo as yolov4
yolo = yolov4.YOLO()

import cv2

from deep_sort import nn_matching
from deep_sort.detection import Detection
from deep_sort.tracker import Tracker
from tools import generate_detections as gdet

import os
from io import StringIO
import pandas as pd

if __name__ == '__main__':
	filename = "0Ow4cotKOuw_0" ### ok
	# filename = "1Kbw1bUw_1"    ### nope
	# filename = "39BFeYnbu-I_0" ### nope
	# filename = "EFv961C5RgY_0" ### ok

	# filename = "10uSOcwS_0" ### blank
            # MP4
            v_suffix = "mp4"
            fourcc = cv2.VideoWriter_fourcc(*'MP4V')
            video_output_fname = str(write_image_dir_Path / "{}-result.{}".format(video_id, v_suffix))
            video_output = cv2.VideoWriter(video_output_fname, fourcc, 15, (w, h))

        list_file = open('detection.txt', 'w')
        frame_index = -1 

    # init
    fps = 0.0

    #===========================================================================
    # Load YOLO
    yolov3 = yolo.YOLO(model_path  = yolo_model_path,
                      anchors_path = yolo_anchors_path,
                      classes_path = yolo_classes_path)


    #===========================================================================
    # Log Preprocess
    if frame_total_num % log_len == 0:
        log_interval = frame_total_num // log_len
    else:
        log_interval = (frame_total_num + log_len) // log_len
    log_idxes = [i for i in range(log_interval-1, frame_total_num, log_interval)]
    print("len(log_idxes) : {}".format(len(log_idxes)))


    #===========================================================================
    # Loop
Exemple #21
0
#from keras.applications import Xception # TensorFlow ONLY
#from keras.applications import VGG16
#from keras.applications import VGG19
#from keras.applications import imagenet_utils
#from keras.applications.inception_v3 import preprocess_input
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import load_img
from keras.models import load_model, Model
from keras.layers import Input, Lambda, Conv2D
from keras import backend as K
import tensorflow as tf
import numpy as np
import argparse
import sys
# YOLOv3 model functions
# sys.path.append('/mnt/python/keras-yolo3/')
from yolo3.model import yolo_eval  # Evaluate YOLO model on given input and return filtered boxes.
# from pydarknet import Detector, Image # YOLOv3 package
# import cv2 # OpenCV, OpenCV 3.4.1 will fail with darknet
import yolo
import yolo_video

# yolo_model = load_model("model_data/yolo-openimages.h5") # load the model
# yolo_model.summary() # show a summary of the model layers
# configure the default to YOLOv3 on Open Images
yolo.YOLO._defaults['model_path'] = 'model_data/yolo-openimages.h5'
yolo.YOLO._defaults['classes_path'] = 'model_data/openimages.names'
yolo.YOLO._defaults['anchors_path'] = 'model_data/yolo_anchors.txt'
yolo_video.detect_img(yolo.YOLO(
))  # comment r_image.show(), and add r_image.save(filename) to yolo_video.py
# yolo_video.detect_img() uses yolo.detect_image(), with additional ability to input multiple images on the fly
Exemple #22
0
import numpy as np
from PIL import Image
import yolo

config = {
    "model_path": 'model_data/yolo-tiny.h5',
    "anchors_path": 'model_data/tiny_yolo_anchors.txt',
    "classes_path": 'model_data/coco_classes.txt',
    "score": 0.3,
    "iou": 0.45,
    "model_image_size": (416, 416),
    "gpu_num": 1,
}

o = yolo.YOLO(**config)
import cv2
cap = cv2.VideoCapture(0)
camera_scale = 1.

while True:
    ret, image = cap.read()
    if cv2.waitKey(10) == 27:
        break
    h, w = image.shape[:2]
    rh = int(h * camera_scale)
    rw = int(w * camera_scale)
    image = cv2.resize(image, (rw, rh))
    image = image[:, :, (2, 1, 0)]
    image = Image.fromarray(image)
    r_image = o.detect_image(image)
def load_yolo():
    yolo = yolo_utils.YOLO()
    # yolo._make_predict_function()
    session = K.get_session()
    return yolo, session
Exemple #24
0
def main():
    threshold = 100

    # Setup depth stream
    pipeline, config = setup_pipeline()
    align = align_frames(pipeline, config)
    color_frame, aligned_depth_frame = get_aligned_frames(pipeline, align)
    color_image, depth_image = rgbd_to_numpy(color_frame, aligned_depth_frame)

    # Init YOLO model
    YOLO = yolo.YOLO()

    try:
        i = 0
        while True:
            print("frame ", i)
            i += 1
            import time
            start = time.time()
            # Get frameset of color and depth
            color_frame, aligned_depth_frame = get_aligned_frames(
                pipeline, align)

            # Validate that both frames are valid
            if not aligned_depth_frame or not color_frame:
                continue

            color_image, depth_image = rgbd_to_numpy(color_frame,
                                                     aligned_depth_frame)
            color_image_ioi = color_image.copy()

            # Apply YOLO to this image and get bounding boxes
            hands, items = YOLO.get_item_of_interest(color_image)
            result = box_iou(hands, items)

            # get hand centroid
            hand_centroids = []
            if len(hands) > 0 and result.shape[1] != 0:
                hand_of_interest = get_hand_of_interest(hands, result)
                if hand_of_interest is not None:
                    median_depth_hand = get_median_depth(
                        hand_of_interest, depth_image)
                    hand_centroids.append(
                        (median_depth_hand, hand_of_interest))
                    color_image = draw_boundingBox(
                        color_image, hand_of_interest,
                        str('Depth: ' + str(median_depth_hand)))

            # Get Item centroid
            item_centroids = []
            for box in items:
                if box is not None:
                    median_depth_item = get_median_depth(box, depth_image)
                    item_centroids.append((median_depth_item, box))
                    color_image = draw_boundingBox(color_image,
                                                   box,
                                                   str('Depth: ' +
                                                       str(median_depth_item)),
                                                   box_color=(255, 0, 0))

            # All hands and items are detected
            if (len(hand_centroids) != 0) and (len(item_centroids) != 0):
                final_box = get_item_of_interest(hand_centroids,
                                                 item_centroids,
                                                 threshold=threshold)
                if final_box is not None:
                    color_image_ioi = draw_boundingBox(color_image_ioi,
                                                       final_box,
                                                       box_color=(0, 0, 0),
                                                       box_thickness=4)
            #print(time.time() - start)
            #show_image('yolo', color_image)
            #show_image('ioi', color_image_ioi)
    finally:
        pipeline.stop()
Exemple #25
0
from flask import Flask, request, Response, send_file
import yolo
import io
from PIL import Image
from tempfile import NamedTemporaryFile
from shutil import copyfileobj
from os import remove
app = Flask(__name__)

yolo.YOLO._defaults['model_path'] = 'model_data/yolo-openimages.h5'
yolo.YOLO._defaults['classes_path'] = 'model_data/openimages.names'
yolo.YOLO._defaults['anchors_path'] = 'model_data/yolo_anchors.txt'
model = yolo.YOLO()


@app.route('/isAlive', methods=['POST', 'GET'])
def index():
    return "true"


@app.route('/prediction/image', methods=['POST'])
def image_prediction():
    file = request.files['file']
    try:
        image = Image.open(file)
    except:
        return str("Image Error")
    else:
        image = model.detect_image(image)
        image.save("output.jpg")
        return send_file("output.jpg", mimetype='image/jpeg')
Exemple #26
0
ret, frame = video.read()
avgframe = frame

# Use the default YOLOv3 model data and its font.
kwargs = {
    "model_path": yolo_path + '/model_data/yolo.h5',
    "anchors_path": yolo_path + '/model_data/yolo_anchors.txt',
    "classes_path": yolo_path + '/model_data/coco_classes.txt',
    "font_path": yolo_path + '/font/FiraMono-Medium.otf'
}

# create the plugin
cls = getattr(importlib.import_module(plugin[0]), plugin[1])
ha_detect = cls(yaml_cfg)

yolo = yolo.YOLO(**kwargs)

while (1):
    ret, frame = video.read()
    subframe = cv2.subtract(frame, avgframe)
    grayscaled = cv2.cvtColor(subframe, cv2.COLOR_BGR2GRAY)
    retval2, th1 = cv2.threshold(grayscaled, 35, 255, cv2.THRESH_BINARY)
    avgframe = cv2.addWeighted(frame, 0.1, avgframe, 0.9, 0.0)

    if show:
        cv2.imshow('Frame', frame)
        cv2.imshow('Treshold diff', th1)

    th1 = th1 / 255
    w, h = th1.shape
    sum = cv2.sumElems(th1)[0] / (w * h)
Exemple #27
0
import matplotlib.pyplot as plt
import numpy as np
import sys
import cv2
import tensorflow as tf
from numpy import newaxis
from sklearn.utils import shuffle
from unityagents import UnityEnvironment
import yolo as yl
from PIL import Image


print("Python version:")
print(sys.version)

yolo=yl.YOLO()

# check Python version
if (sys.version_info[0] < 3):
    raise Exception("ERROR: ML-Agents Toolkit (v0.3 onwards) requires Python 3")

env = UnityEnvironment(file_name=env_name)

# Examine environment parameters
print(str(env))

# Set the default brain to work with
default_brain = env.brain_names[0]
brain = env.brains[default_brain]

env_info = env.reset(train_mode=train_mode)[default_brain]
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author : kly time:2019/4/14
import yolo
import os
from PIL import Image
import glob
if __name__ == '__main__':
    yolo = yolo.YOLO()
    path = "D:\work//new\Test\INA-T//*.jpg"
    outdir = "D:\work//new\Test\out"
    for jpgfile in glob.glob(path):
        img = Image.open(jpgfile)
        num = jpgfile[-10:-4]
        print(num)
        img = yolo.detect_image(img, num)
        #img.save(os.path.join(outdir, os.path.basename(jpgfile)))
        print('ok')
    yolo.close_session()
Exemple #29
0
        readyData.ready(dataPath, jsonPath)
        # train._main()
        #     ------------------分类数据准备---------------------
        trainClf = r"trainClf1.txt"
        valClf = r"valClf1.txt"
        readyData.getTrinClf(trainClf, flag="train", sliceResize=[48, 48])
        readyData.getTrinClf(valClf, flag="val", sliceResize=[48, 48])
        classifycation._main(sliceResize=[48, 48])
    else:
        #------------------测试---------------------
        #------------------测试数据准备---------------------
        # print("准备:jpg test.txt 在测试准备数据时还存在一个cnn网络分类t1 t2")
        readyData.step1Test(dataPath=dataTestPath, Totxt=r"test.txt")

        print("检测")
        yolo.detect_imgs(yolo.YOLO(),
                         testpath=r"test.txt")  #得到结果---->resultStep1.txt

        print("优化 resultStep1.txt")
        optimise.clearTxt(r"resultStep1.txt",
                          optxt=r'resultStep_clear.txt',
                          dataTestPath=dataTestPath)
        optimise.train_regression2test(train=False,
                                       getResultPath=r"resultStep_clear.txt",
                                       toFile="resultStep_regression.txt")

        optimise.optimiseTxt2(r"resultStep_regression.txt",
                              optxt=r'resultStep_optimise2.txt',
                              dataTestPath=dataTestPath)

        print("准备切片分类")
Exemple #30
0
def create_model(train_weight_final, anchors_path, yolo_classname, vpath,
                 timecode):

    #create the model
    score = 0.25
    num_gpu = 1
    yolo = y.YOLO(
        **{
            "model_path": train_weight_final,
            "anchors_path": anchors_path,
            "classes_path": yolo_classname,
            "score": score,
            "gpu_num": num_gpu,
            "model_image_size": (416, 416),
        })

    # create the dataframe
    df = pd.DataFrame(columns=[
        "cut_nb",
        "frame_ID",
        "xmin",
        "ymin",
        "xmax",
        "ymax",
        "label",
        "confidence",
    ])

    onoff = False
    cut_nb = 0
    index = 0

    # brut force en fonction du json des times codes

    for i in timecode:
        if onoff:
            end = i['time']
            end = end['secondes']
            onoff = False
            cut_nb += 1
            videotoclips(vpath, start, end, cut_nb)
            # labels to draw on images
            class_file = open(yolo_classname, "r")

            input_labels = [
                line.rstrip("\n") for line in class_file.readlines()
            ]
            print("Found {} input labels: {} ...".format(
                len(input_labels), input_labels))

            df, index, width, height = y.detect_video(
                yolo,
                "res/img/video_" + str(cut_nb) + ".mp4",
                df,
                cut_nb,
                index,
                #output_path="res/img/video_detect" + str(cut_nb) + ".mp4",
                Video_on=False)

            os.remove("res/img/video_" + str(cut_nb) + ".mp4")

        if i['category'] == 'CAMERA':
            if i['description'] == 'BAD_camera vert':
                start = i['time']
                start = start['secondes']
                onoff = True

    yolo.close_session()

    df = transform_df(df, width, height)

    return df