示例#1
0
class Camera_sonywifi(Camera):
    def __init__(self, picture_size, preview_size, zoom=30, ssid="DIRECT-LKE0:ILCE-6000", pw="UeyxbxAG",
                 iface="wlp2s0"):
        Camera.__init__(self, picture_size, preview_size, zoom, type='sony_wifi')
        self.live_stream = None
        if not sony_enabled:
            raise CameraException("pysony module not installed")
        self.previous_wifi = \
        subprocess.check_output(["nmcli", "--fields", "NAME", "c", "show", "--active"]).decode("utf-8").split("\n")[
            1].strip()
        self.sony_api_version = "1.0"
        if self.previous_wifi == ssid:
            self.previous_wifi = ""
        else:
            try:
                wifi_list = subprocess.check_output(["nmcli", "--fields", "SSID", "device", "wifi"]).decode(
                    "utf-8").split("\n")
                wifi_list = [wifi_list[i].strip() for i in range(len(wifi_list))]

                if ssid in wifi_list:
                    subprocess.check_call(["nmcli", "con", "up", "id", ssid])
                else:
                    raise CameraException("Sony Wifi not found")

            except subprocess.CalledProcessError:
                raise CameraException("Cannot connect to wifi")
        search = ControlPoint()
        cameras = search.discover()
        self.last_preview_frame = Image.new('RGB', preview_size, (0, 0, 0))

        if len(cameras):
            self.camera = SonyAPI(QX_ADDR=cameras[0])
        else:
            raise CameraException("No camera found")


        options = self.camera.getAvailableApiList()['result'][0]
        logging.info(str(options))

    def zoom_in(self):
        self.camera.actZoom(["in", "1shot"])

    def zoom_out(self):
        self.camera.actZoom(["out", "1shot"])

    def __del__(self):
        self.teardown()

    def teardown(self):
        if sony_enabled:
            if self.live_stream is not None:
                self.stop_preview_stream()
            if self.previous_wifi is not "":
                try:
                    subprocess.check_call(["nmcli", "con", "up", "id", self.previous_wifi])
                except subprocess.CalledProcessError:
                    raise CameraException("Cannot connect to previous wifi " + self.previous_wifi)

    def start_preview_stream(self):
        # For those cameras which need it
        options = self.camera.getAvailableApiList()['result'][0]
        logging.info("starting preview")
        if 'startRecMode' in options:
            self.camera.startRecMode()
            time.sleep(1)
            options = self.camera.getAvailableApiList()['result'][0]
            print(str(options))
        self.camera.setLiveviewFrameInfo([{"frameInfo": False}])
        url = self.camera.liveview()
        assert isinstance(url, str)
        print(url)
        self.live_stream = SonyAPI.LiveviewStreamThread(url)
        self.live_stream.start()
        self.preview_active = True

    def stop_preview_stream(self):
        logging.info("stopping preview")
        if self.live_stream is not None:
            self.live_stream.stop()
            options = self.camera.getAvailableApiList()['result'][0]
            # if self.preview_active and 'endRecMode' in (options):
            #    self.camera.stopRecMode()
            # if self.live_stream.is_active():
            #    self.camera.stopLiveview() # todo:  is this correct?
        self.preview_active = False

    def get_preview_frame(self, filename=None):
        # read header, confirms image is also ready to read
        header = False
        while not header:
            header = self.live_stream.get_header()

        if header:
            # image_file = io.BytesIO(self.live_stream.get_latest_view())
            # incoming_image = Image.open(image_file)
            # frame_info = self.live_stream.get_frameinfo()
            data = self.live_stream.get_latest_view()
            img = Image.open(io.BytesIO(data))
            # img=img.resize(self.preview_size, Image.ANTIALIAS)
        else:
            img = self.last_preview_frame
        if filename is not None:
            img.save(filename)
        return img

    def take_picture(self, filename="/tmp/picture.jpg"):
        options = self.camera.getAvailableApiList()['result'][0]
        url = self.camera.actTakePicture()
        logging.info(url)
        response = requests.get(url['result'][0][0].replace('\\', ''))
        img = Image.open(io.BytesIO(response.content))
        if filename is not None:
            img.save(filename)
        return img