Explorar o código

Support linux on labelme_on_docker

Kentaro Wada %!s(int64=8) %!d(string=hai) anos
pai
achega
056df0794e
Modificáronse 2 ficheiros con 36 adicións e 12 borrados
  1. 12 0
      docker/Dockerfile
  2. 24 12
      scripts/labelme_on_docker

+ 12 - 0
docker/Dockerfile

@@ -1,5 +1,14 @@
 FROM ubuntu:trusty
 
+# Replace 1000 with your user / group id
+RUN export uid=1000 gid=1000 && \
+    mkdir -p /home/developer && \
+    echo "developer:x:${uid}:${gid}:Developer,,,:/home/developer:/bin/bash" >> /etc/passwd && \
+    echo "developer:x:${uid}:" >> /etc/group && \
+    echo "developer ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/developer && \
+    chmod 0440 /etc/sudoers.d/developer && \
+    chown ${uid}:${gid} -R /home/developer
+
 RUN \
   apt-get update -qq && \
   apt-get upgrade -qq -y && \
@@ -19,3 +28,6 @@ RUN apt-get install -qq -y python-scipy
 RUN apt-get install -qq -y python-skimage
 
 RUN pip install -v git+https://github.com/wkentaro/labelme.git
+
+USER developer
+ENV HOME /home/developer

+ 24 - 12
scripts/labelme_on_docker

@@ -2,22 +2,30 @@
 
 import argparse
 import json
+import os
 import os.path as osp
+import platform
 import shlex
 import subprocess
 import sys
 
 
 def get_ip():
-    cmd = 'ifconfig en0'
-    output = subprocess.check_output(shlex.split(cmd))
-    for row in output.splitlines():
-        cols = row.strip().split(' ')
-        if cols[0] == 'inet':
-            ip = cols[1]
-            return ip
+    dist = platform.platform().split('-')[0]
+    if dist == 'Linux':
+        return ''
+    elif dist == 'Darwin':
+        cmd = 'ifconfig en0'
+        output = subprocess.check_output(shlex.split(cmd))
+        for row in output.splitlines():
+            cols = row.strip().split(' ')
+            if cols[0] == 'inet':
+                ip = cols[1]
+                return ip
+        else:
+            raise RuntimeError('No ip is found.')
     else:
-        raise RuntimeError('No ip is found.')
+        raise RuntimeError('Unsupported platform.')
 
 
 def labelme_on_docker(image_file, out_file):
@@ -31,19 +39,21 @@ def labelme_on_docker(image_file, out_file):
     else:
         open(osp.abspath(out_file), 'w')
 
-    cmd = 'docker run -it --rm -e DISPLAY={0}:0' \
+    cmd = 'docker run -it --rm' \
+        ' -e DISPLAY={0}:0' \
+        ' -e QT_X11_NO_MITSHM=1' \
         ' -v /tmp/.X11-unix:/tmp/.X11-unix' \
         ' -v {1}:{2}' \
         ' -v {3}:{4}' \
-        ' -w /root' \
+        ' -w /home/developer' \
         ' labelme' \
         ' labelme {2} -O {4}'
     cmd = cmd.format(
         ip,
         osp.abspath(image_file),
-        osp.join('/root', osp.basename(image_file)),
+        osp.join('/home/developer', osp.basename(image_file)),
         osp.abspath(out_file),
-        osp.join('/root', osp.basename(out_file)),
+        osp.join('/home/developer', osp.basename(out_file)),
     )
     subprocess.call(shlex.split(cmd))
 
@@ -51,6 +61,8 @@ def labelme_on_docker(image_file, out_file):
         json.load(open(out_file))
         return out_file
     except Exception as e:
+        if open(out_file).read() == '':
+            os.remove(out_file)
         raise RuntimeError('Annotation is cancelled.')