-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgit-extract-image-texts
More file actions
executable file
·104 lines (82 loc) · 2.92 KB
/
Copy pathgit-extract-image-texts
File metadata and controls
executable file
·104 lines (82 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
import os
import sys
import json
import logging
import mimetypes
import shutil
import subprocess
from pathlib import Path
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
tesseract_path = shutil.which("tesseract")
if tesseract_path == None:
logger.error("please ensure tesseract is on your path")
sys.exit(1)
try:
import git
except ModuleNotFoundError:
logger.error("please install GitPython to use this script")
sys.exit(1)
def extract_text(commit_id, path):
with subprocess.Popen(
('git', 'show', f'{commit_id}:{path}'),
text=False,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
) as git_show:
return subprocess.check_output(
(tesseract_path, '-', '-'),
stdin=git_show.stdout,
stderr=subprocess.DEVNULL,
text=True,
)
def get_git_image_text_as_jsonl(repo_path: str):
logger.info(f"Opening repository at {repo_path}...")
try:
repo = git.Repo(repo_path)
except git.exc.InvalidGitRepositoryError:
logger.error(
f"Error: Path '{repo_path}' does not contain a valid git repository."
)
return False
logger.info("Generating JSONL file...")
data = {}
try:
for commit in repo.iter_commits("--all"):
commit_id = commit.hexsha
for item in commit.tree.traverse():
if not isinstance(item, git.Blob):
continue
mimetype, encoding = mimetypes.guess_type(item.path)
if not (mimetype and mimetype.startswith("image/")):
continue
if encoding is not None:
logger.warning("skipping encoded item: encoding=%s path=%s", encoding, item.path)
continue
try:
text = extract_text(commit_id, item.path)
path, filename = os.path.split(item.path)
loc_in_data = data
# dump the data like {"commit": {"path": {"to": {"image.png": "text"}}}}
for part in [commit_id] + path.lstrip('/').split('/'):
if not part:
continue
if part not in loc_in_data:
loc_in_data[part] = {}
loc_in_data = loc_in_data[part]
loc_in_data[filename] = text
except Exception as e:
logger.error("could not extract text: %s", e)
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return False
if data:
print(json.dumps(data))
return True
if __name__ == "__main__":
repo_paths = sys.argv[1:] or ["."]
for repo_path in repo_paths:
get_git_image_text_as_jsonl(repo_path)