Esempio n. 1
0
def load_yolo(load_weight):
    # model은 사용하는 cfg model
    options = {"model": "cfg/size-yolo.cfg",
               "load": load_weight}  # 최신 checkpoint를 load하려면 -1
    tfnet = TFNet(options)
    tfnet.load_from_ckpt()
    return tfnet
def main():
    options = {
        "model": "cfg/yolo_custom.cfg",
        "load": -1
        # "gpu": 1.0
    }

    tfnet2 = TFNet(options)

    tfnet2.load_from_ckpt()

    results_list = []

    for i in range(1, 292):
        original_img = cv2.imread(os.getcwd() +
                                  "/screen_shots_ready/images/FIFA" +
                                  str(i).zfill(3) + ".jpg")
        original_img = cv2.cvtColor(original_img, cv2.COLOR_BGR2RGB)
        results = tfnet2.return_predict(original_img)
        if results:
            results_list.append([i, original_img, results])
        print(i, results)

    for i in results_list:
        #fig, ax = plt.subplots(figsize=(20, 20))
        #ax.imshow(boxing(i[0], i[1]))
        new_image = boxing(i[1], i[2])
        cv2.imwrite(os.getcwd() + '/testing_results/' + str(i[0]) + '.jpg',
                    new_image)
def inference(model, load, threshold, gpu, test_path):
    params = {'model': model, 'load': load, 'threshold': threshold, 'gpu': gpu}
    gif = test_path
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    tfnet = TFNet(params)
    tfnet.load_from_ckpt()

    colors = [tuple(255 * np.random.rand(3)) for _ in range(10)]
    capture = cv2.VideoCapture(gif)
    out = cv2.VideoWriter('/home/an/shoeslace/test/test_video.mp4', fourcc,
                          20.0, (int(capture.get(3)), int(capture.get(4))))

    while (capture.isOpened()):
        start = time.time()
        ret, frame = capture.read()

        if ret:
            results = tfnet.return_predict(frame)
            for c, r in zip(colors, results):
                top_left = (r["topleft"]["x"], r["topleft"]["y"])
                bottom_right = (r["bottomright"]["x"], r["bottomright"]["y"])
                label = r["label"]
                confidence = r['confidence']

                print(top_left, bottom_right, label, confidence)
                frame = cv2.rectangle(frame,
                                      top_left,
                                      bottom_right,
                                      c,
                                      thickness=2)
                frame = cv2.putText(frame,
                                    label + " " + str(confidence),
                                    top_left,
                                    cv2.FONT_HERSHEY_COMPLEX,
                                    fontScale=0.5,
                                    color=c,
                                    thickness=1)
                out.write(frame)
                print("WRITING FRAMES")

            # Insert FPS/quit text and show image
            fps = "{:.0f} FPS".format(1 / (time.time() - start))
            cv2.imshow('Frame', frame)
            frame = cv2.putText(frame,
                                fps, (0, 20),
                                cv2.FONT_HERSHEY_SIMPLEX,
                                fontScale=0.8,
                                color=(0, 0, 0))
            # q to quit
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        else:
            capture.release()
            out.release()
            cv2.destroyAllWindows()
            break
Esempio n. 4
0
class Vision:
    def __init__(self, cam_num):
        self.options = {
            'model': 'cfg/yolov2-tiny-custom.cfg',
            'load': -500,
            'threshold': 0.50,
            'gpu': 0.0
        }

        self.results = 0
        self.stime = time.time()
        self.tl = (0, 0)
        self.br = (0, 0)
        self.label = []
        self.tfnet = TFNet(self.options)
        self.tfnet.load_from_ckpt()
        self.boxes_dict = {}
        self.capture = cv2.VideoCapture(cam_num)
        self.colors = [tuple('0, 225, 0'), tuple('255, 0, 0')]
        self.ret, self.frame = self.capture.read()

    def vision_generator(self, show=False):
        while True:
            ret, frame = self.capture.read()
            if ret:
                results = self.tfnet.return_predict(frame)
                for color, result in zip(self.colors, results):
                    label = result['label']
                    tl = (result['topleft']['x'], result['topleft']['y'])
                    br = (result['bottomright']['x'],
                          result['bottomright']['y'])
                    coord = tuple(tl, br)
                    self.boxes_dict.append({label, coord})
                    if show:
                        frame = cv2.rectangle(frame, tl, br, color, 7)
                        frame = cv2.putText(frame, label, tl,
                                            cv2.FONT_HERSHEY_COMPLEX, 1,
                                            (0, 0, 0), 2)
                if show:
                    cv2.imshow('frame', frame)
                    print('FPS {:.1f}'.format(1 / (time.time() - self.stime)))
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
            else:
                self.capture.release()
                cv2.destroyAllWindows()
                break
            yield self.boxes_dict

    def get_frame(self):
        if (time.time() - self.stime) >= 0.50:
            self._vision_generator()
            self.stime = time.time()
        else:
            print('Not Ready')
Esempio n. 5
0
def main(
        video_file_path: str,
        fps: float,
        width: int,
        height: int,
        sensibility: float,
        analyzed_frames_frequency: int,
):
    model = TFNet(
        {"model": "cfg/yolo.cfg", "load": "bin/yolo.weights", 'threshold': sensibility}
    )
    model.load_from_ckpt()

    #model = TFNet(
    #    {"model": "cfg/yolo.cfg", "load": "bin/yolo.weights", 'threshold': sensibility}
    #)
    print(f"Reading input video ...")
    start = time.time()
    capture = cv2.VideoCapture(video_file_path)
    print(fps, width, height)
    video = cv2.VideoWriter(
        "./video.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height)
    )

    print("Detecting poachers ...")
    success, count, predictions = True, 0, []
    while success:
        success, frame = capture.read()
        if frame is not None:
            frame = cv2.resize(frame, (width, height))
            if count % int(fps / analyzed_frames_frequency) == 0:
                predictions = [
                    prediction
                    for prediction in model.return_predict(frame)
                    if prediction["label"] == "person"
                ]
            for prediction in predictions:
                frame = cv2.rectangle(
                    img=frame,
                    pt1=(prediction["topleft"]["x"], prediction["topleft"]["y"]),
                    pt2=(
                        prediction["bottomright"]["x"],
                        prediction["bottomright"]["y"],
                    ),
                    color=[0, 0, 255],
                    thickness=1,
                )
            video.write(frame)
        count += 1
    print(f"{count} frames")
    print("Releasing video ...")

    video.release()
    print(f"{round(time.time() - start, 2)} seconds")
Esempio n. 6
0
def test_yolo(model_cfg, load, threshold, label_path, backup_path):
    options = {
        "model": model_cfg,
        "load": load,
        'threshold': threshold,
        "verbalise": False,
        "labels": label_path,
        "backup": backup_path
    }

    tfnet2 = TFNet(options)
    tfnet2.load_from_ckpt()
    return tfnet2
def detection():
    time.sleep(0.10)
    path = currentDirectory + path_Folder_Resources + path_Folder_cfg + "yolov2.cfg"
    isFile = os.path.isfile(path)
    if (False == isFile):
        msg = "File\t" + path + "\tmissing"
        messagebox.showerror("Error", msg)
        root.destroy()

    options = {"model": path, "load": -1, "gpu": 1.0}

    tfnet2 = TFNet(options)
    tfnet2.load_from_ckpt()
    print(root.filename)
    cap = cv2.VideoCapture(root.filename)
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)

    fourcc = cv2.VideoWriter_fourcc(*'DIVX')
    path = currentDirectory + path_Folder_outputs
    isFile = os.path.isdir(path)
    if (False == isFile):
        msg = "Folder\t" + path + "\tmissing"
        messagebox.showerror("Error", msg)
        root.destroy()
    out = cv2.VideoWriter(path + 'output.mp4', fourcc, 20.0,
                          (int(width), int(height)))

    while (True):
        # Capture frame-by-frame
        ret, frame = cap.read()

        if ret == True:
            frame = np.asarray(frame)
            results = tfnet2.return_predict(frame)
            new_frame = boxing(frame, results)
            # Display the resulting frame
            out.write(new_frame)
            cv2.imshow('frame', new_frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        else:
            break

    update_img("completed.jpg")
    # When everything done, release the capture
    cap.release()
    out.release()
    cv2.destroyAllWindows()

    return
Esempio n. 8
0
def main():
    #loading model
    options = {"model": "my_cfg/cfg/yolo_custom.cfg", "load": -1, "gpu": 1.0}
    tfnet2 = TFNet(options)
    tfnet2.load_from_ckpt()
    if (len(sys.argv) != 2):
        print("please only insert a path to a video file")
        exit(1)
    try:
        with open(sys.argv[1]) as file:
            pass
    except IOError as e:
        print("Unable to open file")  #Does not exist OR no read permissions
        exit(1)

    print(sys.argv[1])
    name = str.split(sys.argv[1], '.')
    cap = cv2.VideoCapture(sys.argv[1])
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    fourcc = cv2.VideoWriter_fourcc(*'DIVX')
    out = cv2.VideoWriter(name[0] + '_result.' + name[1], fourcc, 20.0,
                          (int(width), int(height)))
    while (cap.isOpened()):
        # Capture frame-by-frame
        ret, frame = cap.read()
        if ret == True:

            # Display the resulting frame
            cv2.imshow('Frame', frame)
            results = tfnet2.return_predict(frame)
            new_frame = boxing(frame, results)
            out.write(new_frame)
            # Press Q on keyboard to  exit
            if cv2.waitKey(25) & 0xFF == ord('q'):
                break

        # Break the loop
        else:
            break

    # When everything done, release the video capture object
    cap.release()
    cap.release()
    out.release()
    cv2.destroyAllWindows()
    print("finished tracking")
Esempio n. 9
0
def pred_img(options):
    #This loads pre-trained parameters from the checkpoint that we just specified in options.
    tfnetPredict = TFNet(options)
    tfnetPredict.load_from_ckpt()

    #We will take one of the images that has not been used for training to predict the class, bounding box and confidence
    original_img = cv2.imread("testimgs/soccer2.jpg")

    original_img = cv2.cvtColor(original_img, cv2.COLOR_BGR2RGB)
    result = tfnetPredict.return_predict(original_img)
    print(result)

    # pull out some info from the results
    for i in range(0, len(result)):
        tl = (result[i]['topleft']['x'], result[i]['topleft']['y'])
        br = (result[i]['bottomright']['x'], result[i]['bottomright']['y'])
        label = result[i]['label']  # add the box and label and display it
        img = cv2.rectangle(original_img, tl, br, (0, 255, 0), 7)
        img = cv2.putText(img, label, tl, cv2.FONT_HERSHEY_COMPLEX, 1,
                          (0, 0, 0), 2)
    plt.imshow(original_img)
    plt.show()
Esempio n. 10
0
}

options_predict_weight = {
    "model": args.model,
    "load": args.weight,
    "batch": args.batch_size,
    "imgdir": args.imgdir,
    "threshold": args.threshold,
    "gpu": args.gpu
}

load_type = {
    "n": options_train_null,
    "c": options_train_ckpt,
    "w": options_train_weight
}
load_type_predict = {"c": options_predict_ckpt, "w": options_predict_weight}

if (args.train):
    tfnet = TFNet(load_type[args.load])
    if (args.load == "c"):
        tfnet.load_from_ckpt()
    tfnet.train()
else:
    tfnet = TFNet(load_type_predict[args.load])
    if (args.load == "c"):
        tfnet.load_from_ckpt()
    imgcv = cv2.imread(args.imgdir)
    result = tfnet.return_predict(imgcv)
    print(result)
Esempio n. 11
0
def predict():
    options = {"model": "cfg/tiny-yolo-voc-1c.cfg",
            "load": -1,
            'threshold': 0.1
            #    "gpu": 1.0
            }

    tfnet = TFNet(options)

    tfnet.load_from_ckpt()

    Total_TP, Total_FP, Total_TN, Total_FN = 0, 0, 0, 0
    with open("../verified_videos.txt") as fin, open('predict_results.csv', 'w') as fout, open('predict_results_detail.json', 'w') as fout2:
        sql = 'select hash, title from videos where hash = ?'

        result_detail = {}
        for line in fin.readlines():
            video_hash = line.strip()
            video_hash, video_title = db.queryone(sql, video_hash)

            print(video_title, video_hash)

            video = video_title.strip() + '_' + video_hash
            
            result_detail[video] = {}
            with open(os.path.join(images_dir, video, "predict.json")) as fin2:
                predict_info = json.load(fin2)

            with open(os.path.join(images_dir, video, "frames.txt")) as fin2:
                frames = fin2.readlines()[0].split()
                
                TP, FP, TN, FN = 0, 0, 0, 0
                for frame in frames:
                    img_path = os.path.join(images_dir, video, frame+'.png')
                    frame_img = cv2.imread(img_path)
                    frame_img = cv2.cvtColor(frame_img, cv2.COLOR_BGR2RGB)

                    results = tfnet.return_predict(frame_img)
                    # print results
                    
                    if str(frame) not in predict_info:
                        continue

                    result_detail[video][str(frame)] = results
                    hasCode = False
                    for r in results:
                        if r['label'] == 'Code':
                            # print frame, predict_info[str(frame)]['label'], 'Code Region:', r['topleft'], r['bottomright']
                            hasCode = True
                            if predict_info[str(frame)]['label'] == "valid":
                                TP += 1
                            else:
                                FP += 1
            
                    
                    if not hasCode:
                        # print frame, predict_info[str(frame)]['label'], ' No Code Region'
                        if predict_info[str(frame)]['label'] == "invalid":
                            TN += 1
                        else:
                            FN += 1
                
                print(TP, FP, TN, FN)
                precison1 = TP*1.0/(TP+FP) if TP+FP != 0 else 0
                recall1 = TP*1.0/(TP+FN) if TP+FN != 0 else 0
                precison2 = TN*1.0/(TN+FN) if TN+FN != 0 else 0
                recall2 = TN*1.0/(TN+FP) if TN+FP != 0 else 0
                fout.write('%s,%s,%d,%d,%d,%d,%.2f,%.2f,%.2f,%.2f,%.2f\n' % (video_title, video_hash, TP, FP, TN, FN, (TP+TN)*1.0/(TP+TN+FP+FN), precison1, recall1, precison2, recall2))
                Total_TP += TP
                Total_FP += FP
                Total_TN += TN
                Total_FN += FN

        json.dump(result_detail, fout2, indent=4, cls=PythonObjectEncoder)        

    print(Total_TP, Total_FP, Total_TN, Total_FN, (Total_TP + Total_TN)*1.0/(Total_TP+Total_FP+Total_TN+Total_FN))
Esempio n. 12
0
from darkflow.net.build import TFNet

options = {
    'model': 'cfg/tiny-yolo-id-card.cfg',
    'load': -1,
    'batch': 8,
    'epoch': 1,
    'gpu': 1.0,
    'train': True,
    'annotation': 'id-card-model/annotation',
    'dataset': 'id-card-model/image'
}

tfnet = TFNet(options)
tfnet.load_from_ckpt()  #load latest checkpointing loads...
tfnet.train()