Example #1
0
    def parse(self, response):
        for row in response.xpath("//tr")[2:]:
            # Initialize dictionary
            result = {}

            result['id'] = row.xpath('td[1]/text()').extract_first()
            result['card_name'] = row.xpath('td[3]/a/@title').extract_first()
            result['relative_url'] = row.xpath('td[3]/a/@href').extract_first()

            # Get icons
            icons = row.css('td:nth-child(2)').xpath('.//@src').extract()
            for icon in icons:
                if icon.startswith('d'):  # Or 'data'
                    icons.remove(icon)

            result['image'] = get_raw_image(icons[0])
            if icons.__len__() == 2:
                result['special_icon'] = get_raw_image(icons[1])

            yield result
Example #2
0
    def parse(self, response):
        weapon_table = response.css('table')[1]

        count = 0
        for weapon in weapon_table.css('tr'):
            if count == 0:
                count += 1
                continue
            # if count > 13:
            #     break
            url = 'http://zh.battlegirl.wikia.com'
            weapon_type = weapon.css('td')[2].css('::text').extract_first()
            type_icon = get_raw_image(weapon.css('td')[2].css('noscript').css('img::attr(src)').extract_first())
            yield scrapy.Request(url=(url + weapon.css('td')[1].css('a::attr(href)').extract_first()), callback=self.parseMore,
                                 meta={'weapon_type': weapon_type, 'type_icon': type_icon})
            count += 1
Example #3
0
def start_single_record(counter, cap, cam, controller, listener, q, left_coord,
                        left_coeff, right_coord, right_coeff):
    directory_rr = "./single_data/{}/R/raw".format(counter)
    directory_lr = "./single_data/{}/L/raw".format(counter)
    directory_ru = "./single_data/{}/R/undistorted".format(counter)
    directory_lu = "./single_data/{}/L/undistorted".format(counter)
    directory_leap_info = "./single_data/{}/leap_motion/tracking_data".format(
        counter)
    directory_rgb = "./single_data/{}/rgb".format(counter)
    directory_z = "./single_data/{}/depth/z".format(counter)
    directory_ir = "./single_data/{}/depth/ir".format(counter)

    list_img_rr = []
    list_img_ru = []
    list_img_lr = []
    list_img_lu = []
    list_json = []
    list_img_rgb = []
    list_img_z = []
    list_img_ir = []

    record_if_valid = False
    frame_counter = 0

    if not cap:
        print("error rgb cam")
        exit(-1)
    error_queue = False
    cam.startCapture()
    while True:
        # print(frame_counter)
        if (cv2.waitKey(1) == ord('s') and record_if_valid and frame_counter > args.n_min_frames) \
                or error_queue:
            # print(error_queue)
            break

        frame = controller.frame()

        # controllo di validità per inizio registrazione (OPZIONALE)
        # inizia a registrare i frame solo se leap motion rileva correttamente la mano

        # print(self.listener.recording)
        if utils.hand_is_valid(frame) and not record_if_valid:
            print('\nhand is valid -> ready to start')
            record_if_valid = True
            # print(self.listener.recording)
            print("start gesture")
            listener.setRecording(True)
            # print(self.listener.recording)

        if record_if_valid:
            print("\rrecord valid -> showing {}".format(frame_counter), end="")
            utils.draw_ui(text="recording - press S to stop",
                          circle=True,
                          thickness=-1)
            # RGB CAM
            # get rgb image
            # print(1)
            ret, img_rgb = cap.read()
            # print(2)
            # resize dim img rgb
            if not ret:
                print("\nrgb cam not working")
                exit(-1)
            # cv2.imshow('img_rgb', img_rgb)
            # cv2.waitKey(1)

            # Leap Motion
            if frame.is_valid:
                image_l = frame.images[0]
                image_r = frame.images[1]
                # print(3)
            else:
                print("\rframe {} not valid".format(frame_counter), end="")
                continue

            if image_l.is_valid and image_r.is_valid:
                # print(4)
                raw_img_l = utils.get_raw_image(image_l)
                raw_img_r = utils.get_raw_image(image_r)
                # undistorted images
                undistorted_left = utils.undistort(image_l, left_coord,
                                                   left_coeff, 400, 400)
                undistorted_right = utils.undistort(image_r, right_coord,
                                                    right_coeff, 400, 400)
                # print(5)

                # show images
                # previous position cv2.imshow()
                # cv2.imshow('img_leap', undistorted_right)

                # json
                json_obj = utils.frame2json_struct(frame)
                # print(6)
                # PICOFLEXX
                # imgs == (z, ir)
                ret_pico, imgs = utils.get_images_from_picoflexx(q)
                # print("ret_pico, z, ir", ret_pico, imgs[0], imgs[1])
                if not ret_pico:
                    print("pico image not valid")
                    error_queue = True
                    continue

                cv2.moveWindow('img_rgb', -700, 325)
                cv2.moveWindow('img_leap', -1150, 400)
                cv2.moveWindow('img_ir', -1500, 600)
                cv2.imshow('img_leap', undistorted_right)
                cv2.imshow('img_rgb', img_rgb)
                cv2.imshow('img_ir', imgs[1])

                # print(7)
                list_img_rr.append(raw_img_r.copy())
                list_img_ru.append(undistorted_right.copy())
                list_img_lr.append(raw_img_l.copy())
                list_img_lu.append(undistorted_left.copy())
                list_img_rgb.append(img_rgb.copy())
                list_json.append(json_obj)
                list_img_z.append(imgs[0].copy())
                list_img_ir.append(imgs[1].copy())

                # list_img_z.append(z.copy())
                # list_img_ir.append(ir.copy())

                frame_counter += 1
                # print(8)
            else:
                print('image not valid')

        else:
            print("\rerror in getting valid leap motion frame", end="")

    # print(self.listener.recording)
    listener.setRecording(False)
    cam.stopCapture()
    cap.release()

    #write single record
    print("saving record")
    utils.draw_ui(text="Saving session...")

    utils.save_single_record(list_img_rr, list_img_ru, list_img_lr,
                             list_img_lu, list_json, list_img_rgb, list_img_z,
                             list_img_ir, directory_rr, directory_ru,
                             directory_lr, directory_lu, directory_leap_info,
                             directory_rgb, directory_z, directory_ir)
    def parse(self, response):
        # Initialization
        result = {}

        # ID and icons
        result['id'] = response.meta['id']
        result['japanese_name'] = response.meta['japanese_name']
        result['image'] = response.meta['image']
        result['special_icon'] = response.meta['special_icon']

        # Images
        images = response.css('#mw-content-text > div:nth-child(1)')
        result['art'] = get_raw_image(get_lazy_image(images.css('div:nth-child(1) > img')[-1]))

        if images.css('div:nth-child(3) > img').__len__() > 0:
            if int(images.css('div:nth-child(3) > img')[0].xpath('@height').extract_first()) > 300:
                result['special_front'] = get_raw_image(get_lazy_image(images.css('div:nth-child(3) > img')[0]))
                result['front_top'] = get_raw_image(get_lazy_image(images.css('div:nth-child(4) > img')[0]))
                result['front_bottom'] = get_raw_image(get_lazy_image(images.css('div:nth-child(5) > img')[0]))
                result['front_name'] = get_raw_image(get_lazy_image(images.css('div:nth-child(6) > img')[0]))
                result['front_rarity'] = get_raw_image(get_lazy_image(images.css('div:nth-child(7) > img')[0]))
                result['front_weapon'] = get_raw_image(get_lazy_image(images.css('div:nth-child(8) > img')[0]))
            else:
                result['front_top'] = get_raw_image(get_lazy_image(images.css('div:nth-child(3) > img')[0]))
                result['front_bottom'] = get_raw_image(get_lazy_image(images.css('div:nth-child(4) > img')[0]))
                result['front_name'] = get_raw_image(get_lazy_image(images.css('div:nth-child(5) > img')[0]))
                result['front_rarity'] = get_raw_image(get_lazy_image(images.css('div:nth-child(6) > img')[0]))
                result['front_weapon'] = get_raw_image(get_lazy_image(images.css('div:nth-child(7) > img')[0]))

        # Subcard effect
        result['subcard_effect'] = response.css(
            '#mw-content-text > table:nth-child(7) ::text').extract_first() == u'副卡牌效果'

        # Tables
        tables = response.css('table')

        # Name
        name_table = tables[0]
        result['i_rarity'] = name_table.css('tr:nth-child(3)>td::text').extract_first()
        result['i_weapon'] = name_table.css('tr:nth-child(4)>td::text').extract_first()
        result['character'] = response.css(
            '#PageHeader > div.page-header__main > div.page-header__categories > div > a:nth-child(2) ::text').extract_first().split(
            u'\uff1a')[-1]

        # Parameter
        parameter_table = tables[1]
        result['hp_1'] = parameter_table.css('tr:nth-child(2) > td:nth-child(2) ::text').extract_first()
        result['sp_1'] = parameter_table.css('tr:nth-child(3) > td:nth-child(2) ::text').extract_first()
        result['atk_1'] = parameter_table.css('tr:nth-child(4) > td:nth-child(2) ::text').extract_first()
        result['def_1'] = parameter_table.css('tr:nth-child(5) > td:nth-child(2) ::text').extract_first()
        result['hp_50'] = parameter_table.css('tr:nth-child(2) > td:nth-child(3) ::text').extract_first()
        result['sp_50'] = parameter_table.css('tr:nth-child(3) > td:nth-child(3) ::text').extract_first()
        result['atk_50'] = parameter_table.css('tr:nth-child(4) > td:nth-child(3) ::text').extract_first()
        result['def_50'] = parameter_table.css('tr:nth-child(5) > td:nth-child(3) ::text').extract_first()
        result['hp_70'] = parameter_table.css('tr:nth-child(2) > td:nth-child(4) ::text').extract_first()
        result['sp_70'] = parameter_table.css('tr:nth-child(3) > td:nth-child(4) ::text').extract_first()
        result['atk_70'] = parameter_table.css('tr:nth-child(4) > td:nth-child(4) ::text').extract_first()
        result['def_70'] = parameter_table.css('tr:nth-child(5) > td:nth-child(4) ::text').extract_first()

        # Obtain method
        obtain_table = tables[2]
        result['obtain_method'] = "".join(obtain_table.css('tr > td ::text').extract())

        # Action skill
        if tables[3].css('::text').extract_first() != u'副卡牌專用':
            action_skill_table = tables[3]
            result['japanese_skill_name'] = action_skill_table.css(
                'tr:nth-child(1) > td:nth-child(3) > b ::text').extract_first()
            result['skill_SP'] = action_skill_table.css('tr:nth-child(2) > td ::text').extract_first()
            result['skill_range'] = '\n'.join(action_skill_table.css('tr:nth-child(4) > td ::text').extract())

            skill_hits = action_skill_table.css('tr:nth-child(3) > td ::text').extract_first()
            if skill_hits.split(u'\uFF0F').__len__() == 2:
                result['skill_affinity'] = skill_hits.split(u'\uFF0F')[-1]

            skill_combo = action_skill_table.css('tr:nth-child(1) > td:nth-child(5) ::text').extract()
            # Change in skill combo after evolution
            if skill_combo.__len__() == 2:
                result['action_skill_combo'] = skill_combo[0].split(u'\uff1a')[-1]
                result['evolved_action_skill_combo'] = skill_combo[1].split(u'\uff1a')[-1]
            else:
                result['action_skill_combo'] = result['evolved_action_skill_combo'] = skill_combo[0]

            result['action_skill_damage'] = '\n'.join(
                action_skill_table.css('tr:nth-child(5) > td ::text').extract())
            if action_skill_table.css('tr:nth-child(6) > td ::text').extract_first() != u'\uff0d':
                result['evolved_action_skill_damage'] = '\n'.join(
                    action_skill_table.css('tr:nth-child(6) > td ::text').extract())

            result['action_skill_effects'] = '\n'.join(action_skill_table.css('tr:nth-child(3) > td').xpath(
                'hr/following-sibling::text()').extract())

        # Nakayoshi
        try:
            if tables[4].css('::text').extract_first() != u'副卡牌專用':
                nakayoshi_table = tables[4]
                result['japanese_nakayoshi_title'] = nakayoshi_table.css(
                    'tr:nth-child(1) > td > b ::text').extract_first()
                unevolved_nakayoshi = nakayoshi_table.css('tr:nth-child(2) > td ::text').extract_first().split(
                    u'\uFF0F')
                result['nakayoshi_skill_target'] = unevolved_nakayoshi[0]
                result['nakayoshi_skill_effect'] = unevolved_nakayoshi[1]
                evolved_nakayoshi = nakayoshi_table.css('tr:nth-child(3) > td ::text').extract_first().split(u'\uFF0F')
                if evolved_nakayoshi[0] != u'\uff0d':
                    result['evolved_nakayoshi_skill_target'] = evolved_nakayoshi[0]
                    result['evolved_nakayoshi_skill_effect'] = evolved_nakayoshi[1]
        except IndexError as err:
            print err.args
            pass

        yield result
Example #5
0
    def record(self):

        list_img_rr = []
        list_img_ru = []
        list_img_lr = []
        list_img_lu = []
        list_json = []
        list_img_rgb = []
        list_img_z = []
        list_img_ir = []

        record_if_valid = False
        frame_counter = 0
        # print("ready to go")
        # open rgb camera
        # cap = cv2.VideoCapture(1)
        #
        # cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280.0)
        # cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720.0)
        # print(cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

        if not self.cap:
            print("error rgb cam")
            exit(-1)
        error_queue = False
        self.cam.startCapture()
        while True:
            # print(frame_counter)
            if (cv2.waitKey(1) == ord('s') and record_if_valid and frame_counter > args.n_min_frames)\
                    or error_queue:
                # print(error_queue)
                break

            frame = self.controller.frame()

            # controllo di validità per inizio registrazione (OPZIONALE)
            # inizia a registrare i frame solo se leap motion rileva correttamente la mano

            # print(self.listener.recording)
            if utils.hand_is_valid(frame) and not record_if_valid:
                print('\nhand is valid -> ready to start')
                record_if_valid = True
                # print(self.listener.recording)
                print("start gesture")
                self.listener.setRecording(True)
                # print(self.listener.recording)

            if record_if_valid:
                print("\rrecord valid -> showing {}".format(frame_counter),
                      end="")
                utils.draw_ui(text="recording - press S to stop",
                              circle=True,
                              thickness=-1)
                # RGB CAM
                # get rgb image
                # print(1)
                ret, img_rgb = self.cap.read()
                # print(2)
                # resize dim img rgb
                if not ret:
                    print("\nrgb cam not working")
                    exit(-1)
                # cv2.imshow('img_rgb', img_rgb)
                # cv2.waitKey(1)

                # Leap Motion
                if frame.is_valid:
                    image_l = frame.images[0]
                    image_r = frame.images[1]
                    # print(3)
                else:
                    print("\rframe {} not valid".format(frame_counter), end="")
                    continue

                if image_l.is_valid and image_r.is_valid:
                    # print(4)
                    raw_img_l = utils.get_raw_image(image_l)
                    raw_img_r = utils.get_raw_image(image_r)
                    # undistorted images
                    undistorted_left = utils.undistort(image_l,
                                                       self.left_coord,
                                                       self.left_coeff, 400,
                                                       400)
                    undistorted_right = utils.undistort(
                        image_r, self.right_coord, self.right_coeff, 400, 400)
                    # print(5)

                    # show images
                    # previous position cv2.imshow()
                    # cv2.imshow('img_leap', undistorted_right)

                    # json
                    json_obj = utils.frame2json_struct(frame)
                    # print(6)
                    # PICOFLEXX
                    #imgs == (z, ir)
                    ret_pico, imgs = utils.get_images_from_picoflexx(self.q)
                    # print("ret_pico, z, ir", ret_pico, imgs[0], imgs[1])
                    if not ret_pico:
                        print("pico image not valid")
                        error_queue = True
                        continue

                    cv2.moveWindow('img_rgb', -700, 325)
                    cv2.moveWindow('img_leap', -1150, 400)
                    cv2.moveWindow('img_ir', -1500, 600)
                    cv2.imshow('img_leap', undistorted_right)
                    cv2.imshow('img_rgb', img_rgb)
                    cv2.imshow('img_ir', imgs[1])

                    # print(7)
                    list_img_rr.append(raw_img_r.copy())
                    list_img_ru.append(undistorted_right.copy())
                    list_img_lr.append(raw_img_l.copy())
                    list_img_lu.append(undistorted_left.copy())
                    list_img_rgb.append(img_rgb.copy())
                    list_json.append(json_obj)
                    list_img_z.append(imgs[0].copy())
                    list_img_ir.append(imgs[1].copy())

                    # list_img_z.append(z.copy())
                    # list_img_ir.append(ir.copy())

                    frame_counter += 1
                    # print(8)
                else:
                    print('image not valid')

            else:
                print("\rerror in getting valid leap motion frame", end="")

        # print(self.listener.recording)
        self.listener.setRecording(False)
        self.cam.stopCapture()

        # print(self.listener.recording)
        # release rgb camera
        # cap.release()

        print('\nrecord {}/2 of g{} completed'.format(self.record_number,
                                                      self.gesture_id))
        record_if_valid = False
        # scrittura su disco
        # if not args.on_disk:
        if self.rewrite:
            rewrite = True
        else:
            rewrite = False

        return utils.GestureData(self.gesture_id,
                                 list_img_rr,
                                 list_img_ru,
                                 list_img_lr,
                                 list_img_lu,
                                 list_json,
                                 list_img_rgb,
                                 list_img_z,
                                 list_img_ir,
                                 self.directory_rr,
                                 self.directory_ru,
                                 self.directory_lr,
                                 self.directory_lu,
                                 self.directory_leap_info,
                                 self.directory_rgb,
                                 self.directory_z,
                                 self.directory_ir,
                                 rewrite=rewrite)