def __init__(self):
        self.output_video_file_name = '/home/algernon/samba/video_queue/SalienceRecognizer/videos/processed/output2.mkv'
        self.emotion_recognizer = EmotionRecognizer(self.EMOTION_PROB_THRESH)

        self.segmentator = None
        # self.clothes_detector = ClothesDetector("yolo/df2cfg/yolov3-df2.cfg", "yolo/weights/yolov3-df2_15000.weights", "yolo/df2cfg/df2.names")
        self.video_file_name = '/home/algernon/Videos/source_videos/interview_anna.webm'
        self.captioner = Captioner()
        self.video_reader = VideoReader(self.video_file_name)
        self.joke_picker = JokePicker('joke_picker/shortjokes.csv',
                                      'joke_picker/joke_picker.fse')
        self.video_writer = cv2.VideoWriter(
            self.output_video_file_name, cv2.VideoWriter_fourcc(*"XVID"),
            self.video_reader.fps,
            (self.video_reader.width, self.video_reader.height))
        self.face_recognizer = FaceRecognizer()

        self.segmentator = SceneSegmentator(self.video_reader.fps *
                                            self.DELAY_TO_DETECT_IN_SECS)
        self.object_detection_reader = DetectionReader('detections.json')
        self.object_detector = ObjectDetector(
            './configs/COCO-Detection/faster_rcnn_R_101_FPN_3x.yaml',
            'https://dl.fbaipublicfiles.com/detectron2/COCO-Detection/faster_rcnn_R_101_FPN_3x/137851257/model_final_f6e8b1.pkl'
        )
        self.age_gender_predictor = AgeGenderPredictor()
        self.beauty_estimator = BeautyEstimator(
            '/home/algernon/DNNS/isBeauty/weights/epoch_50.pkl')
        self.main_person_definer = MainPersonDefiner()
        self.context_generator = ContextGenerator(self.object_detector.classes)
        self.speech_recognizer = SpeechRecognizer(self.video_file_name)
    def __callback_get_obj_relative_pose(self, req):
        # Reading last image
        img = self.__video_stream.read_undistorted()
        if img is None:
            rospy.logwarn(
                "Vision Node - Try to get object relative pose while stream is not running !"
            )
            return CommandStatus.VIDEO_STREAM_NOT_RUNNING, ObjectPose(
            ), "", "", CompressedImage()

        # Extracting parameters from request
        obj_type = ObjectType[req.obj_type]
        obj_color = ColorHSV[req.obj_color]
        workspace_ratio = req.workspace_ratio
        ret_image = req.ret_image

        # Creating ObjectDetector, an object for object detection
        self.__object_detector = ObjectDetector(
            obj_type=obj_type,
            obj_color=obj_color,
            workspace_ratio=workspace_ratio,
            ret_image_bool=ret_image,
        )

        # Launching pipeline
        status, msg_res_pos, obj_type, obj_color, im_draw = self.__object_detector.extract_object_with_hsv(
            img)
        if self.__object_detector.should_ret_image():
            _, msg_img = generate_msg_from_image(
                im_draw, compression_quality=self.__debug_compression_quality)
        else:
            msg_img = CompressedImage()
        return status, msg_res_pos, obj_type, obj_color, msg_img
Exemplo n.º 3
0
def object_detect(*args, **kwargs):
    file_path = kwargs.get('bn_path', None)
    print(kwargs)
    if file_path and isfile(file_path):
        object_detector = ObjectDetector(file_path)
    else:
        assert len(args) < 2 and len(args) > 0, 'Only one file argument'
        object_detector = ObjectDetector()
        object_detector.img_src = args[0]
    process_bn_image(file_path, **kwargs)
Exemplo n.º 4
0
 def __init__(self, parent, controller):
     tk.Frame.__init__(self, parent)
     self.controller = controller
     label = tk.Label(self, text="Camera Detection", font=controller.title_font)
     label.pack(side="top", fill="x", padx=30, pady=20)
     f = Frame(self, height=3, width=1000, bg="white")
     f.pack()
     button = tk.Button(self, text="Return to main menu",
                        command=lambda: controller.show_frame("StartPage"), padx=20, pady=10)
     button.pack()
     f = Frame(self, height=3, width=1000, bg="white")
     f.pack()
     self.od = ObjectDetector()
Exemplo n.º 5
0
    def detectObjectsImage(self):
        self.od = ObjectDetector()
        self.od.detectOcjectsFromImagesSetup()

        tk.Tk().withdraw()
        self.file_path = filedialog.askopenfilename()
        if self.file_path == "":
            return
        image = Image.open(self.file_path)
            # the array based representation of the image will be used later in order to prepare the
            # result image with boxes and labels on it.
        image_np = self.od.load_image_into_numpy_array(image)
            # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
        image_np_expanded = np.expand_dims(image_np, axis=0)
            # Actual detection.
        output_dict = self.od.run_inference_for_single_image(image_np_expanded, self.od.detection_graph)
            # Visualization of the results of a detection.
        vis_util.visualize_boxes_and_labels_on_image_array(
            image_np,
            output_dict['detection_boxes'],
            output_dict['detection_classes'],
            output_dict['detection_scores'],
            self.od.category_index,
                instance_masks=output_dict.get('detection_masks'),
                use_normalized_coordinates=True,
                line_thickness=8)

        plt.cla()
        plt.clf()
        plt.close(fig='all')

        plt.close(plt.gcf())
        plt.figure(clear=True)
        fig = plt.figure(figsize=self.od.IMAGE_SIZE)
        im = plt.imshow(image_np)
        plt.minorticks_off()
        ax = plt.gca()
        ax.set_xticklabels([])
        ax.set_yticklabels([])

        # a tk.DrawingArea
        try:
            self.canvas.get_tk_widget().pack_forget()
        except AttributeError:
            pass
        f = Figure(figsize=(5, 1))
        aplt = f.add_subplot(111)
        aplt.plot([1, 2, 3, 4])
        self.canvas = FigureCanvasTkAgg(fig, master=self)
        self.canvas.draw()
        self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
Exemplo n.º 6
0
    def __init__(self):
        if Controller.__instance != None:  # if the constructor of this class is called more than once
            raise Exception("This class is a singleton!")
        else:
            # puts the created instance in the "__instance" variable
            Controller.__instance = self
            self.cameraBool = True
            self.ledBool = True
            self.previousRoutine = " "
            self.actionList = [511, 511, 511, 511, "man"]
            self.motorState = [511, 511]
            GPIO.setmode(GPIO.BCM)
            self.remote = Remote.getInstance()
            self.mvcontroller = MovementController.getInstance()
            self.microphone = Microphone.getInstance()
            self.beatCount = 0
            self.prevLow = 0
            self.forward = 0
            try:  # try catch so the program can still run if the camera is not plugged in
                self.objDetector = ObjectDetector.getInstance()
            except Exception:
                self.cameraBool = False
                pass
            try:  # try catch so the program can still run if the camera is not plugged in
                self.ledStrip = LedStrip(16, 19)
            except Exception:
                self.ledBool = False
                pass

            self.prevLowToneValue = 0
Exemplo n.º 7
0
    def __init__(self):
        super().__init__()

        self.ImageReceiver = ImageReceiver()
        self.ObjectDetector = ObjectDetector()
        # self.AlertProcessor = AlertProcessor()

        self.ImageQueue = Queue()
        # self.ObjectQueue = Queue()

        self.objects = []
Exemplo n.º 8
0
    def __init__(self, imageTopic, segmentedTopic):
        self.imageTopic = imageTopic
        self.segmentedTopic = segmentedTopic
        self.imageSubscriber = rospy.Subscriber(imageTopic,
                                                Image,
                                                callback=self.callback)
        self.cvbridge = CvBridge()
        self.imagePublisher = rospy.Publisher(segmentedTopic,
                                              Image,
                                              queue_size=100)
        self.backgroundSegmentator = BackgroundSegmentator()

        self.objectDetector = ObjectDetector(filters=[MinimumAreaFilter(600)])

        self.objectTracker = ObjectTracker()
Exemplo n.º 9
0
 def __init__(self, ledstrip):
     self.microphone = Microphone.getInstance()
     self.mvcontroller = MovementController.getInstance()
     self.objdetector = ObjectDetector.getInstance()
     self.ledStrip = ledstrip
     self.beatCount = 0
     self.prevLow = 0
     self.forward = False
     self.ledBool = True
     self.motorState = [511, 511]
     try:
         self.ledStrip.setColor(Color(0, 0, 0))
     except Exception:
         self.ledBool = False
         pass
Exemplo n.º 10
0
def transform_image(image,duration=30, dummy_image = './foo.png', temp_file = 'temp'):
    """
        Given a image file it returns the corresponding Signal processor object with 
        the segments already detected. Once the image is loaded (and assuming it doesn't have any noise)
        the function should return an object to get the desired features. 
    """
    o_detector = None
    if isinstance(image,str):
        o_detector = ObjectDetector(image)
    elif isinstance(image, np.ndarray):
        o_detector = ObjectDetector(dummy_image)
        o_detector.img_src = image
    
    transformation_image = ImageSignalDigitalizer.bw_transform_special(o_detector.img_src)
    coordinates = o_detector.get_super_cluster(transform_image)
    retriever_object = SignalRetriever(array = coordinates, arr_name = temp_file)
    retriever_object.get_record_signal()

    processor = SignalProcessor(temp_file)
    processor.detect_segments_new()
    return processor
class VisionNode:
    """
    Object which will contains all ROS Publishers & Services relate to image processing
    """
    def __init__(self):
        rospy.logdebug("Vision Node - Entering in Init")
        # -- ROS
        self.__path_package = rospkg.RosPack().get_path('niryo_robot_vision')
        self.__simulation_mode = rospy.get_param('~simulation_mode')

        # PUBLISHERS
        self.__publisher_compressed_stream = rospy.Publisher(
            '~compressed_video_stream', CompressedImage, queue_size=1)

        # OBJECT DETECTION
        rospy.Service('~obj_detection_rel', ObjDetection,
                      self.__callback_get_obj_relative_pose)

        # CALIBRATION
        if not self.__simulation_mode:
            self.__calibration_object = self.__generate_calib_object_from_setup(
            )
        else:
            self.__calibration_object = self.__generate_calib_object_from_gazebo_topic(
            )

        self.__camera_intrinsics_publisher = rospy.Publisher(
            '~camera_intrinsics', CameraInfo, latch=True, queue_size=1)
        self.publish_camera_intrinsics()
        rospy.logdebug("Vision Node - Camera Intrinsics published !")

        # Debug features
        rospy.Service('~take_picture', TakePicture,
                      self.__callback_take_picture)

        self.__debug_compression_quality = rospy.get_param(
            "~debug_compression_quality")
        rospy.Service('~debug_markers', DebugMarkers,
                      self.__callback_debug_markers)
        rospy.Service('~debug_colors', DebugColorDetection,
                      self.__callback_debug_color)

        # -- VIDEO STREAM
        rospy.logdebug("Vision Node - Creating Video Stream object")
        cls_ = GazeboStream if self.__simulation_mode else WebcamStream
        self.__video_stream = cls_(self.__calibration_object,
                                   self.__publisher_compressed_stream)
        rospy.logdebug("Vision Node - Video Stream Created")

        self.__video_stream.start()

        # Set a bool to mentioned this node is initialized
        rospy.set_param('~initialized', True)

        rospy.loginfo("Vision Node - Initialized")

    def __generate_calib_object_from_setup(self):
        calibration_object_name = rospy.get_param("~obj_calib_name")

        path_yaml = os.path.join(
            self.__path_package,
            "config/{}.yaml".format(calibration_object_name))
        if not os.path.isfile(path_yaml):
            rospy.logwarn(
                "Vision Node - Intrinsics file '{}' does not exist".format(
                    calibration_object_name))
            return CalibrationObject.set_empty()
        with open(path_yaml, "r") as input_file:
            yaml_file = yaml.load(input_file)
        return CalibrationObject.set_from_yaml(yaml_file)

    @staticmethod
    def __generate_calib_object_from_gazebo_topic():
        camera_info_message = rospy.wait_for_message(
            "/gazebo_camera/camera_info", CameraInfo)
        mtx = np.reshape(camera_info_message.K, (3, 3))
        dist = np.expand_dims(camera_info_message.D, axis=0)
        return CalibrationObject.set_from_values(mtx, dist)

    def publish_camera_intrinsics(self):
        mtx, dist = self.__calibration_object.get_camera_info()

        msg_camera_info = CameraInfo()
        msg_camera_info.K = list(mtx.flatten())
        msg_camera_info.D = list(dist.flatten())
        return self.__camera_intrinsics_publisher.publish(msg_camera_info)

    # - CALLBACK
    def __callback_get_obj_relative_pose(self, req):
        # Reading last image
        img = self.__video_stream.read_undistorted()
        if img is None:
            rospy.logwarn(
                "Vision Node - Try to get object relative pose while stream is not running !"
            )
            return CommandStatus.VIDEO_STREAM_NOT_RUNNING, ObjectPose(
            ), "", "", CompressedImage()

        # Extracting parameters from request
        obj_type = ObjectType[req.obj_type]
        obj_color = ColorHSV[req.obj_color]
        workspace_ratio = req.workspace_ratio
        ret_image = req.ret_image

        # Creating ObjectDetector, an object for object detection
        self.__object_detector = ObjectDetector(
            obj_type=obj_type,
            obj_color=obj_color,
            workspace_ratio=workspace_ratio,
            ret_image_bool=ret_image,
        )

        # Launching pipeline
        status, msg_res_pos, obj_type, obj_color, im_draw = self.__object_detector.extract_object_with_hsv(
            img)
        if self.__object_detector.should_ret_image():
            _, msg_img = generate_msg_from_image(
                im_draw, compression_quality=self.__debug_compression_quality)
        else:
            msg_img = CompressedImage()
        return status, msg_res_pos, obj_type, obj_color, msg_img

    def __callback_take_picture(self, req):
        path = os.path.expanduser(req.path)
        if not os.path.isdir(path):
            os.makedirs(path)
        time_string = time.strftime("%Y%m%d-%H%M%S")

        img_full_path = "{}.jpg".format(os.path.join(path, time_string))
        im = self.__video_stream.read_raw_img()
        res_bool = cv2.imwrite(img_full_path, im)

        if res_bool:
            rospy.logdebug("Vision Node - Picture taken & saved")
        else:
            rospy.logwarn("Vision Node - Cannot save picture")

        return res_bool

    def __callback_debug_markers(self, _):
        img = self.__video_stream.read_undistorted()
        if img is None:
            rospy.logwarn_throttle(
                2.0,
                "Vision Node - Try to get debug markers while stream is not running !"
            )
            return False, CompressedImage()
        markers_detected, img_res = debug_markers(img)
        _, msg_img = generate_msg_from_image(
            img_res, compression_quality=self.__debug_compression_quality)

        return markers_detected, msg_img

    def __callback_debug_color(self, req):
        img = self.__video_stream.read_undistorted()
        if img is None:
            rospy.logwarn_throttle(
                2.0,
                "Vision Node - Try to get debug colors while stream is not running !"
            )
            return CompressedImage()
        color = ColorHSV[req.color]
        img_res = debug_threshold_color(img, color)
        _, msg_img = generate_msg_from_image(
            img_res, compression_quality=self.__debug_compression_quality)

        return msg_img
class SalienceRecognizer:
    EMOTION_PROB_THRESH = 0
    DELAY_TO_DETECT_IN_SECS = 5
    FONT = cv2.FONT_HERSHEY_SIMPLEX

    def __init__(self):
        self.output_video_file_name = '/home/algernon/samba/video_queue/SalienceRecognizer/videos/processed/output2.mkv'
        self.emotion_recognizer = EmotionRecognizer(self.EMOTION_PROB_THRESH)

        self.segmentator = None
        # self.clothes_detector = ClothesDetector("yolo/df2cfg/yolov3-df2.cfg", "yolo/weights/yolov3-df2_15000.weights", "yolo/df2cfg/df2.names")
        self.video_file_name = '/home/algernon/Videos/source_videos/interview_anna.webm'
        self.captioner = Captioner()
        self.video_reader = VideoReader(self.video_file_name)
        self.joke_picker = JokePicker('joke_picker/shortjokes.csv',
                                      'joke_picker/joke_picker.fse')
        self.video_writer = cv2.VideoWriter(
            self.output_video_file_name, cv2.VideoWriter_fourcc(*"XVID"),
            self.video_reader.fps,
            (self.video_reader.width, self.video_reader.height))
        self.face_recognizer = FaceRecognizer()

        self.segmentator = SceneSegmentator(self.video_reader.fps *
                                            self.DELAY_TO_DETECT_IN_SECS)
        self.object_detection_reader = DetectionReader('detections.json')
        self.object_detector = ObjectDetector(
            './configs/COCO-Detection/faster_rcnn_R_101_FPN_3x.yaml',
            'https://dl.fbaipublicfiles.com/detectron2/COCO-Detection/faster_rcnn_R_101_FPN_3x/137851257/model_final_f6e8b1.pkl'
        )
        self.age_gender_predictor = AgeGenderPredictor()
        self.beauty_estimator = BeautyEstimator(
            '/home/algernon/DNNS/isBeauty/weights/epoch_50.pkl')
        self.main_person_definer = MainPersonDefiner()
        self.context_generator = ContextGenerator(self.object_detector.classes)
        self.speech_recognizer = SpeechRecognizer(self.video_file_name)

    def launch(self):
        for _ in trange(self.video_reader.frame_count):
            frame = self.video_reader.get_next_frame()

            frame_for_detection = self.get_clearest_frame(frame)
            use_prev_detects = True if frame_for_detection is None else False

            speech = self.speech_recognizer.rewind_audio(
                self.video_reader.get_cur_timestamp())

            if not use_prev_detects:
                # faces
                detected_faces = self.face_recognizer.recognize_faces_on_image(
                    frame_for_detection, get_prev=use_prev_detects)

            # main person
            main_person_index = self.main_person_definer.get_main_person_by_face_size(
                detected_faces, get_prev=use_prev_detects)

            # emotions
            emotion_detections = self.emotion_recognizer.detect_emotions_on_frame(
                frame_for_detection, detected_faces, get_prev=use_prev_detects)

            emotion = [emotion_detections[main_person_index]
                       ] if main_person_index is not None else []

            # age gender
            age_gender_predictions = self.age_gender_predictor.detect_age_dender_by_faces(
                frame_for_detection, detected_faces, get_prev=use_prev_detects)
            age_gender_prediction = [
                age_gender_predictions[main_person_index]
            ] if main_person_index is not None else []
            # beauty
            beauty_scores = self.beauty_estimator.estimate_beauty_by_face(
                frame_for_detection, detected_faces, get_prev=use_prev_detects)
            beauty_score = [beauty_scores[main_person_index]
                            ] if main_person_index is not None else []
            # clothes
            # clothes_detections = self.clothes_detector.detect_clothes(frame)
            # clothes_detections = []
            # self.draw_clothes(frame, clothes_detections)

            # caption
            # caption = self.captioner.caption_img(frame_for_detection, get_prev=use_prev_detects)

            # objects
            object_detections = self.object_detector.forward(
                frame_for_detection, get_prev=use_prev_detects)
            # object_detections = self.object_detection_reader.get_detections_per_specified_frame(cur_frame_num)
            # context = self.context_generator.generate_context(object_detections, caption, emotion, age_gender_prediction,
            #                                                   beauty_score, get_prev=use_prev_detects)
            # cv2.putText(frame, 'Context:', (0, 360), cv2.FONT_HERSHEY_SIMPLEX, 0.8, Color.GOLD, 2)
            # cv2.putText(frame, context, (0, 400), cv2.FONT_HERSHEY_SIMPLEX, 0.8, Color.GOLD, 2)
            # jokes = self.joke_picker.pick_jokes_by_context(context, get_prev=use_prev_detects)

            # self.apply_jokes_on_frame(jokes, frame)
            self.apply_emotions_on_frame(frame, emotion_detections)
            self.apply_beauty_scores_on_frame(frame, detected_faces,
                                              beauty_scores)
            self.apply_age_gender_on_frame(frame, detected_faces,
                                           age_gender_predictions)
            # self.apply_caption_on_frame(frame, caption)
            frame = self.object_detector.draw_boxes(frame, object_detections)

            cv2.imshow('frame', frame)
            self.video_writer.write(frame)
            cv2.waitKey(1)

    def apply_age_gender_on_frame(self, frame, faces, age_gender_predictions):
        for i, face in enumerate(faces):
            tl_x, tl_y = face[i]
            cv2.putText(frame, f'{age_gender_predictions[i]}',
                        (tl_x - 5, tl_y + 60), self.FONT, 1, Color.BLACK, 2)

    def apply_beauty_scores_on_frame(self, frame, faces, beauty_scores):
        for i, face in enumerate(faces):
            tl_x, tl_y = face[i]
            cv2.putText(
                frame,
                f'{ContextGenerator.beauty_score2desc(beauty_scores[i])} ({beauty_scores[i]})',
                (tl_x - 5, tl_y + 30), self.FONT, 1, Color.BLACK, 2)

    def apply_jokes_on_frame(self, jokes, frame):
        height = frame.shape[0]
        joke_height = 40
        joke_y = height - joke_height * len(jokes)
        for joke in jokes:
            cv2.putText(frame, joke, (0, joke_y), cv2.FONT_HERSHEY_SIMPLEX,
                        0.8, Color.GOLD, 2)
            joke_y += joke_height

    def apply_emotions_on_frame(self, frame, emotion_detections):
        for emotion_pos, emotion in emotion_detections:
            self.draw_emotion_box(frame, emotion_pos, emotion)

    def get_clearest_frame(self, frame):
        self.segmentator.push_frame(frame)
        most_clear_img = self.segmentator.get_most_clear_frame()

        return most_clear_img

    def apply_caption_on_frame(self, frame, caption):
        cv2.putText(frame, caption, (0, 30), cv2.FONT_HERSHEY_SIMPLEX, 1,
                    Color.BLUE, 2)

    def draw_clothes(self, frame, clothes_detections):
        # clothes_detections: [[label: str, ((x1, y1), (x2, y2)), prob], ..]
        color = Color.BLACK
        for label, ((x1, y1), (x2, y2)), prob in clothes_detections:
            text = f'{label} ({prob}%)'
            cv2.rectangle(frame, (x1, y1), (x2, y2), color, 3)
            cv2.rectangle(frame, (x1 - 2, y1 - 25), (x1 + 8.5 * len(text), y1),
                          color, -1)
            cv2.putText(frame, text, (x1, y1 - 5), self.FONT, 0.5, color.WHITE,
                        1, cv2.LINE_AA)

    def draw_emotion_box(self, frame, emotion_pos, emotion: list):

        cv2.rectangle(frame, *emotion_pos, Color.GOLD, 2)
        cv2.putText(frame, f'{emotion[0]} - {emotion[1]}',
                    (emotion_pos[0][0], emotion_pos[0][1] - 5), self.FONT, 1,
                    Color.BLACK, 3)

    def is_rect_inside_rect(self, in_rect: tuple, out_rect: tuple):
        lt_in_box_point_inside_out_box = all([
            out_rect[0][i] <= in_rect[0][i] <= out_rect[1][i] for i in range(2)
        ])
        rb_in_box_point_inside_out_box = all([
            out_rect[0][i] <= in_rect[0][i] <= out_rect[1][i] for i in range(2)
        ])
        return lt_in_box_point_inside_out_box and rb_in_box_point_inside_out_box

    # def generate_video_overlay(self, command: Command, coords: tuple):
    #     video_cap = cv2.VideoCapture(command.media.file_name)
    #     duration = command.media.duration if command.duration == 0 else command.duration
    #     return VideoOverlay(video_cap, duration, coords, self.video_reader.one_frame_duration)
    #
    # def generate_image_overlay_object(self, command: Command, coords: tuple):
    #     image = cv2.imread(command.media.file_name)
    #     return ImageOverlay(image, command.duration, coords, self.video_reader.one_frame_duration)
    #
    # def generate_text_overlay_object(self, command: Command, coords: tuple):
    #     texts = self.read_text_from_file(command.media.file_name)
    #     ellipse, text_rect = generate_thought_balloon_by_text(texts)
    #     return TextOverlay((ellipse, text_rect), command.duration, coords, self.video_reader.one_frame_duration)

    # def read_text_from_file(self, txt_file):
    #     with open(txt_file) as txt:
    #         texts = txt.readlines()
    #     return texts

    def close(self):
        if self.video_reader:
            self.video_reader.close()
        if self.video_writer:
            self.video_writer.release()
Exemplo n.º 13
0
            print Exception, err
            return -1


def on_exit(signal, frame=None):
    print('You pressed Ctrl+C!')
    r = requests.post('http://' + serverIP + '/api/worker/remove',
                      params={'desc': 'Server with TITAN X GPU'})
    if (r.status_code == 200):
        print "OK: Removing from server"
    else:
        print "Fail: Removing from server"
    sys.exit(0)


serverIP = '192.168.1.126'
if __name__ == "__main__":
    r = requests.post('http://' + serverIP + '/api/worker/register',
                      params={'desc': 'Server with TITAN X GPU'})
    if (r.status_code == 200):
        print "OK: Registering"
        s = zerorpc.Server(ImageProcessingWorker(), heartbeat=20)
        s.connect("tcp://" + serverIP + ":3000")
        gevent.signal(signal.SIGINT, on_exit)
        #win32api.SetConsoleCtrlHandler(on_exit, 1)
        print "Start server"
        objDetector = ObjectDetector('VGG16')
        s.run()
    else:
        print "Fail: Registering"
Exemplo n.º 14
0
from flask import Flask, request, render_template
from werkzeug.exceptions import abort
import cv2
import numpy as np
import sys
import base64
import json
import time
import os

pseudo = False
if not pseudo:
    sys.path.append(os.path.join(os.path.dirname(__file__), 'yolov5'))
    from ObjectDetector import ObjectDetector
    from conversion_utils import closest_detection, detection_center
    detection_model = ObjectDetector('./yolov5/weights/yolov5m.pt')

app = Flask(__name__)

# 独自学習させた判別器


class NpEncoder(json.JSONEncoder):
    """ Numpyオブジェクトを含むオブジェクトのJsonエンコーダ """
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        elif isinstance(obj, np.floating):
            return float(obj)
        elif isinstance(obj, np.ndarray):
            return obj.tolist()
Exemplo n.º 15
0
import tensorflow as tf
import numpy as np
import os.path as osp
import sys
import gc
from Network import Network
from config import cfg
from Imdb import Imdb
from ObjectDetector import ObjectDetector

if __name__ == '__main__':
    """
    tests a pretrained faster rcnn model on the coco test2017 dataset
    """

    sess = tf.Session(graph=tf.Graph())
    net = Network(cfg, False, sess)

    imdb = Imdb('/content/image_data', 'test2017', cfg)
    od = ObjectDetector(net, imdb, cfg)
    od.evaluate(
        '/content/gdrive/My Drive/Faster-R-CNN/results/coco_results.json',
        True)
Exemplo n.º 16
0
class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Image Detection", font=controller.title_font)
        label.pack(side="top", fill="x", padx=30, pady=20)

        f = Frame(self, height=3, width=1000, bg="white")
        f.pack()
        button = tk.Button(self, text="Return to main menu",
                           command=lambda: controller.show_frame("StartPage"), padx=20, pady=10)
        button.pack()
        button2 = tk.Button(self, text="New image", command=lambda: self.detectObjectsImage(), padx=20,
                            pady=10)
        button2.pack()
        f = Frame(self, height=3, width=1000, bg="white")
        f.pack()


        self.od = ObjectDetector()
        self.od.detectOcjectsFromImagesSetup()


    def detectObjectsImage(self):
        self.od = ObjectDetector()
        self.od.detectOcjectsFromImagesSetup()

        tk.Tk().withdraw()
        self.file_path = filedialog.askopenfilename()
        if self.file_path == "":
            return
        image = Image.open(self.file_path)
            # the array based representation of the image will be used later in order to prepare the
            # result image with boxes and labels on it.
        image_np = self.od.load_image_into_numpy_array(image)
            # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
        image_np_expanded = np.expand_dims(image_np, axis=0)
            # Actual detection.
        output_dict = self.od.run_inference_for_single_image(image_np_expanded, self.od.detection_graph)
            # Visualization of the results of a detection.
        vis_util.visualize_boxes_and_labels_on_image_array(
            image_np,
            output_dict['detection_boxes'],
            output_dict['detection_classes'],
            output_dict['detection_scores'],
            self.od.category_index,
                instance_masks=output_dict.get('detection_masks'),
                use_normalized_coordinates=True,
                line_thickness=8)

        plt.cla()
        plt.clf()
        plt.close(fig='all')

        plt.close(plt.gcf())
        plt.figure(clear=True)
        fig = plt.figure(figsize=self.od.IMAGE_SIZE)
        im = plt.imshow(image_np)
        plt.minorticks_off()
        ax = plt.gca()
        ax.set_xticklabels([])
        ax.set_yticklabels([])

        # a tk.DrawingArea
        try:
            self.canvas.get_tk_widget().pack_forget()
        except AttributeError:
            pass
        f = Figure(figsize=(5, 1))
        aplt = f.add_subplot(111)
        aplt.plot([1, 2, 3, 4])
        self.canvas = FigureCanvasTkAgg(fig, master=self)
        self.canvas.draw()
        self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
import os
import json
import yaml
import asyncio
from copy import deepcopy
import base64

import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import paho.mqtt.subscribe as subscribe

from ObjectDetector import ObjectDetector

det = ObjectDetector()

# consts
config_path = 'obj-det-mapper.yaml'

# configs
device_id = os.environ['DEVICE_ID']

with open(config_path, 'r') as config_file:
    config = yaml.full_load(config_file)

    mqtt_broker_host = config['mqtt_broker_host']
    mqtt_broker_port = config['mqtt_broker_port']
    sync_interval = config['sync_interval']

# deep copy the following templates and fill actual values
# to create request body in sync_twin()
Exemplo n.º 18
0
from ObjectDetector import ObjectDetector
from rplidar import RPLidar
import time

obd = ObjectDetector(ObjectDetector.gen_segments(
    right = ObjectDetector.segment_range(30, 90),
    front = ObjectDetector.segment_range(-30, 30),
    left =  ObjectDetector.segment_range(-90, -30)
))

try:
    lidar = RPLidar('/dev/ttyUSB0')

    print('Initializing')
    time.sleep(5)
    print('Collecting data')
    
    for data in lidar.iter_measurments(max_buf_meas=800):
        if obd.update(data):
            print(obd.detect())
    
except KeyboardInterrupt:
    pass
    
lidar.stop()
lidar.stop_motor()
lidar.disconnect()
Exemplo n.º 19
0
        pass

    vid = VideoGetter(source, get_latest=type(source) is int or "rtsp://" in source.lower())
    writer = None
    msg = Array('B', range(100))
    file_path = Array('B', range(100))
    lock = Lock()

    if args.output_folder:
        writer = ImageWriter(args.output_folder)

    if not vid.isOpened():
        print(f"Can't open video/stream at link '{args.source}'")
        exit(0)

    detector = ObjectDetector(args.weight, args.config)
    thresh_tracking = Thread(target=tracking, args=(args, vid, detector, writer, msg, file_path, lock))
    thresh_tracking.start()

    @app.route("/preview")
    def view_alert():
        def yield_msg():
            while 1:
                if not msg.value:
                    time.sleep(0.5)
                    continue

                http_link = f"http://{args.host}:{args.port}/capture/{file_path.value}"
                yield f"{msg.value} <a href='{http_link}' target='_blank'>{http_link}</a><br>"
                msg.value = ""
        return Response(yield_msg())
Exemplo n.º 20
0
import tensorflow as tf
import numpy as np
import os
import os.path as osp
import sys
import gc
from Network import Network
from config import cfg
from Imdb import Imdb
from ObjectDetector import ObjectDetector

if __name__ == '__main__':
    """
    trains the faster rcnn model on the coco train2017 dataset 

    """
    sess = tf.Session(graph=tf.Graph())
    net = Network(cfg, True, sess)

    imdb = Imdb('/content/image_data', 'train2017', cfg)
    od = ObjectDetector(net, imdb, cfg)
    od.train()