Selaa lähdekoodia

Add labelme_on_docker

Kentaro Wada 8 vuotta sitten
vanhempi
commit
01ebb5e835
3 muutettua tiedostoa jossa 94 lisäystä ja 12 poistoa
  1. 17 11
      README.md
  2. 72 0
      scripts/labelme_on_docker
  3. 5 1
      setup.py

+ 17 - 11
README.md

@@ -21,25 +21,25 @@ Installation
 On Ubuntu:
 
 ```bash
-$ sudo apt-get install python-qt4 pyqt4-dev-tools
+sudo apt-get install python-qt4 pyqt4-dev-tools
 
-$ sudo pip install labelme
+sudo pip install labelme
 ```
 
 On OS X:
 
 ```bash
-$ brew install qt qt4
+brew install qt qt4
 
-$ pip install labelme
+pip install labelme
 ```
 
 On macOS Sierra:
 
 ```bash
-$ brew install pyqt5
+brew install pyqt5
 
-$ pip install git+https://github.com/wkentaro/labelme.git@pyqt5
+pip install git+https://github.com/wkentaro/labelme.git@pyqt5
 ```
 
 
@@ -51,9 +51,15 @@ Usage
 Run `labelme --help` for detail.
 
 ```bash
-$ labelme  # Open GUI
-$ labelme _static/IMG_6319.jpg  # Specify file
-$ labelme _static/IMG_6319.jpg -O _static/IMG_6319.json  # Close window after the save
+labelme  # Open GUI
+labelme _static/IMG_6319.jpg  # Specify file
+labelme _static/IMG_6319.jpg -O _static/IMG_6319.json  # Close window after the save
+```
+
+If you are installed [docker](https://www.docker.com), you can run:
+
+```bash
+labelme_on_docker _static/IMG_6319.jpg -O _static/IMG_6319.json
 ```
 
 The annotations are saved as a [JSON](http://www.json.org/) file. The
@@ -64,7 +70,7 @@ file includes the image itself.
 To view the json file quickly, you can use utility script:
 
 ```bash
-$ labelme_draw_json _static/IMG_6319.json
+labelme_draw_json _static/IMG_6319.json
 ```
 
 **Convert to Dataset**
@@ -73,7 +79,7 @@ To convert the json to set of image and label, you can run following:
 
 
 ```bash
-$ labelme_json_to_dataset _static/IMG_6319.json
+labelme_json_to_dataset _static/IMG_6319.json
 ```
 
 

+ 72 - 0
scripts/labelme_on_docker

@@ -0,0 +1,72 @@
+#!/usr/bin/env python
+
+import argparse
+import json
+import os.path as osp
+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
+    else:
+        raise RuntimeError('No ip is found.')
+
+
+def labelme_on_docker(image_file, out_file):
+    ip = get_ip()
+    cmd = 'xhost + %s' % ip
+    subprocess.check_output(shlex.split(cmd))
+
+    out_file = osp.abspath(out_file)
+    if osp.exists(out_file):
+        raise RuntimeError('File exists: %s' % out_file)
+    else:
+        open(osp.abspath(out_file), 'w')
+
+    cmd = 'docker run -it --rm -e DISPLAY={0}:0' \
+        ' -v /tmp/.X11-unix:/tmp/.X11-unix' \
+        ' -v {1}:{2}' \
+        ' -v {3}:{4}' \
+        ' -w /root' \
+        ' labelme' \
+        ' labelme {2} -O {4}'
+    cmd = cmd.format(
+        ip,
+        osp.abspath(image_file),
+        osp.join('/root', osp.basename(image_file)),
+        osp.abspath(out_file),
+        osp.join('/root', osp.basename(out_file)),
+    )
+    subprocess.call(shlex.split(cmd))
+
+    try:
+        json.load(open(out_file))
+        return out_file
+    except Exception as e:
+        raise RuntimeError('Annotation is cancelled.')
+
+
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument('image_file')
+    parser.add_argument('-O', '--output', required=True)
+    args = parser.parse_args()
+
+    try:
+        out_file = labelme_on_docker(args.image_file, args.output)
+        print('Saved to: %s' % out_file)
+    except RuntimeError as e:
+        sys.stderr.write(e.__str__() + '\n')
+        sys.exit(1)
+
+
+if __name__ == '__main__':
+    main()

+ 5 - 1
setup.py

@@ -73,5 +73,9 @@ setup(
     ],
     package_data={'labelme': ['icons/*', 'resources.qrc']},
     entry_points={'console_scripts': ['labelme=labelme.app:main']},
-    scripts=['scripts/labelme_draw_json', 'scripts/labelme_json_to_dataset'],
+    scripts=[
+        'scripts/labelme_draw_json',
+        'scripts/labelme_json_to_dataset',
+        'scripts/labelme_on_docker',
+    ],
 )