expvgg = None
            if args.network != 0:
                expvgg = expimg + "_vgg"
                os.makedirs(expvgg, exist_ok=True)

            inplab = os.path.join(args.inpdir, split + "/label")
            explab = os.path.join(args.expdir, split + "/label")

            # YOLO detection & training
            if args.yolo_on:
                if split == "test":
                    f = open('test-image_yolo.csv', "w")
                    writer = csv.writer(f, quotechar="'")
                # YOLO
                yolo = YOLO(
                    model_path="yolo/model_data/trained_weights_final.h5",
                    classes_path="yolo/model_data/vs_classes.txt",
                    anchors_path="yolo/model_data/yolo_anchors.txt")

                images = glob.glob(os.path.join(inpimg, "*.png"))
                print(images)
                images = [s for s in images if "_" not in s]
                images.sort()
                print(images)
                # error : no file
                if images == []:
                    print("No such file or directory: '" + inpimg + "/*.png'")

                for img in images:
                    # get file name
                    name, ext = os.path.splitext(os.path.basename(img))
                    print(img)
示例#2
0
from yolo import YOLO
import PIL.Image as Image
import numpy as np
from PIL import ImageFile
from matplotlib import pyplot as plt
ImageFile.LOAD_TRUNCATED_IMAGES = True

model = YOLO(model_path="./trained_weights.h5")
lines = ["./frame0010.jpg"]
for p in lines:
    print(p)
    img = Image.open(p).convert('RGB')
    _, _, pred_classes = model.detect_image(img)
    print(pred_classes)
    plt.imshow(img)
    plt.show()
示例#3
0
from yolo import YOLO
from yolo import detect_video

if __name__ == '__main__':
    video_path = 'path2your-video'
    detect_video(YOLO(), 0)
示例#4
0
        '--image', default=False, action="store_true",
        help='Image detection mode, will ignore all positional arguments'
    )
    '''
    Command line positional arguments -- for video detection mode
    '''
    parser.add_argument(
        "--input", nargs='?', type=str,required=False,default='./path2your_video',
        help = "Video input path"
    )

    parser.add_argument(
        "--output", nargs='?', type=str, default="",
        help = "[Optional] Video output path"
    )

    FLAGS = parser.parse_args()

    if FLAGS.image:
        """
        Image detection mode, disregard any remaining command line arguments
        """
        print("Image detection mode")
        if "input" in FLAGS:
            print(" Ignoring remaining command line arguments: " + FLAGS.input + "," + FLAGS.output)
        detect_img(YOLO(**vars(FLAGS)))
    elif "input" in FLAGS:
        detect_video(YOLO(**vars(FLAGS)), FLAGS.input, FLAGS.output)
    else:
        print("Must specify at least video_input_path.  See usage with --help.")
示例#5
0
    #         r_image, temp = yolo.detect_image(image)
    #         r_image.show()
    #         plt.imshow(r_image)
    #         plt.show()
    # yolo.close_session()
    filenames = os.listdir(path_test)
    info = []
    for filename in tqdm(filenames):
        img_path = path_test + filename
        try:
            image = Image.open(img_path)
            r_image, temp = yolo.detect_image(image)
            # r_image.show()
            plt.imshow(r_image)
            plt.show()
            temp.insert(0, filename)
            # print(temp)
            info.append(temp)
        except Exception as e:
            print("错误文件:"+img_path)

    with open(path_result, 'w')as file:
        for cutWords in info:
            file.write('\t'.join(cutWords) + '\n')


if __name__ == '__main__':


    detect_img(YOLO(), path_result)
示例#6
0
def main(video_url, frame_size=1):
    yolo = YOLO()
    yolo_face = YOLO_detect_face()
    # Definition of the parameters
    max_cosine_distance = 0.3
    nn_budget = None
    nms_max_overlap = 1.0

    # deep_sort
    root_path = os.getcwd()
    model_filename = root_path + '/src/model/face_recognition/model_data/mars-small128.pb'
    encoder = gdet.create_box_encoder(model_filename, batch_size=1)

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

    writeVideo_flag = True

    video_capture = cv2.VideoCapture(video_url)

    if writeVideo_flag:
        # Define the codec and create VideoWriter object
        # w = int(video_capture.get(3))
        # h = int(video_capture.get(4))
        w = 640 * frame_size
        h = 480 * frame_size
        fourcc = cv2.VideoWriter_fourcc(*'MPEG')
        out = cv2.VideoWriter('static/result/video/output.avi', fourcc, 30,
                              (w, h))
        list_file = open('static/result/textfile/detection.txt', 'w')
        # json_brief_file = open('static/result/data/brief.json', 'w+')
        frame_index = -1

    fps = 0.0
    # start the class of calculate emotion with track id
    emotion = calculate_emotion_dict()
    # initial emotion dashboard
    dashboard = emotion_dashboard(size_time=frame_size)
    # initial domain emotion with face id
    domain_emotion_analysis = domain_emotion_with_id_calculating()
    # domain emotion dict pointer
    domain_dict = domain_emotion_analysis.emotion_dict_with_id
    # init frame id
    frame_id = 0
    # init data frame
    df = pd.DataFrame(columns=[
        'face_id', "frame_id", "domain_emotion", "angry", "disgust", "scared",
        "happy", "sad", "surprised", "neutral"
    ])
    # init face dashboard
    face_dashboard = FaceDashboard()
    # init face image save
    face_img_save = FaceImg()
    while True:
        ret, frame = video_capture.read()  # frame shape 640*480*3
        # frame = cv2.resize(frame, (640*3, 480*3))
        if ret != True:
            break
        t1 = time.time()

        image = Image.fromarray(frame)

        boxs = yolo.detect_image(
            image)  # eg. [[481, 91, 398, 398], [5, 103, 469, 395]]
        features = encoder(frame, boxs)  # features eg. the num of boxes matrix

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

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh
                          for d in detections])  # reshape boxes and float
        scores = np.array([d.confidence for d in detections
                           ])  # score confidence to 1 with each box
        # avoid overlapping boxes
        # https://www.pyimagesearch.com/2015/02/16/faster-non-maximum-suppression-python/
        indices = preprocessing.non_max_suppression(
            boxes, nms_max_overlap,
            scores)  # only return non-overlapping index
        detections = [detections[i] for i in indices]

        # Call the tracker
        tracker.predict()
        tracker.update(detections)
        for track in tracker.tracks:
            if track.is_confirmed() and track.time_since_update > 1:
                continue
            bbox = track.to_tlbr()
            if (int(bbox[1]) - 50) > 0:
                img_p = frame[(int(bbox[1]) - 50):(int(bbox[3])),
                              (int(bbox[0])):(int(bbox[2]))]
            else:
                img_p = frame[0:(int(bbox[3])), (int(bbox[0])):(int(bbox[2]))]
            # if shape not have 0, start face detection
            if 0 in img_p.shape:
                continue
            # start face detection model
            face_locations, face_names = yolo_face.face_detect(
                img_p, track.track_id)

            # compare face_names and track id
            face_id = int(face_names[0]
                          ) if face_names and face_names[0] else track.track_id
            # save new face image
            face_img_save.save_face_img(face_locations, face_id, img_p)

            # emotion predict
            emotion_dict, frame_emotion_dict, sum_frame_emotion_dict, domain_emotion = yolo_face.draw_face_emotion(
                face_locations, face_names, img_p)

            # emotion add to dataframe (df)
            if domain_emotion:
                temp_dict = sum_frame_emotion_dict.copy()
                temp_dict['frame_id'] = frame_id
                temp_dict['face_id'] = face_id
                temp_dict['domain_emotion'] = domain_emotion
                df.loc[frame_id] = temp_dict
                # domain emotion accumlation
                domain_emotion_analysis.calculating(domain_emotion, face_id)
                # draw face dashboard
                frame = face_dashboard.face_interal_five(
                    face_locations, img_p, frame, face_id)

            emotion_id = emotion.sum_track_id_emotion_dict(
                int(face_id), sum_frame_emotion_dict)
            # try:
            #     json_brief_file.read()
            #     json_brief_file.seek(0)
            #     json_brief_file.truncate()
            # except:
            #     pass
            # json_brief_file.write(json.dumps(emotion_id))

            with open('static/result/data/brief.json', 'w+') as f:
                f.write(json.dumps(emotion_id))

            dashboard.draw_canvas(int(face_id), img_p, emotion_id, domain_dict)
            cv2.putText(frame, str(face_id), (int(bbox[0]), int(bbox[1])), 0,
                        5e-3 * 200, (0, 255, 0), 2)

            # if face_names and face_names[0]:
            #     emotion_id = emotion.sum_track_id_emotion_dict(int(face_names[0]), sum_frame_emotion_dict)
            #     dashboard.draw_canvas(int(face_names[0]), img_p, emotion_id, domain_dict)
            #     cv2.putText(frame, face_names[0],(int(bbox[0]), int(bbox[1])),0, 5e-3 * 200, (0,255,0),2)
            # else:
            #     emotion_id = emotion.sum_track_id_emotion_dict(track.track_id, sum_frame_emotion_dict)
            #     dashboard.draw_canvas(track.track_id, img_p, emotion_id, domain_dict)
            #     cv2.putText(frame, str(track.track_id),(int(bbox[0]), int(bbox[1])),0, 5e-3 * 200, (0,255,0),2)

            # draw rectangle and put body id text
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                          (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2)
            # cv2.putText(frame, str(track.track_id),(int(bbox[0]), int(bbox[1])),0, 5e-3 * 200, (0,255,0),2)
            frame_id += 1

        if writeVideo_flag:
            # save a frame
            frame = cv2.resize(frame, (640 * frame_size, 480 * frame_size))

            out.write(frame)
            frame_index = frame_index + 1
            list_file.write(str(frame_index) + ' ')
            if len(boxs) != 0:
                for i in range(0, len(boxs)):
                    list_file.write(
                        str(boxs[i][0]) + ' ' + str(boxs[i][1]) + ' ' +
                        str(boxs[i][2]) + ' ' + str(boxs[i][3]) + ' ')
            list_file.write('\n')

            # write emotion data by dataframe
            df.to_csv('static/result/data/summary.csv')

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

        # Press Q to stop!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    return
示例#7
0
import sys
from yolo import YOLO
from PIL import Image


#
def detect_img(yolo, image):
    r_img = yolo.detect_image(image)
    r_img.show()
    yolo.close_session()


model_path = "/logs/trained_weights_final.h5"
anchors_path = "/data_set/annotation/yolo_clusters.txt"
classes_path = "/data_set/annotation/classes.txt"
model_image_size = (416, 416)
gpu_num = 1

if __name__ == '__main__':

    yolo = YOLO(model_path=model_path,
                anchors_path=anchors_path,
                classes_path=classes_path,
                model_image_size=model_image_size)
    image_path = "/data_set/test_img/test1.jpg"
    image = Image.open(image_path)
    detect_img(yolo, image)
示例#8
0
    parser.add_argument("--output",
                        nargs='?',
                        type=str,
                        default="",
                        help="[Optional] Video output path")

    parser.add_argument("--write_image_path",
                        type=str,
                        required=False,
                        help="path to save image with predicted bboxes on it")

    FLAGS = parser.parse_args()

    if FLAGS.image:
        """
        Image detection mode, disregard any remaining command line arguments
        """
        print("Image detection mode")
        if "input" in FLAGS:
            print(" Ignoring remaining command line arguments: " +
                  FLAGS.input + "," + FLAGS.output)
        detect_img(YOLO(**vars(FLAGS)))
    elif "input" in FLAGS:
        detect_video(YOLO(**vars(FLAGS)), FLAGS.input, FLAGS.output)
    elif "val_annotation_file" in FLAGS:
        detect_img_to_file(YOLO(**vars(FLAGS)), FLAGS.val_annotation_file,
                           FLAGS.output, FLAGS.write_image_path)
    else:
        print(
            "Must specify at least video_input_path.  See usage with --help.")
 def __init__(self):
     self.yolo = YOLO()
import sys

if len(sys.argv) < 2:
    print("Usage: $ python {0} [video_path, [, output_path]]", sys.argv[0])
    exit()

from yolo import YOLO
from yolo import detect_video

if __name__ == '__main__':
    video_path = sys.argv[1]
    output_path = ""
    if len(sys.argv) > 2:
        output_path = sys.argv[2]
    detect_video(YOLO(), video_path, output_path)
示例#11
0
def main():
    #output = sys.stdout
    #outputfile = open('log.txt', 'w')
    #sys.stdout = outputfile

    yolo = YOLO()
    # Definition of the parameters
    max_cosine_distance = 0.3
    nn_budget = None
    nms_max_overlap = 1.0

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

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

    writeVideo_flag = True

    video_capture = cv2.VideoCapture('demo.avi')
    #video_capture.set(5, 5)

    if writeVideo_flag:
        # Define the codec and create VideoWriter object
        #w = int(video_capture.get(3))
        #h = int(video_capture.get(4))
        # if version == 3:
        #     fourcc = cv2.VideoWriter_fourcc(*'MJPG')
        # else:
        #     fourcc = cv2.cv.CV_FOURCC(*'MJPG')
        #out = cv2.VideoWriter('output.avi', fourcc, 30, (w, h))
        list_file = open('detection.txt', 'w')
        track_file = open('tracker.txt', 'w')

    #print('start', psutil.virtual_memory().available/(1024*1024))
    frame_index = -1
    fps = 0.0
    while True:
        frame_index = frame_index + 1
        ret, frame = video_capture.read()  # frame shape 640*480*3
        if ret != True:
            break
        #if (frame_index+1) % 3 != 0:
        #    continue
        t1 = time.time()
        image = Image.fromarray(frame)

        boxs, other_boxs, other_class = yolo.detect_image(image)
        features = encoder(frame, boxs)

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

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

        # Call the tracker
        print(frame_index)
        tracker.predict()
        tracker.update(detections)

        for track in tracker.tracks:
            if track.is_confirmed() and track.time_since_update > 1:
                continue
            bbox = track.to_tlbr()
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                          (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2)
            cv2.putText(frame, str(track.track_id),
                        (int(bbox[0]), int(bbox[1])), 0, 5e-3 * 200,
                        (0, 255, 0), 2)

        #print('draw track box', psutil.virtual_memory().available/(1024*1024))
        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)

        for box, label in zip(other_boxs, other_class):
            cv2.rectangle(frame, (int(box[0]), int(box[1])),
                          (int(box[2] + box[0]), int(box[3] + box[1])),
                          (255, 0, 0), 2)
            cv2.putText(frame, label, (int(box[0]), int(box[1])), 0,
                        5e-3 * 200, (0, 255, 0), 2)

        if writeVideo_flag:
            # save a frame
            #out.write(frame)
            frame_index = frame_index + 1
            list_file.write(str(frame_index) + ' ')
            if len(boxs) != 0:
                for i in range(0, len(boxs)):
                    list_file.write(
                        str(boxs[i][0]) + ' ' + str(boxs[i][1]) + ' ' +
                        str(boxs[i][2]) + ' ' + str(boxs[i][3]) + ' ')
            list_file.write('\n')

            track_file.write(str(frame_index) + ' ')
            if len(tracker.tracks) != 0:
                for track in tracker.tracks:
                    #print(track.time_since_update, track.age, track.state, track.track_id)
                    bbox = track.to_tlbr()
                    track_file.write(
                        str(track.track_id) + ' ' + str(track.is_confirmed()) +
                        ' ' + str(track.time_since_update) + ' ' +
                        str(int(bbox[0])) + ' ' + str(int(bbox[1])) + ' ' +
                        str(int(bbox[2])) + ' ' + str(int(bbox[3])) + ' ')
            track_file.write('\n')
        #print('detect:', time.time()-dt)
        fps = (fps + (1. / (time.time() - t1))) / 2
        #print("fps= %f"%(fps))

        cv2.imshow('detect result', frame)
        # Press Q to stop!
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break

        #if frame_index == 10:
        #    break

    # print('save .avi')
    yolo.close_session()
    video_capture.release()

    if writeVideo_flag:
        #out.release()
        list_file.close()
        track_file.close()
    cv2.destroyAllWindows()
示例#12
0
def read(stack):
    print('Process to read: %s' % os.getpid())
    yolo = YOLO()
    # Definition of the parameters
    max_cosine_distance = 0.3
    nn_budget = None
    nms_max_overlap = 1.0
    # deep_sort
    model_filename = 'model_data/mars-small128.pb'
    encoder = gdet.create_box_encoder(model_filename, batch_size=1)

    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    tracker = Tracker(metric)
    max_boxs = 0
    face = ['A']
    # face = []
    # cur1 = conn.cursor()  # 获取一个游标
    # sql = "select * from worker"
    # cur1.execute(sql)
    # data = cur1.fetchall()
    # for d in data:
    #     # 注意int类型需要使用str函数转义
    #     name = str(d[1])
    #
    #     face.append(name)
    # cur1.close()  # 关闭游标
    yolo2 = YOLO2()
    #目标上一帧的点
    history = {}
    #id和标签的字典
    person = {}
    #赋予新标签的id列表
    change = []
    while True:
        if len(stack) != 0:
            frame = stack.pop()
            t1 = time.time()
            localtime = time.asctime(time.localtime(time.time()))
            # 进行安全措施检测
            #frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
            img = Image.fromarray(frame)
            #img.save('frame.jpg')
            frame, wear = yolo2.detect_image(img)
            frame = np.array(frame)
            # frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
            # 获取警戒线
            transboundaryline = line.readline()
            utils.draw(frame, transboundaryline)
            # image = Image.fromarray(frame)
            image = Image.fromarray(frame[..., ::-1])  # bgr to rgb
            boxs = yolo.detect_image(image)
            # print("box_num",len(boxs))
            features = encoder(frame, boxs)

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

            # Run non-maxima suppression.
            boxes = np.array([d.tlwh for d in detections])
            scores = np.array([d.confidence for d in detections])
            indices = preprocessing.non_max_suppression(
                boxes, nms_max_overlap, scores)
            detections = [detections[i] for i in indices]
            if len(boxs) > max_boxs:
                max_boxs = len(boxs)
            # Call the tracker
            tracker.predict()
            tracker.update(detections)
            #一帧信息
            info = {}
            target = []
            for track in tracker.tracks:
                #一帧中的目标
                per_info = {}
                if not track.is_confirmed() or track.time_since_update > 1:
                    continue
                if track.track_id not in person:
                    person[track.track_id] = str(track.track_id)
                bbox = track.to_tlbr()
                PointX = bbox[0] + ((bbox[2] - bbox[0]) / 2)
                PointY = bbox[3]
                dis = int(PointX) - 1200
                try:
                    if dis < 15:
                        if track.track_id not in change:
                            person[track.track_id] = face.pop(0)
                            change.append(track.track_id)
                except:
                    print('非法入侵')
                #当前目标
                if track.track_id not in change:
                    per_info['worker_id'] = 'unknow' + str(track.track_id)
                else:
                    per_info['worker_id'] = person[track.track_id]
                #当前目标坐标
                yoloPoint = (int(PointX), int(PointY))
                per_info['current_point'] = yoloPoint

                # 卡尔曼滤波预测
                if per_info['worker_id'] not in utils.KalmanNmae:
                    utils.myKalman(per_info['worker_id'])
                if per_info['worker_id'] not in utils.lmp:
                    utils.setLMP(per_info['worker_id'])
                cpx, cpy = utils.predict(yoloPoint[0], yoloPoint[1],
                                         per_info['worker_id'])

                if cpx[0] == 0.0 or cpy[0] == 0.0:
                    cpx[0] = yoloPoint[0]
                    cpy[0] = yoloPoint[1]
                per_info['next_point'] = (int(cpx), int(cpy))
                # 写入安全措施情况
                wear_dic = {}
                per_info['wear'] = 'safe wear'
                if len(wear) > 0:

                    for w in wear:
                        wear_dis = int(
                            math.sqrt(
                                pow(w[0] - yoloPoint[0], 2) +
                                pow(w[1] - yoloPoint[1], 2)))
                        wear_dic[wear_dis] = w
                    wear_dic = sorted(wear_dic.items(),
                                      key=operator.itemgetter(0),
                                      reverse=False)
                    if wear_dic[0][0] < 180:
                        if wear[wear_dic[0][1]] == 1:
                            per_info['wear'] = 'no_helmet'

                        elif wear[wear_dic[0][1]] == 2:
                            per_info['wear'] = 'no work cloths'

                        elif wear[wear_dic[0][1]] == 3:
                            per_info['wear'] = 'unsafe wear'
                # 写入越线情况
                if per_info['worker_id'] in history:
                    per_info['transboundary'] = 'no'
                    # print(transboundaryline)

                    line1 = [
                        per_info['next_point'], history[per_info['worker_id']]
                    ]
                    a = line.IsIntersec2(transboundaryline, line1)

                    if a == '有交点':
                        print('越线提醒')

                        per_info['transboundary'] = 'yes'

                history[per_info['worker_id']] = per_info['current_point']

                #print(per_info)
                #画目标框
                #cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2)
                cv2.putText(frame, per_info['worker_id'],
                            (int(bbox[0]), int(bbox[1])), 0, 5e-3 * 200,
                            (0, 255, 0), 2)
                target.append(per_info)
            info['time'] = localtime
            #info['frame'] = str(img.tolist()).encode('base64')
            info['frame'] = 'frame'
            info['target'] = target
            #写入josn
            info_json = json.dumps(info)
            info_queue.put(info_json)
            getInfo(info_queue)
            cv2.imshow("img", frame)
            key = cv2.waitKey(1) & 0xFF
            if key == ord('q'):
                break
parser.add_argument('--yolo_anchors',
                    help='Classes path (.txt)',
                    default='../model_data/tiny_yolo_anchors.txt')

parser.add_argument('--video_source',
                    help='Video source (0) for webcam',
                    default="../Datasets/test_suspect.mp4")

args = parser.parse_args()

model_path = args.model_path
classes_path = args.classes_path
yolo_anchors = args.yolo_anchors
video_source = args.video_source

Yolo_Obj = YOLO(classes_path, yolo_anchors, model_path)

font = cv2.FONT_HERSHEY_SIMPLEX

cap = cv2.VideoCapture(video_source)
print("Width : ", int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)))
print("Height : ", int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))

fps_display_interval = 5  # seconds
frame_rate = 0
frame_count = 0
start_time = time.time()
frame_rate_tab = []

while (True):
    ret, frame = cap.read()
示例#14
0
            out.write(frame)
            frame_index = frame_index + 1
            list_file.write(str(frame_index) + ' ')
            if len(boxs) != 0:
                for i in range(0, len(boxs)):
                    list_file.write(
                        str(boxs[i][0]) + ' ' + str(boxs[i][1]) + ' ' +
                        str(boxs[i][2]) + ' ' + str(boxs[i][3]) + ' ')
            list_file.write('\n')

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

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

    video_capture.release()
    if writeVideo_flag:
        out.release()
        list_file.close()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    pt1 = (0, 0)
    pt2 = (0, 0)
    topLeft_clicked = False
    bottomRight_clicked = False
    main(YOLO(), LicenseNumberDetector())
示例#15
0
    'helmet': {
        'model_path': 'model_data/yolov3_shantou_0512.h5',
        'classes_path': 'model_data/yolov3_shantou_classes.txt',
    }
}

# 定义启动使用的模型
detect_config = detect_configs['card']

# 获取目标类别
detect_classes = []
with open(detect_config['classes_path']) as f:
    detect_classes = [t.strip() for t in f.readlines() if len(t.strip()) > 0]

# 预加载模型
yolo = YOLO(**detect_config)


def detect_images(filenames, classes=None):
    """检测多个图片
    :param filenames 文件名列表
    :param classes 需要检测的对象分类列表
    :return
    """
    res = []  # 保存结果数据
    for path in filenames:
        image_type = 'png' if path.lower().endswith('.png') else 'jpg'
        path = format_input_path(path)
        img = parse_input_image(image_path=path, image_type=image_type)
        _, data = yolo.detect_image(img)
        res.append(format_output_data(data, classes))
示例#16
0
    image_ids = open(os.path.join(VOCdevkit_path, "VOC2007/ImageSets/Main/test.txt")).read().strip().split()

    if not os.path.exists(map_out_path):
        os.makedirs(map_out_path)
    if not os.path.exists(os.path.join(map_out_path, 'ground-truth')):
        os.makedirs(os.path.join(map_out_path, 'ground-truth'))
    if not os.path.exists(os.path.join(map_out_path, 'detection-results')):
        os.makedirs(os.path.join(map_out_path, 'detection-results'))
    if not os.path.exists(os.path.join(map_out_path, 'images-optional')):
        os.makedirs(os.path.join(map_out_path, 'images-optional'))

    class_names, _ = get_classes(classes_path)

    if map_mode == 0 or map_mode == 1:
        print("Load model.")
        yolo = YOLO(confidence = 0.001, nms_iou = 0.5)
        print("Load model done.")

        print("Get predict result.")
        for image_id in tqdm(image_ids):
            image_path  = os.path.join(VOCdevkit_path, "VOC2007/JPEGImages/"+image_id+".jpg")
            image       = Image.open(image_path)
            if map_vis:
                image.save(os.path.join(map_out_path, "images-optional/" + image_id + ".jpg"))
            yolo.get_map_txt(image_id, image, class_names, map_out_path)
        print("Get predict result done.")
        
    if map_mode == 0 or map_mode == 2:
        print("Get ground truth result.")
        for image_id in tqdm(image_ids):
            with open(os.path.join(map_out_path, "ground-truth/"+image_id+".txt"), "w") as new_f:
示例#17
0
import matplotlib
import matplotlib.pyplot as plt
import argparse
from PIL import Image
from yolo import YOLO, detect_video

import argparse
import cv2
#keras-yolo에서 image처리를 주요 PIL로 수행.

LOCAL_PACKAGE_DIR = os.path.abspath("./keras-yolo3")
sys.path.append(LOCAL_PACKAGE_DIR)
default_yolo_dir = '/content/DLCV/Detection/yolo'
config_dict = {}
yolo = YOLO(model_path='./model_data/yolo.h5',
            anchors_path='./model_data/yolo_anchors.txt',
            classes_path='./model_data/coco_classes.txt')

img = Image.open(os.path.join('../../../data/image/beatles01.jpg'))
detected_img = yolo.detect_image(img)

plt.figure(figsize=(12, 12))
plt.imshow(detected_img)
#plt.show()


def detect_video_yolo(model, input_path, output_path=""):

    start = time.time()
    cap = cv2.VideoCapture(input_path)
from yolo import YOLO, detect_video, detect_img

if __name__ == '__main__':
    # 检测图片
    detect_img(YOLO(), 'test/person.jpg')

    # 检测视频
    # detect_video(YOLO(), 'test/test_video.mp4', 'test/test_video_out.mp4')

    # 检测camera
    # detect_video(YOLO(), video_path=0, output_path='test/test_video_out.mp4')
示例#19
0
def classify(path):
    yolo = YOLO()
    image = Image.open(path)
    r_image = yolo.detect_image(image)
    r_image.show()
    yolo.close_session()
示例#20
0
    for item in input_paths:
        if item.endswith(img_endings):
            input_image_paths.append(item)
        elif item.endswith(vid_endings):
            input_video_paths.append(item)

    output_path = args.output
    if not os.path.exists(output_path):
        os.makedirs(output_path)

    # define YOLO detector
    yolo = YOLO(
        **{
            "model_path": args.model_path,
            "anchors_path": args.anchors_path,
            "classes_path": args.classes_path,
            "score": args.score,
            "gpu_num": args.gpu_num,
            "model_image_size": (416, 416),
        })

    # Make a dataframe for the prediction outputs
    out_df = pd.DataFrame(columns=[
        'image', 'image_path', 'xmin', 'ymin', 'xmax', 'ymax', 'label',
        'confidence', 'x_size', 'y_size'
    ])

    # labels to draw on images
    class_file = open(args.classes_path, 'r')
    input_labels = [line.rstrip('\n') for line in class_file.readlines()]
    print('Found {} input labels: {} ...'.format(len(input_labels),
def main():
    # Open YOLO
    yolo = YOLO('full')

    # Open MobileNet SSD
    #model_mnet = Mobilenetdnn()

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

    # deep_sort
    model_filename = 'model_data/mars-small128.pb'
    encoder = gdet.create_box_encoder(model_filename, batch_size=1)
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    tracker = Tracker(metric)

    # File text to log
    face_text = open('face.txt', 'w')
    person_text = open('person.txt', 'w')

    # Facenet-based face recognizer
    person_to_follow = 'bach'
    face_dettect = Recognizer('resnet10')
    # Count how many frames until we switch to person-tracking
    person_found = False
    pno = 0
    total_pno = 0

    # Flag to choose which model to run
    face_flag = True
    yolosort = False

    # Flag to override autopilot
    auto_engaged = False
    # This is for controlling altitude manually
    auto_throttle = True

    # Transfer to person tracking
    person_to_track = None
    face_locked = False
    confirmed_number = 0
    # String to convert to ID that needs tracking
    confirmed_string = ""

    # Open stream
    video_capture = VideoGet("rtsp://192.168.4.102:8554/test").start()

    # Enter drone and control speed
    do_you_have_drone = True
    velocity = 1
    velocity2 = 2
    if do_you_have_drone:
        drone = naza('192.168.4.102', 5005).start()

    # Enter safety zone coordinate
    # For face tracking
    safety_x = 100
    safety_y = 100
    # For person tracking
    safety_x_person = 150
    safety_y_person = 100

    writeVideo_flag = True
    if writeVideo_flag:
        # Define the codec and create VideoWriter object
        w = 1280
        h = 720
        fourcc = cv2.VideoWriter_fourcc(*'MJPG')
        localtime = strftime("m%md%d-%H%M%S")
        out = cv2.VideoWriter('output %s.avi' % localtime, fourcc, 15, (w, h))
        frame_index = -1

    fps = 0.0
    f_drop = 0

    while True:
        ret, frame = video_capture.update()
        '''
        if ret != True:
            f_drop += 1
            if f_drop > 10:
                print("Dropped frames.")
                break
        else:
            f_drop = 0
        '''
        t1 = time.time()
        #frame = cv2.flip(frame, 1)

        # Resize frame
        resize_to = (1280, 720)
        resize_div_2 = (int(resize_to[0] / 2), int(resize_to[1] / 2))
        frame = cv2.resize(frame, resize_to)

        # Show control on the right corner of frame
        control_disp = ""

        # Show autopilot status
        if auto_engaged:
            print_out = "AUTOPILOT "
        else:
            print_out = "MANUAL "
        # Display if controlling altitude automatically
        if auto_throttle:
            print_out += "ATh "

        # Show model in use
        if face_flag:
            model_in_use = "FACE"
        elif yolosort:
            model_in_use = "PERSON"

        # Face recognizer
        vector_true = np.array((resize_div_2[0], resize_div_2[1]))
        if face_flag:
            face_bbox = face_dettect.recognize(frame, person_to_follow)
            person_found = False
            if auto_engaged:
                # Prevent autopilot when there is no face detected
                if len(face_bbox) == 0:
                    if do_you_have_drone:
                        drone.yaw = 8
                        drone.pitch = 8
                        if auto_throttle:
                            drone.throttle = 9
                else:
                    for i in range(len(face_bbox)):
                        face_name = face_bbox[i][4]
                        # Calculate face area
                        face_area = int(face_bbox[i][2] -
                                        face_bbox[i][0]) * int(
                                            face_bbox[i][3] - face_bbox[i][1])

                        if face_name == person_to_follow:
                            person_found = True
                            pno += 1
                            total_pno += 1

                            # This calculates the vector from your ROI to the center of the screen
                            center_of_bound_box = np.array(
                                ((face_bbox[i][0] + face_bbox[i][2]) / 2,
                                 (face_bbox[i][1] + face_bbox[i][3]) / 2))
                            vector_target = np.array(
                                (int(center_of_bound_box[0]),
                                 int(center_of_bound_box[1])))
                            vector_distance = vector_true - vector_target

                            if vector_distance[0] > safety_x:
                                print("Yaw left.")
                                control_disp += "y<- "
                                if do_you_have_drone:
                                    drone.yaw = 8 - 1
                            elif vector_distance[0] < -safety_x:
                                print("Yaw right.")
                                control_disp += "y-> "
                                if do_you_have_drone:
                                    drone.yaw = 8 + 1
                            else:
                                if do_you_have_drone:
                                    drone.yaw = 8

                            if vector_distance[1] > safety_y:
                                print("Fly up.")
                                control_disp += "t^ "
                                if do_you_have_drone:
                                    if auto_throttle:
                                        drone.throttle = 10
                            elif vector_distance[1] < -safety_y:
                                print("Fly down.")
                                control_disp += "tV "
                                if do_you_have_drone:
                                    if auto_throttle:
                                        drone.throttle = 6
                            else:
                                if do_you_have_drone:
                                    if auto_throttle:
                                        drone.throttle = 9

                            if face_area < 9000:
                                print("Push forward")
                                control_disp += "p^ "
                                if do_you_have_drone:
                                    drone.pitch = 8 + 3
                            elif face_area > 16000:
                                print("Pull back")
                                control_disp += "pV "
                                if do_you_have_drone:
                                    drone.pitch = 8 - 2
                            else:
                                if do_you_have_drone:
                                    drone.pitch = 8

                            # Print center of bounding box and vector calculations
                            print_out += str(face_area)
                            cv2.circle(frame, (int(center_of_bound_box[0]),
                                               int(center_of_bound_box[1])), 5,
                                       (0, 100, 255), 2)
                            # Draw the safety zone
                            cv2.rectangle(frame, (resize_div_2[0] - safety_x,
                                                  resize_div_2[1] - safety_y),
                                          (resize_div_2[0] + safety_x,
                                           resize_div_2[1] + safety_y),
                                          (0, 255, 255), 2)

                            # Transfer face to person tracking
                            if total_pno >= 20 and (pno / total_pno) >= 0.5:
                                person_to_track = face_bbox[i][0:4]
                                if do_you_have_drone:
                                    drone.default()
                                face_flag = False
                                yolosort = True
                                pno = 0
                                total_pno = 0

                        # Draw bounding box over face
                        cv2.rectangle(frame,
                                      (face_bbox[i][0], face_bbox[i][1]),
                                      (face_bbox[i][2], face_bbox[i][3]),
                                      (0, 255, 0), 2)
                        _text_x = face_bbox[i][0]
                        _text_y = face_bbox[i][3] + 20

                        # Write name on frame
                        cv2.putText(frame,
                                    str(face_name),
                                    (_text_x, _text_y + 17 * 2),
                                    cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                    1, (255, 255, 255),
                                    thickness=1,
                                    lineType=2)
                        cv2.putText(frame,
                                    str(face_bbox[i][0:4]), (_text_x, _text_y),
                                    cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                    1, (255, 255, 255),
                                    thickness=1,
                                    lineType=2)
                        cv2.putText(frame,
                                    str(round(face_bbox[i][5][0], 3)),
                                    (_text_x, _text_y + 17),
                                    cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                    1, (255, 255, 255),
                                    thickness=1,
                                    lineType=2)

                # Count how many frames until tracked person is lost
                if not person_found:
                    if pno > 0:
                        total_pno += 1
                    if total_pno >= 20 and (pno / total_pno) < 0.5:
                        pno = 0
                        total_pno = 0

        # Person tracking
        if yolosort:
            image = Image.fromarray(frame[..., ::-1])  #bgr to rgb
            boxs = yolo.detect_image(image)
            #boxs = model_mnet.detect(frame, 0.7)
            features = encoder(frame, boxs)

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

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

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

            for track in tracker.tracks:
                if not track.is_confirmed() or track.time_since_update > 1:
                    if auto_engaged and confirmed_number == track.track_id:
                        if do_you_have_drone:
                            drone.yaw = 8
                            drone.pitch = 8
                            if auto_throttle:
                                drone.throttle = 9
                        # Swap back to face detection if person can't be found
                        face_flag = True
                        yolosort = False
                    continue
                bbox = track.to_tlbr()
                # Only track 1 person
                if person_to_track:
                    if person_to_track[0] > bbox[0] and person_to_track[
                            2] < bbox[2] and person_to_track[3] < bbox[
                                3] and person_to_track[0] < bbox[
                                    2] and person_to_track[2] > bbox[
                                        0] and person_to_track[3] > bbox[1]:
                        print("Captured.")
                        face_locked = True
                        confirmed_number = track.track_id
                    else:
                        face_locked = False
                        print("Retry capture.")
                        face_flag = True
                        yolosort = False
                person_to_track = None

                # Calculate person bounding box area
                person_area = int(bbox[2] - bbox[0]) * int(bbox[3] - bbox[1])

                if face_locked:
                    if confirmed_number == track.track_id:
                        if auto_engaged:
                            # This calculates the vector from your ROI to the center of the screen
                            center_of_bound_box = np.array(
                                ((bbox[0] + bbox[2]) / 2,
                                 (bbox[1] + bbox[3]) / 2))
                            vector_target = np.array(
                                (int(center_of_bound_box[0]),
                                 int(center_of_bound_box[1])))
                            vector_distance = vector_true - vector_target

                            if vector_distance[0] > safety_x_person:
                                print("Yaw left.")
                                control_disp += "y<- "
                                if do_you_have_drone:
                                    drone.yaw = 8 - 1
                            elif vector_distance[0] < -safety_x_person:
                                print("Yaw right.")
                                control_disp += "y-> "
                                if do_you_have_drone:
                                    drone.yaw = 8 + 1
                            else:
                                if do_you_have_drone:
                                    drone.yaw = 8

                            if vector_distance[1] > safety_y_person:
                                print("Fly up.")
                                control_disp += "t^ "
                                if do_you_have_drone:
                                    if auto_throttle:
                                        drone.throttle = 10
                            elif vector_distance[1] < -safety_y_person:
                                print("Fly down.")
                                control_disp += "tV "
                                if do_you_have_drone:
                                    if auto_throttle:
                                        drone.throttle = 6
                            else:
                                if do_you_have_drone:
                                    if auto_throttle:
                                        drone.throttle = 9
                                #pass

                            if person_area < 60000:
                                print("Push forward")
                                control_disp += "p^ "
                                if do_you_have_drone:
                                    drone.pitch = 8 + 3
                            elif person_area > 90000:
                                print("Pull back")
                                control_disp += "pV "
                                if do_you_have_drone:
                                    drone.pitch = 8 - 2
                            else:
                                if do_you_have_drone:
                                    drone.pitch = 8
                                #pass

                            # Print center of bounding box and vector calculations
                            print_out += str(confirmed_number) + " "
                            print_out += str(person_area)
                            cv2.circle(frame, (int(center_of_bound_box[0]),
                                               int(center_of_bound_box[1])), 5,
                                       (0, 255, 255), 2)
                        # Draw selected bounding box
                        cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                                      (int(bbox[2]), int(bbox[3])),
                                      (255, 255, 255), 2)
                        cv2.putText(frame, "Tracked-" + str(track.track_id),
                                    (int(bbox[0]), int(bbox[1]) + 100), 0,
                                    5e-3 * 200, (0, 255, 0), 2)
                        # Draw the safety zone
                        cv2.rectangle(frame,
                                      (resize_div_2[0] - safety_x_person,
                                       resize_div_2[1] - safety_y_person),
                                      (resize_div_2[0] + safety_x_person,
                                       resize_div_2[1] + safety_y_person),
                                      (0, 255, 255), 2)
                        break
                else:
                    # Draw bounding box and calculate box area
                    cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                                  (int(bbox[2]), int(bbox[3])),
                                  (255, 255, 255), 2)
                    cv2.putText(frame,
                                str(track.track_id) + "," + str(person_area),
                                (int(bbox[0]), int(bbox[1]) + 100), 0,
                                5e-3 * 200, (0, 255, 0), 2)

            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)

        # Show number while typing
        print_out += confirmed_string
        # Draw the center of frame as a circle and autopilot status
        middle_of_frame = (int(resize_div_2[0]), int(resize_div_2[1]))
        cv2.circle(frame, middle_of_frame, 5, (255, 128, 0), 2)
        cv2.putText(frame, print_out, (0, (frame.shape[0] - 10)), 0, 0.8,
                    (0, 0, 255), 2)

        # Keypress action
        k = cv2.waitKey(1) & 0xFF
        if k == ord('q'):
            break
        if k == ord('r'):
            face_flag = not face_flag
            yolosort = not yolosort
            face_locked = False
            pno = 0
            total_pno = 0
            # Reset control to prevent moving when switching model
            if do_you_have_drone:
                drone.default()
        # Number key for entering ID to track
        num_string = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
        if not face_locked and yolosort:
            if k >= 48 and k <= 57:
                confirmed_string += num_string[k - 48]
            if k == 13 and confirmed_string != '':
                confirmed_number = int(confirmed_string)
                face_locked = True
                confirmed_string = ""
        if k == ord('c'):
            confirmed_string = ""
            face_locked = False

        # Override autopilot
        if k == ord('o'):
            auto_engaged = not auto_engaged
            if do_you_have_drone:
                drone.default()

        if do_you_have_drone:
            # Control drone
            # Override auto throttle control
            if k == ord('u'):
                auto_throttle = not auto_throttle

            # Takeoff
            if k == ord('y'):
                drone.ignite()
                #pass

            # Throttle
            if not auto_throttle and not auto_engaged:
                if k == ord('w'):
                    drone.throttle_up()
                    control_disp += "t^ "
                    #drone.incremt(0,0,1,0)
                elif k == ord('s'):
                    drone.throttle_dwn()
                    control_disp += "tV "
                    #drone.incremt(0,0,-1,0)

            if not auto_engaged:
                # Yaw
                if k == ord('a'):
                    drone.yaw_left()
                    control_disp += "y<- "
                    #drone.incremt(0,0,0,-velocity2)
                elif k == ord('d'):
                    drone.yaw_right()
                    control_disp += "y-> "
                    #drone.incremt(0,0,0,velocity2)

                # Pitch
                if k == ord('i'):
                    drone.pitch_fwd()
                    control_disp += "p^ "
                    #drone.incremt(0,velocity2,0,0)
                elif k == ord('k'):
                    drone.pitch_bwd()
                    control_disp += "pV "
                    #drone.incremt(0,-velocity2,0,0)

                # Roll
                if k == ord('j'):
                    drone.roll_left()
                    control_disp += "r<- "
                    #drone.incremt(-velocity2,0,0,0)
                elif k == ord('l'):
                    drone.roll_right()
                    control_disp += "r-> "
                    #drone.incremt(velocity2,0,0,0)

                if k == ord('f'):
                    drone.incremt(0, 0, 0, 0)

        # Show model in use on frame
        cv2.putText(frame, model_in_use, (0, 20), 0, 0.8, (255, 0, 0), 2)
        # Draw drone control
        cv2.putText(frame, control_disp,
                    ((frame.shape[1] - 150), (frame.shape[0] - 10)), 0, 0.8,
                    (0, 0, 255), 2)
        # Scalable window
        cv2.namedWindow('Camera', cv2.WINDOW_NORMAL)
        cv2.imshow('Camera', frame)

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

        fps = (fps + (1. / (time.time() - t1))) / 2
        if face_flag:
            face_text.write(str(fps) + "\n")
        if yolosort:
            person_text.write(str(fps) + "\n")
        print("fps= %f" % (fps))
        print("bach= %d/%d, person_found= %d" % (pno, total_pno, person_found))

    # Exiting
    video_capture.stop()
    if do_you_have_drone:
        drone.close_connection()
    if writeVideo_flag:
        out.release()
    cv2.destroyAllWindows()
    face_text.close()
    person_text.close()
示例#22
0
#python yolo_video.py --input car.mp4 --output car.avi
#python yolo_video.py --input test_data/akiha.mp4
#python yolo_video.py --image
import sys
import argparse
from yolo import YOLO, detect_video
from PIL import Image

#detect_video(YOLO(), 0 ,"saved_cam.avi")

detect_video(YOLO(), "http://192.168.43.232:8080/video", "saved_cam.mp4")
示例#23
0
            bbox = det.to_tlbr()
            cv2.rectangle(frame,(int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])),(255,0,0), 2)
            
        cv2.imshow('', frame)
        
        if writeVideo_flag:
            # save a frame
            out.write(frame)
            frame_index = frame_index + 1
            list_file.write(str(frame_index)+' ')
            if len(boxs) != 0:
                for i in range(0,len(boxs)):
                    list_file.write(str(boxs[i][0]) + ' '+str(boxs[i][1]) + ' '+str(boxs[i][2]) + ' '+str(boxs[i][3]) + ' ')
            list_file.write('\n')
            
        fps  = ( fps + (1./(time.time()-t1)) ) / 2
        print("fps= %f"%(fps))
        
        # Press Q to stop!
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    video_capture.release()
    if writeVideo_flag:
        out.release()
        list_file.close()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main(YOLO())
def press():
    new_model = load_model('Letter_Model_V3_999_1')
    alph = 'ABCDEFGHIJKLMNOPQRSTUVWXY'
    alph_dict = {}
    for i,n in enumerate(alph):
        alph_dict.update({i:n})
    
    camera=cv2.VideoCapture(0)
    img_counter = 0
    List_alph = [i for i in alph]
    if tkvar.get()=='Random':
        letter = random.choice(List_alph)
    else:
        letter=tkvar.get()
    while True:
        ret, frame = camera.read()
        if not ret:
            print("failed to grab frame")
            break
        color = (0, 255, 255)
        cv2.putText(frame,'Please try to sign the letter: ' + (letter),(20,20),cv2.FONT_HERSHEY_SIMPLEX,
                    0.5, color, 2)
        cv2.imshow("Gesture Detector", frame)
    
        k = cv2.waitKey(1)
        if k%256 == 27:
            # ESC pressed
            print("Escape hit, closing...")
            break
        elif k%256 == 32:
            # SPACE pressed
            img_name = "opencv_frame_{}.png".format(img_counter)
            print("{} written!".format(img_name))
            x=frame
            img_counter += 1
            
            yolo = YOLO("/Users/Denny/Desktop/Hack_The_Northeast/yolo-hand-detection/models/cross-hands.cfg", "/Users/Denny/Desktop/Hack_The_Northeast/yolo-hand-detection/models/cross-hands.weights", ["hand"])
            width, height, inference_time, results = yolo.inference(x)
            frame = x        
            for detection in results:
                id, name, confidence, x, y, w, h = detection
                cx = x + (w / 2)
                cy = y + (h / 2)
                crop_img = frame[y-50:y+h+50, x-50:x+w+50]
                im = Image.fromarray(crop_img)
                im.save("your_file.png")
                im = cv2.imread('your_file.png',0)
                new_img = cv2.resize(im,(28,28))
                the_class = new_model.predict_classes(new_img.reshape(1,28,28,1))
                Answer = alph_dict[the_class[0]]
                # draw a bounding box rectangle and label on the image
                color = (0, 255, 255)

                if Answer == letter:
                    cv2.rectangle(frame, (x, y), (x + w, y + h), (0,255,0), 2)
                    text = 'Correct Answer for: ' + Answer
                    cv2.putText(frame, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,
                        0.5, (0,255,0), 2)
                else:
                    cv2.rectangle(frame, (x, y), (x + w, y + h), (0,0,255), 2)
                    text = 'Try again!'
                    cv2.putText(frame, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,
                        0.5, (0,0,255), 2)
                    
            cv2.imshow("preview", frame)

    camera.release()
    
    cv2.destroyAllWindows()
示例#25
0
from yolo import YOLO
import color_classifier as cc
import cv2
import numpy as np
from PIL import Image
import os
if __name__ == "__main__":

    y = YOLO()
    car_color_list = []
    i = 0
    for r, _, files in os.walk(r"../../dataset"):
        for f in files:
            # print()
            in_path = os.path.joinappend(r, f)
            out_path = in_path.replace("dataset", "cropped")
            #print("file",in_path)
            if not os.path.exists(os.path.dirname(out_path)):
                os.makedirs(os.path.dirname(out_path))
            img = Image.open(in_path)
            resp = y.detect_image(img)
            for _i, _r in enumerate(resp):
                #print(_r)
                _img = img.crop(_r)
                _img = _img.resize((224, 224))
                op, ext = os.path.splitext(out_path)
                op = op + "_{}".format(_i) + "." + ext
                print(op)
                _img.save(op)
                _img = np.array(_img)
                car_color = cc.detect_color(_img)
示例#26
0

#---------------------------------
#  1.加载模型
#---------------------------------

# coco
modelpath = "model/"
start = time.time()
pose_model = general_coco_model(modelpath)  # 1.加载模型

print("[INFO]Pose Model loads time: ", time.time() - start)

# yolo
start = time.time()
_yolo = YOLO() # 1.加载模型

print("[INFO]yolo Model loads time: ", time.time() - start)

imgpath='D:/myworkspace/dataset/My_test/bagofwords/you/you_1_32.png'

bgImg_save(imgpath,'')

'''
X = []  # 定义图像名称
Y = []  # 定义图像分类类标
# Z = [] #定义图像像素

path = 'D:/myworkspace/dataset/My_test/dataset/hand_classification/'
savepath = 'D:/myworkspace/dataset/My_test/dataset/hand_background_classification/'
示例#27
0
def main():

    PATH_TO_CKPT_PERSON = 'models/faster_rcnn_restnet50.pb'

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

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

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

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

    if asyncVideo_flag:
        video_capture.start()

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

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

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

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

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

        # features = encoder(frame, boxs)
        if len(cropped_persons) > 0:
            features = reid.extract_feature_imgTensor(cropped_persons)
            # print(features.shape)
            detections = [
                Detection(bbox, confidence,
                          feature) for bbox, confidence, feature in zip(
                              boxs, confidence, features)
            ]

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

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

            for track in tracker.tracks:
                if not track.is_confirmed() or track.time_since_update > 1:
                    continue
                bbox = track.to_tlbr()
                cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                              (int(bbox[2]), int(bbox[3])), (0, 0, 255), 2)
                cv2.putText(frame, str(track.track_id),
                            (int(bbox[0]), int(bbox[1])), 0, 5e-3 * 200,
                            (0, 255, 0), 2)

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

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

                track_count += 1

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

        cv2.imshow('YOLO DeepSort', frame)

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

        fps_imutils.update()

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

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

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

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

    if writeVideo_flag:
        out.release()

    cv2.destroyAllWindows()
示例#28
0
from yolo import YOLO
from yolo import detect_video

if __name__ == '__main__':
    video_path = '/media/wuwenfu5/Win_Ubuntu_Swap/Python_/Material/MyMOT/shitang7.AVI'
    # video_path = 0
    detect_video(YOLO(), video_path)
示例#29
0
#-------------------------------------#
#       调用摄像头检测
#-------------------------------------#
from yolo import YOLO
from PIL import Image
import numpy as np
import cv2
import time
yolo = YOLO()
# 调用摄像头
capture=cv2.VideoCapture("img/video-01.avi") # capture=cv2.VideoCapture(0) #

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)
示例#30
0
def _main():
    parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS)
    FLAGS = parser.parse_args()
    detect_cam(YOLO(**vars(FLAGS)))