Setting up a webcam in Repetier-Server for Mac

To use a webcam inside Repetier-Server, you need to stream the data as JPG or MJPG. You can use special IP webcams or a simple software, turning your webcam into a streaming webcam. Feel free to use other software as the one described here.

mjpg_stream_webcam

Unfortunately we do currently not know any easy to install software to get a mjpg stream from a OS X device. One thing we found is this python software that does actually exactly what you need. Without extra configuration it’s use is a bit awkward, so I will show here one solution to integrate it perfectly into Repetier-Server 1.2.1 or newer. In these server versions it is allowed to use a mjpg stream as source for static images as well, so it will work ideally.

  1. Install the software from https://github.com/meska/mjpeg_stream_webcam in a location of your choice.
  2. Add a script control inside the streamer folder like this using your favourite editor:
    #!/bin/bash
    CMD=$1
    PORT=5001
    # Build in camera is 0
    CAMERA=0
    
    PID=$(ps aux | grep "\-\-port $PORT" | awk '{print $2}')
    DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    cd $DIR
    
    if [ "$PID" != "" ]; then
      echo "Killing running instance"
      kill -HUP $PID
    fi
    if [ "$CMD" == "start" ]; then
      echo "Starting webcam"
     while :
     do
      .env/bin/python mjpegsw.py --camera $CAMERA --port $PORT --ipaddress 0.0.0.0 > /dev/null 2>/dev/null
        EXIT=$?
        if [ "$EXIT" != "139" ]; then
          exit 1
        fi
      done
    fi
  3. Change permission of the created control file, so it is executable:
    chmod 755 control
  4. Test if script works:
    path_to_dir/control start

    should start the webcam on all IPs with the given port. In this example https://12.0.0.1:5001 whould should now show the webcam on your browser. Next test if stopping works as well:

    path_to_dir/control
  5. Now there is a problem with further integration with the mac security. It would be nice to automatically start the webcam when printer activates. All server version prior to 1.2.2 do not have this security feature enabled, so it can not be started within the server. In 1.2.2 we added this flag so it can be integrated. On first usage you get a question from OS X if server should be allowed to access the webcam. Answer yes or it will not work. For me I needed to run the start command described later on twice when I got the question as the script was already killed due to the permission problem. So that is normal.
  6. Create or add the start and stop command as executable in /Library/Application Support/Repetier-Server/database/extcommands.xml – It might already contain some content which I marked with … so only add the two execute tags if the file exists. If it does not exist copy all and remove the … Also adjust your_path so it points to the correct control program:
    <config>
        ...
        <execute name="startwebcam" sync="false" stopOnFail="false" allowParams="false">your_path/mjpeg_stream_webcam-master/control start</execute>
    <execute name="stopwebcam" sync="false" stopOnFail="false" allowParams="false">your_path/mjpeg_stream_webcam-master/control</execute>
    </config>
  7. The extcommands.xml only get read on startup so restart server:
    sudo launchctl unload /Library/LaunchDaemons/com.repetier-server.RepetierServer.plist
    sudo launchctl load /Library/LaunchDaemons/com.repetier-server.RepetierServer.plist
  8. Test if it works. On first usage you should get a question if you want to allow access to camera. Answer with yes, then call start command again – now it should work:
    @execute startwebcam
  9. Now that you can start it within the server you should go to printer configuration->G-Codes->Event Dependent->Run on Activation and add
    @execute startwebcam
    Then in Run on Deactivation you add:
    @execute stopwebcam
    Now save and go back to printer. Deactivate printer in printer context menu and you should see webcam stops. Activate it and it should appear again.
  10. Go back to printer configuration->Webcam and add a webcam Enter as URL for static and dynamic URL: http://127.0.0.1:5001/cam.mjpg
    Adjust port if you changed it in control script. You should see the webcam now if printer is activated – save and go back to printer. Now every time the printer is activated you have the webcam running.

By default it uses the native resolution. If this uses to much bandwidth/cpu or you just want smaller images you can edit mjpegsw.py – near the end you see

capture = cv2.VideoCapture(params['camera'])

After that add

capture.set(3,640);
capture.set(4,480);

With 640 and 480 being width and height you want. Watch out to use same indentation!