Spaces:
Sleeping
Sleeping
Yaz Hobooti
commited on
Commit
·
257098e
1
Parent(s):
fed2115
Add debugging to barcode detection to identify the issue
Browse files
app.py
CHANGED
|
@@ -935,13 +935,17 @@ def parse_gs1(text: str) -> Optional[dict]:
|
|
| 935 |
return out or None
|
| 936 |
|
| 937 |
def _decode_zxing(pil: Image.Image) -> List[Dict[str,Any]]:
|
| 938 |
-
if not HAS_ZXING:
|
|
|
|
|
|
|
| 939 |
arr = np.asarray(pil.convert("L"))
|
| 940 |
out=[]
|
| 941 |
hints = _zxing_hints_all()
|
| 942 |
try:
|
| 943 |
res = zxingcpp.read_barcodes(arr, hints=hints) if hints is not None else zxingcpp.read_barcodes(arr)
|
| 944 |
-
|
|
|
|
|
|
|
| 945 |
res = []
|
| 946 |
|
| 947 |
for r in res or []:
|
|
@@ -1025,6 +1029,8 @@ def _decode_variants(pil: Image.Image) -> List[Dict[str,Any]]:
|
|
| 1025 |
r["width"] = int(round(r.get("width", 0) / scale))
|
| 1026 |
r["height"] = int(round(r.get("height",0) / scale))
|
| 1027 |
return res
|
|
|
|
|
|
|
| 1028 |
|
| 1029 |
# 1) Whole-page variants
|
| 1030 |
W,H = pil.size
|
|
@@ -1042,6 +1048,7 @@ def _decode_variants(pil: Image.Image) -> List[Dict[str,Any]]:
|
|
| 1042 |
for vimg, sc in variants:
|
| 1043 |
res = _decode_and_rescale(vimg, sc)
|
| 1044 |
if res:
|
|
|
|
| 1045 |
return res
|
| 1046 |
|
| 1047 |
# 2) Tiled fallback (helps tiny or stacked GS1)
|
|
@@ -1067,7 +1074,9 @@ def _decode_variants(pil: Image.Image) -> List[Dict[str,Any]]:
|
|
| 1067 |
# width/height already scaled
|
| 1068 |
hits.append(r)
|
| 1069 |
if hits:
|
|
|
|
| 1070 |
return hits
|
|
|
|
| 1071 |
return []
|
| 1072 |
|
| 1073 |
def _pix_to_pil(pix) -> Image.Image:
|
|
@@ -1205,8 +1214,20 @@ def compare_pdfs(file_a, file_b):
|
|
| 1205 |
print(f"Spell check results - A: {len(misspell_a)} boxes, B: {len(misspell_b)} boxes")
|
| 1206 |
|
| 1207 |
# Always attempt barcode scan. The PDF path uses ZXing-CPP / pyzbar / dmtx / cv2 if available.
|
| 1208 |
-
|
| 1209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1210 |
|
| 1211 |
# Always enable CMYK analysis
|
| 1212 |
cmyk_entries = compute_cmyk_diffs(a, b, red_boxes)
|
|
|
|
| 935 |
return out or None
|
| 936 |
|
| 937 |
def _decode_zxing(pil: Image.Image) -> List[Dict[str,Any]]:
|
| 938 |
+
if not HAS_ZXING:
|
| 939 |
+
print("ZXing not available")
|
| 940 |
+
return []
|
| 941 |
arr = np.asarray(pil.convert("L"))
|
| 942 |
out=[]
|
| 943 |
hints = _zxing_hints_all()
|
| 944 |
try:
|
| 945 |
res = zxingcpp.read_barcodes(arr, hints=hints) if hints is not None else zxingcpp.read_barcodes(arr)
|
| 946 |
+
print(f"ZXing found {len(res) if res else 0} barcodes")
|
| 947 |
+
except Exception as e:
|
| 948 |
+
print(f"ZXing error: {e}")
|
| 949 |
res = []
|
| 950 |
|
| 951 |
for r in res or []:
|
|
|
|
| 1029 |
r["width"] = int(round(r.get("width", 0) / scale))
|
| 1030 |
r["height"] = int(round(r.get("height",0) / scale))
|
| 1031 |
return res
|
| 1032 |
+
|
| 1033 |
+
print(f"Starting _decode_variants on image {pil.size}")
|
| 1034 |
|
| 1035 |
# 1) Whole-page variants
|
| 1036 |
W,H = pil.size
|
|
|
|
| 1048 |
for vimg, sc in variants:
|
| 1049 |
res = _decode_and_rescale(vimg, sc)
|
| 1050 |
if res:
|
| 1051 |
+
print(f"Found {len(res)} barcodes in whole-page variant at scale {sc}")
|
| 1052 |
return res
|
| 1053 |
|
| 1054 |
# 2) Tiled fallback (helps tiny or stacked GS1)
|
|
|
|
| 1074 |
# width/height already scaled
|
| 1075 |
hits.append(r)
|
| 1076 |
if hits:
|
| 1077 |
+
print(f"Found {len(hits)} barcodes in tiled fallback")
|
| 1078 |
return hits
|
| 1079 |
+
print("No barcodes found in any variant")
|
| 1080 |
return []
|
| 1081 |
|
| 1082 |
def _pix_to_pil(pix) -> Image.Image:
|
|
|
|
| 1214 |
print(f"Spell check results - A: {len(misspell_a)} boxes, B: {len(misspell_b)} boxes")
|
| 1215 |
|
| 1216 |
# Always attempt barcode scan. The PDF path uses ZXing-CPP / pyzbar / dmtx / cv2 if available.
|
| 1217 |
+
try:
|
| 1218 |
+
print(f"Starting barcode detection for file A: {file_a.name}")
|
| 1219 |
+
bar_a, info_a = find_barcode_boxes_and_info_from_pdf(file_a.name, image_size=image_size)
|
| 1220 |
+
print(f"Barcode detection A complete: {len(bar_a)} boxes, {len(info_a)} infos")
|
| 1221 |
+
|
| 1222 |
+
print(f"Starting barcode detection for file B: {file_b.name}")
|
| 1223 |
+
bar_b, info_b = find_barcode_boxes_and_info_from_pdf(file_b.name, image_size=image_size)
|
| 1224 |
+
print(f"Barcode detection B complete: {len(bar_b)} boxes, {len(info_b)} infos")
|
| 1225 |
+
except Exception as e:
|
| 1226 |
+
print(f"Barcode detection error: {e}")
|
| 1227 |
+
import traceback
|
| 1228 |
+
traceback.print_exc()
|
| 1229 |
+
bar_a, info_a = [], []
|
| 1230 |
+
bar_b, info_b = [], []
|
| 1231 |
|
| 1232 |
# Always enable CMYK analysis
|
| 1233 |
cmyk_entries = compute_cmyk_diffs(a, b, red_boxes)
|