openpaperwork / pyinsane

Python library to access and use image scanners (Linux/Windows/etc) (Sane/WIA) -- Moved to Gnome's Gitlab
https://gitlab.gnome.org/World/OpenPaperwork/pyinsane
63 stars 24 forks source link

Works only with sudo #40

Closed ghost closed 6 years ago

ghost commented 6 years ago

I'm on Fedora 27, and if I use the scripts with sudo, everything works. Without sudo it gives me:

`... process 6828: arguments to dbus_connection_send() were incorrect, assertion "connection != NULL" failed in file dbus-connection.c line 3308. This is normally a bug in some application using the D-Bus library. D-Bus not built with -rdynamic so unable to print a backtrace Traceback (most recent call last): File "custom.py", line 7, in devices = pyinsane2.get_devices() File "/home/nikolai/anaconda3/lib/python3.6/site-packages/pyinsane2/sane/abstract_proc.py", line 278, in get_devices for x in remote_do('get_devices', local_only) File "/home/nikolai/anaconda3/lib/python3.6/site-packages/pyinsane2/sane/abstract_proc.py", line 68, in remote_do length = struct.unpack("i", length)[0] struct.error: unpack requires a buffer of 4 bytes

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "custom.py", line 35, in pyinsane2.exit() File "/home/nikolai/anaconda3/lib/python3.6/site-packages/pyinsane2/sane/abstract_proc.py", line 130, in exit remote_do('exit') File "/home/nikolai/anaconda3/lib/python3.6/site-packages/pyinsane2/sane/abstract_proc.py", line 64, in remote_do os.write(fifo_c2s, length) BrokenPipeError: [Errno 32] Broken pipe ` Honestly is driving me crazy for the last 2 days :(

jflesch commented 6 years ago

The sane implementation forks and actually run in a background process (this is done like that in case the scanner driver crashes, so it doesn't crash the whole application as well). Here it seems the background process has crashed.

What you can try is:

import pyinsane2.sane.abstract
pyinsane2.sane.abstract.init()
pyinsane2.sane.abstract.get_devices()

It will access the device list directly without going through a background process.

By the way, does scanning with simple-scan work ?

ghost commented 6 years ago

Yes, simple scan works fine. Will try your suggestion right now.

ghost commented 6 years ago

Nope :(

[kvs7097] sane_init [kvs7097] This is panasonic KV-S7097/S7077 version 1.0 build 47-1012 [kvs7097] sane_init: leave [kvs7097] sane_get_devices: enter [kvs7097] sane_get_devices: leave [kvs2087] sane_init [kvs2087] This is panasonic KV-S2087 / KV-S1015C version 1.0 build 3 [kvs2087] sane_init: leave [kvs2087] sane_get_devices: enter [kvs2087] sane_get_devices: leave [kvs1057] sane_init [kvs1057] This is panasonic KV-S1057C / KV-S1027C version 1.0 build 3 [kvs1057] kv_usb_enum_devices: enter [kvs1057] attaching USB scanner libusb:002:002 [kvs1057] kvs1057_usb.c attach_scanner_usb product=3689 [kvs1057] FindScannerEx: Fail to find scanner of KV-SS081 [kvs1057] kv_usb_enum_devices: leave with 1 devices. [kvs1057] sane_init: leave [kvs1057] sane_get_devices: enter [kvs1057] sane_get_devices: leave [kvs1026] sane_init [kvs1026] This is panasonic KV-S1026C / KV-S1015C version 1.0 build 3 [kvs1026] kv_usb_enum_devices: enter [kvs1026] kv_usb_enum_devices: leave with 0 devices. [kvs1026] sane_init: leave [kvs1026] sane_get_devices: enter [kvs1026] sane_get_devices: leave [kvs10_series] sane_init [kvs10_series] This is panasonic KV-S1020C / KV-S1025C version 1.0 build 3 [kvs10_series] kv_usb_enum_devices: enter [kvs10_series] kv_usb_enum_devices: leave with 0 devices. [kvs10_series] sane_init: leave [kvs10_series] sane_get_devices: enter [kvs10_series] sane_get_devices: leave process 3754: arguments to dbus_connection_send() were incorrect, assertion "connection != NULL" failed in file dbus-connection.c line 3308. This is normally a bug in some application using the D-Bus library. D-Bus not built with -rdynamic so unable to print a backtrace Aborted (core dumped)

ghost commented 6 years ago

it works without sudo if I use this:

import pyinsane2

pyinsane2.init() try: device = pyinsane2.Scanner(name="kvs1057:libusb:002:003") print("I'm going to use the following scanner: %s" % (str(device))) finally: pyinsane2.exit()

so definetely the problem is in the get_devices() function.. Something with permissions I guess..

jflesch commented 6 years ago

[kvs2087] sane_init: leave

Are you calling init() / exit() many times ? pyinsane2.init() is supposed to be called only once in the lifetime of your application. This may be the source of your problem. (I need to add that to the documentation I think)

ghost commented 6 years ago

no.. I'm just using your example mainly:

import pyinsane2
import numpy as np
import cv2

pyinsane2.init()
try:
    device = pyinsane2.Scanner(name="kvs1057:libusb:002:003")
    print("I'm going to use the following scanner: %s" % (str(device)))

    pyinsane2.set_scanner_opt(device, 'resolution', [300])

    pyinsane2.set_scanner_opt(device, 'mode', ['Color'])

    pyinsane2.maximize_scan_area(device)

    scan_session = device.scan(multiple=False)
    try:
        while True:
            scan_session.scan.read()
    except EOFError:
        pass
    image = scan_session.images[-1]
    open_cv_image = np.array(image)
    open_cv_image = open_cv_image[:, :, ::-1].copy()
    cv2.imshow("image",open_cv_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
finally:
    pyinsane2.exit()
jflesch commented 6 years ago

I'm confused. From where do these messages [kvs...] sane_init: leave come ? oO

ghost commented 6 years ago

wait a sec.. I'll do the bare minimum

jflesch commented 6 years ago

Try something like this: (it should bypass the background process and make debugging easier)

import pyinsane2
import pyinsane2.sane.abstract

pyinsane2.sane.abstract.init()
try:
    devices = pyinsane2.sane.abstract.get_devices()
    assert(len(devices) > 0)
    device = devices[0]
    print("I'm going to use the following scanner: %s" % (str(device)))

    pyinsane2.set_scanner_opt(device, 'resolution', [300])
    pyinsane2.set_scanner_opt(device, 'mode', ['Color'])
    pyinsane2.maximize_scan_area(device)

    scan_session = device.scan(multiple=False)
    try:
        while True:
            scan_session.scan.read()
    except EOFError:
        pass
    image = scan_session.images[-1]
finally:
    pyinsane2.sane.abstract.exit()
ghost commented 6 years ago

I'll try your code next.. But first I've tried:

import pyinsane2

pyinsane2.init()
try:
    device = pyinsane2.Scanner(name="kvs1057:libusb:002:004")
    print("I'm going to use the following scanner: %s" % (str(device)))

    # pyinsane2.set_scanner_opt(device, 'resolution', [300])
finally:
    pyinsane2.exit()

and the output was only the print, as expected. If I try to set a parameter with pyinsane2.set_scanner_opt(device, 'resolution', [300]) I'm getting this verbose explosion:

I'm going to use the following scanner: 'kvs1057:libusb:002:004' (Unknown, Unknown, Unknown)
[kvs1057] sane_init
[kvs1057] This is panasonic KV-S1057C / KV-S1027C version 1.0 build 3
[kvs1057] kv_usb_enum_devices: enter
[kvs1057] attaching USB scanner libusb:002:004
[kvs1057] kvs1057_usb.c attach_scanner_usb product=3689
[kvs1057] FindScannerEx: Fail to find scanner of KV-SS081
[kvs1057] kv_usb_enum_devices: leave with 1 devices.
[kvs1057] sane_init: leave
[kvs1057] sane_open: enter (dev_name=libusb:002:004)
[kvs1057] kvs1057_low.c kv_open start
[kvs1057] kv_usb_open: enter
[kvs1057] kvs1057_usb.c kv_usb_already_open (dev->usb_fd > -1)=0
[kvs1057] kvs1057_usb.c check_xhci xhci=true (/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/8p, 5000M
)
[kvs1057] kv_usb_open: Probably RHEL 7 or later. sane.model=KV-S1057C
[kvs1057] kv_usb_open: fix_xhci_mode=2
[kvs1057] kv_usb_open: sanei_usb_clear_halt()=0
[kvs1057] kv_usb_open: sanei_usb_reset()=0
[kvs1057] kv_usb_open: leave
[kvs1057] kvs1057_low.c RETRAY_NUM=3,i=0
[kvs1057] CMD_test_unit_ready
[kvs1057] kvs1057_usb.c kv_usb_already_open (dev->usb_fd > -1)=1
[kvs1057] kvs1057_usb.c kv_usb_escape start
[kvs1057] kvs1057_usb.c kv_usb_already_open (dev->usb_fd > -1)=1
[kvs1057] kv_usb_escape: get response.
[kvs1057] response
[kvs1057] 000: 00 00 00 10 00 03 a0 00 00 00 00 00 00 00 00 00
[kvs1057] kvs1057_usb.c kv_usb_send_command s=0
[kvs1057] kvs1057_low.c CMD_test_unit_ready,status=0,* ready=1
[kvs1057] CMD_read_support_info
[kvs1057] kvs1057_usb.c kv_usb_already_open (dev->usb_fd > -1)=1
[kvs1057] kvs1057_usb.c kv_usb_escape start
[kvs1057] kvs1057_usb.c kv_usb_already_open (dev->usb_fd > -1)=1
[kvs1057] kvs1057_opt.c dev->sane.model =KV-S1057C
[kvs1057] kv_usb_escape: get response.
[kvs1057] response
[kvs1057] 000: 00 00 00 10 00 03 a0 00 00 00 00 00 00 00 00 00
[kvs1057] kvs1057_usb.c kv_usb_send_command s=0
[kvs1057] test.
[kvs1057] support_info.memory_size = 64 (MB)
[kvs1057] support_info.min_resolution = 300 (DPI)
[kvs1057] support_info.max_resolution = 600 (DPI)
[kvs1057] support_info.step_resolution = 300 (DPI)
[kvs1057] support_info.support_duplex = TRUE
[kvs1057] support_info.support_lamp = FALSE
[kvs1057] kv_init_options: enter
[kvs1057] sane_control_option: enter, option OPT_PAPER_SIZE, action W
[kvs1057] SANE_ACTION_SET_VALUE 
[kvs1057] sane_control_option: enter, option OPT_MODE, action W
[kvs1057] SANE_ACTION_SET_VALUE 
[kvs1057] kv_init_options: exit
[kvs1057] sane_open: leave
[kvs1057] sane_get_option_descriptor: enter, option OPT_NUM_OPTS
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_control_option: enter, option OPT_NUM_OPTS, action R
[kvs1057] SANE_ACTION_GET_VALUE 
[kvs1057] opt value = 32
[kvs1057] sane_get_option_descriptor: enter, option OPT_PREVIEW
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_MODE_GROUP
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_MODE
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_RESOLUTION
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_DUPLEX
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_SCAN_SOURCE
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_FEEDER_MODE
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_LONGPAPER
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_LENGTHCTL
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_MANUALFEED
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_FEED_TIMEOUT
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_DBLFEED
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_FIT_TO_PAGE
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_BACKGRUOUN_COLOR
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_GEOMETRY_GROUP
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_PAPER_SIZE
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_LANDSCAPE
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_TL_X
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_TL_Y
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_BR_X
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_BR_Y
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_ENHANCEMENT_GROUP
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_BRIGHTNESS
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_CONTRAST
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_HALFTONE_PATTERN
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_WHITE_LEVEL
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_NOISE_REDUCTION
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_IMAGE_EMPHASIS
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_GAMMA
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_LAMP
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_SEPARATION_SHEET
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_get_option_descriptor: enter, option OPT_RESOLUTION
[kvs1057] sane_get_option_descriptor: exit
[kvs1057] sane_control_option: enter, option OPT_RESOLUTION, action W
[kvs1057] SANE_ACTION_SET_VALUE 
[kvs1057] option OPT_RESOLUTION, input = 300, value = 300
jflesch commented 6 years ago

I think this output comes from the Sane driver of your scanner. It's the first time I see one so verbose by default.

ghost commented 6 years ago

I think so as well.. I think he might as well be the reason of my permissions problem in the scanner discovery phase.. I guess I'll have to investigate on that side, since it is not pyinsane's fault. Am I right?

jflesch commented 6 years ago

I doubt it's Pyinsane or Sane fault. Since it works as root, as you suggested, I would be looking for a permission problem. Maybe SELinux is blocking something it shouldn't. If I'm not mistaken, you can keep an eye on /var/log/audit/audit.log in case it's related to SELinux. If not, you can try using strace on python+your script to see when it crashes exactly (but you will need to disable Pyinsane background process first).

ghost commented 6 years ago

Thanks a lot for the help! I will investigate that then. Goodnight!

jflesch commented 6 years ago

Good luck