Exemplo n.º 1
0
    def __init__(self, **kwargs):

        super(Editor, self).__init__(**kwargs)
        self.cam_show = True
        self.detector = Detector()
        self.img1 = Image(size_hint=(1.0, 0.8))
        self.showData = ShowData()
 def __init__(self):
     self.detector = Detector()
     self.model = TrainNetwork().load_trained_model()
     self.cap = cv2.VideoCapture(0)
     if not self.cap.isOpened():
         print('Webcam is not open.')
         exit()
    def __init__(self):
        super(PeopleObjectDetectionNode, self).__init__()

        # init the node
        rospy.init_node('people_object_detection', anonymous=False)

        # Get the parameters
        (model_name, num_of_classes, label_file, camera_namespace,
         video_name) = self.get_parameters()

        # Create Detector
        self._detector = Detector(model_name, num_of_classes, label_file)

        self._bridge = CvBridge()

        # Advertise the result of Object Detector
        self.pub_detections = rospy.Publisher(
            'people_object_detection/detections', DetectionArray, queue_size=1)

        # Advertise the result of Object Detector
        self.pub_detections_image = rospy.Publisher(
            'people_object_detection/detections_image', Image, queue_size=1)

        self.read_from_video()
        # spin
        rospy.spin()
Exemplo n.º 4
0
def main(video=0, dowsample=True):
    print('QUAD Vision, Python:', version, '  OpenCV:', cv2.__version__)
    vid = Video(video,
                'ORB Detection testing',
                delay=1,
                fast=False,
                skip=20,
                downsample=downsample)
    detector = Detector()

    if ros_mode:
        import rospy
        from sensor_msgs.msg import Image
        from cv_bridge import CvBridge, CvBridgeError
        from quad.msg import Target_coordinates

        pub_detection = rospy.Publisher('detection',
                                        Target_coordinates,
                                        queue_size=5)
        pub_frame = rospy.Publisher('annotated_frame',
                                    msg.image,
                                    queue_size=15)
        rospy.init_node('detection', anonymous=True)
        rospy.init_node('annotated_frame', anonymous=True)

    # Detection profiles
    detector.load_profile(profiles.idea_orange)
    # detector.load_profile(profiles.quadbox_black)
    # detector.load_profile(profiles.quadbox_white)
    # detector.load_profile(profiles.idea_red)
    # detector.load_profile(profiles.sim_drone)

    vid.skip(20)
    t = Timer('Detection')

    while vid.next():
        frame = vid.img

        t.start()  # start timer
        img, confidence, x, y = detector.detect(frame)
        print('Detection:', confidence, x, y)
        t.end()
        vid.display(img)

        if ros_mode:
            detection = Target_coordinates()
            detection.confidence = confidence
            detection.x = x
            detection.y = y
            try:
                pub_detection.publish(detection)
            except CvBridgeError as e:
                print(e)
            # ros_img = CvBridge.cv2_to_imgmsg(img, encoding="passthrough")
            # pub_frame.publish(ros_img)

    vid.close()
    t.output()
Exemplo n.º 5
0
def analyze_mic_data():
    from detection import Detector
    detector = Detector()

    def callback(pred):
        # idx,conf = pred
        if pred > 0.6:
            print(pred, 'what up')

    detector.start(callback)
Exemplo n.º 6
0
def extract_regions_in_folder(img_names, img_folder, output_folder):
    not_found_face = 0
    detector = Detector()
    for img_name in tqdm(img_names):
        cropped_img = detector.get_eye_area_roi(os.path.join(img_folder, img_name))
        if cropped_img is not False:
            cv2.imwrite(str(output_folder/img_name), cropped_img, [int(cv2.IMWRITE_JPEG_QUALITY), 90])
        else:
            not_found_face += 1
    print('The detector could not detect faces in {} images'.format(not_found_face))
    return
Exemplo n.º 7
0
 def __init__(self):
     cv2.setNumThreads(4)
     self.weathercock_id = 17
     roe = rospy.get_param(
         "~roe", {
             'min': {
                 "x": -4,
                 "y": -4,
                 "t": -3.14
             },
             'max': {
                 "x": 4,
                 "y": 4,
                 "t": 6.292
             }
         })
     self.weathercock_stabilisation_time = rospy.get_param(
         "~weathercock_stabilisation_time", 30)
     self.roe_min = [roe["min"]["x"], roe["min"]["x"], roe["min"]["t"]]
     self.roe_max = [roe["max"]["x"], roe["max"]["y"], roe["max"]["t"]]
     self.d = Detector.Detector()
     rospy.set_param('isWeathercockSouth', False)
     self.is_set = False
     # subscribed Topic
     self.img_topic = "camera/image_raw/compressed"
     self.remaining_time_topic = "/remaining_time"
     self.pose_subscriber = rospy.Subscriber("odom",
                                             Odometry,
                                             callback=self.callback,
                                             queue_size=1)
Exemplo n.º 8
0
 def __init__(self):
     self.p = Plateau.Plateau((600, 400), 'resources/img', 200, 42,
                              np.array([[300, 250, 0]]))
     self.f = PinholeFrame.PinholeFrame(
         id_=1, parameters='resources/parameters_webcam_victor.txt')
     self.d = Detector.Detector()
     self.Rref, self.Tref = None, None
     self.initialized = False
Exemplo n.º 9
0
def main():

    args = parse_args()
    det = Detector(model_path = args.model_path, model=args.model_type, ctx=args.ctx)
    if args.output_path:
        out_file = args.output_path
    else:
        out_file = pjoin(cfg.data_folder, 'videos', f'{args.model_type}_{args.th}.mp4')
    predict_frames(args.input_file, out_file, det, th=args.th)
Exemplo n.º 10
0
class TLD_IVMIT:
    def __init__(self, frame, window, init_frames_count = 20):
        self.buffer = [cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)]
        self.position = Position(self.buffer, *window)
        self.learning_component = LearningComponent(self.position.calculate_patch())
        self.detector = Detector(self.position, self.learning_component)
        self.tracker = Tracker(self.position)
        self.is_visible = True
        self.integrator = Integrator(self.learning_component)
        self.init_frames_count = init_frames_count
        self.detected_windows = None
        self.tracked_window = None

    def start(self, frame):
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        if self.init_frames_count == 0:
            start = time()
            self.tracked_window = self.tracker.track(frame, self.position)
            self.buffer[0] = frame
            print "Tracking:", time()- start

            start = time()
            self.detected_windows = self.detector.detect(self.position, self.tracked_window is not None)
            print "Detected windows count:", len(self.detected_windows)
            print "Detection:", time()- start

            start = time()
            # filtered_detected_windows = [(window, patch, proba) for window, patch, proba in self.detected_windows if proba > 0.7]
            single_window, self.is_visible = self.integrator.get_single_window(self.position, self.detected_windows, self.tracked_window)
            print "Integration:", time()- start

            if self.is_visible:
                self.position.update(*single_window)
            # start = time()
            # self.learning_component.n_expert()
            # self.learning_component.p_expert()
            # print "Update training set:", time()- start
        else:
            self.tracked_window = self.tracker.track(frame, self.position)
            self.buffer[0] = frame
            if self.tracked_window is not None:
                i = 0
                while i < 5:
                    self.position.update(x=np.random.randint(0,self.buffer[0].shape[1]-self.position.width))
                    if self.position.is_correct() and windows_intersection(self.position.get_window(), self.tracked_window) == 0:
                        self.learning_component.update_negatives(self.position.calculate_patch())
                        i += 1

                self.position.update(*self.tracked_window)
                self.learning_component.update_positives(self.position.calculate_patch())

                self.init_frames_count -= 1
            else:
                self.init_frames_count = 0
                self.is_visible = False

        return self.position
Exemplo n.º 11
0
    def __init__(self):
        """
        Class to run HSR lego task

        """
        self.robot = hsrb_interface.Robot()
        self.rgbd_map = RGBD2Map()

        self.omni_base = self.robot.get('omni_base')
        self.whole_body = self.robot.get('whole_body')

        self.side = 'BOTTOM'

        self.cam = RGBD()
        self.com = COM()

        self.com.go_to_initial_state(self.whole_body)

        self.grasp_count = 0
        self.helper = Helper(cfg)

        self.br = tf.TransformBroadcaster()
        self.tl = TransformListener()

        self.gp = GraspPlanner()
        self.gripper = Crane_Gripper(self.gp, self.cam, self.com.Options,
                                     self.robot.get('gripper'))
        self.suction = Suction_Gripper(self.gp, self.cam, self.com.Options,
                                       self.robot.get('suction'))

        self.gm = GraspManipulator(self.gp, self.gripper, self.suction,
                                   self.whole_body, self.omni_base, self.tl)

        self.dl = DataLogger("stats_data/model_base", cfg.EVALUATE)

        self.web = Web_Labeler(cfg.NUM_ROBOTS_ON_NETWORK)

        model_path = 'main/output_inference_graph.pb'
        label_map_path = 'main/object-detection.pbtxt'
        self.det = Detector(model_path, label_map_path)

        print "after thread"
Exemplo n.º 12
0
 def __init__(self, frame, window, init_frames_count = 20):
     self.buffer = [cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)]
     self.position = Position(self.buffer, *window)
     self.learning_component = LearningComponent(self.position.calculate_patch())
     self.detector = Detector(self.position, self.learning_component)
     self.tracker = Tracker(self.position)
     self.is_visible = True
     self.integrator = Integrator(self.learning_component)
     self.init_frames_count = init_frames_count
     self.detected_windows = None
     self.tracked_window = None
Exemplo n.º 13
0
def main():

    args = parse_args()
    det = Detector(model_path=args.model_path, model=args.model_type, ctx=args.ctx)
    if args.output_path:
        out_file = args.output_path
    else:
        fn = os.path.basename(args.input_file)
        fn = fn.split('.')[0]
        out_file = f'{fn}_{args.model_type}_{args.th}.mp4'
    predict_frames(args.input_file, out_file, det, th=args.th)
Exemplo n.º 14
0
    def test_load(self):

        ava_datasets = Detector.list_datasets()
        for dataset_name in ava_datasets:
            models = Detector.list_models(dataset_name)
            for model_name in models:
                try:
                    classes = dcfg.classes
                    det = Detector(model=model_name,
                                   dataset=dataset_name,
                                   ctx='cpu',
                                   classes=classes)
                except:
                    classes = dcfg.classes_grasp
                    det = Detector(model=model_name,
                                   dataset=dataset_name,
                                   ctx='cpu',
                                   classes=classes)

        gdet = GDetector()
Exemplo n.º 15
0
    def add_extra_images(self):
        """
        If --extra flag exists, adds images from extra-images dir to dataset
        :return: tuple of list dataset images, dataset labels
        """

        detector = Detector()
        print('\nAdding extra images...')
        for emotion_index, emotion in enumerate(EMOTIONS):
            print('\nEmotion', emotion)
            files = glob.glob('{}\\{}\\*'.format(EXTRA_DIR, emotion))
            total = len(files)
            for i, f in enumerate(files):
                image = cv2.imread(f)
                detected_faces = detector.detect_faces(image)
                for face in detected_faces:
                    self.images.append(face)
                    self.emotions.append(
                        PrepareData._emotion_to_vec(emotion_index))
                    self.count.append(emotion_index)
                print('Progress: {}/{} {:.2f}%'.format(i, total,
                                                       i * 100.0 / total))
Exemplo n.º 16
0
def main():
    """Create a trained feature set.
        1. Extract features from a cropped image set
        2. Evaluate features on labelled training data
        3. Remove features with high false positive rate
        4. Evaluate features again on same data
    """

    trainer = Trainer()
    detector = Detector(750)
    trainer.set_detector(detector)
    # feat = load_features('data/gazebo1_932.dat')
    # trainer.set_features(feat)

    trainer.load_data('train/gazebo/pos_info_PreyEmptyWorld.dat')
    trainer.load_data('train/gazebo/pos_info_PurplePrey1.dat')
    trainer.load_data('train/gazebo/pos_info_PurplePrey2.dat')
    trainer.load_data('train/gazebo/sim2.dat')

    # trainer.load_data('train/quadbox/white_courtyard1.dat')
    # trainer.load_data('train/quadbox/white_courtyard2.dat')
    # trainer.load_data('train/quadbox/black_drone_court.dat')
    # trainer.load_data('train/quadbox/pos_info.txt')

    # trainer.load_data('train/idea2/pos_info_red-inclass.dat')
    # trainer.load_data('train/idea2/red2m.dat')

    # trainer.load_data('train/idea2/courtyard323.dat')
    # trainer.load_data('train/idea2/orange10am.dat')
    # trainer.load_data('train/idea2/orange7_30am.dat')
    # trainer.load_data('train/pos_info_Courtyard_multi.dat')
    # trainer.load_data('train/idea2/orange9am.dat')

    # trainer.subsample_data(2)
    trainer.train_and_test(.8)
    trainer.evaluate(1)
    trainer.feature_selection()
    # trainer.evaluate(subsample=0.4)
    # trainer.feature_selection()
    # trainer.evaluate(subsample=0.7)
    trainer.save_features('sim2')
    return

    trainer.train_and_test(show=False)
    trainer.feature_selection()
    trainer.evaluate(show=False)
    trainer.save_features('ir_2')

    return
Exemplo n.º 17
0
    def __init__(self):
        path_calibration_camera = rospy.get_param(
            "~calib_file", 'resources/parameters_fisheye_pi_simu.txt')
        rospy.loginfo(
            f"loading camera calibration from {path_calibration_camera}")
        self.f = FisheyeFrame.FisheyeFrame(id_=0,
                                           parameters=path_calibration_camera,
                                           balance=0.5)
        self.d = Detector.Detector()
        self.r_origin, self.t_origin = [[0, 0, 0]], [[0, 0, 0]]
        self.origin_id = 42
        self.initialized = False
        self.marker_sizes = {
            0: 0.07,
            1: 0.07,
            2: 0.07,
            3: 0.07,
            4: 0.07,
            5: 0.07,
            6: 0.07,
            7: 0.07,
            8: 0.07,
            9: 0.07,
            10: 0.07,
            42: 0.10
        }
        self.image_pub_undistort = rospy.Publisher(
            "aruco_to_pose/debug/undistort/compressed",
            CompressedImage,
            queue_size=1)
        self.robots_pose_pub = []
        for i in range(0, 11):
            self.robots_pose_pub.append(
                rospy.Publisher(f"/pose_robots/{i}", PoseStamped,
                                queue_size=1))

        # subscribed Topic
        self.img_subscriber = rospy.Subscriber("camera/image_raw/compressed",
                                               CompressedImage,
                                               self.callback,
                                               queue_size=1)
Exemplo n.º 18
0
        self.segmentation_text.set(
            '\nSegmentation:\n{:20}\n'.format(classlabel))

log.basicConfig(format="[ %(levelname)s ] %(message)s",
                level=log.INFO,
                stream=sys.stdout)
#prepare classifier pathes @@@ todo create a main method and hand it over or extend the gui
#expects the model and a label file there:
clabelspath = './openvino/labels.txt'
cmodelpath = './openvino/model_leaf_01.xml'

#slabelspath='./openvino/voc.names'
smodelpath = './openvino/model_leaf_segmentation_02.xml'

#dlabelspath='./openvino/voc.names'
dmodelpath = './openvino/model_leaf_detection_01.xml'

#needed on intel desktop
#device='CPU'
#cpu_extension='/opt/intel/openvino/inference_engine/samples/build/intel64/Release/lib/libcpu_extension.so'
#needed on raspberry + nsc2
device = 'MYRIAD'
cpu_extension = ''
#init the classifier
imagepath = "./leaf_test.jpg"
classifier = Classifier(cmodelpath, device, cpu_extension, clabelspath)
segmentationDetector = Segmentation(smodelpath, device, cpu_extension)
boundingboxDetector = Detector(dmodelpath, device, cpu_extension)

gui = Gui(classifier, segmentationDetector, boundingboxDetector, imagepath)
Exemplo n.º 19
0
class PeopleObjectDetectionNode(object):
    """docstring for PeopleObjectDetectionNode."""
    def __init__(self):
        super(PeopleObjectDetectionNode, self).__init__()

        # init the node
        rospy.init_node('people_object_detection', anonymous=False)

        # Get the parameters
        (model_name, num_of_classes, label_file, camera_namespace,
         video_name) = self.get_parameters()

        # Create Detector
        self._detector = Detector(model_name, num_of_classes, label_file)

        self._bridge = CvBridge()

        # Advertise the result of Object Detector
        self.pub_detections = rospy.Publisher(
            'people_object_detection/detections', DetectionArray, queue_size=1)

        # Advertise the result of Object Detector
        self.pub_detections_image = rospy.Publisher(
            'people_object_detection/detections_image', Image, queue_size=1)

        self.read_from_video()
        # spin
        rospy.spin()

    def read_from_video(self):

        video_name = -1
        cap = cv2.VideoCapture(video_name)

        while (cap.isOpened()):
            ret, frame = cap.read()

            if frame is not None:
                image_message = self._bridge.cv2_to_imgmsg(frame, "bgr8")
                #self._sub = rospy.Subscriber('image', Image, self.rgb_callback, queue_size=1)
                self.rgb_callback(image_message)

            else:
                break

        cap.release()
        cv2.destroyAllWindows()

        print("Video has been processed!")

        self.shutdown()

    def get_parameters(self):
        """
        Gets the necessary parameters from parameter server

        Args:

        Returns:
        (tuple) (model name, num_of_classes, label_file)

        """
        rospy.set_param("~model_name", 'ssd_mobilenet_v1_coco_11_06_2017')
        rospy.set_param("~num_of_classes", '90')
        rospy.set_param("~label_file", 'mscoco_label_map.pbtxt')
        rospy.set_param("~camera_namespace", 'no')
        rospy.set_param("~video_name", '-1')
        #rospy.set_param("~num_workers",'-1')
        model_name = rospy.get_param("~model_name")
        num_of_classes = rospy.get_param("~num_of_classes")
        label_file = rospy.get_param("~label_file")
        camera_namespace = rospy.get_param("~camera_namespace")
        video_name = rospy.get_param("~video_name")
        #num_workers = rospy.get_param("~num_workers")

        return (model_name, num_of_classes, label_file, \
                camera_namespace,video_name)

    def shutdown(self):
        """
        Shuts down the node
        """
        rospy.signal_shutdown("See ya!")

    def rgb_callback(self, data):
        """
        Callback for RGB images
        """
        try:
            # Convert image to numpy array
            cv_image = self._bridge.imgmsg_to_cv2(data, "bgr8")

            # Detect
            (output_dict, category_index) = self._detector.detect(cv_image)

            # Create the message
            msg = utils.create_detection_msg(data, output_dict, category_index,
                                             self._bridge)

            # Draw bounding boxes
            image_processed = self._detector.visualize(cv_image, output_dict)

            # Convert numpy image into sensor img
            msg_im = self._bridge.cv2_to_imgmsg(image_processed,
                                                encoding="passthrough")

            cv2.imshow("Image window", image_processed)
            cv2.waitKey(3)
            # Publish the messages
            self.pub_detections.publish(msg)
            self.pub_detections_image.publish(msg_im)

        except CvBridgeError as e:
            print(e)
Exemplo n.º 20
0
def multi_target_tracking(seq, figure=None, gen_dect=False):
    print('Sequence: %d' % (seq))
    velo_dir = os.path.join(ROOT_DIR, 'velodyne', '%04d' % seq)
    _, num_velo_files = load_list_from_folder(velo_dir)
    str_frame = 0
    num_frames = num_velo_files
    save_file_dir = os.path.join(ROOT_DIR, 'results', '%04d' % seq)
    save_img_dir = os.path.join(save_file_dir, 'img')
    mkdir_if_missing(save_file_dir)
    mkdir_if_missing(save_img_dir)
    # initial classes
    z_range = [5, -8]
    dist_range = [50, 2]
    roi_e = ROIExtractor(z_range[0], z_range[1], dist_range[0], dist_range[1])
    gd_seg = GroundSeg()
    ngc_detector = Detector()
    tracker = MOTracker()
    for frame in range(str_frame, str_frame + num_frames):
        print('frame: %d' % (frame))
        velo_file = os.path.join(velo_dir, '%06d.bin' % (frame))
        save_path = os.path.join(save_img_dir, '%06d.png' % (frame))
        dect_dir = os.path.join(save_file_dir, 'dect', '%06d' % (frame))
        mkdir_if_missing(dect_dir)
        gd_pc_file = os.path.join(dect_dir, 'gd_pc.npy')
        clusters_list_file = os.path.join(dect_dir, 'clusters_list.npy')
        ngd_other_file = os.path.join(dect_dir, 'ngd_other.npy')
        dects_file = os.path.join(dect_dir, 'dects.npy')
        flag = is_path_exists(gd_pc_file) & is_path_exists(
            clusters_list_file) & is_path_exists(
                ngd_other_file) & is_path_exists(dects_file)
        # generate / get detections at current frame
        if not flag or gen_dect:
            pc = load_velo_scan(velo_file)
            # region of interest extraction
            pc = roi_e.roi_extract(pc)
            # ground and non-ground segmentation
            gd_pc, ngd_pc = gd_seg.segment(pc)
            clusters_list, ngd_other = ngc_detector.clustering(ngd_pc,
                                                               filter=True)
            dect_boxes_2d = ngc_detector.bounding_box_2d(
                clusters_list, criterion='variance')  # [(4x2)]
            np.save(gd_pc_file, gd_pc)
            np.save(clusters_list_file, clusters_list)
            np.save(ngd_other_file, ngd_other)
            np.save(dects_file, dect_boxes_2d)
        else:
            # load detection results
            gd_pc = np.load(gd_pc_file)
            clusters_list = np.load(clusters_list_file, allow_pickle=True)
            clusters_list = list(clusters_list)
            ngd_other = np.load(ngd_other_file)
            dect_boxes_2d = np.load(dects_file)

        # tracking
        tracks = tracker.update(dect_boxes_2d)

        if figure != None:
            fig = mlab_show(figure,
                            gd_pc,
                            ngd_pc_=[clusters_list, ngd_other],
                            ngd_bb_2d=dect_boxes_2d,
                            tracks=tracks)
            mlab.savefig(save_path, figure=fig)
Exemplo n.º 21
0
from flask import Response
from flask import Flask
from flask import render_template
from threading import Thread, Lock
import datetime
import time
from queue import Queue
import cv2
from detection import Detector

app = Flask(__name__)
img_queue = Queue()
detector = Detector(img_queue)


@app.route('/')
@app.route('/index')
def index():
	return render_template('index.html')

@app.route('/video_feed')
def video_feed():
	return Response(pull_image(), mimetype="multipart/x-mixed-replace; boundary=frame")

def pull_image():
	while True:
		frame = img_queue.get()
		if frame is None:
			continue
			# encode the frame in JPEG format
		(flag, encodedImage) = cv2.imencode(".jpg", frame)
Exemplo n.º 22
0
 def __init__(self, Detector=Detector()):
     self.Detector = Detector
Exemplo n.º 23
0
class Editor(BoxLayout):
    def __init__(self, **kwargs):

        super(Editor, self).__init__(**kwargs)
        self.cam_show = True
        self.detector = Detector()
        self.img1 = Image(size_hint=(1.0, 0.8))
        self.showData = ShowData()

    def start_cam(self, src=0):
        self._cap = cv2.VideoCapture(src)
        while not self._cap.isOpened():
            pass
        self.cam_show = True
        print('cam started!')
        self.count = 0
        self.fps = 0
        self.start_time = time.time()

    def close_cam(self):
        self.cam_show = False
        self._cap.release()

    def update(self, dt):
        if self._cap.isOpened() is False:
            return

        _, img = self._cap.read()
        # get pose
        pose_img, send_pose = self.detector.detect(img)
        self.showData.set_text(send_pose)
        show_img = cv2.flip(pose_img, 0)
        # set texture
        texture1 = Texture.create(size=(show_img.shape[1], show_img.shape[0]),
                                  colorfmt='bgr')
        texture1.blit_buffer(show_img.tostring(),
                             colorfmt='bgr',
                             bufferfmt='ubyte')
        # show the results
        if self.cam_show:
            self.img1.texture = texture1
        else:
            self.img1.texture = None

        self.count += 1
        if self.count == 10:
            self.calc_fps()

    def calc_fps(self):
        self.fps = 10 / (time.time() - self.start_time)
        self.count = 0
        self.start_time = time.time()

    # event handle
    def connect_zmq(self, ip_id, port_id):
        self.detector.disconnect_zmq()
        ip = ip_id.text
        port = int(port_id.text)
        print('connect zmq')
        self.detector.connect_zmq(ip, port)

    def cam_load(self, src):
        print('cam load', src.text)
        self.close_cam()
        if src.text.find('mp4') != -1 or src.text.find('m4a') != -1:
            self.start_cam(src.text)
        else:
            self.start_cam(int(src.text))

    def toggle_cam_show(self, cb):
        print(cb.active)
        self.cam_show = cb.active

    def change_scale(self, k):
        rk = round(k.value, 2)
        self.detector.set_scale_factor(rk)
        print('scale changed to', rk)

    def change_pose_num(self, k):
        ik = int(k.value)
        self.detector.set_pose_num(ik)
        print('pose num changed to', ik)

    def change_min_pose_score(self, k):
        rk = round(k.value, 2)
        print('min pose score changed to', rk)

    def change_min_part_score(self, k):
        rk = round(k.value, 2)
        print('min part score changed to', rk)
Exemplo n.º 24
0
def real_data_generator(labels,
                        width,
                        height,
                        augmenter=None,
                        area_threshold=0.5,
                        ):
    labels = labels.copy()

    for index in itertools.cycle(range(len(labels))):
        image_filepath, lines = labels[index]
        image = tools.read(image_filepath)

        if augmenter is not None:
            image, lines = tools.augment(boxes=lines,
                                         boxes_format='lines',
                                         image=image,
                                         area_threshold=area_threshold,

                                         augmenter=augmenter)

        image, scale = tools.fit(image,
                                width=width,
                                 height=height,
                                 mode='letterbox',
                                 return_scale=True)

        lines = tools.adjust_boxes(boxes=lines, boxes_format='lines', scale=scale)
        confidence_mask = np.zeros((image.shape[0], image.shape[1]), np.float32)

        confidences = []
        # character_bboxes = np.array([]).reshape(0, 4, 2)
        # new_words = []
        lines_label = []

        detector = Detector()
        if len(lines)==1:
            lines = lines[0]
        for i, line in enumerate(lines):
            word_label = []
            word_bbox, word = line[0], line[1]
            word = word.replace(',', '')
            word_bbox = np.float32(word_bbox)
            if len(word_bbox) > 0:
                for _ in range(len(word_bbox)):
                    if word == '###' or len(word.strip()) == 0:

                        cv2.fillPoly(confidence_mask, [np.int32(word_bbox)], (0))
            pursedo_bboxes, bbox_region_scores, confidence = inference_character_box(detector,
                                                                                     image,
                                                                                     word,
                                                                                     word_bbox)
            confidences.append(confidence)
            cv2.fillPoly(confidence_mask, [np.int32(word_bbox)], (confidence))
            for j in range(len(pursedo_bboxes)):
                if j>len(word)-1:
                    continue
                word_label.append((pursedo_bboxes[j], word[j]))
            lines_label.append(word_label)
            # new_words.append(word)
            # character_bboxes = np.concatenate((character_bboxes, pursedo_bboxes), 0)
            # character_bboxes.append(pursedo_bboxes)

        yield image, lines_label, 1
init_frames_count = 100
max_train_count = 2*init_frames_count
max_test_count = max_train_count
start_time = time.time()
positive_samples = get_samples(folder, pos_list, samples_subfolder, max_train_count+max_test_count)
negative_samples = get_samples(folder, neg_list, "", 5*max_train_count+max_test_count)
positive_patches = [Position(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), 0, 0, frame.shape[1], frame.shape[0]).calculate_patch() for frame in positive_samples]
negative_patches = [Position(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), 0, 0, frame.shape[1], frame.shape[0]).calculate_patch() for frame in negative_samples]
print "Positive samples:", len(positive_samples)
print "Negative samples:", len(negative_samples)
print "Generate training set:", time.time() - start_time
print

start_time = time.time()
learning_component = LearningComponent(positive_patches[0])
detector = Detector(learning_component)
for patch in positive_patches[1:max_train_count]:
    learning_component.update_positives(patch)
for patch in negative_patches[:5*max_train_count]:
    learning_component.update_negatives(patch)
print "Update training set:", time.time() - start_time
print

start_time = time.time()
detector.ensemble_classifier.relearn()
print "Learn detector:", time.time() - start_time
print

for param in xrange(10):
    print "Param:", param/10.0
    detector.threshold_patch_variance = 0.3
Exemplo n.º 26
0
class PrepareData:
    """
    Class for prepare dataset
    """
    def __init__(self):
        self.images = []
        self.emotions = []
        self.count = []
        self.detector = Detector()

    def create_dataset_from_csv(self):
        """
        Create dataset from fer2013 dataset csv file
        :return: tuple of list dataset images, dataset labels
        """

        print('\nCreating dataset...')
        data = pd.read_csv(CSV_FILE_NAME)
        total = data.shape[0]
        for i, row in data.iterrows():
            image = PrepareData._data_to_image(row['pixels'])
            emotion = PrepareData._emotion_to_vec(row['emotion'])
            detected_faces = self.detector.detect_faces(image,
                                                        fer2013_image=True)
            for face in detected_faces:
                self.images.append(face)
                self.emotions.append(emotion)
                self.count.append(row['emotion'])
            print('Progress: {}/{} {:.2f}%'.format(i, total,
                                                   i * 100.0 / total))

        return self.images, self.emotions, self.count

    def add_extra_images(self):
        """
        If --extra flag exists, adds images from extra-images dir to dataset
        :return: tuple of list dataset images, dataset labels
        """

        detector = Detector()
        print('\nAdding extra images...')
        for emotion_index, emotion in enumerate(EMOTIONS):
            print('\nEmotion', emotion)
            files = glob.glob('{}\\{}\\*'.format(EXTRA_DIR, emotion))
            total = len(files)
            for i, f in enumerate(files):
                image = cv2.imread(f)
                detected_faces = detector.detect_faces(image)
                for face in detected_faces:
                    self.images.append(face)
                    self.emotions.append(
                        PrepareData._emotion_to_vec(emotion_index))
                    self.count.append(emotion_index)
                print('Progress: {}/{} {:.2f}%'.format(i, total,
                                                       i * 100.0 / total))

    @staticmethod
    def _data_to_image(data):
        """
        Private method. Convert csv row of pixels to image
        :param data: row of pixels from csv file
        :return:
        """

        data_image = [int(pixel) for pixel in data.split(' ')]
        image = np.asarray(data_image).reshape(IMG_SIZE, IMG_SIZE)
        image = cv2.resize(image.astype('uint8'), (IMG_SIZE, IMG_SIZE))
        # image = image.astype('float32') / 255.
        return image

    @staticmethod
    def _emotion_to_vec(x):
        """
        Private method. Convert num of emotion to emotions labels array([0., 0., 0. ,1., 0., 0., 0.,])
        :param x: num of emotion
        :return: array
        """

        d = np.zeros(len(EMOTIONS))
        d[x] = 1.0
        return d
Exemplo n.º 27
0
import numpy as np


routes = web.RouteTableDef()


async def read_image(request):
    reader = await request.multipart()
    field = await reader.next()
    data = await field.read(decode=False)
    img = Image.open(BytesIO(data))
    return img


detector = Detector('half_h416_w416.pt', (416, 416), 0.85, 0.3)
detector_bag = Detector('half_h416_w416_bag.pt', (416, 416), 0.85, 0.3)
feature = Feature('r92m91.pt')
attribute = Attribut('model.pth', .7)


@routes.post('/det')
async def det(request):
    img = await read_image(request)
    img = np.array(img)
    ret = await detector(img)
    return web.json_response(ret)

@routes.post('/det_bag')
async def det(request):
    img = await read_image(request)
    parser.add_argument('--batch_size',
                        type=int,
                        default=32,
                        help='number of workers')
    parser.add_argument('--time', action='store_true')
    args = parser.parse_args()

    img_paths = glob(args.img_folder + "/*")

    model = SimpleVGG(2)
    if torch.cuda.is_available():
        model.cuda()
    model.load_state_dict(torch.load(args.model_weights))
    model.eval()

    detector_default = Detector()
    detector_cnn = Detector(cnn=True)

    if args.time:
        all_time = 0.0
        n = 0
        from time import time

    for img_path in img_paths:
        cropped_img = detector_default.get_eye_area_roi(img_path)
        if cropped_img is False:
            cropped_img = detector_cnn.get_eye_area_roi(img_path)
        if cropped_img is not False:
            if args.time:
                begin = time()
            predict = inference_on_single_image(cropped_img, model,
Exemplo n.º 29
0
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)


def drawLandmarks(image, landmarks, text=False):
    for index, mark in enumerate(landmarks):
        cv2.circle(image, tuple(mark), 3, color=(0, 255, 255))
        if text:
            cv2.putText(image,
                        str(index),
                        tuple(mark),
                        fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                        fontScale=0.4,
                        color=(0, 0, 255))


if __name__ == '__main__':
    from detection import Detector
    detector = Detector()

    image = cv2.imread("images/test/face.jpg")
    faces = detector.dlibDetection(image)
    for face in faces:
        masks = getMasks(
            image,
            face,
            (0, 67),
        )

    cv2.imshow("Masked", image)
    cv2.waitKey(0)
Exemplo n.º 30
0
class Recognizer:
    """
    Class of recognizer. Recognize emotions on image using trained model
    """

    def __init__(self):
        self.detector = Detector()
        self.model = TrainNetwork().load_trained_model()
        self.cap = cv2.VideoCapture(0)
        if not self.cap.isOpened():
            print('Webcam is not open.')
            exit()

    def recognize(self, frame):
        """
        Recognize emotions on frame for each face
        :param frame: input frame from webcam
        :return: tuple of list faces and predictions
        """

        detected_faces = self.detector.detect_faces(frame)
        predictions = []

        for face in detected_faces:
            face = face.reshape([-1, IMG_SIZE, IMG_SIZE, 1])
            prediction = self.model.predict(face)
            print(EMOTIONS[np.argmax(prediction[0])])
            predictions.append(prediction[0])

        return detected_faces, predictions

    def run(self):
        """
        Gets frames from webcam and push it to recognizer
        Stops by CTRL+C
        :return:
        """
        predictions = []
        times = []
        try:
            while self.cap.isOpened():
                sleep(0.5)
                _, frame = self.cap.read()

                faces, preds = self.recognize(frame)

                for f, p in zip(faces, preds):
                    predictions.append(p)
                    times.append(datetime.now().time())
                    Recognizer._save_recognized_image(frame, p, datetime.now())

        except KeyboardInterrupt:
            self.cap.release()
            return times, predictions

    @staticmethod
    def _save_recognized_image(face, prediction, dt):
        """
        Saves image from webcam with face(user data)
        :param face: face image
        :param prediction: emotion prediction
        :param dt: datetime
        :return:
        """

        if not os.path.exists(os.path.join(os.getcwd(), USER_DATA_DIR)):
            os.mkdir(USER_DATA_DIR)

        emotion = EMOTIONS[np.argmax(prediction)]
Exemplo n.º 31
0
 def __init__(self):
     self.images = []
     self.emotions = []
     self.count = []
     self.detector = Detector()
Exemplo n.º 32
0
 def __init__(self, model_detection, model_finemapping, model_seq_rec):
     Detector.__init__(self, model_detection, model_finemapping)
     Recognizer.__init__(self, model_seq_rec)
Exemplo n.º 33
0
    #     labels=train,
    #     augmenter=None,
    #     **generator_kwargs
    # )
    # validation_image_generator = datasets.get_detector_image_generator(
    #     labels=validation,
    #     **generator_kwargs
    # )
    # print(len(train))
    # print(len(validation))
    # print(next(training_image_generator))

    """
    word to char
    """
    detector = Detector()

    augmenter = imgaug.augmenters.Sequential([
        imgaug.augmenters.Affine(
            scale=(1.0, 1.2),
            rotate=(-5, 5)
        ),
        imgaug.augmenters.GaussianBlur(sigma=(0, 0.5)),
        imgaug.augmenters.Multiply((0.8, 1.2), per_channel=0.2)
    ])

    dataset = prepare_dataset('../ICDAR2017/training_images/')
    data_loader = get_detector_ws_image_generator(dataset,
                                                  width=640,
                                                  height=640,
                                                  augmenter=None,