-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocrScript.py
More file actions
159 lines (114 loc) · 3.49 KB
/
ocrScript.py
File metadata and controls
159 lines (114 loc) · 3.49 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import easyocr
import cv2
import re
import sys
# -----------------------------
# Image Preprocessing
# -----------------------------
def preprocess(image_path):
img = cv2.imread(image_path)
if img is None:
raise ValueError("Invalid image path")
# Resize (helps OCR accuracy)
img = cv2.resize(img, None, fx=1.5, fy=1.5)
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Slight denoise
gray = cv2.GaussianBlur(gray, (3, 3), 0)
return gray
# -----------------------------
# Extract OCR Lines
# -----------------------------
def extract_lines(image_path):
reader = easyocr.Reader(['en'])
img = preprocess(image_path)
results = reader.readtext(img)
# Sort top → bottom
results.sort(key=lambda x: x[0][0][1])
lines = []
for bbox, text, conf in results:
if conf > 0.4:
lines.append(text.strip())
return lines
# -----------------------------
# Merge broken OCR lines
# -----------------------------
def merge_lines(lines):
merged = []
buffer = ""
for line in lines:
line = line.strip()
if not line:
continue
# If line ends with price → complete item
if re.search(r"\d{2,4}$", line):
merged_line = (buffer + " " + line).strip()
merged.append(merged_line)
buffer = ""
else:
buffer += " " + line
return [m.strip() for m in merged if m.strip()]
# -----------------------------
# Parse Menu Items
# -----------------------------
def parse_menu(lines):
menu_items = []
# Price at end of line
price_pattern = r"(?:₹\s*)?(\d{2,4})(?:\.\d{1,2})?$"
for line in lines:
line = line.strip()
# Extract price
match = re.search(price_pattern, line)
if not match:
continue
price = int(match.group(1))
# Remove price from text
name = re.sub(price_pattern, "", line).strip()
# Clean text
name = re.sub(r"[^a-zA-Z0-9\s&()/-]", "", name)
name = re.sub(r"\s+", " ", name)
if len(name) < 3:
continue
# -----------------------------
# Category detection
# -----------------------------
category = "unknown"
veg_keywords = [
"paneer", "veg", "aloo", "mushroom", "dal", "idly",
"dosa", "uttapam", "rice", "thali", "sabzi"
]
nonveg_keywords = [
"chicken", "mutton", "fish", "egg", "lollypop",
"kabab", "keema", "pepper"
]
lower_name = name.lower()
if any(word in lower_name for word in veg_keywords):
category = "veg"
elif any(word in lower_name for word in nonveg_keywords):
category = "non-veg"
menu_items.append({
"name": name,
"price": price,
"category": category
})
return menu_items
# -----------------------------
# Main Execution
# -----------------------------
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python menu_ocr.py <image_path>")
sys.exit(1)
image_path = sys.argv[1]
print("\n🔍 Processing image...\n")
# Step 1: OCR
lines = extract_lines(image_path)
# Step 2: Fix broken lines
merged_lines = merge_lines(lines)
# Step 3: Parse menu
menu = parse_menu(merged_lines)
# Output
print("🍽️ Final Structured Menu:\n")
for item in menu:
print(item)
print("\n✅ Done")