Spaces:
Sleeping
Sleeping
Commit
·
f8ebfdd
1
Parent(s):
98253f0
Add debug image
Browse files
app.py
CHANGED
|
@@ -60,11 +60,12 @@ def extract_flip7_value(card_img):
|
|
| 60 |
def detect_cards_and_sum(image, game):
|
| 61 |
try:
|
| 62 |
if image is None:
|
| 63 |
-
return 0, 0, "No image provided."
|
| 64 |
|
| 65 |
if not isinstance(image, np.ndarray):
|
| 66 |
image = np.array(image.convert("RGB"))
|
| 67 |
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
|
|
|
| 68 |
|
| 69 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 70 |
blur = cv2.GaussianBlur(gray, (5,5), 0)
|
|
@@ -94,14 +95,20 @@ def detect_cards_and_sum(image, game):
|
|
| 94 |
detected_values.append(value)
|
| 95 |
total += value
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
if not detected_values:
|
| 98 |
-
return 0, 0, "No valid card values detected."
|
| 99 |
|
| 100 |
-
|
| 101 |
-
return len(detected_values), total, None
|
| 102 |
|
| 103 |
except Exception as e:
|
| 104 |
-
return 0, 0, f"Error: {str(e)}"
|
|
|
|
| 105 |
|
| 106 |
|
| 107 |
# Gradio UI
|
|
@@ -114,12 +121,13 @@ with gr.Blocks() as demo:
|
|
| 114 |
num_cards = gr.Number(label="Number of Cards Detected")
|
| 115 |
total = gr.Number(label="Total Sum")
|
| 116 |
error = gr.Textbox(label="Error Message")
|
|
|
|
| 117 |
|
| 118 |
run_btn = gr.Button("Process")
|
| 119 |
run_btn.click(
|
| 120 |
fn=detect_cards_and_sum,
|
| 121 |
inputs=[image_input, game_choice],
|
| 122 |
-
outputs=[num_cards, total, error]
|
| 123 |
)
|
| 124 |
|
| 125 |
if __name__ == "__main__":
|
|
|
|
| 60 |
def detect_cards_and_sum(image, game):
|
| 61 |
try:
|
| 62 |
if image is None:
|
| 63 |
+
return 0, 0, "No image provided.", None
|
| 64 |
|
| 65 |
if not isinstance(image, np.ndarray):
|
| 66 |
image = np.array(image.convert("RGB"))
|
| 67 |
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 68 |
+
display_img = img.copy()
|
| 69 |
|
| 70 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 71 |
blur = cv2.GaussianBlur(gray, (5,5), 0)
|
|
|
|
| 95 |
detected_values.append(value)
|
| 96 |
total += value
|
| 97 |
|
| 98 |
+
# Draw bounding box + label
|
| 99 |
+
cv2.polylines(display_img, [approx], True, (0,255,0), 3)
|
| 100 |
+
x, y = approx[0][0]
|
| 101 |
+
cv2.putText(display_img, str(value), (int(x), int(y)-10),
|
| 102 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0,0,255), 3)
|
| 103 |
+
|
| 104 |
if not detected_values:
|
| 105 |
+
return 0, 0, "No valid card values detected.", cv2.cvtColor(display_img, cv2.COLOR_BGR2RGB)
|
| 106 |
|
| 107 |
+
return len(detected_values), total, None, cv2.cvtColor(display_img, cv2.COLOR_BGR2RGB)
|
|
|
|
| 108 |
|
| 109 |
except Exception as e:
|
| 110 |
+
return 0, 0, f"Error: {str(e)}", None
|
| 111 |
+
|
| 112 |
|
| 113 |
|
| 114 |
# Gradio UI
|
|
|
|
| 121 |
num_cards = gr.Number(label="Number of Cards Detected")
|
| 122 |
total = gr.Number(label="Total Sum")
|
| 123 |
error = gr.Textbox(label="Error Message")
|
| 124 |
+
annotated = gr.Image(type="numpy", label="Detected Cards with Values")
|
| 125 |
|
| 126 |
run_btn = gr.Button("Process")
|
| 127 |
run_btn.click(
|
| 128 |
fn=detect_cards_and_sum,
|
| 129 |
inputs=[image_input, game_choice],
|
| 130 |
+
outputs=[num_cards, total, error, annotated]
|
| 131 |
)
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|