Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix --file and support expanding ~ #187

Merged
merged 2 commits into from
Sep 22, 2022
Merged

Fix --file and support expanding ~ #187

merged 2 commits into from
Sep 22, 2022

Conversation

Freed-Wu
Copy link
Contributor

Fix --file and support expanding ~

Fix #176

@Freed-Wu
Copy link
Contributor Author

Have rebased to solve conflict.

❯ pix2tex -f ~/Desktop/a.png
S=\int_{x}\left\{\frac{1}{2}\sum_{a}\partial^{\mu}\chi_{a}\partial_{\mu}\chi_{a}+V(\rho)\right\},

@Freed-Wu
Copy link
Contributor Author

pix2tex -f FILE only support OCRing a file. How about pix2tex FILE [...]? Then user can use pix2tex *.png to get all OCR results of png. It will be more convenient.

$ pix2tex *.png
a.png: \alpha
b.png: \beta
...

@lukas-blecher
Copy link
Owner

Yes, that would be useful!

@Freed-Wu
Copy link
Contributor Author

I have tested it can work for me. Please test in your machine 😄

❯ pix2tex
Predict LaTeX code for image ("?"/"h" for help). /
[Errno 21] Is a directory: '/'
Predict LaTeX code for image ("?"/"h" for help). /home/wzy/Desktop/a.png
S=\int_{x}\left\{\frac{1}{2}\sum_{a}\partial^{\mu}\chi_{a}\partial_{\mu}\chi_{a}+V(\rho)\right\},
Predict LaTeX code for image ("?"/"h" for help). empty
[Errno 2] No such file or directory: 'empty'
Last image is: S=\int_{x}\left\{\frac12\sum_{a}\partial^{\mu}\chi_{a}\partial_{\mu}\chi_{a}+V(\rho)\right\},
Predict LaTeX code for image ("?"/"h" for help).
ImageGrab.grabclipboard() is macOS and Windows only
Last image is: S=\int_{x}\left\{\frac{1}{2}\sum_{a}\partial^{\mu}\chi_{a}\partial_{\mu}\chi_{a}+V(\rho)\right\},
Predict LaTeX code for image ("?"/"h" for help). %
❯ pix2tex / ~/Desktop/a.png empty ~/Desktop/a.png
/: [Errno 21] Is a directory: '/'
/home/wzy/Desktop/a.png: S=\int_{x}\left\{\frac12\sum_{a}\partial^{\mu}\chi_{a}\partial_{\mu}\chi_{a}+V(\rho)\right\},
empty: [Errno 2] No such file or directory: 'empty'
/home/wzy/Desktop/a.png: S=\int_{x}\left\{\frac12\sum_{a}\partial^{\mu}\chi_{a}\partial_{\mu}\chi_{a}+V(\rho)\right\},

@lukas-blecher
Copy link
Owner

In __main__.py there is now the rouge argument arguments.gnome in L24.

Then user can use pix2tex *.png to get all OCR results of png.

Also the glob doesn't work for me.
(And relative paths would be nice :) )

@Freed-Wu
Copy link
Contributor Author

Fixed.

@lukas-blecher
Copy link
Owner

Here is glob and relative path support. Can you apply the patch please?

# pix2tex/cli.py
@@ -3,8 +3,9 @@ import pandas.io.clipboard as clipboard
 from PIL import ImageGrab
 from PIL import Image
 import os
+from pathlib import Path
 import sys
-from typing import Tuple
+from typing import List, Optional, Tuple
 import atexit
 from contextlib import suppress
 import logging
@@ -178,21 +179,34 @@ def output_prediction(pred, args):
 
 
 def predict(model, file, arguments):
-    with suppress(KeyboardInterrupt):
-        img = None
-        if file:
-            try:
-                img = Image.open(os.path.expanduser(file))
-            except Exception as e:
-                print(e, end='')
-        else:
-            try:
-                img = ImageGrab.grabclipboard()
-            except NotImplementedError as e:
-                print(e, end='')
-        pred = model(img)
-        output_prediction(pred, arguments)
+    img = None
+    if file:
+        try:
+            img = Image.open(os.path.expanduser(file))
+        except Exception as e:
+            print(e, end='')
+    else:
+        try:
+            img = ImageGrab.grabclipboard()
+        except NotImplementedError as e:
+            print(e, end='')
+    pred = model(img)
+    output_prediction(pred, arguments)
 
+def check_file_path(paths:List[Path], wdir:Optional[Path]=None)->List[str]:
+    files = []
+    for path in paths:
+        if type(path)==str:
+            if path=='':
+                continue
+            path=Path(path)
+        pathsi = ([path] if wdir is None else [path, wdir/path])
+        for p in pathsi:
+            if p.exists():
+                files.append(str(p.resolve()))
+            elif '*' in path.name:
+                files.extend([str(pi.resolve()) for pi in p.parent.glob(p.name)])
+    return list(set(files))
 
 def main(arguments):
     path = user_data_dir('pix2tex')
@@ -204,11 +218,12 @@ def main(arguments):
         with suppress(OSError):
             readline.read_history_file(history_file)
         atexit.register(readline.write_history_file, history_file)
-    files = list(map(lambda file: os.path.realpath(file), arguments.file))
+    files = check_file_path(arguments.file)
+    wdir = Path(os.getcwd())
     with in_model_path():
         model = LatexOCR(arguments)
         if files:
-            for file in files:
+            for file in check_file_path(arguments.file, wdir):
                 print(file + ': ', end='')
                 predict(model, file, arguments)
                 model.last_pic = None
@@ -218,7 +233,7 @@ def main(arguments):
         pat = re.compile(r't=([\.\d]+)')
         while True:
             try:
-                instructions = input('Predict LaTeX code for image ("?"/"h" for help). ')
+                instructions = input('Predict LaTeX code for image ("h" for help). ')
             except KeyboardInterrupt:
                 # TODO: make the last line gray
                 print("")
@@ -263,5 +278,13 @@ def main(arguments):
                 model.args.temperature = float(t)+1e-8
                 print('new temperature: T=%.3f' % model.args.temperature)
                 continue
-            predict(model, file, arguments)
+            files = check_file_path(file.split(' '), wdir)
+            with suppress(KeyboardInterrupt):
+                if files:
+                    for file in files:
+                        if len(files)>1:
+                            print(file + ': ', end='')
+                        predict(model, file, arguments)
+                else:
+                    predict(model, file, arguments)
             file = None

@Freed-Wu
Copy link
Contributor Author

It is not a legal patch because miss some lines. Edit then git diff > a.patch to get a legal patch. And you can edit directly because

Screenshot from 2022-09-22 23-04-20

@lukas-blecher
Copy link
Owner

lukas-blecher commented Sep 22, 2022

I know I can but I'm not allowed to because of my contract rn.

diff --git a/pix2tex/cli.py b/pix2tex/cli.py
index f590896..57867f6 100644
--- a/pix2tex/cli.py
+++ b/pix2tex/cli.py
@@ -3,8 +3,9 @@ import pandas.io.clipboard as clipboard
 from PIL import ImageGrab
 from PIL import Image
 import os
+from pathlib import Path
 import sys
-from typing import Tuple
+from typing import List, Optional, Tuple
 import atexit
 from contextlib import suppress
 import logging
@@ -178,21 +179,34 @@ def output_prediction(pred, args):
 
 
 def predict(model, file, arguments):
-    with suppress(KeyboardInterrupt):
-        img = None
-        if file:
-            try:
-                img = Image.open(os.path.expanduser(file))
-            except Exception as e:
-                print(e, end='')
-        else:
-            try:
-                img = ImageGrab.grabclipboard()
-            except NotImplementedError as e:
-                print(e, end='')
-        pred = model(img)
-        output_prediction(pred, arguments)
+    img = None
+    if file:
+        try:
+            img = Image.open(os.path.expanduser(file))
+        except Exception as e:
+            print(e, end='')
+    else:
+        try:
+            img = ImageGrab.grabclipboard()
+        except NotImplementedError as e:
+            print(e, end='')
+    pred = model(img)
+    output_prediction(pred, arguments)
 
+def check_file_path(paths:List[Path], wdir:Optional[Path]=None)->List[str]:
+    files = []
+    for path in paths:
+        if type(path)==str:
+            if path=='':
+                continue
+            path=Path(path)
+        pathsi = ([path] if wdir is None else [path, wdir/path])
+        for p in pathsi:
+            if p.exists():
+                files.append(str(p.resolve()))
+            elif '*' in path.name:
+                files.extend([str(pi.resolve()) for pi in p.parent.glob(p.name)])
+    return list(set(files))
 
 def main(arguments):
     path = user_data_dir('pix2tex')
@@ -204,11 +218,12 @@ def main(arguments):
         with suppress(OSError):
             readline.read_history_file(history_file)
         atexit.register(readline.write_history_file, history_file)
-    files = list(map(lambda file: os.path.realpath(file), arguments.file))
+    files = check_file_path(arguments.file)
+    wdir = Path(os.getcwd())
     with in_model_path():
         model = LatexOCR(arguments)
         if files:
-            for file in files:
+            for file in check_file_path(arguments.file, wdir):
                 print(file + ': ', end='')
                 predict(model, file, arguments)
                 model.last_pic = None
@@ -218,7 +233,7 @@ def main(arguments):
         pat = re.compile(r't=([\.\d]+)')
         while True:
             try:
-                instructions = input('Predict LaTeX code for image ("?"/"h" for help). ')
+                instructions = input('Predict LaTeX code for image ("h" for help). ')
             except KeyboardInterrupt:
                 # TODO: make the last line gray
                 print("")
@@ -263,5 +278,13 @@ def main(arguments):
                 model.args.temperature = float(t)+1e-8
                 print('new temperature: T=%.3f' % model.args.temperature)
                 continue
-            predict(model, file, arguments)
+            files = check_file_path(file.split(' '), wdir)
+            with suppress(KeyboardInterrupt):
+                if files:
+                    for file in files:
+                        if len(files)>1:
+                            print(file + ': ', end='')
+                        predict(model, file, arguments)
+                else:
+                    predict(model, file, arguments)
             file = None

@Freed-Wu
Copy link
Contributor Author

OK.

@lukas-blecher lukas-blecher merged commit 54dca2a into lukas-blecher:main Sep 22, 2022
@Freed-Wu Freed-Wu deleted the file branch September 22, 2022 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature] support ~
2 participants