chenguittiMaroua commited on
Commit
66d1ff7
·
verified ·
1 Parent(s): 519b4dc

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +24 -4
main.py CHANGED
@@ -220,7 +220,20 @@ async def startup_event():
220
 
221
 
222
 
223
-
 
 
 
 
 
 
 
 
 
 
 
 
 
224
 
225
 
226
 
@@ -310,17 +323,24 @@ def extract_text(content: bytes, file_ext: str) -> str:
310
  return "\n".join(page.get_text("text") for page in pdf)
311
 
312
  elif file_ext in {"jpg", "jpeg", "png"}:
 
313
  try:
314
  image = Image.open(io.BytesIO(content))
 
 
315
  text = pytesseract.image_to_string(image, config='--psm 6')
316
  if text.strip():
317
  return text
318
-
 
 
 
 
319
  captioner = get_image_captioner()
320
  result = captioner(image)
321
  return result[0]['generated_text']
322
- except Exception as img_e:
323
- logger.error(f"Image processing failed: {str(img_e)}")
324
  raise ValueError("Could not extract text or caption from image")
325
 
326
  except Exception as e:
 
220
 
221
 
222
 
223
+ def preprocess_image_for_ocr(image):
224
+ """Apply basic image processing to improve OCR results"""
225
+ # Convert to grayscale
226
+ image = image.convert('L')
227
+
228
+ # Increase contrast
229
+ from PIL import ImageEnhance
230
+ enhancer = ImageEnhance.Contrast(image)
231
+ image = enhancer.enhance(2.0)
232
+
233
+ # Apply threshold
234
+ image = image.point(lambda x: 0 if x < 128 else 255, '1')
235
+
236
+ return image
237
 
238
 
239
 
 
323
  return "\n".join(page.get_text("text") for page in pdf)
324
 
325
  elif file_ext in {"jpg", "jpeg", "png"}:
326
+ # First try OCR
327
  try:
328
  image = Image.open(io.BytesIO(content))
329
+ # Pre-process image for better OCR results
330
+ image = image.convert('L') # Convert to grayscale
331
  text = pytesseract.image_to_string(image, config='--psm 6')
332
  if text.strip():
333
  return text
334
+ except Exception as ocr_error:
335
+ logger.warning(f"OCR failed: {str(ocr_error)}")
336
+
337
+ # If OCR fails, try image captioning
338
+ try:
339
  captioner = get_image_captioner()
340
  result = captioner(image)
341
  return result[0]['generated_text']
342
+ except Exception as caption_error:
343
+ logger.error(f"Image captioning failed: {str(caption_error)}")
344
  raise ValueError("Could not extract text or caption from image")
345
 
346
  except Exception as e: