コード例 #1
0
    def __init__(self, cam_num, parent=None):
        self.cam_num = cam_num
        #載入資料庫的資料
        self.reload()

        QtWidgets.QMainWindow.__init__(self)
        #self.setWindowTitle("ggez")
        self.ui = uic.loadUi("webcamV2.ui", self)
        #self.ui.setFixedSize(self.size())
        self.ui.tabWidget.setTabText(0, "Main")
        self.ui.tabWidget.setTabText(1, "Search")
        self.ui.tabWidget.setTabText(2, "Setting")

        self.ui.Open_Button.clicked.connect(self.open_detect)
        self.ui.pushButton.clicked.connect(self.save_image)
        self.ui.Stop_Button.clicked.connect(self.stop)
        self.ui.lineEdit.returnPressed.connect(self.save_image)

        self.ui.Send_Button.clicked.connect(self.search_event)
        self.ui.lineEdit_2.returnPressed.connect(self.search_event)

        #由資料庫抓取資料
        self.ui.reload_image.clicked.connect(self.reload)

        self.ui.Recognition_checkbox.clicked.connect(
            self.Recognition_check_event)

        self.Recognition_checkbox.setStyleSheet(
            "QPushButton{background:black;border-radius:20;}")

        self.widget.setStyleSheet(
            "QWidget{border-width:2px;}"
            "QWidget{border-color:black;}"
            "QWidget{border-style:outset;}"
            "QWidget{height:100;}"
            "QWidget{border-radius:5px;}"
            "QWidget{background-color:qlineargradient(x1 : 0, y1 : 0, x2 : 0, y2 : 1, stop :  0.0 #f5f9ff,stop :   0.5 #c7dfff,stop :   0.55 #afd2ff,stop :   1.0 #c0dbff);}"
        )
        self.ui.label_4.setStyleSheet("QLabel{font: bold 20px;}")
        self.ui.setting_checkBox.setStyleSheet('''QCheckBox{font: bold 35px;}
                                                  QCheckBox::indicator{width: 35px;height: 35px}'''
                                               )
        self.ui.close_button.clicked.connect(self.close_camera)
        self.ui.tabWidget.setTabIcon(0, QtGui.QIcon("home.png"))
        self.ui.tabWidget.setIconSize(QtCore.QSize(30, 30))
        self.ui.tabWidget.setTabIcon(1, QtGui.QIcon("search.png"))
        self.ui.tabWidget.setIconSize(QtCore.QSize(30, 30))
        self.ui.tabWidget.setTabIcon(2, QtGui.QIcon("setting.png"))
        self.ui.tabWidget.setIconSize(QtCore.QSize(30, 30))
        self.setWindowIcon(QtGui.QIcon("window_icon.png"))

        tab_shape = QtWidgets.QTabWidget.Triangular
        self.tabWidget.setTabShape(tab_shape)
        self.check = 0
        self.i = 0
        self.in_or_out = 0

        self.Recognition_check = False
        self.label2 = QLabel(self)
        self.label1 = QLabel(self)

        self.label1.setStyleSheet(
            "QLabel{background:0;}"
            "QLabel{color:rgb(300,300,300,120);font-size:30px;font-weight:bold;font-family:宋体;}"
        )
        # 動態顯示時間在label上
        timer = QTimer(self)
        timer.timeout.connect(self.showtime)
        timer.start(10)
        self.label1.setAlignment(QtCore.Qt.AlignCenter)
        self.label2.setGeometry(self.label.width() + 20, 150, 45, 45)
        #self.label1.resize(self.width(),50)

        # In[]
        #----------------口罩辨識初始化-----------------
        self.frame_queue = Queue()
        self.darknet_image_queue = Queue(maxsize=1)
        self.detections_queue = Queue(maxsize=1)
        self.fps_queue = Queue(maxsize=1)
        #宣告一個放照片的Queue
        self.YOLO_image_queue = Queue(maxsize=1)
        #宣告一個放人臉的Queue
        self.YOLO_face_queue = Queue(maxsize=1)

        self.face = 0

        self.recorded_people = []
        self.clock = 1

        self.args = parser(cam_num)

        check_arguments_errors(self.args)
        self.network, self.class_names, self.class_colors = darknet.load_network(
            self.args.config_file,
            self.args.data_file,
            self.args.weights,
            batch_size=1)

        # Darknet doesn't accept numpy images.
        # Create one with image we reuse for each detect
        self.d_width = darknet.network_width(self.network)
        self.d_height = darknet.network_height(self.network)
        self.darknet_image = darknet.make_image(self.d_width, self.d_height, 3)
        # In[] 雜七雜八flag
        #紀錄yolo辨識所花時間
        self.yolo_t = 0
        #每五分鐘reload記錄一次的flag
        self.r = 0
        #辨識成功視窗flag
        self.show_dialog2 = 0
        #辨識失敗視窗flag
        self.show_dialog3 = 0
        #紀錄辨識失敗次數
        self.dialog3_counter = 0
        #記錄口罩配戴狀況
        self.label_flag = ''
        #進入setting畫面時的flag
        self.first_time_enter_setting = 0
        #拍照時用到的flag 要求good跟none各一張
        self.good_setting = 0
        self.none_setting = 0

        self.show()
コード例 #2
0
frame_width = int(960)
frame_height = int(960)
# cap.set(cv2.CAP_PROP_FRAME_WIDTH, frame_width)
# cap.set(cv2.CAP_PROP_FRAME_HEIGHT, frame_height)
# # cv2.namedWindow('inference', cv2.WINDOW_FREERATIO)
# # cv2.resizeWindow('inference', frame_width, frame_height)

# cap.set(cv2.CAP_PROP_BRIGHTNESS, 0)
# cap.set(cv2.CAP_PROP_FOURCC, MJPG_CODEC)
# #cap.set(cv2.CAP_PROP_AUTOFOCUS, cap_AUTOFOCUS)
# cap.set(cv2.CAP_PROP_FOCUS, cap_FOCUS)
##############################################################################################
width = darknet.network_width(network)
height = darknet.network_height(network)
darknet_image = darknet.make_image(width, height, 3)

frame = cv2.imread(image_path)
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame_resized = cv2.resize(frame_rgb, (width, height),
                           interpolation=cv2.INTER_LINEAR)
darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())

detections = darknet.detect_image(network,
                                  class_names,
                                  darknet_image,
                                  thresh=thresh_hold,
                                  hier_thresh=.5,
                                  nms=.45)

voc = []
コード例 #3
0
def YOLO():

    configPath = "./yolov3_best.cfg"
    weightPath = "./yolov3_best_final.weights"
    metaPath = "./cfg/maskYolo.data"
    netMain = darknet.load_net_custom(configPath.encode(
        "ascii"), weightPath.encode("ascii"), 0, 1)  # batch size = 1

    metaMain = darknet.load_meta(metaPath.encode("ascii"))
    try:
        with open(metaPath) as metaFH:
            metaContents = metaFH.read()
            import re
            match = re.search("names *= *(.*)$", metaContents,
                              re.IGNORECASE | re.MULTILINE)
            if match:
                result = match.group(1)
            else:
                result = None
            try:
                if os.path.exists(result):
                    with open(result) as namesFH:
                        namesList = namesFH.read().strip().split("\n")
                        altNames = [x.strip() for x in namesList]
            except TypeError:
                pass
    except Exception:
        pass
	
    model = masknet.create_model()
    model.summary()
    model.load_weights("yolov3mask_28.hdf5")
    cap = cv2.VideoCapture("test_video/4.mov")
    print("Starting the YOLO")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                    darknet.network_height(netMain), 3)
    n = 0
    while True:
        ret, frame_read = cap.read()
        n = n + 1
        if frame_read is None:
            print('\nEnd of Video')
            break
        prev_time = time.time()
        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(frame_rgb,
                                   (darknet.network_width(netMain),
                                    darknet.network_height(netMain)),
                                   interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())

        detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.5)
        tensor_11 = darknet.get_tensor(netMain, 11)
        tensor_36 = darknet.get_tensor(netMain, 36)
        tensor_61 = darknet.get_tensor(netMain, 61)
        tensor_74 = darknet.get_tensor(netMain, 74)
        tensor_11 = np.ctypeslib.as_array(tensor_11, [1, 128, 80, 80]).transpose(0, 2, 3, 1)  # B,H,W,C
        tensor_36 = np.ctypeslib.as_array(tensor_36, [1, 256, 40, 40]).transpose(0, 2, 3, 1)
        tensor_61 = np.ctypeslib.as_array(tensor_61, [1, 512, 20, 20]).transpose(0, 2, 3, 1)
        tensor_74 = np.ctypeslib.as_array(tensor_74, [1, 1024, 10, 10]).transpose(0, 2, 3, 1)
        for im, single_out in zip([frame_rgb], detections):
            image, rois, true_rois = cvDrawBoxes(detections, im, darknet.network_width(netMain), darknet.network_height(netMain))
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            masknet_inp = rois[np.newaxis, ...]
            masks = model.predict([tensor_11, tensor_36, tensor_61, tensor_74, masknet_inp])
            masks = masks[0, :true_rois, :, :, 0]

            for i in range(true_rois):
                roi = rois[i]
                mask = masks[i]

                y1, x1, y2, x2 = roi
                if y1 > 1:
                    y1 = 1
                if y1 < 0:
                    y1 = 0
                if y2 > 1:
                    y2 = 1
                if y2 < 0:
                    y2 = 0
                if x1 > 1:
                    x1 = 1
                if x1 < 0:
                    x1 = 0
                if x2 > 1:
                    x2 = 1
                if x2 < 0:
                    x2 = 0

                left = int(x1 * 1920)
                top = int(y1 * 1080)
                right = int(x2 * 1920)
                bot = int(y2 * 1080)

                mask = imresize(mask, (bot - top, right - left), interp='bilinear').astype(np.float32) / 255.0
                mask2 = np.where(mask >= 0.5, 1, 0).astype(np.uint8)

                if (i % 3) == 0:
                    mask3 = cv2.merge((mask2 * 0, mask2 * 0, mask2 * 255))
                elif (i % 3) == 1:
                    mask3 = cv2.merge((mask2 * 0, mask2 * 255, mask2 * 0))
                else:
                    mask3 = cv2.merge((mask2 * 255, mask2 * 0, mask2 * 0))
                try:
                    image[top:bot, left:right] = cv2.addWeighted(image[top:bot, left:right], 1.0, mask3, 0.8, 0)
                except:
                    print(x1, x2, y1, y2)

            print("FPS:", round(1/(time.time()-prev_time), 2))
            cv2.namedWindow('Demo', 0)
            cv2.resizeWindow('Demo', (1536, 864))
            cv2.imshow('Demo', image)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
コード例 #4
0
def YOLO(args):

    def processFrame(frameToProcess, args, netMain):
        print('processFrame was called')
        darknet_image = darknet.make_image(1920, 1080, 3)
        # frame = cv2.cvtColor(
        #     frameToProcess, cv2.COLOR_BGR2RGB)  # convert to rgb
        if(args.resize):
            frame = cv2.resize(frame, (darknet.network_width(netMain), darknet.network_height(
                netMain)), interpolation=cv2.INTER_LINEAR)  # resize the image to neural network dimensions using interpolation
        darknet.copy_image_from_bytes(
            darknet_image, frameToProcess.tobytes())
        # profile[1] = profile[1] + (_time.time() - tempPrev)
        # tempPrev = _time.time()
        detections = darknet.detect_image(
            netMain, metaMain, darknet_image, thresh=args.confidence, nms=args.nms_thresh, debug=False)
        # profile[2] = profile[2] + (_time.time() - tempPrev)
        # tempPrev = _time.time()
        # draw bounding boxes on the processed frame
        markedImage = cvDrawBoxes(detections, frameToProcess)
        # profile[3] = profile[3] + (_time.time() - tempPrev)
        # convert colorspace back to rgb from opencv native
        out.write(markedImage)  # cv2.cvtColor(markedImage, cv2.COLOR_BGR2RGB)

    global metaMain, netMain, altNames
    configPath = args.cfg
    weightPath = args.weights
    metaPath = args.data

    if DEBUG_PRINT:
        print('+*Using:\n\tCFG: ' + configPath + '\n\tWeights: ' +
              weightPath + '\n\tMetadata: ' + metaPath)

    if not os.path.exists(configPath):
        raise ValueError('Invalid config path `' +
                         os.path.abspath(configPath)+'`')
    if not os.path.exists(weightPath):
        raise ValueError('Invalid weight path `' +
                         os.path.abspath(weightPath)+'`')
    if not os.path.exists(metaPath):
        raise ValueError('Invalid data file path `' +
                         os.path.abspath(metaPath)+'`')
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode(
            'ascii'), weightPath.encode('ascii'), 0, args.bs)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode('ascii'))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search('names *= *(.*)$', metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split('\n')
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass

    fileType = checkType(args.source)
    if not fileType == 2:
        inputType = filetype.guess(args.source)  # file type of the input image
        output = args.output
        if output == '___':
            toStrip = len(str(inputType.extension)) + 1
            output = args.source[:-toStrip] + \
                '_proc.' + str(inputType.extension)

    if fileType == -1:
        print('Input is not a supported image or video format. Try running the python script with: --help for more information')
        sys.exit()

    if fileType == 0:  # input is a video
        if DEBUG_PRINT:
            print('Validated: Source input is a video.')
        cap = cv2.VideoCapture(args.source)  # set to 0 to use webcam input
        cap.set(3, 1920)
        cap.set(4, 1080)
        num_frames = cap.get(7)
        print('Starting the YOLO loop...')

        #__________________MULTIPROCESSING___________________#
        threadn = cv2.getNumberOfCPUs()
        print(str(threadn))
        pool = ThreadPool(processes=threadn)
        pending = deque()

        threaded_mode = True

        currFrame = 0
        # darknet_image = darknet.make_image(1920, 1080, 3)
        out = cv2.VideoWriter(
            output, cv2.VideoWriter_fourcc(*'MJPG'), args.fps, (1920, 1080))
        while True:
            prev_time = _time.time()
            ret, frame_read = cap.read()
            # _________
            while len(pending) > 0 and pending[0].ready():
                res, t0 = pending.popleft().get()
            if len(pending) < threadn:
                ret, frame = cap.read()
                t = clock()
                task = pool.apply_async(
                    processFrame, (frame_read, args, netMain))
                pending.append(task)
            ch = cv2.waitKey(1)
            if ch == 27:
                break
            print('Done')
            # _________
            # if currFrame == 0:
            #height, width, channels = frame_read.shape
            # create an image we reuse for each detect

            # if cv2.waitKey(1) & 0xFF == ord('q'):  # press q to quit
            #     break
        #     if(ret):
        #         profile[0] = profile[0] + (_time.time() - prev_time)
        #         tempPrev = _time.time()
        #         currFrame += 1
        #         processedFrame = processFrame(
        #             frame_read, args, darknet_image, netMain, tempPrev)
        #         tempPrev = _time.time()
        #         # add processed frame to the output file
        #         #out.write(processedFrame)
        #         profile[4] = profile[4] + (_time.time() - tempPrev)
        #         print('fps: ' + str(int(1/(_time.time()-prev_time))) +
        #               ' frames processed: ' + str(currFrame) + '/' + str(num_frames), end='\r')
        #         sys.stdout.flush()
        #         if(args.show):
        #             cv2.imshow('Demo', processedFrame)
        #         if(currFrame == num_frames):
        #             print('Successfully finished and exported to: ' + output)
        #             break
        # cap.release()
        out.release()
        # index = 0
        # for time in profile:
        #     profile[index] = time/num_frames
        #     index += 1
        # print(profile)

    if fileType == 1:  # input is an image
        if DEBUG_PRINT:
            print('Validated: Source input is an image.')
        frame_read = cv2.imread(args.source)
        print('Starting the YOLO loop...')
        height, width, channels = frame_read.shape
        # create an image we reuse for each detect
        darknet_image = darknet.make_image(width, height, channels)
        processedFrame = processFrame(
            frame_read, args, darknet_image, netMain)
        if(args.show):
            cv2.imshow('Demo', processedFrame)
            cv2.waitKey(args.displayLength)
        cv2.imwrite(output, processedFrame)
        print('Successfully finished and exported to: ' + output)

    if filetype == 2:  # input is a directory
        if DEBUG_PRINT:
            print('Validated: Source input is a directory.')
        image_path_list = []
        print('Populating list with all files in directory...')
        for file in os.listdir(args.source):
            image_path_list.append(os.path.join(args.source, file))
        print('Starting the YOLO loop...')
        # loop through image_path_list to open each image
        for imagePath in image_path_list:
            # determine output path for processed frame
            inputType = filetype.guess(imagePath)
            toStrip = len(str(inputType.extension)) + 1
            output = imagePath[:-toStrip] + '_proc.' + str(inputType.extension)

            frame_read = cv2.imread(args.source)
            height, width, channels = frame_read.shape
            # create an image we reuse for each detect
            darknet_image = darknet.make_image(width, height, channels)
            processedFrame = processFrame(
                frame_read, args, darknet_image, netMain)
            if(args.show):
                cv2.imshow('Demo', processedFrame)
                cv2.waitKey(args.displayLength)
            cv2.imwrite(output, processedFrame)
            print('Successfully finished frame and exported to: ' +
                  output + '\nMoving on to next frame...', end='\r')
コード例 #5
0
ファイル: compute_focus-40x.py プロジェクト: dwaithe/amca
channel = 1






dataset_path ="/media/nvidia/Dominic/prescan/"
out_File_folder =  '/media/nvidia/Dominic/scanned/'
out_File_folder2 = '/media/nvidia/Dominic/processed/'
files_in_dir = [f for f in listdir(dataset_path) if isfile(join(dataset_path, f))]

output_wid = dk.network_width(netMain)
output_hei = dk.network_height(netMain)
darknet_image = dk.make_image(output_wid,output_hei,3)

f = open(out_File_folder2+'POS_FILE.txt','w')

for file in files_in_dir:
	imageFile = ".".join(file.split(".")[:-1])
	file_ext = file.split(".")[-1]
	if file != ".DS_Store" and "."+file_ext == ext:	
		#imageFile = lfile.split(".")[0]
		out_roiFile = imageFile+'all.svg'
        

		tfile = tifffile.TiffFile(dataset_path+imageFile+ext)


		metadata = tfile.imagej_metadata
コード例 #6
0
def YOLO(vid_input, vid_output):
    '''
    vid_input: Directory of video input. Example: vid_input = "input/001.wmv".
    vid_output: Directory of video output. Example: vid_output = "output/001.avi".
    '''
    global metaMain, netMain, altNames
    configPath = "./cfg/yolov3.cfg"
    weightPath = "./yolov3.weights"
    metaPath = "./cfg/coco.data"
    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass
    #cap = cv2.VideoCapture(0)
    cap = cv2.VideoCapture(vid_input)
    cap.set(3, 1280)
    cap.set(4, 720)
    out = cv2.VideoWriter(vid_output, cv2.VideoWriter_fourcc(*"MJPG"), 24.0,
                          (640, 480))
    print("Starting the YOLO loop...")

    # Create an image we reuse for each detect
    # Video default size (640x480)
    darknet_image = darknet.make_image(640, 480, 3)

    #Inicializar o Sort
    mot_tracker = Sort()
    while True:

        prev_time = time.time()
        ret, frame_read = cap.read()
        if frame_read is None:
            break
        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(frame_rgb, (640, 480),
                                   interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())

        detections = darknet.detect_image(netMain,
                                          metaMain,
                                          darknet_image,
                                          thresh=0.30)
        #Sort Track update
        track_bbs_ids = mot_tracker.update(detections)

        image = cvDrawBoxes(track_bbs_ids, frame_resized)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        #print(1/(time.time()-prev_time))
        cv2.imshow('Demo', image)
        cv2.waitKey(3)
        out.write(image)
    cap.release()
    out.release()
コード例 #7
0
ファイル: detect.py プロジェクト: samlee64/gulp-step
def YOLO():
    global metaMain, netMain, altNames

    try:
        weightPath = sys.argv[1]
    except:
        print("Need to provide weights file")
        sys.exit()
    configPath = "moon-jellyfish.cfg"
    metaPath = "moon-jellyfish.data"

    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))

    results_file = open("results.txt", "w")

    capture = cv2.VideoCapture("./data/videos/moon-jellyfish-capture4.mkv")

    frame_width = int(capture.get(3))
    frame_height = int(capture.get(4))
    #    new_height, new_width = frame_height // 2, frame_width // 2
    new_height, new_width = frame_height, frame_width
    print("Video Resolution: ", (frame_width, frame_height))
    print("new widths, heights", new_width, new_height)

    (weightName, ext) = os.path.splitext(weightPath)
    outputPath = os.path.join("output", weightName)
    print("outputPath", outputPath)

    if not os.path.exists(outputPath):
        os.makedirs(outputPath)

    output = cv2.VideoWriter(outputPath + "/sorted-output.avi",
                             cv2.VideoWriter_fourcc(*"MJPG"), 10.0,
                             (new_width, new_height))

    darknet_image = darknet.make_image(new_width, new_height, 3)

    mot_tracker = Sort(max_age=1, min_hits=0)

    while True:
        prev_time = time.time()
        ret, frame_read = capture.read()

        if not ret:
            break

        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(frame_rgb, (new_width, new_height),
                                   interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())

        detections = darknet.detect_image(netMain,
                                          metaMain,
                                          darknet_image,
                                          thresh=0.25)

        trackers = update_tracker(mot_tracker, detections)
        draw_tracked_bb(trackers, frame_resized)

        write_trackers(results_file, trackers)

        image = draw_bounding_boxes(detections, frame_resized)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        cv2.imshow('Movie', image)
        cv2.waitKey(3)
        output.write(image)

    capture.release()
    output.release()
    results_file.close()
    print("Done")
def YOLO(roipt1, roipt2, progressObj, filename):
    global metaMain, netMain, altNames, metaMain1, netMain1, altNames1, car_cnt, truck_cnt, motorbike_cnt, bus_cnt, time_last, last_count, count_inc, cur_frame, time_interval, memory, counter, twoaxel, threeaxel, fouraxel, fiveaxel, sixaxel
    configPath = "./cfg/yolov4.cfg"
    weightPath = "./yolov4.weights"
    metaPath = "./cfg/coco.data"

    configPath1 = "./cfg/multi_classify.cfg"
    weightPath1 = "./weights/multiclassify_4000.weights"
    metaPath1 = "./data/multi_classify.data"

    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass

    if not os.path.exists(configPath1):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath1) + "`")
    if not os.path.exists(weightPath1):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath1) + "`")
    if not os.path.exists(metaPath1):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath1) + "`")
    if netMain1 is None:
        netMain1 = darknet.load_net_custom(configPath1.encode("ascii"),
                                           weightPath1.encode("ascii"), 0,
                                           1)  # batch size = 1
    if metaMain1 is None:
        metaMain1 = darknet.load_meta(metaPath1.encode("ascii"))
    if altNames1 is None:
        try:
            with open(metaPath1) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames1 = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass

    cap = cv2.VideoCapture(filename)
    print(cap.get(cv2.CAP_PROP_FPS))
    tot_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    cap.set(3, 1280)
    cap.set(4, 720)
    out = cv2.VideoWriter(
        "axelCount_output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0,
        (darknet.network_width(netMain), darknet.network_height(netMain)))
    print("Starting the YOLO loop...")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                       darknet.network_height(netMain), 3)

    darknet_image1 = darknet.make_image(darknet.network_width(netMain),
                                        darknet.network_height(netMain), 3)

    while True:
        print(cur_frame * 100 / tot_frames)
        progressObj.setProperty("value", cur_frame * 100 / tot_frames)
        prev_time = time.time()
        ret, frame_read = cap.read()
        cur_frame += 1
        if ret:
            ROI = np.copy(frame_read)
            ROI[:, :, :] = 0
            reqdRegion = np.copy(frame_read)
            reqdRegion[:, :, :] = 0
            roipi1x = roipt1[0]
            roipt1y = roipt1[1]
            roipt2x = roipt2[0]
            roipt2y = roipt2[1]
            #region = frame_read[240:790 , 400:1400]
            #ROI[240:790 , 400:1400] = region
            #cv2.rectangle(frame_read , (400 , 240) , (1400 , 790) , (173 , 50, 200) , 2)
            region = frame_read[roipt1y:roipt2y, roipi1x:roipt2x]
            ROI[roipt1y:roipt2y, roipi1x:roipt2x] = region
            cv2.rectangle(frame_read, roipt1, roipt2, (173, 50, 200), 2)
            frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)

            frame_resized = cv2.resize(frame_rgb,
                                       (darknet.network_width(netMain),
                                        darknet.network_height(netMain)),
                                       interpolation=cv2.INTER_LINEAR)
            ROI_resized = cv2.resize(ROI, (darknet.network_width(netMain),
                                           darknet.network_height(netMain)),
                                     interpolation=cv2.INTER_LINEAR)
            reqdRegion_resized = cv2.resize(reqdRegion,
                                            (darknet.network_width(netMain),
                                             darknet.network_height(netMain)),
                                            interpolation=cv2.INTER_LINEAR)

            darknet.copy_image_from_bytes(darknet_image, ROI_resized.tobytes())

            detections = darknet.detect_image(netMain,
                                              metaMain,
                                              darknet_image,
                                              thresh=0.25)
            image = cvDrawBoxes(detections, frame_resized)
            image_clean = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

            # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
            dets = []
            full_dets = []
            if len(detections) > 0:
                # loop over the indexes we are keeping
                for i in range(0, len(detections)):
                    if detections[i][0].decode() != 'person':
                        #print(len(detections))
                        (x, y) = (detections[i][2][0], detections[i][2][1])
                        (w, h) = (detections[i][2][2], detections[i][2][3])
                        dets.append([
                            float(x - w / 2),
                            float(y - h / 2),
                            float(x + w / 2),
                            float(y + h / 2),
                            float(detections[i][1])
                        ])
                        full_dets.append([
                            int((float(x - w / 2) + float(x + w / 2)) / 2),
                            int((float(y - h / 2) + float(y + h / 2)) / 2),
                            detections[i][0].decode()
                        ])
                        #print(np.shape(dets))
                        #print(full_dets)

            print(full_dets)
            print('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
            dets = np.asarray(dets)
            print(dets)
            print("##################################")
            tracks = tracker.update(dets)
            #print(tracks)

            boxes = []
            indexIDs = []
            c = []
            previous = memory.copy()
            memory = {}

            for track in tracks:
                # As boxes co-ordinates and indexes are appended one by one we are storing them
                # in a dictionary
                #print(track)
                for i, det_cent in enumerate(full_dets):
                    euclidean_distance = math.sqrt(
                        (det_cent[0] - (track[0] + track[2]) / 2)**2 +
                        (det_cent[1] - (track[1] + track[3]) / 2)**2)
                    if euclidean_distance <= 4:
                        boxes.append([
                            track[0], track[1], track[2], track[3], det_cent[2]
                        ])
                        indexIDs.append(int(track[4]))
                        memory[indexIDs[-1]] = boxes[-1]
                        break

            if len(boxes) > 0:
                i = int(0)
                for box in boxes:
                    # extract the bounding box coordinates
                    (x, y) = (int(box[0]), int(box[1]))
                    (w, h) = (int(box[2]), int(box[3]))
                    cv2.rectangle(image, (int(x), int(y)), (int(w), int(h)),
                                  (255, 0, 0), 2)

                    if indexIDs[i] in previous:
                        previous_box = previous[indexIDs[i]]
                        (x2, y2) = (int(previous_box[0]), int(previous_box[1]))
                        (w2, h2) = (int(previous_box[2]), int(previous_box[3]))
                        # p0 = prevCentroid
                        # p1 = current centroid
                        # condition for counter to increment is if line between prev centroid and current centroid intersect then increment counter
                        p0 = (int(x + (w - x) / 2), int(y + (h - y) / 2))
                        p1 = (int(x2 + (w2 - x2) / 2), int(y2 + (h2 - y2) / 2))
                        cv2.putText(image, str(indexIDs[i]), p0,
                                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0),
                                    2)
                        cv2.line(image, p0, p1, (0, 255, 0), 2)

                        countwheel = 0
                        # If diagonal intersects with line then increment counter
                        if intersect(p0, p1, (340, 366), (340, 138)):
                            print('entered')

                            if box[4] == 'car':
                                car_cnt += 1
                            if box[4] == 'truck':
                                print("Counting axels...........")
                                region = image_clean[y - 20:h + 20,
                                                     x - 20:w + 20]
                                reqdRegion_resized[y - 20:h + 20,
                                                   x - 20:w + 20] = region
                                reqdRegion_resized = cv2.cvtColor(
                                    reqdRegion_resized, cv2.COLOR_RGB2BGR)
                                darknet.copy_image_from_bytes(
                                    darknet_image1,
                                    reqdRegion_resized.tobytes())
                                detections = darknet.detect_image(
                                    netMain1,
                                    metaMain1,
                                    darknet_image1,
                                    thresh=0.25)
                                reqdRegion_resized = cv2.cvtColor(
                                    reqdRegion_resized, cv2.COLOR_BGR2RGB)
                                reqdRegion_resized = cvDrawBoxes(
                                    detections, reqdRegion_resized)
                                if len(detections) > 0:
                                    for j in range(0, len(detections)):
                                        if detections[j][0].decode(
                                        ) == 'wheel':
                                            countwheel += 1
                                print('No of wheeles are: ', countwheel)
                                #cv2.imshow("img" , reqdRegion_resized)
                                #cv2.waitKey(0)
                                print("Done counting..........")
                                truck_cnt += 1
                                if countwheel == 2:
                                    twoaxel += 1
                                if countwheel == 3:
                                    threeaxel += 1
                                if countwheel == 4:
                                    fouraxel += 1
                                if countwheel == 5:
                                    fiveaxel += 1
                                if countwheel == 6:
                                    sixaxel += 1
                            if box[4] == 'motorbike':
                                motorbike_cnt += 1
                            if box[4] == 'bus':
                                bus_cnt += 1
                            counter += 1
                    i += 1
            cv2.putText(image, "Total Count = {}".format(counter), (10, 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, [0, 255, 0], 2)
            cv2.putText(image, "Car Count = {}".format(car_cnt), (10, 30),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, [0, 255, 0], 2)
            cv2.putText(image, "Truck Count = {}".format(truck_cnt), (10, 50),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, [0, 255, 0], 2)
            cv2.putText(image, "2-Axel Truck Count = {}".format(twoaxel),
                        (20, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.4, [0, 255, 0],
                        2)
            cv2.putText(image, "3-Axel Truck Count = {}".format(threeaxel),
                        (20, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.4, [0, 255, 0],
                        2)
            cv2.putText(image, "4-Axel Truck Count = {}".format(fouraxel),
                        (20, 110), cv2.FONT_HERSHEY_SIMPLEX, 0.4, [0, 255, 0],
                        2)
            cv2.putText(image, "5-Axel Truck Count = {}".format(fiveaxel),
                        (20, 130), cv2.FONT_HERSHEY_SIMPLEX, 0.4, [0, 255, 0],
                        2)
            cv2.putText(image, "6-Axel Truck Count = {}".format(sixaxel),
                        (20, 150), cv2.FONT_HERSHEY_SIMPLEX, 0.4, [0, 255, 0],
                        2)
            cv2.putText(image, "Motorbike Count = {}".format(motorbike_cnt),
                        (10, 170), cv2.FONT_HERSHEY_SIMPLEX, 0.5, [0, 255, 0],
                        2)
            cv2.putText(image, "Bus Count = {}".format(bus_cnt), (10, 190),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, [0, 255, 0], 2)
            # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
            cv2.line(image, (340, 366), (340, 138), (255, 0, 0), 2)
            #print(1/(time.time()-prev_time))
            out.write(image)
            #cv2.imshow('original' , frame_read)
            cv2.imshow('Demo', image)
            #cv2.imshow('ROI' , ROI)
            k = cv2.waitKey(3)
            if k & 0xFF == ord('q'):
                break
        else:
            print("DONE")
            break
    cap.release()
    out.release()
コード例 #9
0
def YOLO():

    global metaMain, netMain, altNames
    configPath = argv[1] 
    weightPath = argv[2]
    metaPath = argv[3]
    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath)+"`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath)+"`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath)+"`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode(
            "ascii"), weightPath.encode("ascii"), 0, 1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass
    #cap = cv2.VideoCapture(0)
    cap = cv2.VideoCapture(argv[4])
    num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    fps = int(cap.get(cv2.CAP_PROP_FPS))
    out = cv2.VideoWriter(
        "output_123.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps,
        (darknet.network_width(netMain), darknet.network_height(netMain)))
    print("Starting the YOLO loop...")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                    darknet.network_height(netMain),3)
    curr_frame = 0
    start_time = time.time()
    while True:
        prev_time = time.time()
        ret, frame_read = cap.read()
        if not ret:
            break
        curr_frame = 1
        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(frame_rgb,
                                   (darknet.network_width(netMain),
                                    darknet.network_height(netMain)),
                                   interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(darknet_image,frame_resized.tobytes())

        detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25)
        image = cvDrawBoxes(detections, frame_resized)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        print(1/(time.time()-prev_time))
        elapsed = time.time()-start_time
        print(f'{curr_frame*100/num_frames:.2f} % {elapsed:.2f}')
        out.write(image)
    out.release()
コード例 #10
0
def YOLO():

    global metaMain, netMain, altNames
    configPath = "./cfg/yolov3.cfg"
    weightPath = "./yolov3.weights"
    metaPath = "./cfg/coco.data"
    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass
    #cap = cv2.VideoCapture(0)
    # folder_name = parsed_args.input
    _, _, filenames = next(os.walk(folder))
    for image_name in filenames:
        print("file name inside folder: ", image_name)
        # print("file being used: ",image_name) # keeping track of current file being used
        # image_name = parsed_args.input
        ext = image_name.split('.')
        print("variable ext: ", ext)
        # Create an image we reuse for each detect
        darknet_image = darknet.make_image(darknet.network_width(netMain),
                                           darknet.network_height(netMain), 3)
        frame_read = cv2.imread(folder + image_name)
        # frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(
            frame_read,
            (darknet.network_width(netMain), darknet.network_height(netMain)),
            interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())

        detections = darknet.detect_image(netMain,
                                          metaMain,
                                          darknet_image,
                                          thresh=0.25)
        cvDrawBoxes(detections, frame_resized, ext[0])
        darknet_image = None
        frame_read = None
        frame_rgb = None
        frame_resized = None
        detections = None
コード例 #11
0
def YOLO():
    global metaMain, netMain, altNames
    configPath = "./cfg/yolov3-tiny.cfg"
    weightPath = "./cfg/yolov3-tiny.weights"
    metaPath = "./cfg/coco.data"
    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass
    cap = cv2.VideoCapture(0)
    #cap = cv2.VideoCapture("/home/mumin/İndirilenler/VID_20191009_133209.mp4")
    #cap.set(3, 1280)
    #cap.set(4, 720)

    frame_size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
                  int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    #print(frame_size)
    #print(cv2.CAP_PROP_FPS)

    out = cv2.VideoWriter(
        "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0,
        (darknet.network_width(netMain), darknet.network_height(netMain)))

    print("Starting the 'I4U' loop..")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                       darknet.network_height(netMain), 3)

    if cap.isOpened():
        print("Webcam online.")
    else:
        print("Webcam offline.")

    while True:
        prev_time = time.time()
        ret, frame_read = cap.read()

        # gelen frame bos ise while sonlaniyor
        if not ret:
            #if np.shape(frame_read) == ():
            break

        frame_read = cv2.flip(frame_read, 1)

        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(
            frame_rgb,
            (darknet.network_width(netMain), darknet.network_height(netMain)),
            interpolation=cv2.INTER_LINEAR)

        imageToShowingCorped = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2RGB)

        darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())

        detections = darknet.detect_image(netMain,
                                          metaMain,
                                          darknet_image,
                                          thresh=0.25)

        #nparr = np.fromstring(img_str, np.uint8)
        #img1 = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR) # cv2.IMREAD_COLOR in OpenCV 3.1

        image = cvDrawBoxes(detections, frame_resized)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        for detection in detections:
            label = detection[0].decode(
                'UTF-8')  # b'person' sorunun cozmek icin artik person yaziyor
            confidence = detection[1]
            bounds = detection[2]

            if (showCropedImageStatus or saveCropedImageStatus):
                crop_img = darknet.CropPictureByBoundingBox(
                    bounds, imageToShowingCorped)

            if (showCropedImageStatus == True):
                #cv2.imshow(str(label) + " oran: %" + str(confidence), crop_img)
                cv2.imshow(label, crop_img)

            if (saveCropedImageStatus == True):
                darknet.SaveCropedImage(label, crop_img)

        str = "FPS : %0.1f" % float(1 / (time.time() - prev_time))

        cv2.putText(image, str, (0, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                    (0, 255, 0), 2)

        cv2.imshow('Showing I4U Video', image)
        key = cv2.waitKey(1)
        if (key == 27):
            break

    cap.release()
    out.release()
def YOLO():
    global metaMain, netMain, altNames
    configPath = "yolov3-tiny-obj.cfg"
    weightPath = "yolov3-tiny-obj_3000.weights"
    metaPath = "obj.data"

    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass

    # cap = cv2.VideoCapture(0)
    face_cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
    cap = cv2.VideoCapture("MOV_2400.mp4")
    # cap.set(3, 1280)
    # cap.set(4, 720)
    vid_cod = cv2.VideoWriter_fourcc(*'XVID')
    states_file = open("FER_card_detection_labels.txt", "w")
    print("Starting the YOLO loop...")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                       darknet.network_height(netMain), 3)
    states = []

    while True:
        ret, frame_read = cap.read()
        face_ret, face_read = face_cap.read()
        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
        face_frame_rgb = cv2.cvtColor(face_read, cv2.COLOR_BGR2GRAY)

        frame_resized = cv2.resize(
            frame_rgb,
            (darknet.network_width(netMain), darknet.network_height(netMain)),
            interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())
        detections = darknet.detect_image(netMain,
                                          metaMain,
                                          darknet_image,
                                          thresh=0.25)

        label = FER(face_frame_rgb)
        image = Card_classification(detections, frame_resized, card_model)
        result = label + " " + str(image)
        states.append(result)
        if len(states) == 10:
            most_common_state = s.mode(states)[0]
            states_file.write(str(most_common_state) + "\n")
            states.clear()
        else:
            continue

    cap.release()
    face_cap.release()
    cv2.destroyAllWindows()
    states_file.close()
def main():
    # Setup yolo config
    with open("yolo_config.yml", 'r') as ymlfile:
        if sys.version_info[0] > 2:
            cfg = yaml.load(ymlfile, Loader=yaml.FullLoader)
        else:
            cfg = yaml.load(ymlfile)

    yolo_data = cfg['yolo_data']
    yolo_config = cfg['yolo_config']
    yolo_weights = cfg['yolo_weights']
    pistol_net = dn.load_net(yolo_config, yolo_weights, 0)
    pistol_meta = dn.load_meta(yolo_data)

    coco_data = cfg['coco_data']
    coco_config = cfg['coco_config']
    coco_weights = cfg['coco_weights']
    coco_net = dn.load_net(coco_config, coco_weights, 0)
    coco_meta = dn.load_meta(coco_data)

    # # setup openpose config
    # with open("openpose_config.yml", 'r') as opymlfile:
    # 	if sys.version_info[0] > 2:
    # 		op_cfg = yaml.load(opymlfile, Loader=yaml.FullLoader)
    # 	else:
    # 		op_cfg = yaml.load(opymlfile)
    # Custom Params (refer to include/openpose/flags.hpp for more parameters)
    op_params = dict()
    op_params["model_folder"] = cfg['model_folder']
    op_params["model_pose"] = cfg['model_pose']

    # Starting OpenPose
    opWrapper = op.WrapperPython()
    opWrapper.configure(op_params)
    opWrapper.start()
    datum = op.Datum()

    # Generate image list
    image_list = []
    for (dirpath, dirnames, filenames) in walk(cfg['src_folder']):
        image_list.extend(filenames)
        break

    # Load images
    images_processed = 0
    keypoints_generated = 0
    pistols_detected = 0
    tiny_persons = 0
    v3_persons = 0

    if cfg['timer_bool']:
        tiny_time = timer_for_YOLO(cfg['src_folder'], image_list, pistol_net,
                                   pistol_meta)
        v3_time = timer_for_YOLO(cfg['src_folder'], image_list, coco_net,
                                 coco_meta)
        print("YOLO-tiny time over 300 images: {} seconds, avg: {} seconds"
              ).format(tiny_time, tiny_time / 300)
        print("YOLO-v3 time over 300 images: {} seconds, avg: {} seconds"
              ).format(v3_time, v3_time / 300)
        return

    timer_image_list = []
    pistol_index = 0
    for image in image_list:
        current_image = cfg['src_folder'] + image
        # print("current_image: {}").format(current_image)

        cv_image = cv2.imread(current_image,
                              cv2.IMREAD_COLOR)  #load image in cv2
        height = cv_image.shape[0]
        width = cv_image.shape[1]
        # print("original height: {}").format(height)
        # print("original width: {}").format(width)
        box_image = cv_image.copy()
        #resize image to match network dimensions
        skeleton_image = cv2.resize(
            box_image,
            (dn.network_width(pistol_net), dn.network_height(pistol_net)),
            interpolation=cv2.INTER_LINEAR)

        # Detect objects
        frame_resized = cv2.resize(
            box_image,
            (dn.network_width(pistol_net), dn.network_height(pistol_net)),
            interpolation=cv2.INTER_LINEAR)
        threat_resized = cv2.resize(
            box_image,
            (dn.network_width(pistol_net), dn.network_height(pistol_net)),
            interpolation=cv2.INTER_LINEAR)
        darknet_image = dn.make_image(dn.network_width(pistol_net),
                                      dn.network_height(pistol_net), 3)
        dn.copy_image_from_bytes(darknet_image, frame_resized.tobytes())
        pistol_detections = dn.detect_image(pistol_net,
                                            pistol_meta,
                                            darknet_image,
                                            thresh=0.25)
        coco_detections = dn.detect_image(coco_net,
                                          coco_meta,
                                          darknet_image,
                                          thresh=0.25)

        # print("resized height: {}").format(dn.network_height(pistol_net))
        # print("resized width: {}").format(dn.network_width(pistol_net))

        height_scale = float(cv_image.shape[0]) / dn.network_height(pistol_net)
        width_scale = float(cv_image.shape[1]) / dn.network_height(pistol_net)

        # print("height_scale: {}").format(height_scale)
        # print("width_scale: {}").format(width_scale)

        # get bounding boxes
        pistol_boxes = get_bounding_boxes(pistol_detections, (255, 0, 0))
        # coco_boxes = get_bounding_boxes(coco_detections, (0,0,255))
        BBoxes = []
        for box in pistol_boxes:
            BBoxes.append(box)
        # for box in coco_boxes:
        # 	BBoxes.append(box)

        if cfg['save_bbox_images']:
            for box in pistol_boxes:
                if box['class'] == "pistol":
                    crop_pistol = frame_resized[box["ymin"]:box["ymax"],
                                                box["xmin"]:box["xmax"]]
                    crop_img_h = crop_pistol.shape[0]
                    crop_img_w = crop_pistol.shape[1]
                    newX, newY = crop_img_w * width_scale, crop_img_h * height_scale
                    if (newX > 0) and (newY > 0):
                        pistol_bbox_image = cv2.resize(crop_pistol,
                                                       (int(newX), int(newY)))
                        pistol_index += 1
                        # print(("{}pistol_{}.jpg").format(cfg['bbox_output_folder'], pistol_index))
                        cv2.imwrite(("{}pistol_{}.jpg").format(
                            cfg['bbox_output_folder'], pistol_index),
                                    pistol_bbox_image)

        fontFace = cv2.FONT_HERSHEY_DUPLEX
        text_color = (0, 0, 255)
        fontScale = 1
        text_thickness = 1

        box_thickness = 2
        box_color = (255, 0, 0)

        pistols_bool = False
        tiny_people_bool = False
        for box in pistol_boxes:
            # print("pistol_boxes  {}").format(box['class'])
            if box['class'] == "pistol":
                box_color = (255, 0, 0)
                pistols_bool = True
            if box['class'] == "person":
                box_color = (0, 0, 255)
                tiny_people_bool = True
            upper_left = (box['xmin'], box['ymin'])
            lower_right = (box['xmax'], box['ymax'])
            cv2.rectangle(frame_resized, upper_left, lower_right, box_color,
                          box_thickness)
            cv2.putText(frame_resized, box['class'], upper_left, fontFace,
                        fontScale, box_color, text_thickness, cv2.LINE_AA)

        # for box in pistol_boxes:
        # 	if box['class'] == "pistol":
        # 		upper_left = (box['xmin'],box['ymin'])
        # 		lower_right = (box['xmax'],box['ymax'])
        # 		cv2.rectangle(frame_resized,upper_left,lower_right,box_color,box_thickness)
        # 		cv2.putText(frame_resized,box['class'],upper_left, fontFace, fontScale, box_color,text_thickness,cv2.LINE_AA)
        # 		pistols_bool = True

        # 	if box['class'] == "person":
        # 		upper_left = (box['xmin'],box['ymin'])
        # 		lower_right = (box['xmax'],box['ymax'])
        # 		cv2.rectangle(frame_resized,upper_left,lower_right,box_color,box_thickness)
        # 		cv2.putText(frame_resized,box['class'],upper_left, fontFace, fontScale, box_color,text_thickness,cv2.LINE_AA)
        # 		tiny_people_bool = True

        box_color = (0, 0, 255)
        v3_people_bool = False
        # for box in coco_boxes:
        # 	# print("coco_boxes  {}").format(box['class'])
        # 	if box['class'] == "person":
        # 		upper_left = (box['xmin'],box['ymin'])
        # 		lower_right = (box['xmax'],box['ymax'])
        # 		cv2.rectangle(frame_resized,upper_left,lower_right,box_color,box_thickness)
        # 		cv2.putText(frame_resized,box['class'],upper_left, fontFace, fontScale, box_color,text_thickness,cv2.LINE_AA)
        # 		v3_people_bool = True

        if pistols_bool:
            pistols_detected += 1
        if tiny_people_bool:
            tiny_persons += 1
        if v3_people_bool:
            v3_persons += 1

        # cv2.imshow("frame_resized", frame_resized)
        # cv2.waitKey(0)
        # cv2.destroyWindow("frame_resized")

        # check overlapping boxes
        person_boxes = []
        pistol_boxes = []
        potential_threats = []
        for box in BBoxes:
            # print("box[class] = {}").format(box)
            if box['class'] == "person":
                person_boxes.append({
                    'xmin': box['xmin'],
                    'ymin': box['ymin'],
                    'xmax': box['xmax'],
                    'ymax': box['ymax']
                })

            if box['class'] == "pistol":
                pistol_boxes.append({
                    'xmin': box['xmin'],
                    'ymin': box['ymin'],
                    'xmax': box['xmax'],
                    'ymax': box['ymax']
                })

        for person_box in person_boxes:
            for pistol_box in pistol_boxes:
                if box_overlap(person_box, pistol_box):
                    potential_threats.append(person_box)
                    timer_image_list.append(current_image)

                    # print("~~~ Overlappting boxes: {}").format(current_image)

        person_number = 0
        for person_box in potential_threats:
            person_number += 1
            # generate sub images for skeleton
            if (person_box["ymin"] < 0):
                person_box["ymin"] = 0
            if (person_box["xmin"] < 0):
                person_box["xmin"] = 0

            if (person_box["ymax"] > skeleton_image.shape[0]):
                person_box["ymax"] = skeleton_image.shape[0]
            if (person_box["xmax"] > skeleton_image.shape[1]):
                person_box["xmax"] = skeleton_image.shape[0]
            crop_img = skeleton_image[person_box["ymin"]:person_box["ymax"],
                                      person_box["xmin"]:person_box["xmax"]]

            # rescale sub image back to original dimensions
            crop_img_height = crop_img.shape[0]
            crop_img_width = crop_img.shape[1]
            # print("crop_img_width * width_scale : {}*{}={}").format(crop_img_width, width_scale, crop_img_width*width_scale)
            # print("crop_img_height * height_scale : {}*{}={}").format(crop_img_height, height_scale, crop_img_height*height_scale)
            newX, newY = crop_img_width * width_scale, crop_img_height * height_scale
            if (newX > 0) and (newY > 0):
                # process sub image for skeleton
                skel_image = cv2.resize(crop_img, (int(newX), int(newY)))

                datum.cvInputData = skel_image
                opWrapper.emplaceAndPop([datum])

                # Save numpy arrays
                if cfg['save_keypoints']:
                    print("Body keypoints: \n {}").format(datum.poseKeypoints)
                    keypoint_file = cfg[
                        'keypoint_folder'] + "keypoints_{}".format(
                            keypoints_generated)
                    np.save(keypoint_file, datum.poseKeypoints)
                    keypoints_generated += 1

        images_processed += 1
        # if images_processed%100 == 0:
        # print("\n    Images to process remaining in {} : {} \n").format(cfg['src_folder'], len(image_list)-images_processed)

    print("Using weights: {}").format(cfg['yolo_weights'])
    print("Checking folder: {}").format(cfg['src_folder'])
    print("YOLO-tiny pistols detected, correct or incorrect: {}").format(
        pistols_detected)
    print("YOLO-tiny people detected, correct or incorrect: {}").format(
        tiny_persons)
    print("YOLO-v3 people detected, correct or incorrect: {}").format(
        v3_persons)
コード例 #14
0
def YOLO():

    global metaMain, netMain, altNames
    configPath = "yolov3-tiny.cfg"
    weightPath = "420.weights"
    metaPath = "obj.data"
    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass

    cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
    cap.set(cv2.CAP_PROP_SETTINGS, 1)
    cv2.namedWindow('Demo')
    cv2.setMouseCallback('Demo', draw_boundary)
    out = cv2.VideoWriter(
        "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0,
        (darknet.network_width(netMain), darknet.network_height(netMain)))
    print("Starting the YOLO loop...")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                       darknet.network_height(netMain), 3)

    # setup initial boundaries
    ret, frame_read = cap.read()
    frame_resized = cv2.resize(
        frame_read,
        (darknet.network_width(netMain), darknet.network_height(netMain)),
        interpolation=cv2.INTER_LINEAR)
    cv2.imshow('Demo', frame_resized)
    while len(tableBoundariesSet) != 6:
        cv2.waitKey(1)

    state = GameState()

    while True:
        prev_time = time.time()
        ret, frame_read = cap.read()
        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(
            frame_rgb,
            (darknet.network_width(netMain), darknet.network_height(netMain)),
            interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())

        detections = darknet.detect_image(netMain,
                                          metaMain,
                                          darknet_image,
                                          thresh=0.75)
        bestpt1, bestpt2 = detectionHandler(detections, frame_resized)

        ballLocation = getCenterPoint(bestpt1[0], bestpt1[1], bestpt2[0],
                                      bestpt2[1])

        image = cvDrawBoxes(bestpt1, bestpt2, frame_resized)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        if isBallOnTable(ballLocation) and collisionDetection(
                ballLocation, image):
            state.addBounce()
            print("added a bounce")
        # if isBallOnTable(ballLocation):
        # 	addBallLocation(ballLocation, image)

        #get ball side
        tableSide = getTableSide(ballLocation)
        if tableSide != "":
            # if state.side != tableSide:
            # 	#it changed side
            # 	didBounce = colDetection2()
            # 	if didBounce:
            # 		print("it bounced")
            # 	else:
            # 		print("no bounce detected")
            # 	resetLocations()
            state.updateSide(tableSide)

        scoreP1, scoreP2 = state.tick()

        #draw ball side
        tableBoundaryHandler(image, tableBoundariesSet)
        if tableSide == "left":
            cv2.putText(image, tableSide, (200, 200), cv2.FONT_HERSHEY_SIMPLEX,
                        1, (0, 255, 255))
        else:
            cv2.putText(image, tableSide, (375, 200), cv2.FONT_HERSHEY_SIMPLEX,
                        1, (0, 255, 255))

        cv2.putText(image, "P1: " + str(scoreP1), (100, 100),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255))
        cv2.putText(image, "P2: " + str(scoreP2), (300, 100),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255))
        out.write(image)
        cv2.imshow('Demo', image)
        if cv2.waitKey(20) & 0xFF == 27:
            break
        # cv2.waitKey(3)
    cap.release()
    out.release()
コード例 #15
0
def YOLO(is_cam, camera_id, address, space, pro_dist, pro_peo, blurring):
    global metaMain, netMain, altNames, net, meta
    configPath = "./cfg/yolov4-mask.cfg"
    weightPath = "./yolov4-mask_final.weights"  # 나중에 train한 weight 넣기  -  backup에서 가져오면 됨
    metaPath = "./cfg/mask.data"
    tracking = False

    if not os.path.exists(
            configPath
    ):  # Checks whether file exists otherwise return ValueError
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:  # Checks the metaMain, NetMain and altNames. Loads it in script
        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass

    # cctv 카메라 영상, 저장된 영상 중 하나 선택
    if is_cam:
        cap = cam_stream.CamStream(address)
        cap.set_frame_rate(".4")
        cap.start()
    else:
        cap = cv2.VideoCapture(address)

    frame_width = 854  # Returns the width and height of capture video
    frame_height = 480

    print("Starting the YOLO loop...")
    socket = connect_socket.SocketClient("http://115.145.212.100:51122",
                                         camera_id)

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(
        frame_width, frame_height,
        3)  # Create image according darknet for compatibility of network
    cluster_list = []
    old_cluster_list = []

    maintain_info = mt.Maintain()

    #tracking 여부에 따라 선택하기
    if tracking:
        track_c = for_tracking.Track(frame_width, frame_height)
        track_c.start_track()

    try:
        if is_cam:
            prev_time = time.time()
            elapsed = cap.get_frame_rate()
            after_show = False
            image_to_show = None
            while True:  # Load the input frame and write output frame.
                if after_show and (cv2.waitKey(20) & 0xFF == ord('q')):
                    break

                im = cap.get_latest_frame()

                if elapsed >= cap.get_frame_rate():
                    image_to_show = im
                else:
                    elapsed = time.time() - prev_time
                    continue

                if image_to_show is None:
                    continue

                prev_time = time.time()

                frame_rgb = cv2.cvtColor(
                    image_to_show, cv2.COLOR_BGR2RGB
                )  # Convert frame into RGB from BGR and resize accordingly
                frame_resized = cv2.resize(frame_rgb,
                                           (frame_width, frame_height),
                                           interpolation=cv2.INTER_LINEAR)

                darknet.copy_image_from_bytes(
                    darknet_image, frame_resized.tobytes(
                    ))  # Copy that frame bytes to darknet_image
                now = datetime.now()
                cap_time = now.strftime("%Y-%m-%d %H:%M:%S")
                # yolov4를 사용하여 마스크 착용여부 분류하기
                detections = darknet.detect_image(
                    netMain, metaMain, darknet_image, thresh=0.25
                )  # Detection occurs at this line and return detections, for customize we can change the threshold.

                dict_result = {}
                # 이미지에 결과 표시 및 감염 위험도 및 혼잡도 계산하기
                image, dict_result, maintain_info = cvDrawBoxes(
                    detections, frame_resized, space, pro_dist, pro_peo,
                    cluster_list, old_cluster_list, maintain_info, blurring)

                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                dict_result["camera_id"] = camera_id
                dict_result["time"] = cap_time

                #tracking을 할 경우 사람 feature 정보 저장
                if tracking:
                    track_c.now_tracking(image_to_show)
                    track_c.show_track(image)
                socket.send_data(image, dict_result)

                elapsed = time.time() - prev_time
                after_show = True

        else:
            #out = cv2.VideoWriter('output.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height))
            while True:
                now = datetime.now()
                now_mic = now.microsecond
                if now_mic % 30000 == 0:  # 1 second
                    ret, frame_read = cap.read()
                    if not ret:  # Check if frame present otherwise he break the while loop
                        break
                    if tracking:
                        track_c.now_tracking(frame_read)

                if now_mic % 600000 == 0:  # 2 frame: 600000 / 4 frame: 300000
                    frame_rgb = cv2.cvtColor(
                        frame_read, cv2.COLOR_BGR2RGB
                    )  # Convert frame into RGB from BGR and resize accordingly
                    frame_resized = cv2.resize(frame_rgb,
                                               (frame_width, frame_height),
                                               interpolation=cv2.INTER_LINEAR)
                    darknet.copy_image_from_bytes(darknet_image,
                                                  frame_resized.tobytes())
                    now = datetime.now()
                    cap_time = now.strftime("%Y-%m-%d %H:%M:%S")
                    detections = darknet.detect_image(netMain,
                                                      metaMain,
                                                      darknet_image,
                                                      thresh=0.25)
                    dict_result = {}
                    image, dict_result, maintain_info = cvDrawBoxes(
                        detections, frame_resized, space, pro_dist, pro_peo,
                        cluster_list, old_cluster_list, maintain_info,
                        blurring)
                    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                    if tracking:
                        track_c.show_track(image)
                    dict_result["camera_id"] = camera_id
                    dict_result["time"] = cap_time
                    socket.send_data(image, dict_result)

                    #out.write(image)

    except KeyboardInterrupt:
        print("cancelled")

    finally:
        if is_cam:
            cap.stop()  # For releasing cap and out.
        else:
            cap.release()
            out.release()
        print(":::Video Write Completed")
        socket.disconnect()
コード例 #16
0
def YOLO():

    global metaMain, netMain, altNames
    configPath = 'obj.cfg'
    weightPath = 'obj.weights'
    metaPath = 'obj.data'
    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:
        netMain = darknet.load_net(configPath.encode("ascii"),
                                   weightPath.encode("ascii"), 0,
                                   1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass

    # read video file
    cap = cv2.VideoCapture(pathToFile)
    input_width = int(cap.get(3))
    input_height = int(cap.get(4))
    input_fps = cap.get(cv2.CAP_PROP_FPS)
    input_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    print(f"Found Video: {input_width}x{input_height} - {input_fps} FPS")

    # create output stream
    if fileName is not 'empty':
        out = cv2.VideoWriter((str(fileName) + '.mp4'),
                              cv2.VideoWriter_fourcc(*'X264'), input_fps,
                              (input_width, input_height))

    network_width = darknet.network_width(netMain)
    network_height = darknet.network_height(netMain)
    print(f"Network: {network_width}x{network_height}")
    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(network_width, network_height, 3)

    scale_x = input_width / network_width
    scale_y = input_height / network_height

    print("Starting the YOLO loop...")
    frame_count = 0
    fps = FPS().start()
    while True:

        frame_count += 1

        ret, frame_read = cap.read()
        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)

        frame_resized = cv2.resize(frame_rgb, (network_width, network_height),
                                   interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())
        detections = darknet.detect_image(netMain,
                                          metaMain,
                                          darknet_image,
                                          thresh=0.25)
        position = (-1, -1)

        if not (len(detections) is 0):
            idOfDetection = getIDHighestDetection(detections)
            position = (int(detections[idOfDetection][2][0]),
                        int(detections[idOfDetection][2][1]))
            pts.appendleft(position)
        #else:
        #    pts.append(None)

        # update fps every 10 frames
        if frame_count % 10 == 0:
            percent = 100 / input_frames * frame_count
            print(
                f"Ball: {position[0]},{position[1]} - {percent}% done - {fps.fps()} FPS"
            )

        # loop over the set of tracked points
        for i in range(1, len(pts)):
            # if either of the tracked points are None, ignore
            # them
            if pts[i - 1] is None or pts[i] is None:
                continue

            # otherwise, compute the thickness of the line and
            # draw the connecting lines
            thickness = int(np.sqrt(200 / float(i + 1)) * 2)
            cv2.line(frame_read, pts[i - 1] * scale_x, pts[i] * scale_y,
                     (0, 255, 0), thickness)

        if args["record"] is not 'empty':
            out.write(frame_read)

        if args["showvideo"]:
            cv2.imshow('Demo', frame_read)
            cv2.moveWindow("Demo", 1025, 490)

        fps.update()
        cv2.waitKey(3)

    cap.release()
    out.release()
コード例 #17
0
def YOLO(args):

    global metaMain, netMain, altNames
    configPath = args.cfg
    weightPath = args.weights
    metaPath = args.data

    print("+*Using:\n\tCFG: " + configPath + "\n\tWeights: " +
          weightPath + "\n\tMetadata: " + metaPath)
    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath)+"`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath)+"`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath)+"`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode(
            "ascii"), weightPath.encode("ascii"), 0, args.bs)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass
    # cap = cv2.VideoCapture(0)

    frame_read = cv2.imread(args.source)
    print("Starting the YOLO loop...")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                       darknet.network_height(netMain), 3)
    frame_resized = cv2.resize(frame_read,
                               (darknet.network_width(netMain),
                                darknet.network_height(netMain)),
                               interpolation=cv2.INTER_LINEAR)

    darknet.copy_image_from_bytes(
        darknet_image, frame_resized.tobytes())

    detections = darknet.detect_image(
        netMain, metaMain, darknet_image, thresh=args.confidence, nms=args.nms_thresh, debug=False)
    cv2.imwrite('temp.jpg', frame_resized)
    final_frame = cv2.imread('temp.jpg')
    cropToBoundingBox(detections, final_frame)
    try:
        os.remove('temp.jpg')
    except:
        pass
    print("Successfully finished reading barcodes!")
コード例 #18
0
ファイル: yolov4_face_test.py プロジェクト: CV-IP/swiftface
def YOLO():

    global metaMain, netMain, altNames
    # configPath = "./cfg/yolov4.cfg"#"yolov4_face.cfg"#"./cfg/yolov4.cfg"
    # weightPath = "yolov4.weights"#"./backup/yolov4_face_best.weights"
    # metaPath = "./cfg/coco.data"#"./data/obj.data"#"./cfg/coco.data"

    configPath = "yolov4_face.cfg"  #"./cfg/yolov4.cfg"
    weightPath = "./backup/yolov4_face_final.weights"
    metaPath = "./data/obj.data"  #"./cfg/coco.data"

    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass
    #cap = cv2.VideoCapture(0)
    # cap = cv2.VideoCapture(0)
    # cap.set(3, 1280)
    # cap.set(4, 720)
    # out = cv2.VideoWriter(
    #     "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0,
    #     (darknet.network_width(netMain), darknet.network_height(netMain)))
    print("Starting the YOLO loop...")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                       darknet.network_height(netMain), 3)
    list_file = glob.glob('./WIDER_test/images/*/*.jpg')
    i = 0
    ttt = time.time()
    for f in list_file:
        # if True:#while True:
        prev_time = time.time()
        # ret, frame_read = cap.read()
        frame_read = cv2.imread(f)
        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(
            frame_rgb,
            (darknet.network_width(netMain), darknet.network_height(netMain)),
            interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())
        detections = darknet.detect_image(netMain,
                                          metaMain,
                                          darknet_image,
                                          thresh=0.25)
        # print(detections)
        image = cvDrawBoxes(detections, frame_resized)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        cv2.imwrite("./save/{}.jpg".format(i), image)
        print((time.time() - prev_time))
        i = i + 1
        # if i==2:
        #     break
        # cv2.imshow('Demo', image)
        # cv2.waitKey(3)
    # cap.release()
    # out.release()
    print((time.time() - ttt))
コード例 #19
0
def YOLO():

    global metaMain, netMain, altNames
    configPath = "./cfg/yolov4-mask.cfg"
    weightPath = "./yolov4-mask_final.weights"  # 나중에 train한 weight 넣기  -  backup에서 가져오면 됨
    metaPath = "./cfg/mask.data"

    if not os.path.exists(
            configPath
    ):  # Checks whether file exists otherwise return ValueError
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:  # Checks the metaMain, NetMain and altNames. Loads it in script
        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass

    #cap = cv2.VideoCapture(0)                                      # Uncomment to use Webcam
    cap = CamStream(path=0)
    cap.set_frame_rate("00:01:00")  # 1분에 한번씩 출력
    # cap.set_frame_rate(".30")     # 30fps로 출력

    cap.start()

    frame_width = int(
        cap.get(3))  # Returns the width and height of capture video
    frame_height = int(cap.get(4))
    # Set out for video writer
    out = cv2.VideoWriter(  # Set the Output path for video writer
        "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0,
        (frame_width, frame_height))
    darknet_image = darknet.make_image(frame_width, frame_height, 3)

    # while not cap.is_ready():
    #    time.sleep(5)

    prev_time = time.time()
    elapsed = cap.get_frame_rate()
    after_show = False
    image_to_show = None

    while True:
        if after_show and (cv2.waitKey(20) & 0xFF == ord('q')):
            break

        im = cap.get_latest_frame()
        if elapsed >= cap.get_frame_rate():
            print(elapsed)
            image_to_show = im
        else:
            elapsed = time.time() - prev_time
            continue

        if image_to_show is None:
            continue

        prev_time = time.time()

        ###############################
        # do something costly in here #
        ###############################

        frame_rgb = cv2.cvtColor(
            image_to_show, cv2.COLOR_BGR2RGB
        )  # Convert frame into RGB from BGR and resize accordingly
        frame_resized = cv2.resize(frame_rgb, (frame_width, frame_height),
                                   interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(
            darknet_image,
            frame_resized.tobytes())  # Copy that frame bytes to darknet_image

        detections = darknet.detect_image(
            netMain, metaMain, darknet_image, thresh=0.25
        )  # Detection occurs at this line and return detections, for customize we can change the threshold.
        image = cvDrawBoxes(
            detections, frame_resized
        )  # Call the function cvDrawBoxes() for colored bounding box per class
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        #print(1/(time.time()-prev_time))
        cv2.imshow('Demo', image)  # Display Image window
        cv2.waitKey(3)
        out.write(image)

        #cv2.imshow("demo", image_to_show)
        elapsed = time.time() - prev_time
        after_show = True

    cap.stop()
    out.release()
    """
    cap = cv2.VideoCapture("test.mp4")                             # Local Stored video detection - Set input video
    frame_width = int(cap.get(3))                                   # Returns the width and height of capture video
    frame_height = int(cap.get(4))
    # Set out for video writer
    out = cv2.VideoWriter(                                          # Set the Output path for video writer
        "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0,
        (frame_width, frame_height))

    print("Starting the YOLO loop...")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(frame_width, frame_height, 3) # Create image according darknet for compatibility of network
    while True:                                                      # Load the input frame and write output frame.
        prev_time = time.time()
        ret, frame_read = cap.read()                                 # Capture frame and return true if frame present
        # For Assertion Failed Error in OpenCV
        if not ret:                                                  # Check if frame present otherwise he break the while loop
            break

        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)      # Convert frame into RGB from BGR and resize accordingly
        frame_resized = cv2.resize(frame_rgb,
                                   (frame_width, frame_height),
                                   interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(darknet_image,frame_resized.tobytes())                # Copy that frame bytes to darknet_image

        detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25)    # Detection occurs at this line and return detections, for customize we can change the threshold.                                                                                   
        image = cvDrawBoxes(detections, frame_resized)               # Call the function cvDrawBoxes() for colored bounding box per class
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        #print(1/(time.time()-prev_time))
        #cv2.imshow('Demo', image)                                    # Display Image window
        cv2.waitKey(3)
        out.write(image)                                             # Write that frame into output video
    cap.release()                                                    # For releasing cap and out. 
    out.release()
    """
    print(":::Video Write Completed")
コード例 #20
0
def YOLO():

    global metaMain, netMain, altNames
    configPath = "./tiny9000.cfg"
    weightPath = "./tiny_10000.weights"
    metaPath = "./data/obj.data"
    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass
    path = 'http://192.168.43.156:4747/video'
    #cap = cv2.VideoCapture(path)
    cap = cv2.VideoCapture(CAM_NUM)
    #cap = cv2.VideoCapture('video143.mp4')
    cap.set(3, 1280)
    cap.set(4, 720)
    out = cv2.VideoWriter(
        "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0,
        (darknet.network_width(netMain), darknet.network_height(netMain)))
    print("Starting the YOLO loop...")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                       darknet.network_height(netMain), 3)

    #s.write(str.encode('a'))
    counter = 0
    steering = '2'
    limit_frames = 5
    angle_limit = [0] * limit_frames

    while cap.isOpened():
        try:
            prev_time = time.time()
            ret, frame_read = cap.read()
            frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
            frame_resized = cv2.resize(frame_rgb,
                                       (darknet.network_width(netMain),
                                        darknet.network_height(netMain)),
                                       interpolation=cv2.INTER_LINEAR)
            darknet.copy_image_from_bytes(darknet_image,
                                          frame_resized.tobytes())

            detections = darknet.detect_image(netMain,
                                              metaMain,
                                              darknet_image,
                                              thresh=0.25)
            image = cvDrawBoxes(detections, frame_resized)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            #print(1/(time.time()-prev_time))

            ######################################################
            #############   AAPNA CODE  ##########################
            ######################################################

            # simple inv transform
            inv_image, M = chcone.inv_map(image)
            #print(inv_image.shape)

            # getting inv coordinates on person and cone
            person, mybox, image = get_inv_coor(detections, image, M)

            # alert in case of threat
            if (len(person) != 0):
                for human in person:
                    cv2.circle(inv_image, human, 5, (0, 0, 255), -1)
                    distance = personDistance(human)
                    '''if(distance <= DANGER):
            			print("U wanna Die?")
            		else:
            			print("hatt be")'''

            left_box, right_box, lines = chcone.pathplan(mybox, steering)
            #print(left_box, '\n')
            #print(right_box,'\n')
            #print(lines,'\n\n\n')
            if len(mybox) == 0:
                counter = counter + 1
                if counter == 30:
                    s.write(str.encode('c'))
                    counter = 0

            ######################################################
            ############### DRAWING ONLY    ######################
            ######################################################

            for i in range(len(mybox)):
                cv2.circle(inv_image, mybox[i], 5, (0, 255, 255), -1)  # Filled

            for i in range(len(left_box) - 1):
                cv2.line(inv_image, left_box[i], left_box[i + 1],
                         (125, 125, 255), 3)

            for i in range(len(right_box) - 1):
                cv2.line(inv_image, right_box[i], right_box[i + 1], (0, 0, 0),
                         3)

            ######################################################
            ######################################################

            # encode signal for steering control
            try:
                angle = chcone.angle(lines[0], lines[1])
            except:
                angle = chcone.angle(lines[0], lines[1])

            angle = math.floor(angle)

            #########################
            angle_limit.append(angle)
            del angle_limit[0]
            #########################

            angle_a = steer((sum(angle_limit)) // limit_frames)
            print((sum(angle_limit)) // limit_frames)
            if (steering != angle_a):
                s.write(str.encode(angle_a))
                steering = angle_a
                print('updated')

            # JUST DRAWING
            inv_image = chcone.pathbana(lines, inv_image)
            cv2.circle(image, chcone.pt[0], 5, (255, 255, 255), -1)
            cv2.circle(image, chcone.pt[1], 5, (255, 255, 255), -1)
            cv2.circle(image, chcone.pt[2], 5, (255, 255, 255), -1)
            cv2.circle(image, chcone.pt[3], 5, (255, 255, 255), -1)
            image = cv2.resize(image, (800, 800))
            cv2.imshow('image', image)

            cv2.circle(inv_image, chcone.car_coor, DANGER, (0, 0, 225), 2)
            h, w, c = inv_image.shape
            #draws center line
            #cv2.line(inv_image, (w//2,0), (w//2,h),(255,0,0),5)
            cv2.line(inv_image, (0, chcone.LIMIT_CONE),
                     (416, chcone.LIMIT_CONE), (0, 0, 255), 1)

            if (steering == '4'):
                cv2.line(inv_image, (208, 0), (208, 416), (0, 225, 255), 1)

            elif (steering == '0' or steering == '1' or steering == '2'
                  or steering == '3'):
                cv2.line(inv_image, (104, 0), (208, 416), (0, 225, 255), 1)

            else:
                cv2.line(inv_image, (312, 0), (208, 416), (0, 225, 255), 1)

            inv_image = cv2.resize(inv_image, (800, 800))

            cv2.imshow('transform', inv_image)

            # clear lists
            mybox.clear()
            left_box.clear()
            right_box.clear()
            lines.clear()

            cv2.waitKey(3)
        except Exception as e:
            print(e)
            print('Exception aaya hai!!!!')
            s.write(str.encode('c'))
            break
    cap.release()
    out.release()
コード例 #21
0
ファイル: test2.py プロジェクト: jolim97/Jetson
def YOLO():
    start = time.time()
    global metaMain, netMain, altNames
    configPath = "./cfg/yolov3-tiny.cfg"
    weightPath = "./bin/yolov3-tiny.weights"
    #configPath = "./cfg/yolov3.cfg"
    #weightPath = "./bin/yolov3.weights"
    metaPath = "./cfg/coco.data"
    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass
    #cap = cv2.VideoCapture(0)
    #cap = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, heigh=(int)720, format=(string)NV12, framerate=(fraction)24/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink")
# cap = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720, format=(string)NV12, framerate=(fraction)60/1 ! nvvidconv flip-method=2! video/x-raw, width=(int)1280, height=(int)720, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink")


#cap = cv2.VideoCapture("/home/nvidia-tx2/OpenCV_in_Ubuntu/Data/Lane_Detection_Videos/solidWhiteRight.mp4")
#cap = cv2.VideoCapture("nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)640, height=(int)480, format=(string)I420, framerate=(fraction)30/1 ! nvvidconv flip-method=2 ! video/x-raw, format=(string)I420 ! videoconvert ! video/x-raw, format=(string)BGR ! appsink")
    cap = cv2.VideoCapture(
        "v4l2src device=/dev/video1 ! video/x-raw, width=640, height=360, format=(string)YUY2,framerate=30/1 ! videoconvert ! video/x-raw,width=640,height=360,format=BGR ! appsink"
    )
    #cap.set(3, 1280)
    #cap.set(4, 720)
    #out = cv2.VideoWriter(
    #    "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0,
    #    (darknet.network_width(netMain), darknet.network_height(netMain)))
    print("Starting the YOLO loop...")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                       darknet.network_height(netMain), 3)
    inc3()
    while True:
        prev_time = time.time()
        ret, frame_read = cap.read()
        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(
            frame_rgb,
            (darknet.network_width(netMain), darknet.network_height(netMain)),
            interpolation=cv2.INTER_LINEAR)
        while len(control) >= 10:
            control.remove(control[0])
        while len(signal) >= 2:
            signal.remove(signal[0])

        detections = darknet.detect_image(netMain,
                                          metaMain,
                                          frame_resized,
                                          thresh=0.25)
        det = False
        for d in detections:
            if d[0].decode() == "person":
                det = True
        print("####################\n")
        print(control)
        if not detections or not det:
            control.append(0)
            print("No Human")
        elif det:
            print("Human")

        image = cvDrawBoxes(detections, frame_resized)[0]
        xm = cvDrawBoxes(detections, frame_resized)[1]
        signalcontrol(xm)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        print("Time: " + str(round(time.time() - prev_time, 4)))
        print("\n####################")
        cv2.imshow('Demo', image)
        cv2.waitKey(3)
        if (time.time() - start > 500):
            robot.stop()
            quit()
    cap.release()
    out.release()
コード例 #22
0
def YOLO():

    global metaMain, netMain, altNames
    tiny = True
    if tiny:
        configPath = "./cfg/yolov3-tiny.cfg"
        weightPath = "./backup/tinyfinal1125.weights"
        metaPath = "./cfg/voc.data"
    else:
        configPath = "./cfg/yolov3.cfg"
        weightPath = "./backup/full_final.weights"
        metaPath = "./cfg/voc.data"
    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                       darknet.network_height(netMain), 3)

    cv2.namedWindow("show1", 0)
    while True:
        frame_read = image_to_yolo.copy()

        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(
            frame_rgb,
            (darknet.network_width(netMain), darknet.network_height(netMain)),
            interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())

        detections = darknet.detect_image(netMain,
                                          metaMain,
                                          darknet_image,
                                          thresh=0.6)
        image = cvDrawBoxes(detections, frame_resized)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        cv2.imshow('show1', image)
        cv2.waitKey(3)
コード例 #23
0
    def __main(self):
        # cap = cv2.VideoCapture(1)
        # cap = cv2.VideoCapture('/dev/video2')
        cap = cv2.VideoCapture('0818.mp4')

        cap.set(3, WEIGHT)  # Feed resolution from webcam to YOLO however output is from yolo cfg file resolution
        cap.set(4, HEIGHT)
        out = cv2.VideoWriter("output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 30.0, (darknet.network_width(netMain), darknet.network_height(netMain)))

        print("Starting the YOLO loop...")

        # Create an image we reuse for each detect
        darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3)

        # Variables
        frame_count = 0
        mx = frame_show = ''
        self.last_mission = ''
        self.TF_flag = [0, 0, 0]
        while not self.flag.system_stop: # By stoping system, Reading GPS should be stopped, too.
            if self.flag.exmpale_stop:
                time.sleep(0.1)
            else:
                prev_time = time.time()
                ret, frame_read = cap.read()
                hsv = cv2.cvtColor(frame_read, cv2.COLOR_BGR2HSV)
                h, s, v = cv2.split(hsv)
                mean_brt = np.mean(v)

                #  Brightness Correction
                if mean_brt > BRT_THRESH:
                    frame_read = np.int16(frame_read)
                    frame_read= frame_read * (contrast/127+1) - contrast + brightness
                    frame_read = np.clip(frame_read, 0, 255)
                    frame_read = np.uint8(frame_read)

                frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
                frame_resized = cv2.resize(frame_rgb, (darknet.network_width(netMain),darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) # CHECK RESIZING KEEPING RATIO

                darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())

                detections = darknet.detect_image(netMain, metaMain, darknet_image, THRESH) # Uses YOLO to detect
                detect = self.checkSize(detections) # Gets rid of improper detections

                fps = (1 / (time.time() - prev_time))


                image = self.cvDrawBoxes(detect, frame_resized)
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

                if detect:
                    sg1 = detect[0][0].decode()
                    if sg1 == 'traffic_light':
                        x, y, w, h = detect[0][2]

                        x11, y11, x22, y22 =self.convertBack(float(x), float(y), float(w), float(h))
    
                        self.TF_flag = lightsParser(image, x11, x22, y11, y22)
                        if self.TF_flag is None:
                            self.TF_flag = [0, 0, 0]

                        if self.TF_flag[0] == 1:
                            self.sign['red_light'][0] += 1

                        if self.TF_flag[1] == 1:
                            self.sign['arrow_light'][0] += 1

                        if self.TF_flag[2] == 1:
                            self.sign['green_light'][0] += 1

                    mx = max(self.sign, key=lambda key: self.sign[key][0])

                    if self.sign[mx][0]:
                        frame_show = str(self.sign[mx][0])
                    
                    frame_count = 0

                else:
                    frame_count += 1  # In order to know IDLE, count frames without any reading and store in frame_count

                self.check_lights()

                if frame_count > IDLE_SIZE:  # If No detection for the last IDLE_SIZE frames -> refresh all  (used to avoid random uncertain and uncontinuos detections)
                    self.reset_dict(0)
                    mx = 'SEARCHING'
                    frame_show = ''
                    frame_count = 0
                    self.last_mission = 'GREEN'
                    self.TF_flag = [0, 0, 0]

                m_number = self.get_mission_number()


                # DEBUGING: write frame count, last mission, the sign with max count and its count, fps
                cv2.putText(image, str(frame_count), bottomLeftCornerOfText, font, fontScale, fontColor, lineType)
                cv2.putText(image, self.last_mission, (450, 600), font, fontScale, fontColor, lineType)
                # cv2.putText(image, mx, (400, 570), font, fontScale, fontColor, lineType)
                # cv2.putText(image, frame_show, (550, 570), font, fontScale, fontColor, lineType)
                cv2.putText(image, str(m_number), (10, 370), font, fontScale, fontColor, lineType)
                # cv2.putText(image, self.mission, (10, 400), font, fontScale, fontColor, lineType)
                cv2.putText(image, str(self.TF_flag), (10, 600), font, fontScale, fontColor, lineType)
                cv2.putText(image, 'fps: ' + str(int(fps)), (545, 25), font, 0.5, fontColor, lineType)
                cv2.putText(image, 'brt: ' + str(int(mean_brt)), (540, 45), font, 0.5, fontColor, lineType)
                # cv2.putText(image, 'School: '+ str(self.is_school_zone()), (530, 65), font, 0.5, fontColor, lineType)
                cv2.rectangle(image, (0, 0), (100, 100), (60, 60, 60), 1)
                cv2.rectangle(image, (0, 0), (223, 223), (60, 60, 60), 1)

                out.write(image)
                cv2.imshow('Output', image)
                cv2.waitKey(3)
コード例 #24
0
ファイル: base.py プロジェクト: nizhenko23/YoloV4
def YOLO():
    """
    Perform Object detection
    """
    global metaMain, netMain, altNames
    configPath = "./cfg/yolov4.cfg"
    weightPath = "./yolov4.weights"
    metaPath = "./cfg/coco.data"
    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode(
            "ascii"), weightPath.encode("ascii"), 0, 1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass
    # cap = cv2.VideoCapture(0)
    cap = cv2.VideoCapture("./Input/test5.mp4")  # <----- Replace with your video directory
    frame_width = int(cap.get(3))
    frame_height = int(cap.get(4))
    new_height, new_width = frame_height // 2, frame_width // 2
    # print("Video Reolution: ",(width, height))

    out = cv2.VideoWriter(
        "./Demo/test5_output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0,  # <----- Replace with your output directory
        (new_width, new_height))

    # print("Starting the YOLO loop...")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(new_width, new_height, 3)

    while True:
        prev_time = time.time()
        ret, frame_read = cap.read()
        # Check if frame present :: 'ret' returns True if frame present, otherwise break the loop.
        if not ret:
            break

        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(frame_rgb,
                                   (new_width, new_height),
                                   interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())

        detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25)
        image = cvDrawBoxes(detections, frame_resized)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        print(1 / (time.time() - prev_time))
        cv2.imshow('Demo', image)
        cv2.waitKey(3)
        out.write(image)

    cap.release()
    out.release()
    print(":::Video Write Completed")
コード例 #25
0
def predict_rs_image_yolo_poythonAPI(image_path,
                                     save_dir,
                                     model,
                                     config_file,
                                     yolo_data,
                                     patch_w,
                                     patch_h,
                                     overlay_x,
                                     overlay_y,
                                     batch_size=1):
    '''
    predict an remote sensing using YOLO Python API
    :param image_path:
    :param save_dir:
    :param model:
    :param config_file:
    :param yolo_data:
    :param patch_w:
    :param patch_h:
    :param overlay_x:
    :param overlay_y:
    :param batch_size:
    :return:
    '''
    height, width, band_num, date_type = raster_io.get_height_width_bandnum_dtype(
        image_path)
    # print('input image: height, width, band_num, date_type',height, width, band_num, date_type)

    # divide the image the many small patches, then calcuate one by one, solving memory issues.
    image_patches = split_image.sliding_window(width,
                                               height,
                                               patch_w,
                                               patch_h,
                                               adj_overlay_x=overlay_x,
                                               adj_overlay_y=overlay_y)
    patch_count = len(image_patches)

    if os.path.isdir(save_dir) is False:
        io_function.mkdir(save_dir)

    # group patches based on size, each patch is (xoff,yoff ,xsize, ysize)
    patch_groups = {}
    for patch in image_patches:
        wh_str = 'w%d' % patch[2] + '_' + 'h%d' % patch[3]
        if wh_str in patch_groups.keys():
            patch_groups[wh_str].append(patch)
        else:
            patch_groups[wh_str] = [patch]

    network, class_names, _ = load_darknet_network(config_file,
                                                   yolo_data,
                                                   model,
                                                   batch_size=batch_size)

    # batch detection
    if batch_size > 1:
        return darknet_batch_detection_rs_images(network, image_path, save_dir,
                                                 patch_groups, patch_count,
                                                 class_names, batch_size)

    # read the entire image
    entire_img_data, nodata = raster_io.read_raster_all_bands_np(image_path)
    entire_img_data = entire_img_data.transpose(1, 2, 0)  # to opencv format
    # # RGB to BGR: Matplotlib image to OpenCV https://www.scivision.dev/numpy-image-bgr-to-rgb/
    # entire_img_data = entire_img_data[..., ::-1].copy() # cancel RGB to BGR, since it make results worse, (maybe darknet already read images as RGB during training)
    entire_height, entire_width, band_num = entire_img_data.shape
    print("entire_height, entire_width, band_num", entire_height, entire_width,
          band_num)
    if band_num not in [1, 3]:
        raise ValueError('only accept one band or three band images')

    patch_idx = 0
    for key in patch_groups.keys():
        patches_sameSize = patch_groups[key]

        # get width, height, and band_num of a patch, then create a darknet image.
        # img_data, nodata = raster_io.read_raster_all_bands_np(image_path, boundary=patches_sameSize[0])
        # img_data = img_data.transpose(1, 2, 0)
        # height, width, band_num = img_data.shape
        # if band_num not in [1, 3]:
        #     raise ValueError('only accept one band or three band images')

        height, width = patches_sameSize[0][3], patches_sameSize[0][2]

        # Create one with image we reuse for each detect : images with the same size.
        darknet_image = darknet.make_image(width, height, band_num)

        for idx, patch in enumerate(patches_sameSize):
            t0 = time.time()
            # patch: (xoff,yoff ,xsize, ysize)
            # img_data, nodata = raster_io.read_raster_all_bands_np(image_path,boundary=patch)
            # img_data = img_data.transpose(1, 2, 0)
            img_data = copy_one_patch_image_data(patch, entire_img_data)

            # prediction
            darknet.copy_image_from_bytes(darknet_image, img_data.tobytes())
            # thresh=0.25, relative low, set to 0 will output too many, post-process later
            detections = darknet.detect_image(network,
                                              class_names,
                                              darknet_image,
                                              thresh=0.25)

            # save results
            save_res_json = os.path.join(save_dir, '%d.json' % patch_idx)
            save_one_patch_detection_json(patch, detections, class_names,
                                          save_res_json)

            if patch_idx % 100 == 0:
                print('saving %d patch, total: %d, cost %f second' %
                      (patch_idx, patch_count, time.time() - t0))

            patch_idx += 1

        darknet.free_image(darknet_image)
コード例 #26
0
ファイル: darknet_video.py プロジェクト: AlexeyAB/darknet
def YOLO():

    global metaMain, netMain, altNames
    configPath = "./cfg/yolov3.cfg"
    weightPath = "./yolov3.weights"
    metaPath = "./cfg/coco.data"
    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath)+"`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath)+"`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath)+"`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode(
            "ascii"), weightPath.encode("ascii"), 0, 1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass
    #cap = cv2.VideoCapture(0)
    cap = cv2.VideoCapture("test.mp4")
    cap.set(3, 1280)
    cap.set(4, 720)
    out = cv2.VideoWriter(
        "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0,
        (darknet.network_width(netMain), darknet.network_height(netMain)))
    print("Starting the YOLO loop...")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                    darknet.network_height(netMain),3)
    while True:
        prev_time = time.time()
        ret, frame_read = cap.read()
        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(frame_rgb,
                                   (darknet.network_width(netMain),
                                    darknet.network_height(netMain)),
                                   interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(darknet_image,frame_resized.tobytes())

        detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25)
        image = cvDrawBoxes(detections, frame_resized)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        print(1/(time.time()-prev_time))
        cv2.imshow('Demo', image)
        cv2.waitKey(3)
    cap.release()
    out.release()
コード例 #27
0
ファイル: darknet_video.py プロジェクト: BuddyLim/Dashboard-G
def YOLO(videopath):

    global metaMain, netMain, altNames, framecount, cur_h, cur_w, prev_h, prev_w, w_count, warning_dict, start_time

    configPath = "cfg\yolov3.cfg"
    weightPath = "../../../yolov3.weights"
    metaPath = "data\coco.data"
    # wave_obj = sa.WaveObject.from_wave_file("bleepsfxwav.wav")
    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath)+"`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath)+"`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath)+"`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode(
            "ascii"), weightPath.encode("ascii"), 0, 1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass

    root = Tk()

    window_width = 416
    window_height = 416
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()

    xcoord = int((screen_width/2) - (window_width/2))
    ycoord = int((screen_height/2) - (window_height/2))

    cap = cv2.VideoCapture(videopath)
    cap.set(3, cv2.CAP_PROP_FRAME_HEIGHT)
    cap.set(4, cv2.CAP_PROP_FRAME_WIDTH)

    cv2.namedWindow("Warning-Sys")
    cv2.moveWindow("Warning-Sys", xcoord, ycoord)

    print("Starting the YOLO loop...")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                       darknet.network_height(netMain), 3)

    ct = CentroidTracker()

    w_count = 0

    while True:
        prev_time = time.time()
        ret, frame_read = cap.read()
        framecount += 1
        # If end of video, break loop. In some IDEs like Spyder, without this code,
        # the window frame will still persist and requires the user to force close via task manager
        if(np.shape(frame_read) == ()):
            print("Broke the loop\n")
            print("Framecount: " + str(framecount))
            break

        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(frame_rgb,
                                   (darknet.network_width(netMain),
                                    darknet.network_height(netMain)),
                                   interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())
        detections = darknet.detect_image(
            netMain, metaMain, darknet_image, thresh=0.7)

        # print(len(detections)
        rects = []

        for detection in detections:
            x, y, w, h = detection[2][0],\
                detection[2][1],\
                detection[2][2],\
                detection[2][3]
            if(90 < x < 326 and 90 < y < 326):
                if(w > 35 and h > 50 and w < 355):
                    xmin, ymin, xmax, ymax = convertBack(
                        float(x), float(y), float(w), float(h))
                    rects.append((xmin, ymin, xmax, ymax))

        objects = ct.update(rects)

        for (objectID, values) in objects.items():
            # text = "ID {}".format(objectID)
            # cv2.putText(frame_resized, text, (values[0][0] - 10, values[0][1] - 10),
            #             cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
            pt1 = ((values[1][0], values[1][1]))  # x min & y min
            pt2 = ((values[1][2], values[1][3]))  # x max & y max
            # cv2.rectangle(frame_resized, pt1, pt2, (0, 255, 0), 1)
            # cv2.circle(frame_resized,
            #            (values[0][0], values[0][1]), 4, (0, 255, 0), -1)

            w = (pt2[0] - pt1[0])
            h = (pt2[1] - pt1[1])
            # print("W: {}\nH: {}".format(w,h))

            cur_w[objectID] = w
            cur_h[objectID] = h

            if objectID not in prev_w:
                prev_w[objectID] = w
                prev_h[objectID] = h

            else:
                if(start_time == 0):
                        start_time = time.time()

                if(abs(cur_w[objectID] - prev_w[objectID]) * 2.5 > 32 or abs(cur_h[objectID] - prev_h[objectID]) * 2.5 > 35):
                    cv2.rectangle(frame_resized, pt1, pt2, (255, 0, 0), 1)
                    # print("Current W: {}\nCurrent H: {}".format(cur_w[objectID],cur_h[objectID]))
                    # print("Prev W: {}\nPrev H: {}".format(prev_w[objectID],prev_h[objectID]))
                    # print("ROC W: " + str(abs(cur_w[objectID] - prev_w[objectID]) * 2.5) + "\nROC H: "+ str(abs(cur_h[objectID]-prev_h[objectID]) *2.5))
                    # print("Car {} triggered".format(objectID))
                    wave_obj = sa.WaveObject.from_wave_file("bleepsfxwav.wav")
                    play_obj = wave_obj.play()
                    # play_obj.wait_done()
                    if((time.time() - start_time) >= 2):
                        w_count += 1
                        start_time = 0
                        warning_dict[w_count] = framecount

                elif(w > 173 and h > 220):
                    # print("W: {}\nH: {}".format(w,h))
                    wave_obj = sa.WaveObject.from_wave_file("bleepsfxwav.wav")
                    play_obj = wave_obj.play()

                    if((time.time() - start_time) >= 2):
                        w_count += 1
                        start_time = 0
                        warning_dict[w_count] = framecount

                else:
                    cv2.rectangle(frame_resized, pt1, pt2, (0, 255, 0), 1)

                prev_w[objectID] = w
                prev_h[objectID] = h

        image = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2RGB)
        cv2.imshow('Warning-Sys', image)

        key = cv2.waitKey(1) & 0xff
        # Break the loop when Q is pressed
        if key == ord('q'):
            print('Broke the loop\n')
            print("Framecount: " + str(framecount))
            break

        # Pauses the loop/video
        if (key == ord('p')):
            cv2.waitKey(-1)

        # print(1/(time.time()-prev_time))
        # print((time.time()-prev_time)))
    cap.release()
    root.destroy()
    cv2.destroyWindow('Warning-Sys')

    cur_w.clear()
    cur_h.clear()
    prev_w.clear()
    prev_h.clear()
    del rects[:]
コード例 #28
0
def YOLO():

    global metaMain, netMain, altNames
    configPath = "./cfg/yolov4.cfg"
    weightPath = "./yolov4.weights"
    metaPath = "./cfg/coco.data"

    print('configPath: {}'.format(configPath))
    print('weightPath: {}'.format(weightPath))
    print('metaPath: {}'.format(metaPath))

    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass
    #cap = cv2.VideoCapture(0)
    cap = cv2.VideoCapture("test.mp4")
    cap.set(3, 1280)
    cap.set(4, 720)
    out = cv2.VideoWriter(
        "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0,
        (darknet.network_width(netMain), darknet.network_height(netMain)))
    print("Starting the YOLO loop...")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                       darknet.network_height(netMain), 3)
    while True:
        prev_time = time.time()
        ret, frame_read = cap.read()
        frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(
            frame_rgb,
            (darknet.network_width(netMain), darknet.network_height(netMain)),
            interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())

        detections = darknet.detect_image(netMain,
                                          metaMain,
                                          darknet_image,
                                          thresh=0.25)
        image = cvDrawBoxes(detections, frame_resized)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        print(1 / (time.time() - prev_time))
        cv2.imshow('Demo', image)
        cv2.waitKey(3)
    cap.release()
    out.release()
コード例 #29
0
def YOLO():
    start_time = time.time()
    global model_svm
    model_svm = cv2.ml.SVM_load('svm.xml')
    global metaMain, netMain, altNames
    configPath = "./LP/yolov3-tiny_obj.cfg"
    weightPath = "./yolov3-tiny_obj_4000.weights"
    metaPath = "./LP/LP.data"
    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass

    cap = cv2.VideoCapture(0)

    print("Starting the YOLO loop...")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                       darknet.network_height(netMain), 3)
    init_time = time.time() - start_time
    print("Start time: ", init_time)
    while True:
        prev_time = time.time()
        ret, frame_read = cap.read()
        if frame_read.size != 0:
            frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
            frame_resized = cv2.resize(frame_rgb,
                                       (darknet.network_width(netMain),
                                        darknet.network_height(netMain)),
                                       interpolation=cv2.INTER_LINEAR)
            darknet.copy_image_from_bytes(darknet_image,
                                          frame_resized.tobytes())
            detections = darknet.detect_image(netMain,
                                              metaMain,
                                              darknet_image,
                                              thresh=0.25)
            image = cvDrawBoxes(detections, frame_resized)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            fps = (1 / (time.time() - prev_time))
            print("FPS : %0.1f" % fps)
            #cv2.imshow('Demo', image)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
    cap.release()
    cv2.destroyAllWindows()
    run_time = time.time() - start_time
    print("Run time: ", run_time)
コード例 #30
0
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass
    cap = cv2.VideoCapture(0)
    #cap = cv2.VideoCapture("test.mp4")
    cap.set(3, 1920)
    cap.set(4, 1080)
    #out = cv2.VideoWriter(
    #    "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0,
    #    (darknet.network_width(netMain), darknet.network_height(netMain)))
    print("Starting the YOLO loop...")

    # Create an image we reuse for each detect
    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                    darknet.network_height(netMain),3)
    while True:
        prev_time = time.time()
        ret, frame = cap.read()
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame_resized = cv2.resize(frame_rgb,
                                   (darknet.network_width(netMain),
                                    darknet.network_height(netMain)),
                                   interpolation=cv2.INTER_LINEAR)

        darknet.copy_image_from_bytes(darknet_image,frame_resized.tobytes())

        detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25)
        image = cvDrawBoxes(detections, frame_resized)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        print(1/(time.time()-prev_time))
コード例 #31
0
def YOLO():

    global metaMain, netMain, altNames

    configPath = "./cfg/yolov4.cfg"
    weightPath = "./yolov4.weights"
    metaPath = "./cfg/coco.data"

    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:
        netMain = darknet.load_net_custom(configPath.encode("ascii"),
                                          weightPath.encode("ascii"), 0,
                                          1)  # batch size = 1
    if metaMain is None:
        metaMain = darknet.load_meta(metaPath.encode("ascii"))
    if altNames is None:
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass

    print("Starting the YOLO loop...")

    darknet_image = darknet.make_image(darknet.network_width(netMain),
                                       darknet.network_height(netMain), 3)
    while True:

        frames = pipeline.wait_for_frames()
        aligned_frames = align.process(frames)
        aligned_depth_frame = aligned_frames.get_depth_frame()
        color_frame = aligned_frames.get_color_frame()
        color_image = np.asanyarray(color_frame.get_data())
        frame_rgb = cv2.cvtColor(color_image, cv2.COLOR_BGR2RGB)

        frame_resized = cv2.resize(
            frame_rgb,
            (darknet.network_width(netMain), darknet.network_height(netMain)),
            interpolation=cv2.INTER_LINEAR)
        darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes())
        detections = darknet.detect_image(netMain,
                                          metaMain,
                                          darknet_image,
                                          thresh=0.25)
        LL = len(detections)
        encoding = 'utf-8'
        #print(detections)

        for i in range(LL):
            KK = detections[i][0]
            strr = KK.decode(encoding)
            result = strr.find('person')
            if result is 0:
                detection = detections[i][2:5]
                prob = detections[i][1]
                '''
                if prob <0.8 :
                    continue     
                '''
                distance = get_object_depth(aligned_depth_frame, detection)
                print('Dist=....', distance, 'probability=....', prob)
                image = cv2.resize(frame_resized, (640, 480),
                                   interpolation=cv2.INTER_LINEAR)
                image = cvDrawBoxes(detection, image, distance)

                cv2.imshow('Demo', image)
                key = cv2.waitKey(3)

                if key == ord('q'):
                    break