Webcam stream macOs

Hello,
After installing Octoprint on my MacMini I tried without luck to setup an usb webcam with ffmpeg or vlc.

So I wrote a small python script that uses opencv and streams to http, in case someone needs it you can grab it here: https://github.com/meska/mjpeg_stream_webcam

4 Likes

Hi,
I installed OctoPrint on my Mac Mini (Catalina) using Setting up OctoPrint on MacOS and I am trying to get a C270 webcam working. I came across this and thought I would give it a go seeing as I have also had no luck with FFmpeg.

I ran the install but get the following when running 'python mjpegsw.py ', any idea/advice?

File "mjpegsw.py", line 22
print(f"{self.path}")

..I'm not a python coder.

Just to rule things out

Did you create a virtualenv with python 3.7 and install the requirements with pip install -r requirements.txt ?

You have that error because you tried to launch the script with an older python version.
to have a newer python installed you need to install brew from here https://brew.sh/ ( already installed if you followed the guide for octoprint) .
then open a terminal and write:

pip install virtualenv
brew install python

After that you can go in the folder where you downloaded the script and create a virtualenv with:

virtualenv -p python3.7 .env
.env/bin/pip install -r requirements.txt

then launch it with:

.env/bin/python mjpegsw.py

Thanks, this world to ta point , however I get an "Abort trap: 6" with the camera plugged in.

Without the camera plugged in I get the following, indicating it is starting up ok, just can't find webcam, obviously. I am using the Logitech C270, which works when using the app from Logitech, so I know the Mac is recognising and drivers installed.

xxxxx-Mac-mini:mjpeg_stream_webcam-master xxxxx$ .env/bin/python mjpegsw.py
OpenCV: AVFoundation didn't find any attached Video Input Devices!
OpenCV: camera failed to properly initialize!
Mjpeg server started on http://localhost:5001

I neglected to mention I ssh'd onto the Mac Mini and running the command, this I guess has a problem (display) when trying to run the mjpegsw script. I run directly on the mini and it started successfully.

I managed to get this to work to the point where the webcam is now viewable in the browser (http://localhost:5001) . I just need to work out the correct stream URL entry for Octoprint.

Thanks for your help and work on mjpeg.

*The stream URL was http://localhost:5001/cam.mjpg

1 Like

soooo... will this not work on macOS 10.11?

installed everything as you've stated here. seems like the dependancies all compile and install as intended, however when attempting to run on MacMini with 10.10.5 I get an error that it can't import cv2. I tried to pip install cv2 and it says can't find, but the pip install -r requirements.txt says everything installed correctly there (guessing its an OpenCV issue?) suggestions? also. I am trying to use this with an old firewire iSight. OBS and everything works just fine with it. so hopefully this will too. I just can't figure out how to get a stream that octo likes. Hope you can help

When you tried to install cv2 or run the script did you activated the virtualenv or started the script with

.env/bin/python mjpegsw.py

For enable the virtualenv you need to use:
source .env/bin/activate

will try and report back, I didnt see that in the initial process, so I might have missed it or was tutorial blind.
EDIT:
just tried:
.env/bin/activate
result
-bash: .env/bin/activate: Permission denied
tried sudo .env/bin/activate
result
sudo: .env/bin/activate: command not found

I'm rebooting the computer as a whole right now. I'll let you know if anything changes.

edit #2:

same result after reboot. Β―\(Β°_o)/Β―

this means that you don't have virtualenv configured for the project, like I wrote a couple of posts upper,
ps. the commands that I wrote earlier need to be executed in the folder you cloned from github.

here's what I did:

downloaded the .zip version of the GitHub repo. expanded that, opened terminal and cd'd to that directory.

ran the following:

pip install virtualenv
brew install python (though i have 2.7 for octoprint, you'd said this requires 3)
allowed that time to install/compile
virtualenv -p python3.7 .env
.env/bin/pip install -r requirements.txt
allowed that to process to completion, then from that same directory attempted to run:
.env/bin/python mjpegsw.py

I only get the error listed, nothing else.

For the record i am on macOS Yosemite 10.10.5 on an Intel Mac Mini Late 2009. Octoprint runs perfectly and the iSight camera works with OBS and Facetime on the system (its an OG FireWire 400 Camera hooked up with a Firewire 800 to 400 adapter)

Everything seems correct, are you running from a remote terminal or on the console ?

Running in terminal on the Mac itself. Octoprint is running at the same time. I've tried it both with OP running and not. I am not using SSH.

Edit: I did go and just for a laugh try launching Xcode. I have it installed but apparently never launched the program. it'd never been a problem before so I will let you know if that manages to change anything as Xcode is now installing extras.

Ok finally we found the solution for @jabackes, on MacOs Yosemite you need to install opencv==4.0.0.21
I will leave it here for reference.

I think you meant Yosemite, but yes, that seems to have done it! Octoprint can access the camera, and works with the OG iSight Firewire camera... I do notice it doesnt like to stay working 100% though, getting a segmentation fault : 11 if I quickly access the snap or cam over and over... might just be me fat-fingering though.

ooops Yosemite, you are right, for segmentation fault you may try to put it in a loop inside a batch file.

Hi @meska,

I have the problem listed previously by @liffg if I use virtualenv -p python .env. If I try virtualenv -p python3.7 .env, I get 'The path python3.7 does not exist'. I've tried different variations, including using the direct path (usr/local/bin/python3.7). None work.

Help?

Thanks

Hello Ron,
did you installed python with homebrew?

Marco

Yes. Homebrew updated and then successfully installed Python 3.7.7