Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,61 +2,125 @@ import gradio as gr
|
|
2 |
import tempfile
|
3 |
from pdf2image import convert_from_path
|
4 |
import fitz # PyMuPDF
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
def analyze_pdf(pdf_file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
results = []
|
8 |
overall_rating = "OK"
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
images = convert_from_path(pdf_file.name, dpi=300, output_folder=tmpdir)
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
rect = page.rect
|
43 |
width_pt = rect.width
|
44 |
height_pt = rect.height
|
45 |
dpi_x = width / (width_pt / 72)
|
46 |
dpi_y = height / (height_pt / 72)
|
47 |
dpi_infos.append(f"ページ{i+1}推定DPI: {dpi_x:.1f}x{dpi_y:.1f}")
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
with gr.Blocks() as demo:
|
58 |
-
gr.Markdown("# Audiveris適性チェック(非公式・推定)")
|
59 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
with gr.Row():
|
62 |
pdf_input = gr.File(label="PDFファイル")
|
|
|
2 |
import tempfile
|
3 |
from pdf2image import convert_from_path
|
4 |
import fitz # PyMuPDF
|
5 |
+
import cv2
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
# ========== 解析ロジック関数 ==========
|
9 |
+
|
10 |
+
def estimate_blurriness(pil_image):
|
11 |
+
"""
|
12 |
+
ぼけ度を推定(Laplacian分散)
|
13 |
+
値が小さいほどぼけている
|
14 |
+
"""
|
15 |
+
gray = np.array(pil_image.convert('L'))
|
16 |
+
return cv2.Laplacian(gray, cv2.CV_64F).var()
|
17 |
+
|
18 |
+
def estimate_contrast(pil_image):
|
19 |
+
"""
|
20 |
+
コントラストを推定(標準偏差)
|
21 |
+
値が小さいほどグレーでのっぺり
|
22 |
+
"""
|
23 |
+
gray = np.array(pil_image.convert('L'))
|
24 |
+
return gray.std()
|
25 |
|
26 |
def analyze_pdf(pdf_file):
|
27 |
+
"""
|
28 |
+
アップロードされたPDFの各ページを画像に変換し
|
29 |
+
- 画像サイズ
|
30 |
+
- 総ピクセル数
|
31 |
+
- ぼけ度
|
32 |
+
- コントラスト
|
33 |
+
- 推定DPI
|
34 |
+
などをもとにAudiverisでの利用適性を判定
|
35 |
+
"""
|
36 |
+
MAX_PIXELS_AUDIVERIS = 20_000_000
|
37 |
results = []
|
38 |
overall_rating = "OK"
|
39 |
|
40 |
+
try:
|
41 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
42 |
+
# PDF → 画像変換
|
43 |
images = convert_from_path(pdf_file.name, dpi=300, output_folder=tmpdir)
|
44 |
+
doc = fitz.open(pdf_file.name)
|
45 |
+
|
46 |
+
dpi_infos = []
|
47 |
+
|
48 |
+
for i, (img, page) in enumerate(zip(images, doc)):
|
49 |
+
width, height = img.size
|
50 |
+
total_pixels = width * height
|
51 |
+
reasons = []
|
52 |
+
page_rating = "OK"
|
53 |
+
|
54 |
+
# Audiverisのピクセル上限チェック
|
55 |
+
if total_pixels > MAX_PIXELS_AUDIVERIS:
|
56 |
+
page_rating = "非推奨(画像が大きすぎる)"
|
57 |
+
reasons.append(f"総ピクセル数 {total_pixels:,} > 20,000,000")
|
58 |
+
|
59 |
+
# 解像度チェック
|
60 |
+
if width < 1000 or height < 1000 and "非推奨" not in page_rating:
|
61 |
+
page_rating = "非推奨(解像度が小さい)"
|
62 |
+
reasons.append("幅または高さが1000px未満")
|
63 |
+
elif width < 1500 or height < 1500 and page_rating == "OK":
|
64 |
+
page_rating = "注意(やや低め)"
|
65 |
+
|
66 |
+
# ぼけ度チェック
|
67 |
+
blur_score = estimate_blurriness(img)
|
68 |
+
if blur_score < 30 and "非推奨" not in page_rating:
|
69 |
+
page_rating = "非推奨(ぼけている)"
|
70 |
+
reasons.append(f"ぼけ度={blur_score:.1f}")
|
71 |
+
|
72 |
+
# コントラストチェック
|
73 |
+
contrast_score = estimate_contrast(img)
|
74 |
+
if contrast_score < 30 and "非推奨" not in page_rating:
|
75 |
+
page_rating = "非推奨(コントラストが低い)"
|
76 |
+
reasons.append(f"コントラスト={contrast_score:.1f}")
|
77 |
+
|
78 |
+
# DPI情報
|
79 |
rect = page.rect
|
80 |
width_pt = rect.width
|
81 |
height_pt = rect.height
|
82 |
dpi_x = width / (width_pt / 72)
|
83 |
dpi_y = height / (height_pt / 72)
|
84 |
dpi_infos.append(f"ページ{i+1}推定DPI: {dpi_x:.1f}x{dpi_y:.1f}")
|
85 |
+
|
86 |
+
# 理由をまとめる
|
87 |
+
reasons_str = "; ".join(reasons) if reasons else "条件クリア"
|
88 |
+
|
89 |
+
# ページ結果
|
90 |
+
results.append(
|
91 |
+
f"ページ{i+1}: {width}x{height}px / ピクセル数={total_pixels:,} / "
|
92 |
+
f"ぼけ度={blur_score:.1f} / コントラスト={contrast_score:.1f} → {page_rating} ({reasons_str})"
|
93 |
+
)
|
94 |
+
|
95 |
+
# 総合評価を更新
|
96 |
+
if "非推奨" in page_rating:
|
97 |
+
overall_rating = "非推奨"
|
98 |
+
elif "注意" in page_rating and overall_rating == "OK":
|
99 |
+
overall_rating = "注意"
|
100 |
+
|
101 |
+
# まとめテキスト
|
102 |
+
result_text = "\n".join(results + [""] + dpi_infos)
|
103 |
+
result_text += f"\n\n総合評価: {overall_rating}"
|
104 |
+
|
105 |
+
# 改善提案
|
106 |
+
if overall_rating == "非推奨":
|
107 |
+
result_text += "\n\n推奨対策: スキャン解像度を下げる・画像を縮小するなどして、ファイルサイズや画質を調整してください。"
|
108 |
+
|
109 |
+
return result_text, overall_rating
|
110 |
+
|
111 |
+
except Exception as e:
|
112 |
+
return f"処理中にエラーが発生しました: {str(e)}", "エラー"
|
113 |
+
|
114 |
+
# ========== Gradio UI部分 ==========
|
115 |
|
116 |
with gr.Blocks() as demo:
|
117 |
+
gr.Markdown("# 🎼 Audiveris適性チェック(非公式・推定)")
|
118 |
+
gr.Markdown(
|
119 |
+
"""
|
120 |
+
PDFをアップロードすると、ページごとの画像サイズ、ピクセル数、ぼけ度、コントラストを分析し、
|
121 |
+
Audiverisで使用可能かを「OK / 注意 / 非推奨」で推定します。
|
122 |
+
"""
|
123 |
+
)
|
124 |
|
125 |
with gr.Row():
|
126 |
pdf_input = gr.File(label="PDFファイル")
|