Beispiel #1
0
def yolo_detection():

    cam = cv2.VideoCapture(0)

    if not cam.isOpened():
        sys.exit()

    yolo = YOLO()

    while True:
        _, img = cam.read()

        img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        image = Image.fromarray(img_rgb)

        r_image = detect_img_2019(yolo, image)

        r_img = np.array(r_image)
        r_img_bgr = cv2.cvtColor(r_img, cv2.COLOR_RGB2BGR)

        cv2.imshow('Push Enter Key.', r_img_bgr)

        if cv2.waitKey(1) == 13:
            break

    yolo.close_session()
    cam.release()
    cv2.destroyAllWindows()
def main(path):
    output_path, _ = os.path.splitext(path)

    path = os.path.abspath(path)

    cv2_img = read_image(path)
    pil_image = convert_to_pil(cv2_img)

    yolo = YOLO(**args)
    r_image, out_boxes, out_scores, out_classes = yolo.detect_image(pil_image)
    yolo.close_session()

    cv2_image = cv2.cvtColor(np.array(r_image), cv2.COLOR_RGB2BGR)
    hash = str(datetime.now().strftime("%Y%m%d-%H%M%S"))
    cv2.imwrite(output_path + 'detection-' + hash + '.png', cv2_image)

    with open(output_path + 'detection-' + hash + '.txt', "w+") as file:
        file.write('bounding boxes: {}\n'.format(out_boxes))
        file.write('scores: {}\n'.format(out_scores))
        file.write('classes: {}\n'.format(out_classes))

    cv2.imshow('image', cv2_image)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        return
Beispiel #3
0
def see_big_img():
    yolo = YOLO()
    with open('train.txt') as f:
        lines = f.readlines()
    np.random.shuffle(lines)
    for k in lines[:50]:
        line = k.split()
        img2 = draw(line)
        try:
            image = cv2.imread(line[0])
            if image is None:
                raise FileNotFoundError('666')
        except Exception:
            print('Open Error! Try again!')
            continue
        else:
            image = yolo.detect_big_image(image)
            cv2.imshow('img', image)
            cv2.imshow('img1', img2)
            cv2.waitKey(0)
            cv2.destroyAllWindows()
        k = input('233')
        if k.lower() == 'q':
            break
    yolo.close_session()
Beispiel #4
0
def detectMultiImages(modelFilePath, jpgFilePath_list, out_mp4FilePath=None):
    yolo_model = YOLO(model_path=modelFilePath)
    cv2.namedWindow('result', cv2.WINDOW_NORMAL)
    width = 1000
    height = 618
    size = (width, height)
    cv2.resizeWindow('result', width, height)
    if out_mp4FilePath is not None:
        fourcc = cv2.VideoWriter_fourcc('M', 'P', 'E', 'G')
        videoWriter = cv2.VideoWriter(out_mp4FilePath, fourcc, 1.7, size)
    for jpgFilePath in jpgFilePath_list:
        image = Image.open(jpgFilePath)
        out_image = yolo_model.detect_image(image)
        resized_image = out_image.resize(size, Image.ANTIALIAS)
        resized_image_ndarray = np.array(resized_image)
        #图片第1维是宽,第2维是高,第3维是RGB
        #PIL库图片第三维是RGB,cv2库图片第三维正好相反,是BGR
        cv2.imshow('result', resized_image_ndarray[..., ::-1])
        time.sleep(0.3)
        if out_mp4FilePath is not None:
            videoWriter.write(resized_image_ndarray[..., ::-1])
        if cv2.waitKey(1) and 0xFF == ord('q'):
            break
    yolo_model.close_session()
    cv2.destroyAllWindows()
Beispiel #5
0
def detect_img_for_test():
    yolo = YOLO()
    img_path = 'VOCdevkit/VOC2007/JPEGImages/000004.jpg'
    image = Image.open(img_path)
    r_image = yolo.detect_image(image)
    r_image.show()
    yolo.close_session()
Beispiel #6
0
def process_frames(cam, output_video=None):
    yolo = YOLO()
    accum_time = 0
    curr_fps = 0
    fps = "FPS: ??"
    prev_time = timer()
    while True:
        ret_val, image = cam.read()
        image = Image.fromarray(image)
        image = yolo.detect_image(image)
        result = np.asarray(image)
        curr_time = timer()
        exec_time = curr_time - prev_time
        prev_time = curr_time
        accum_time = accum_time + exec_time
        curr_fps = curr_fps + 1
        if accum_time > 1:
            accum_time = accum_time - 1
            fps = "FPS: " + str(curr_fps)
            curr_fps = 0
        cv2.putText(result,
                    text=fps,
                    org=(13, 25),
                    fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                    fontScale=1.00,
                    color=(255, 100, 100),
                    thickness=2)
        #cv2.namedWindow("Live YoloV3", cv2.WINDOW_NORMAL)
        cv2.imshow("result", result)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        if output_video:
            output_video.write(result)
    yolo.close_session()
def inference():
    model = YOLO(score=SCORE_THRESHOLD)

    for f in os.listdir(PROCESS_PATH):
        if f.endswith(DATA_SUFFIX):
            image = Image.open(PROCESS_PATH + f)
            prediction, boxes = model.detect_image(image)
            if SHOW:
                prediction.show()
            prediction.save(RESULT_PATH + f[:-len(DATA_SUFFIX)] +
                            '_result.png')

            rgb_prediction = Image.new('RGB', prediction.size, (0, 0, 0))
            rgb_prediction.paste(prediction)
            rgb_prediction.save(RESULT_PATH + f[:-len(DATA_SUFFIX)] +
                                '_rgb.png')

            copyfile(PROCESS_PATH + f[:-len(DATA_SUFFIX)] + '_zmap.png',
                     RESULT_PATH + f[:-len(DATA_SUFFIX)] + '_zmap.png')

            with open(RESULT_PATH + f[:-len(DATA_SUFFIX)] + '.txt',
                      'w') as output:
                for box in boxes:
                    print(','.join(str(i) for i in box), file=output)

    model.close_session()
Beispiel #8
0
def detectVideo(path, output_path=""):
    yolo = YOLO()
    if path.isnumeric():
        cap = cv2.VideoCapture(int(path))
    else:
        cap = cv2.VideoCapture(path)
    if not cap.isOpened():
        raise IOError("Couldn't open camera or video")
    video_FourCC = int(cap.get(cv2.CAP_PROP_FOURCC))
    video_fps = cap.get(cv2.CAP_PROP_FPS)
    video_size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
                  int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    isOutput = True if output_path != "" else False
    if isOutput:
        print("!!! TYPE:", type(output_path), type(video_FourCC),
              type(video_fps), type(video_size))
        out = cv2.VideoWriter(output_path, video_FourCC, video_fps, video_size)
    while True:
        # get a frame
        ret, frame = cap.read()
        if not ret:
            print("finish to read capture")
        # show a frame
        result = yolo.detect_image(frame)
        cv2.imshow("capture", result)
        if cv2.waitKey(1) > 0:
            break
        if isOutput:
            out.write(result)
    cap.release()
    cv2.destroyAllWindows()
    print("finish to read capture")
    yolo.close_session()
Beispiel #9
0
def main():
    yolo = YOLO()

    imagepath = '../../../images/clerigos/clerigos-0039.jpg'
    
    image = Image.open(imagepath)

    r_image = yolo.detect_image(image)
    r_image.show()

    yolo.close_session()
Beispiel #10
0
def main():
    global db

    yolo = YOLO(model_path='model_data/yolo_vcom.h5',
                anchors_path='model_data/yolo_anchors.txt',
                classes_path='model_data/vcom_classes.txt')

    if not INT:

        file = open(TRUE, 'w')

        for label in labels:
            for i in range(labels[label], labels_test[label]):
                try:
                    print('Classifying ' + str(label) + ' ' + str(i) + '/' +
                          str(labels_test[label]))
                    sys.stdout.flush()
                    name = label + '-' + str(i).zfill(4)
                    path = db.get_img_path(name)
                    img = Image.open(path)
                    xmin, xmax, ymin, ymax = db.annot_coords(name)
                    _, pxmin, pxmax, pymin, pymax, score, pred = yolo.detect_image(
                        img)
                    file.write(
                        str(label) + ',' + str(pred) + ',' + str(xmin) + ',' +
                        str(xmax) + ',' + str(ymin) + ',' + str(ymax) + ',' +
                        str(pxmin) + ',' + str(pxmax) + ',' + str(pymin) +
                        ',' + str(pymax) + '\n')
                    file.flush()
                except Exception as e:
                    print(e)
                    continue

        file.close()

    else:
        while True:
            image_name = input('Insert image name(e.g. \'arrabida-0000\'): ')
            if image_name == 'exit':
                print('Leaving...')
                exit()
            try:
                path = db.get_img_path(image_name)
            except:
                print('Error reading image, try again')
                continue
            img = Image.open(path)
            xmin, xmax, ymin, ymax = db.annot_coords(image_name)
            detect, pxmin, pxmax, pymin, pymax, score, pred = yolo.detect_image(
                img)
            detect.show()

    yolo.close_session()
Beispiel #11
0
def main():
    yolo = YOLO()

    image = Image.open('test-images/musica0.jpg')
    r_image = yolo.detect_image(image)
    r_image.show()
    image = Image.open('test-images/musica-0018.jpg')
    r_image = yolo.detect_image(image)
    cv.imshow('Yolo Result', r_image)
    r_image.show()
    r_image.save('result.jpg')

    yolo.close_session()
Beispiel #12
0
def main():
    yolo = YOLO(model_path='model_data/yolo_vcom.h5',
                anchors_path='model_data/yolo_anchors.txt',
                classes_path='model_data/vcom_classes.txt')

    imagepath = '../../images/arrabida/arrabida-0033.jpg'

    image = Image.open(imagepath)

    r_image, _, _, _, _, _, _ = yolo.detect_image(image)
    r_image.show()

    yolo.close_session()
Beispiel #13
0
def detect_img_terminal():
    yolo = YOLO()
    while True:
        # 执行程序,在终端窗口上手动输入待预测图片文件路径
        img_path = 'VOCdevkit/VOC2007/JPEGImages/000004.jpg'
        img = input('Input image filename:')
        try:
            image = Image.open(img_path)
        except:
            print('Open Error! Try again!')
            continue
        else:
            r_image = yolo.detect_image(image)
            r_image.show()
    yolo.close_session()
def detect_img(img):
    # img = input('Input image filename:')
    yolo = YOLO()
    try:
        image = Image.open(img)
    except:
        print("open image failed")
    else:
        res = yolo.detect_image(image)
        labeled_image = res.get_image()
        labeled_image.show()
        print("get bounding boxes: ")
        for box in res.get_boxes():
            print(box)
    yolo.close_session()
def main():
    resultsFile = open('results.txt', 'w')
    with open('test_yolo.txt') as f:
        lines = f.readlines()

    yolo = YOLO()

    for i in range(len(lines)):
        row = lines[i]
        rowSplited = row.split(' ')
        imgPath = rowSplited[0]
        print(i, '/', len(lines))
        imgPath = imgPath.replace('\n', '')
        image = Image.open(imgPath)
        r_image = yolo.vcom_detect_image(image, imgPath, resultsFile)

    yolo.close_session()
Beispiel #16
0
def detectImage():
    yolo = YOLO()
    while True:
        path = input("Input filename:")
        if path == "exit":
            break
        image = cv2.imread(path)
        if image is None:
            print('Open Error! Try again!')
            continue
        result = yolo.detect_image(image)
        if not result is None:
            cv2.imshow("Result", result)
            cv2.waitKey()
            cv2.destroyAllWindows()
    print("Input finish")
    yolo.close_session()
Beispiel #17
0
def detect_curling(video_path, txt_path):
    """
    video_path: 视频路径
    txt_path: 保存的结果路径,保存内容为冰壶的[left, top, right, bottom]
    """
    Yolo = YOLO()
    cap = cv2.VideoCapture(video_path)
    f = open(txt_path, "w")
    while True:
        ret, img = cap.read()
        if not ret:
            break
        img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
        img = Yolo.detect_image(img, f)
        f.write("\n")
    Yolo.close_session()
    f.close()
Beispiel #18
0
def detectFolder(path, outputPath="output"):
    if not os.path.exists(outputPath):
        os.mkdir(outputPath)
    yolo = YOLO()
    names = os.listdir(path)
    for name in names:
        filepath = os.path.join(path, name)
        image = cv2.imread(filepath)
        if image is None:
            print("Open file {} error".format(path))
            break
        result = yolo.detect_image(image)
        if not result is None:
            output = os.path.join(outputPath, name)
            print(output)
            cv2.imwrite(output, result)
    yolo.close_session()
Beispiel #19
0
def create_dataset(**kwargs):

    yolo = YOLO(**dict(kwargs))

    input_path = os.path.expanduser(kwargs.get('input_path', ''))
    output_path = os.path.expanduser(kwargs.get('output_path', ''))
    class_name = kwargs.get('class_name', '')
    class_names = yolo._get_class()

    if class_name not in class_names:
        yolo.close_session()
        return

    class_name = class_name.replace(' ', '_')
    output_path = output_path + '/' + class_name + '_dataset/'

    try:
        os.makedirs(output_path)
    except OSError:
        pass

    for root, _, files in os.walk(input_path):
        label = os.path.basename(root).lower()
        if len(files) > 0:
            try:
                os.makedirs(output_path + label)
            except:
                pass
        for file in files:
            input_file = root + '/' + file

            try:
                image = Image.open(input_file)
            except:
                continue
            else:
                _, images = yolo.detect_image(image)
                for image in images:
                    output_file = output_path + label + '/' + str(uuid4()) + \
                        '.png'
                    image.save(output_file)

    yolo.close_session()
Beispiel #20
0
    def run(self):
        # folder = 'augmentation/rotated'
        folder = 'augmentation/noise'
        imgs_to_detect = os.listdir(folder)

        if self.ver == 'yolov3-tiny':
            Y = YOLO(model_path='model_data/yolov3-tiny.h5',
                     anchors_path='model_data/tiny_yolo_anchors.txt')
        elif self.ver == 'yolov3':
            Y = YOLO()
        else:
            raise ValueError('npnpnp')

        for i in imgs_to_detect:
            image = Image.open('{}/{}'.format(folder, i))
            r_image = Y.detect_image(image)
            r_image.save('{}/box_{}'.format(folder, i))
        Y.close_session()
        print('DONE!')
Beispiel #21
0
def detect_img():
    yolo = YOLO()
    while True:
        img = input('Input image filename(input Q to exit):')
        if img.lower() == 'q':
            yolo.close_session()
            return
        try:
            image = cv2.imread(img)
            if image is None:
                raise FileNotFoundError('666')
        except Exception:
            print('Open Error! Try again!')
            continue
        else:
            r_image = yolo.detect_big_image(image)
            cv2.imshow('img', r_image)
            cv2.waitKey(0)
            cv2.destroyWindow('img')
    yolo.close_session()
Beispiel #22
0
class yoloCamera():
    def __init__(self, cameraId=0):
        self.cameraId = cameraId
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        sess = tf.Session(config=config)
        set_session(sess)
        self.yolocore = YOLO()
        self.camera = cv2.VideoCapture(self.cameraId)

    def getFrame(self):
        if not self.camera.isOpened():
            return b''
        ret, img = self.camera.read()
        #img_pil = Image.fromarray(img)
        r_img = self.yolocore.detect_image(img)
        #r_img_cv = numpy.array(r_img)
        ret1, jpeg = cv2.imencode('.jpg', r_img)
        return jpeg.tobytes()

    def close(self):
        self.yolocore.close_session()
Beispiel #23
0
    def run(self):
        imgs_to_detect = os.listdir('imgs')
        pbar = tqdm(total=len(imgs_to_detect))

        ver = 'yolov3-tiny'
        if ver == 'yolov3-tiny':
            Y = YOLO(
                model_path='model_data/yolov3-tiny.h5',
                anchors_path='model_data/tiny_yolo_anchors.txt')
        elif ver == 'yolov3':
            Y = YOLO()
        else:
            raise ValueError('npnpnp')
            
        for i in imgs_to_detect:
            image = Image.open('imgs/{}'.format(i))
            r_image = Y.detect_image(image)
            r_image.save('detected/{}'.format(i))
            pbar.update(1)
        pbar.close()
        Y.close_session()
        print('DONE!')
Beispiel #24
0
def main(args):
    model = YOLO(**vars(args))

    node.create_subscription(Image, 'usb_cam/image_raw', callback)
    pub = node.create_publisher(TimeReference, 'people_info')
    msg = TimeReference()

    while rclpy.ok():
        # for _ in range(5):
        rclpy.spin_once(node)
        try:
            result = detect_image(model, args.classes_path)
            print(result)
        except:
            result = {'objects': []}
            print('no people')
        msg.header = header
        msg.source = json.dumps(result)
        pub.publish(msg)

    model.close_session()
    node.destroy_node()
    rclpy.shutdown()
Beispiel #25
0
def show_camera():
    yolo = YOLO('model_data/yolo-tiny.h5', 'model_data/tiny_yolo_anchors.txt',
                'model_data/coco_classes.txt')

    cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0),
                           cv2.CAP_GSTREAMER)

    if cap.isOpened():
        window_handle = cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE)
        while cv2.getWindowProperty("CSI Camera", 0) >= 0:
            ret_val, frame = cap.read()
            frame = take_and_resize(frame)
            outBoxes = yolo.detect_image(frame)
            frame = np.asarray(frame)
            if len(outBoxes) > 0:
                for box in outBoxes:
                    # extract the bounding box coordinates
                    (x, y) = (int(box[0]), int(box[1]))
                    (w, h) = (int(box[2]), int(box[3]))
                    bbox = [x, y, w, h, box[4]]
                    cv2.rectangle(frame, (bbox[0], bbox[1]),
                                  (bbox[2], bbox[3]), (0, 255, 0), 2)
                    text = 'classID = {}'.format(box[4])
                    cv2.putText(frame, text, (box[0], box[1] - 5),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)

            cv2.imshow("CSI Camera", frame)
            # This also acts as
            keyCode = cv2.waitKey(30) & 0xFF
            # Stop the program on the ESC key
            if keyCode == 27:
                break
        cap.release()
        cv2.destroyAllWindows()
        yolo.close_session()
    else:
        print("Unable to open camera")
Beispiel #26
0
def detect_image_raw(file_path,save_path,app):
    gti.generate_images(file_path,save_path)
    yolo = YOLO()
    detect_img(yolo,save_path,app)
    save_name = 'result1/1.csv'
    seriesuid, coordX, coordY, coordZ, class_label, probability = [], [], [], [], [], []
    clipmin=-1000; clipmax=600
    files = get_file_name(file_path)
    for f_name in tqdm(files):
        result_id = int(f_name.replace('.mhd', ''))
        current_file = os.path.join(file_path, f_name)
        ct, origin, spacing = load_itk(current_file)
        ct = ct.clip(min=clipmin, max=clipmax)
        for num in range(ct.shape[0]):
            image = Image.fromarray(ct[num])
            detect_result = yolo.ddetect_image(image)
            
            for one_result in detect_result:
                result_probability = one_result[1]
                result_label = int(label_dict[one_result[0]])
                result_x = (one_result[2] + one_result[4]) / 2
                result_x = result_x * spacing[2] + origin[2]
                result_y = (one_result[3] + one_result[5]) / 2
                result_y = result_y * spacing[1] + origin[1]
                result_z = num
                result_z = result_z * spacing[0] + origin[0]
                # print(result_id, result_x, result_y, result_z, result_label, result_probability)
                seriesuid.append(result_id)
                coordX.append(result_x)
                coordY.append(result_y)
                coordZ.append(result_z)
                class_label.append(result_label)
                probability.append(result_probability)
    dataframe = pd.DataFrame({'seriesuid': seriesuid, 'coordX': coordX, 'coordY': coordY, 'coordZ': coordZ, 'class': class_label, 'probability': probability})
    columns = ['seriesuid', 'coordX', 'coordY', 'coordZ', 'class', 'probability']
    dataframe.to_csv(save_name, index=False, sep=',', columns=columns)
    yolo.close_session()
Beispiel #27
0
class Yolov3(ModelBase):
    def __init__(self):
        """
        :param weight_path: 权重路径
        :param use_gpu:     是否使用GPU
        """
        # self.weight_path = weight_path
        # self.use_gpu = use_gpu
        # self.net = None
        self.yolo = YOLO()

    def predict(self, imagepath):
        """
        对图片进行预测
        :param image_info:
        :return:
        """
        # filenames = os.listdir(inputpath)
        # for i, file in enumerate(filenames):
        # image_info = {}
        savepath = './output/'
        try:
            image = Image.open(imagepath)
        except:
            print('Open Error! Try again!')
        else:
            r_image, time, walker_bbox = self.yolo.detect_image(image)
            time = round(time, 3)
            if not os.path.exists(savepath):
                os.mkdir(savepath)
            r_image.save(savepath + '_' + str(time) + '.jpg')
        # image = np.array(image, dtype='float32')
        # image_info['image'] = image
        # image_info['walker_bbox'] = walker_bbox
        # return image_info
        self.yolo.close_session()
import os
from yolo import YOLO
from PIL import Image

test_img_path = 'C:/AIoT/Projects/keras-yolo3-master/VOCtest/VOC2007/JPEGImages'
img_list = os.listdir(test_img_path)  # 用于返回指定的文件夹包含的文件或文件夹的名字的列表

yolo_obj = YOLO()

for each_img in img_list:
    img_name, _ = each_img.split('.')
    image = Image.open(test_img_path + '/' + each_img)
    yolo_obj.detect_image(image, img_name)

yolo_obj.close_session()
Beispiel #29
0
capture = cv2.VideoCapture(0)  # capture=cv2.VideoCapture("1.mp4")

fps = 0.0
while (True):
    t1 = time.time()
    # 读取某一帧
    ref, frame = capture.read()
    # 格式转变,BGRtoRGB
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    # 转变成Image
    frame = Image.fromarray(np.uint8(frame))

    # 进行检测
    frame = np.array(yolo.detect_image(frame))

    # RGBtoBGR满足opencv显示格式
    frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)

    fps = (fps + (1. / (time.time() - t1))) / 2
    print("fps= %.2f" % (fps))
    frame = cv2.putText(frame, "fps= %.2f" % (fps), (0, 40),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

    cv2.imshow("video", frame)
    c = cv2.waitKey(30) & 0xff
    if c == 27:
        capture.release()
        break

yolo.close_session()
Beispiel #30
0
        # print(frame.shape)
        frame = mmc.popNextImage()
        # Specify the min and max range
        # frame = conStretch_vec(frame, min_range, max_range)
        # Run detection
        if use_YOLO:
            alter = contrastStretch(frame, min_range, max_range)
            image = Image.fromarray(np.uint8(alter))
            # image = Image.fromarray(np.uint8(cm.gist_earth(frame)))
            output = detect_img(my_yolo,image)
            # output = predict_with_yolo_head(model, frame, config, confidence=0.3, iou_threshold=0.4)
            output = np.array(output)
            cv2.imshow('live',output)
        else:
            cv2.imshow('live', frame)
        # output_image = frame
        end = timer()
        save_time = end-start
        logFileTime.write("\n%s" % save_time)
        # print("Inference time: {:.2f}s".format(end - start))

    if cv2.waitKey(1) & 0xFF == ord('q'): # This break key is critical, otherwise the live image does not load
        break
# Close opencv window, MMC session, YOLOv3 session, and inference time log
cv2.destroyAllWindows()
mmc.stopSequenceAcquisition()
mmc.reset()
my_yolo.close_session() # end yolo session
logFileTime.close()

# print('-----hope you enjoyed the show-----')