Esempio n. 1
0
    def __init__(self):
        super(VisionDetectionModel, self).__init__()
        self.daemon = True

        self.__appConfigSingleton = AppConfigSingleton()
        self.__visDetctRstSingleton = VisionDetecedSingleton()

        self.netMain = darknet.load_net_custom(
            self.__appConfigSingleton.yoloConfigPath.encode("ascii"),
            self.__appConfigSingleton.yoloWeightPath.encode("ascii"), 0, 1)
        self.metaMain = darknet.load_meta(
            self.__appConfigSingleton.yoloMetaPath.encode("ascii"))

        #camera open and configuration
        self.cap = cv2.VideoCapture(0)
        #self.cap = cv2.VideoCapture("MODEL/yolo/test.mp4")
        #self.cap = cv2.VideoCapture("MODEL/yolo/test.webm")
        self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
        self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
        self.cap.set(cv2.CAP_PROP_FPS, 30)
        self.cap.set(cv2.CAP_PROP_FOURCC,
                     cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))

        # Create an image we reuse for each detect
        self.darknet_image = darknet.make_image(
            darknet.network_width(self.netMain),
            darknet.network_height(self.netMain), 3)
        self.__stopFlg = False
        self.__pauseFlg = True
Esempio n. 2
0
    def run(self):

        while (self.cap.isOpened()):
            if self.__stopFlg == True:
                print("Vision Detection model stopped")
                break

            ret, frame_read = self.cap.read()
            if ret:
                frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB)
                frame_resized = cv2.resize(frame_rgb, (darknet.network_width(
                    self.netMain), darknet.network_height(self.netMain)),
                                           interpolation=cv2.INTER_LINEAR)

                darknet.copy_image_from_bytes(self.darknet_image,
                                              frame_resized.tobytes())
                if self.pauseFlg == True:
                    detections = []
                else:
                    detections = darknet.detect_image(self.netMain,
                                                      self.metaMain,
                                                      self.darknet_image,
                                                      thresh=0.9)

                frame_resized = cv2.cvtColor(frame_resized, cv2.COLOR_RGB2BGR)

                if len(detections) is not 0:
                    self.__visDetctRstSingleton.pushToPooling(detections)
                    ign_items = copy.deepcopy(
                        self.__appConfigSingleton.visionIgnoredListFixed)
                    for item in self.__visDetctRstSingleton.fetchIgnoreContent(
                    ):
                        if item not in ign_items:
                            ign_items.append(item)

                    image = self.cvDrawBoxes(detections, frame_resized,
                                             ign_items)
                    image = Image.fromarray(
                        cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
                    self.__imgDetected = ImageTk.PhotoImage(image=image)
                else:
                    image = Image.fromarray(
                        cv2.cvtColor(frame_resized, cv2.COLOR_BGR2RGB))
                    self.__imgDetected = ImageTk.PhotoImage(image=image)

                self.notify_observers()
        self.cap.release()
Esempio n. 3
0
def YOLO():

    global metaMain, netMain, altNames
    configPath = "config/yolov4-hmi-packing.cfg"
    weightPath = "config/yolov4-hmi-packing_2000.weights"
    metaPath = "config/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 = cv2.VideoCapture("/home/ai/train2.mp4")
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
    cap.set(cv2.CAP_PROP_FPS, 30)
    cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
    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.9)

        image = cvDrawBoxes(detections, frame_resized)

        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        #print(1/(time.time()-prev_time))
        cv2.imshow('Demo', image)
        cv2.waitKey(60)
    cap.release()
    out.release()