yeswanthvarma commited on
Commit
986c935
·
verified ·
1 Parent(s): 3eb223d

Update utils/image_processor.py

Browse files
Files changed (1) hide show
  1. utils/image_processor.py +13 -32
utils/image_processor.py CHANGED
@@ -18,56 +18,37 @@ except Exception as e:
18
  print("Text extraction may not work properly.")
19
  ocr_available = False
20
 
21
- def preprocess_image(image):
22
- """
23
- Preprocess image to improve OCR accuracy
24
- """
25
- if len(image.shape) == 3:
26
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
27
- else:
28
- gray = image
29
-
30
- denoised = cv2.fastNlMeansDenoising(gray, h=10)
31
-
32
- processed = cv2.adaptiveThreshold(
33
- denoised, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
34
- cv2.THRESH_BINARY, 11, 2
35
- )
36
-
37
- return processed
38
-
39
  def extract_text_from_image(image_path):
40
- """
41
- Extract text from an image file using EasyOCR
42
- """
43
  try:
 
44
  if not ocr_available:
45
- raise ValueError("EasyOCR is not available")
46
-
 
47
  image = cv2.imread(image_path)
48
  if image is None:
49
- raise ValueError(f"Could not read image at {image_path}")
50
-
51
- processed_image = preprocess_image(image)
52
 
 
53
  temp_path = os.path.join(os.path.dirname(image_path), f"temp_{os.path.basename(image_path)}")
54
  cv2.imwrite(temp_path, processed_image)
 
55
 
56
  results = reader.readtext(temp_path)
 
57
 
58
- try:
59
- os.remove(temp_path)
60
- except:
61
- pass
62
 
63
  text = ' '.join([result[1] for result in results]).strip()
64
-
65
  if not text:
 
66
  results = reader.readtext(image_path)
67
  text = ' '.join([result[1] for result in results]).strip()
68
 
 
69
  return text or "Text extraction failed. Please enter text manually."
70
-
71
  except Exception as e:
72
  print(f"OCR failed: {str(e)}")
73
  return "Text extraction failed. Please enter text manually."
 
18
  print("Text extraction may not work properly.")
19
  ocr_available = False
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def extract_text_from_image(image_path):
 
 
 
22
  try:
23
+ print(f"📂 Reading image from: {image_path}")
24
  if not ocr_available:
25
+ print("EasyOCR not available.")
26
+ return "Text extraction failed. Please enter text manually."
27
+
28
  image = cv2.imread(image_path)
29
  if image is None:
30
+ print(" cv2 could not read image.")
31
+ return "Image could not be read."
 
32
 
33
+ processed_image = preprocess_image(image)
34
  temp_path = os.path.join(os.path.dirname(image_path), f"temp_{os.path.basename(image_path)}")
35
  cv2.imwrite(temp_path, processed_image)
36
+ print(f"🖼️ Temp processed image saved at: {temp_path}")
37
 
38
  results = reader.readtext(temp_path)
39
+ print("🔍 OCR Raw Output:", results)
40
 
41
+ os.remove(temp_path)
 
 
 
42
 
43
  text = ' '.join([result[1] for result in results]).strip()
 
44
  if not text:
45
+ print("⚠️ No text found, retrying with original image...")
46
  results = reader.readtext(image_path)
47
  text = ' '.join([result[1] for result in results]).strip()
48
 
49
+ print("✅ Extracted Text:", text)
50
  return text or "Text extraction failed. Please enter text manually."
51
+
52
  except Exception as e:
53
  print(f"OCR failed: {str(e)}")
54
  return "Text extraction failed. Please enter text manually."