JKrishnanandhaa commited on
Commit
21876da
Β·
verified Β·
1 Parent(s): 78eafd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -7
app.py CHANGED
@@ -64,15 +64,27 @@ class ForgeryDetector:
64
 
65
  def detect(self, image):
66
  """
67
- Detect forgeries in document image
68
 
69
  Args:
70
- image: PIL Image or numpy array
71
 
72
  Returns:
73
  overlay_image: Image with detection overlay
74
  results_json: Detection results as JSON
75
  """
 
 
 
 
 
 
 
 
 
 
 
 
76
  # Convert PIL to numpy
77
  if isinstance(image, Image.Image):
78
  image = np.array(image)
@@ -197,26 +209,47 @@ class ForgeryDetector:
197
  detector = ForgeryDetector()
198
 
199
 
200
- def detect_forgery(image):
201
  """Gradio interface function"""
202
  try:
203
- overlay, results = detector.detect(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
  return overlay, results # Return dict directly, not json.dumps
205
  except Exception as e:
206
- return None, {"error": str(e)}
 
 
 
207
 
208
 
209
  # Create Gradio interface
210
  demo = gr.Interface(
211
  fn=detect_forgery,
212
- inputs=gr.Image(type="pil", label="Upload Document Image"),
213
  outputs=[
214
  gr.Image(type="numpy", label="Detection Result"),
215
  gr.JSON(label="Detection Details")
216
  ],
217
  title="πŸ“„ Document Forgery Detector",
218
  description="""
219
- Upload a document image to detect and classify forgeries.
 
 
 
 
220
 
221
  **Supported Forgery Types:**
222
  - πŸ”΄ Copy-Move: Duplicated regions within the document
 
64
 
65
  def detect(self, image):
66
  """
67
+ Detect forgeries in document image or PDF
68
 
69
  Args:
70
+ image: PIL Image, numpy array, or path to PDF file
71
 
72
  Returns:
73
  overlay_image: Image with detection overlay
74
  results_json: Detection results as JSON
75
  """
76
+ # Handle PDF files
77
+ if isinstance(image, str) and image.lower().endswith('.pdf'):
78
+ import fitz # PyMuPDF
79
+ # Open PDF and convert first page to image
80
+ pdf_document = fitz.open(image)
81
+ page = pdf_document[0] # First page
82
+ pix = page.get_pixmap(matrix=fitz.Matrix(2, 2)) # 2x scale for better quality
83
+ image = np.frombuffer(pix.samples, dtype=np.uint8).reshape(pix.height, pix.width, pix.n)
84
+ if pix.n == 4: # RGBA
85
+ image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
86
+ pdf_document.close()
87
+
88
  # Convert PIL to numpy
89
  if isinstance(image, Image.Image):
90
  image = np.array(image)
 
209
  detector = ForgeryDetector()
210
 
211
 
212
+ def detect_forgery(file):
213
  """Gradio interface function"""
214
  try:
215
+ if file is None:
216
+ return None, {"error": "No file uploaded"}
217
+
218
+ # Get file path
219
+ file_path = file.name if hasattr(file, 'name') else file
220
+
221
+ # Check if PDF
222
+ if file_path.lower().endswith('.pdf'):
223
+ # Pass PDF path directly to detector
224
+ overlay, results = detector.detect(file_path)
225
+ else:
226
+ # Load image and pass to detector
227
+ image = Image.open(file_path)
228
+ overlay, results = detector.detect(image)
229
+
230
  return overlay, results # Return dict directly, not json.dumps
231
  except Exception as e:
232
+ import traceback
233
+ error_details = traceback.format_exc()
234
+ print(f"Error: {error_details}")
235
+ return None, {"error": str(e), "details": error_details}
236
 
237
 
238
  # Create Gradio interface
239
  demo = gr.Interface(
240
  fn=detect_forgery,
241
+ inputs=gr.File(label="Upload Document (Image or PDF)", file_types=["image", ".pdf"]),
242
  outputs=[
243
  gr.Image(type="numpy", label="Detection Result"),
244
  gr.JSON(label="Detection Details")
245
  ],
246
  title="πŸ“„ Document Forgery Detector",
247
  description="""
248
+ Upload a document image or PDF to detect and classify forgeries.
249
+
250
+ **Supported Formats:**
251
+ - πŸ“· Images: JPG, PNG, BMP, TIFF, WebP
252
+ - πŸ“„ PDF: First page will be analyzed
253
 
254
  **Supported Forgery Types:**
255
  - πŸ”΄ Copy-Move: Duplicated regions within the document