0

EDITED POST

When I first wrote this, I was mystified by the varied behaviors of the "imagesnap" USB cam image capture program on MacOS. Sometimes it took pictures and sometimes it didn't, and this seemed to vary with the environment where it was called: such as directly in a Terminal window, in a shell program, in a Python program running in the Terminal, in a Python program running in the Pycharm IDE, called with os.popen(), subprocess.run(), subprocess.call(), subprocess.popen(), etc.

Sometimes imagesnap would work just fine and take images, and sometimes it would fail silently. I was really having a hard time figuring out why the behaviors were varied, and how to get picture taking using imagesnap on my mac to work reliably when called from a Python program.

I searched on keywords like MacOS, Catalina, imagesnap, USB camera, webcam, Python, PyCharm IDE, shell command, os.popen(), subprocess.run(), subprocess.call(), subprocess.Popen(), and more. I did not find the solution anywhere, and I didn't get an answer here.

I've finally understood the problem better, and that's why I've rewritten this question.

What's behind the inconsistent behavior of imagesnap when called from different environments, and how can I call it from a Linux program and get it to reliably take pictures?

I now understand that MacOS X's privacy settings in System Preferences control which environments are allowed or silently denied to access USB cameras.

For more detail, and a workaround, see my own answer below. I hope that others who understand the issues even more will add improved answers.

ORIGINAL POST:

Below is a simple Python test program. It uses os.popen() and captures an image from a selected USB webcam using imagesnap -d and saves an image in image.jpg.

But the imagesnap -d command behaves differently when running in the PyCharm environment, returning a blank response (' ') and failing to save the requested image.

Submitting this same code WITHIN PyCharm's terminal window fails in exactly the same way.

But when the same python commands are submitted to the terminal outside Pycharm, the program takes the image successfully and reports back as shown in the output below.

I'm running Mac OS X Catalina, and using PyCharm 2019.2.3 Community Edition for development. Python version is 3.7.4

Sample code:

import os
return_string = os.popen("imagesnap -d 'DEVICE_NAME' 'image.jpg'").read()
print("'",return_string,"'")

Within the PyCharm development environment this program returns the following and does not create the jpg file:

/Users/mcgregor94086/PycharmProjects/SonaScannerGUI/venv/bin/python

/Users/mcgregor94086/Library/Preferences/PyCharmCE2019.2/scratches/scratch_5.py ' '

Process finished with exit code 0

Outside PyCharm this same code returns:

$ python3
Python 3.7.4 (v3.7.4:e09359112e, Jul  8 2019, 14:54:52) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> return_string = os.popen("imagesnap -d 'DEVICE_NAME' 'image.jpg'").read()
>>> print("'",return_string,"'")
' Capturing image from device "<AVCaptureDALDevice: 0x7fa9bc203330 [DEVICE_NAME][0x141717501bcf0b09]>"... '
>>>
mcgregor94086
  • 1,467
  • 3
  • 11
  • 22

1 Answers1

0

After digging into this problem a great deal, and without any responses, I started to search the web more broadly for answers. And although I found almost no information on the web to guide me what was happening, I now believe that I know what is preventing me from taking pictures programmatically within a python program. I also believe that I now have a reasonable hypothesis as to why I am seeing the results I was seeing.

I have a work around that is currently working, but which I fear may not in the future.

The believe the problem has to do with MacOS and I believe it may vary with the specific release of MacOS. I am currently using MacOS X 10.15.1 Catalina. earlier MacOS X releases might not be as strict.

In the Catalina release at a minimum, ANY connected USB Camera is considered a special device governed by special security, and management of access to the camera device is controlled in the Systems Preferences -> Security & Privacy panel.

When you click the Camera row in the menu on the left of the panel you will see a checkbox list of apps which are allowed to programmatically access you camera. If you unlock the page (lower left), you can change which apps are allowed to access the camera by checking or unchecking them in the list.

On the machines I have available, there is a list of 4 apps in this panel: Terminal, UberConference, Google Chrome, Skype.

The list of available apps varies for other resources, such as Contacts, Calendars, Accessibility, Full Disk Access, etc.

Some of these resources (such as Accessibility and Full Disk Access) selection lists have a pair of "+|-" buttons below the app list that allow you to add, or permanently remove, apps from accessing that resource.

But Camera. Microphone and some other resource do not have such a pair of buttons, and for these devices, the list appear unchangeable by the user.

The panel suggests that the apps get on the list by "requesting access" to the camera, but I have not found an explanation anywhere on line about how to add an app to this access list.

One of the predefined apps in the list is "Terminal". That was checked, so when I ran imagesnap from within a terminal window the program had the necessary privacy privileges and the camera took pictures just fine.

In contrast, when I tried to run a python program within the PyCharm IDE, the PyCharm IDE did not identify itself as a terminal and the request silently failed.

Python has a number of different ways to launch an independent program such as imagesnap. These methods include os.popen(), subprocess.run() and subprocess.Popen(). Some of these methods are deprecated, so we can't rely on them working in the future. Other methods() have additional options which control whether a new "shell" is created or not, and these may complicate determining what environment the app will see.

I found it confusing to try to predict which of these python methods would succeed in opening the camera and which would not, as well as their behavior when run inside the IDE vs. in a terminal window.
Perhaps someone else can compile such a table.

Work around: For the present, I was able to work around this problem by using os.popen, by having it open a new terminal with an initial string. This seems to ensure that Apple security sees the parent as the "Terminal" app which we specifically authorized in the control panel.

I then found

os.popen("open -a Terminal.app imagesnap.sh") 

worked.

While this work around is working for me at present, I was not happy about using os.popen() instead of one of the subprocess methods, as I worry it may be deprecated. Second, this worked with a shell script ("imagesnap.sh") but I have not figured out how to pass parameters or run executables.

Perhaps a knowledgable reader can help with better suggestions.

mcgregor94086
  • 1,467
  • 3
  • 11
  • 22