rongo1 commited on
Commit
50ef2fd
·
1 Parent(s): 657f105

feat: added heic support

Browse files
Files changed (2) hide show
  1. app.py +38 -3
  2. requirements.txt +1 -0
app.py CHANGED
@@ -12,6 +12,15 @@ import logging
12
  import sys
13
  import tempfile
14
 
 
 
 
 
 
 
 
 
 
15
  # Import Google Drive functionality
16
  from google_funcs import (
17
  get_drive_service,
@@ -255,6 +264,24 @@ def extract_business_card_data(image, model_name="gemini-2.5-flash"):
255
  logger.warning("Single card extraction returned no results")
256
  return None
257
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
  def process_business_cards(images, model_name="gemini-2.5-flash", save_images=True):
259
  """Process multiple business card images and create both current run and cumulative Excel files"""
260
 
@@ -295,9 +322,12 @@ def process_business_cards(images, model_name="gemini-2.5-flash", save_images=Tr
295
  image = image_path
296
  filename = f"image_{idx+1}.png"
297
 
298
- loaded_images.append(image)
 
 
 
299
  filenames.append(filename)
300
- logger.debug(f"Successfully loaded image {idx+1}: {filename} (size: {image.size})")
301
 
302
  except Exception as e:
303
  error_msg = f"Error loading {image_path}: {str(e)}"
@@ -663,10 +693,15 @@ with gr.Blocks(title="Business Card Data Extractor") as demo:
663
 
664
  with gr.Row():
665
  with gr.Column():
 
 
 
 
 
666
  image_input = gr.File(
667
  label="Upload Business Cards",
668
  file_count="multiple",
669
- file_types=[".jpg", ".jpeg", ".png", ".webp", ".bmp"]
670
  )
671
 
672
  model_selector = gr.Dropdown(
 
12
  import sys
13
  import tempfile
14
 
15
+ # Import and register HEIF support
16
+ try:
17
+ from pillow_heif import register_heif_opener
18
+ register_heif_opener()
19
+ HEIF_SUPPORTED = True
20
+ except ImportError:
21
+ HEIF_SUPPORTED = False
22
+ logging.warning("pillow-heif not installed. HEIF/HEIC support disabled.")
23
+
24
  # Import Google Drive functionality
25
  from google_funcs import (
26
  get_drive_service,
 
264
  logger.warning("Single card extraction returned no results")
265
  return None
266
 
267
+ def convert_image_for_processing(image, filename):
268
+ """Convert image to RGB JPEG format for better compatibility"""
269
+ try:
270
+ # Convert to RGB if necessary (HEIF images might be in different modes)
271
+ if image.mode != 'RGB':
272
+ image = image.convert('RGB')
273
+
274
+ # Create a buffer for the converted image
275
+ buffer = io.BytesIO()
276
+ image.save(buffer, format='JPEG', quality=95)
277
+ buffer.seek(0)
278
+
279
+ # Return the converted image
280
+ return Image.open(buffer)
281
+ except Exception as e:
282
+ logger.warning(f"Could not convert {filename}: {str(e)}. Using original image.")
283
+ return image
284
+
285
  def process_business_cards(images, model_name="gemini-2.5-flash", save_images=True):
286
  """Process multiple business card images and create both current run and cumulative Excel files"""
287
 
 
322
  image = image_path
323
  filename = f"image_{idx+1}.png"
324
 
325
+ # Convert image for better compatibility (especially for HEIF/HEIC)
326
+ converted_image = convert_image_for_processing(image, filename)
327
+
328
+ loaded_images.append(converted_image)
329
  filenames.append(filename)
330
+ logger.debug(f"Successfully loaded image {idx+1}: {filename} (size: {converted_image.size})")
331
 
332
  except Exception as e:
333
  error_msg = f"Error loading {image_path}: {str(e)}"
 
693
 
694
  with gr.Row():
695
  with gr.Column():
696
+ # Define supported file types including phone formats
697
+ supported_types = [".jpg", ".jpeg", ".png", ".webp", ".bmp"]
698
+ if HEIF_SUPPORTED:
699
+ supported_types.extend([".heif", ".heic"])
700
+
701
  image_input = gr.File(
702
  label="Upload Business Cards",
703
  file_count="multiple",
704
+ file_types=supported_types
705
  )
706
 
707
  model_selector = gr.Dropdown(
requirements.txt CHANGED
@@ -4,6 +4,7 @@ google-generativeai==0.8.0
4
  pandas==2.1.4
5
  openpyxl==3.1.2
6
  Pillow==10.2.0
 
7
  google-auth==2.23.4
8
  google-auth-oauthlib==1.1.0
9
  google-api-python-client==2.108.0
 
4
  pandas==2.1.4
5
  openpyxl==3.1.2
6
  Pillow==10.2.0
7
+ pillow-heif==0.13.1
8
  google-auth==2.23.4
9
  google-auth-oauthlib==1.1.0
10
  google-api-python-client==2.108.0