Update app.py
Browse files2 unmodified lines345678910112 unmodified lines141516171819202122232425262717 unmodified lines45464748495051522 unmodified lines55565758596061626364656667686970712 unmodified linesimport numpy as npimport osfrom huggingface_hub import snapshot_download# --- STRICT UPDATE: Use PPStructureV3 directly ---# Your logs confirmed this class exists in your installed version.from paddleocr import PPStructureV3# --- STEP 1: Download the Model ---2 unmodified linesprint(f"Model downloaded to: {model_path}")# --- STEP 2: Initialize V3 Engine ---# We instantiate PPStructureV3 directly.layout_engine = PPStructureV3(layout_model_dir=model_path,table=False,ocr=False,show_log=True,use_angle_cls=True,enable_mkldnn=False # Keeps the crash fix while using the new model)def analyze_layout(input_image):17 unmodified lines# Iterate through resultsfor region in result:# V3 Output format usually includes 'layout_bbox'if isinstance(region, dict):box = region.get('layout_bbox') or region.get('bbox')label = region.get('label', 'unknown')else:2 unmodified linesif box is None: continue# Draw the boxx1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3])# Color codingcolor = (0, 255, 0)if label == 'title': color = (0, 0, 255)elif label == 'figure': color = (255, 0, 0)elif label == 'table': color = (255, 255, 0)cv2.rectangle(viz_image, (x1, y1), (x2, y2), color, 3)cv2.putText(viz_image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)detections_text.append(f"Found {label} at {box}")return viz_image, "\n".join(detections_text)2 unmodified lines3456782 unmodified lines1112131415161718192021222324252617 unmodified lines4445464748495051522 unmodified lines55565758596061626364656667686970717273742 unmodified linesimport numpy as npimport osfrom huggingface_hub import snapshot_downloadfrom paddleocr import PPStructureV3# --- STEP 1: Download the Model ---2 unmodified linesprint(f"Model downloaded to: {model_path}")# --- STEP 2: Initialize V3 Engine ---layout_engine = PPStructureV3(layout_model_dir=model_path,table=False,ocr=False,# show_log=True, <-- REMOVED (Caused the crash)# In V3, 'use_angle_cls' is often renamed for documents:use_doc_orientation_classify=True,enable_mkldnn=False # Keeps the crash fix)def analyze_layout(input_image):17 unmodified lines# Iterate through resultsfor region in result:# V3 Output format usually includes 'layout_bbox' or 'bbox'if isinstance(region, dict):# Try specific v3 keys first, fallback to genericbox = region.get('layout_bbox') or region.get('bbox')label = region.get('label', 'unknown')else:2 unmodified linesif box is None: continue# Draw the boxtry:x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3])# Color codingcolor = (0, 255, 0) # Default Greenif label == 'title': color = (0, 0, 255) # Redelif label == 'figure': color = (255, 0, 0) # Blueelif label == 'table': color = (255, 255, 0) # Cyancv2.rectangle(viz_image, (x1, y1), (x2, y2), color, 3)cv2.putText(viz_image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)detections_text.append(f"Found {label} at {box}")except Exception:passreturn viz_image, "\n".join(detections_text)
app.py+21-18