github-actions[bot] commited on
Commit
d4ed2a5
·
1 Parent(s): 3c55f2b

Sync from GitHub: bbd3a8da74f29857d203fdb7913c964597eb127b

Browse files
Files changed (1) hide show
  1. inference.py +15 -16
inference.py CHANGED
@@ -69,7 +69,7 @@ class InferenceProcessor:
69
  """Handles VLM inference, validation, and result processing"""
70
 
71
  @staticmethod
72
- def preprocess_image(image_path: str, enhance: bool = None) -> Image.Image:
73
  """Load, enhance (optional), and resize image if needed
74
 
75
  Args:
@@ -77,7 +77,7 @@ class InferenceProcessor:
77
  enhance: Whether to enhance image quality before processing (None=use config default)
78
 
79
  Returns:
80
- Preprocessed PIL Image ready for VLM inference
81
  """
82
  # Use config default if not specified
83
  if enhance is None:
@@ -85,7 +85,6 @@ class InferenceProcessor:
85
 
86
  # Step 1: Enhance image if enabled
87
  enhanced_path = image_path
88
- cleanup_enhanced = False
89
 
90
  if enhance:
91
  try:
@@ -95,7 +94,6 @@ class InferenceProcessor:
95
  scale=ENHANCEMENT_SCALE,
96
  model_name=ENHANCEMENT_MODEL
97
  )
98
- cleanup_enhanced = (enhanced_path != image_path)
99
  except Exception as e:
100
  print(f"⚠️ Enhancement failed: {str(e)}, using original image")
101
  enhanced_path = image_path
@@ -103,13 +101,6 @@ class InferenceProcessor:
103
  # Step 2: Load image
104
  image = Image.open(enhanced_path).convert("RGB")
105
 
106
- # Cleanup enhanced temp file if created
107
- if cleanup_enhanced:
108
- try:
109
- os.unlink(enhanced_path)
110
- except:
111
- pass
112
-
113
  # Step 3: Resize if too large
114
  if max(image.size) > MAX_IMAGE_SIZE:
115
  ratio = MAX_IMAGE_SIZE / max(image.size)
@@ -117,7 +108,8 @@ class InferenceProcessor:
117
  image = image.resize(new_size, Image.LANCZOS)
118
  print(f"🔄 Image resized to {new_size}")
119
 
120
- return image
 
121
 
122
  @staticmethod
123
  def run_vlm_extraction(image: Image.Image) -> Tuple[str, float]:
@@ -348,12 +340,12 @@ class InferenceProcessor:
348
 
349
  # Step 1: Preprocess image (with optional enhancement)
350
  t1 = time.time()
351
- image = InferenceProcessor.preprocess_image(image_path, enhance=enhance)
352
  timing_breakdown['image_preprocessing'] = round(time.time() - t1, 3)
353
 
354
- # Step 2: YOLO Detection
355
  t2 = time.time()
356
- signature_info, stamp_info, signature_conf, stamp_conf = model_manager.detect_sign_stamp(image_path)
357
  timing_breakdown['yolo_detection'] = round(time.time() - t2, 3)
358
 
359
  # Step 3: VLM Extraction
@@ -361,10 +353,17 @@ class InferenceProcessor:
361
  vlm_output, vlm_latency = InferenceProcessor.run_vlm_extraction(image)
362
  timing_breakdown['vlm_inference'] = round(vlm_latency, 3)
363
 
364
- # Clean up image
365
  image.close()
366
  del image
367
 
 
 
 
 
 
 
 
368
  # Step 4: Parse JSON
369
  t4 = time.time()
370
  raw_json = InferenceProcessor.extract_json_from_output(vlm_output)
 
69
  """Handles VLM inference, validation, and result processing"""
70
 
71
  @staticmethod
72
+ def preprocess_image(image_path: str, enhance: bool = None) -> Tuple[Image.Image, str]:
73
  """Load, enhance (optional), and resize image if needed
74
 
75
  Args:
 
77
  enhance: Whether to enhance image quality before processing (None=use config default)
78
 
79
  Returns:
80
+ Tuple of (PIL Image ready for VLM, path to image file for YOLO)
81
  """
82
  # Use config default if not specified
83
  if enhance is None:
 
85
 
86
  # Step 1: Enhance image if enabled
87
  enhanced_path = image_path
 
88
 
89
  if enhance:
90
  try:
 
94
  scale=ENHANCEMENT_SCALE,
95
  model_name=ENHANCEMENT_MODEL
96
  )
 
97
  except Exception as e:
98
  print(f"⚠️ Enhancement failed: {str(e)}, using original image")
99
  enhanced_path = image_path
 
101
  # Step 2: Load image
102
  image = Image.open(enhanced_path).convert("RGB")
103
 
 
 
 
 
 
 
 
104
  # Step 3: Resize if too large
105
  if max(image.size) > MAX_IMAGE_SIZE:
106
  ratio = MAX_IMAGE_SIZE / max(image.size)
 
108
  image = image.resize(new_size, Image.LANCZOS)
109
  print(f"🔄 Image resized to {new_size}")
110
 
111
+ # Return both PIL Image and path (path will be cleaned up by caller)
112
+ return image, enhanced_path
113
 
114
  @staticmethod
115
  def run_vlm_extraction(image: Image.Image) -> Tuple[str, float]:
 
340
 
341
  # Step 1: Preprocess image (with optional enhancement)
342
  t1 = time.time()
343
+ image, enhanced_image_path = InferenceProcessor.preprocess_image(image_path, enhance=enhance)
344
  timing_breakdown['image_preprocessing'] = round(time.time() - t1, 3)
345
 
346
+ # Step 2: YOLO Detection (use enhanced image path for consistency)
347
  t2 = time.time()
348
+ signature_info, stamp_info, signature_conf, stamp_conf = model_manager.detect_sign_stamp(enhanced_image_path)
349
  timing_breakdown['yolo_detection'] = round(time.time() - t2, 3)
350
 
351
  # Step 3: VLM Extraction
 
353
  vlm_output, vlm_latency = InferenceProcessor.run_vlm_extraction(image)
354
  timing_breakdown['vlm_inference'] = round(vlm_latency, 3)
355
 
356
+ # Clean up image and enhanced file if it was created
357
  image.close()
358
  del image
359
 
360
+ # Cleanup enhanced temp file if created
361
+ if enhanced_image_path != image_path:
362
+ try:
363
+ os.unlink(enhanced_image_path)
364
+ except:
365
+ pass
366
+
367
  # Step 4: Parse JSON
368
  t4 = time.time()
369
  raw_json = InferenceProcessor.extract_json_from_output(vlm_output)