Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
import matplotlib.pyplot as plt
|
| 5 |
import json
|
| 6 |
import math
|
| 7 |
|
|
@@ -69,14 +68,14 @@ def detect_and_match(img1_gray, img2_gray, detector_name, ratio_thresh=0.78):
|
|
| 69 |
# === Main processing ===
|
| 70 |
def process_images(flat_img, persp_img, json_file):
|
| 71 |
if flat_img is None or persp_img is None or json_file is None:
|
| 72 |
-
return [
|
| 73 |
|
| 74 |
# Load JSON
|
| 75 |
try:
|
| 76 |
data = json.load(open(json_file.name))
|
| 77 |
except Exception as e:
|
| 78 |
print("JSON read error:", e)
|
| 79 |
-
return [
|
| 80 |
|
| 81 |
roi = data["printAreas"][0]
|
| 82 |
roi_x = roi["position"]["x"]
|
|
@@ -90,13 +89,12 @@ def process_images(flat_img, persp_img, json_file):
|
|
| 90 |
persp_gray = preprocess_gray_clahe(persp_img)
|
| 91 |
|
| 92 |
detectors = ["SIFT", "ORB", "BRISK", "AKAZE", "KAZE"]
|
| 93 |
-
|
| 94 |
|
| 95 |
for det in detectors:
|
| 96 |
kp1, kp2, good_matches = detect_and_match(flat_gray, persp_gray, det)
|
| 97 |
if len(good_matches) < 4:
|
| 98 |
print(f"Not enough matches for {det}")
|
| 99 |
-
results.append(None)
|
| 100 |
continue
|
| 101 |
|
| 102 |
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
|
|
@@ -107,38 +105,30 @@ def process_images(flat_img, persp_img, json_file):
|
|
| 107 |
roi_corners_flat = get_rotated_rect_corners(roi_x, roi_y, roi_w, roi_h, roi_rot_deg)
|
| 108 |
roi_corners_persp = cv2.perspectiveTransform(roi_corners_flat.reshape(-1,1,2), H).reshape(-1,2)
|
| 109 |
|
| 110 |
-
# Draw ROI
|
| 111 |
-
flat_out = flat_img.copy()
|
| 112 |
persp_out = persp_img.copy()
|
| 113 |
-
cv2.polylines(flat_out, [roi_corners_flat.astype(int)], True, (255,0,0), 3)
|
| 114 |
cv2.polylines(persp_out, [roi_corners_persp.astype(int)], True, (0,255,0), 3)
|
| 115 |
|
| 116 |
-
|
|
|
|
| 117 |
|
| 118 |
-
|
| 119 |
|
| 120 |
-
# === Gradio Interface ===
|
| 121 |
-
def wrap_gradio(flat_img, persp_img, json_file):
|
| 122 |
-
outputs = process_images(flat_img, persp_img, json_file)
|
| 123 |
-
# Flatten the outputs for Gallery display
|
| 124 |
-
gallery_images = []
|
| 125 |
-
for item in outputs:
|
| 126 |
-
if item is not None:
|
| 127 |
-
gallery_images.extend([item[0], item[1]])
|
| 128 |
return gallery_images
|
| 129 |
|
|
|
|
| 130 |
iface = gr.Interface(
|
| 131 |
-
fn=
|
| 132 |
inputs=[
|
| 133 |
gr.Image(type="numpy", label="Flat Image"),
|
| 134 |
gr.Image(type="numpy", label="Perspective Image"),
|
| 135 |
gr.File(type="filepath", label="JSON File")
|
| 136 |
],
|
| 137 |
outputs=[
|
| 138 |
-
gr.Gallery(label="
|
| 139 |
],
|
| 140 |
title="Feature Detection with ROI Projection",
|
| 141 |
-
description="Shows SIFT, ORB, BRISK, AKAZE, KAZE feature-based ROI projections
|
| 142 |
)
|
| 143 |
|
| 144 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
import json
|
| 5 |
import math
|
| 6 |
|
|
|
|
| 68 |
# === Main processing ===
|
| 69 |
def process_images(flat_img, persp_img, json_file):
|
| 70 |
if flat_img is None or persp_img is None or json_file is None:
|
| 71 |
+
return []
|
| 72 |
|
| 73 |
# Load JSON
|
| 74 |
try:
|
| 75 |
data = json.load(open(json_file.name))
|
| 76 |
except Exception as e:
|
| 77 |
print("JSON read error:", e)
|
| 78 |
+
return []
|
| 79 |
|
| 80 |
roi = data["printAreas"][0]
|
| 81 |
roi_x = roi["position"]["x"]
|
|
|
|
| 89 |
persp_gray = preprocess_gray_clahe(persp_img)
|
| 90 |
|
| 91 |
detectors = ["SIFT", "ORB", "BRISK", "AKAZE", "KAZE"]
|
| 92 |
+
gallery_images = []
|
| 93 |
|
| 94 |
for det in detectors:
|
| 95 |
kp1, kp2, good_matches = detect_and_match(flat_gray, persp_gray, det)
|
| 96 |
if len(good_matches) < 4:
|
| 97 |
print(f"Not enough matches for {det}")
|
|
|
|
| 98 |
continue
|
| 99 |
|
| 100 |
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
|
|
|
|
| 105 |
roi_corners_flat = get_rotated_rect_corners(roi_x, roi_y, roi_w, roi_h, roi_rot_deg)
|
| 106 |
roi_corners_persp = cv2.perspectiveTransform(roi_corners_flat.reshape(-1,1,2), H).reshape(-1,2)
|
| 107 |
|
| 108 |
+
# Draw ROI on Perspective image only
|
|
|
|
| 109 |
persp_out = persp_img.copy()
|
|
|
|
| 110 |
cv2.polylines(persp_out, [roi_corners_persp.astype(int)], True, (0,255,0), 3)
|
| 111 |
|
| 112 |
+
# Add detector label
|
| 113 |
+
cv2.putText(persp_out, f"{det}", (10,30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,255), 2)
|
| 114 |
|
| 115 |
+
gallery_images.append(persp_out)
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
return gallery_images
|
| 118 |
|
| 119 |
+
# === Gradio Interface ===
|
| 120 |
iface = gr.Interface(
|
| 121 |
+
fn=process_images,
|
| 122 |
inputs=[
|
| 123 |
gr.Image(type="numpy", label="Flat Image"),
|
| 124 |
gr.Image(type="numpy", label="Perspective Image"),
|
| 125 |
gr.File(type="filepath", label="JSON File")
|
| 126 |
],
|
| 127 |
outputs=[
|
| 128 |
+
gr.Gallery(label="Detector-wise Perspective ROI Projection", show_label=True)
|
| 129 |
],
|
| 130 |
title="Feature Detection with ROI Projection",
|
| 131 |
+
description="Shows SIFT, ORB, BRISK, AKAZE, KAZE feature-based ROI projections on the Perspective image only."
|
| 132 |
)
|
| 133 |
|
| 134 |
iface.launch()
|