Spaces:
Sleeping
Sleeping
Commit ·
8b4ba30
1
Parent(s): 9331987
optimize OCR for Skyjo
Browse files
app.py
CHANGED
|
@@ -9,29 +9,45 @@ VALID_PATTERN = re.compile(r"-?\d+")
|
|
| 9 |
|
| 10 |
def extract_skyjo_value(card_img):
|
| 11 |
h, w, _ = card_img.shape
|
| 12 |
-
margin =
|
| 13 |
roi = card_img[margin:h-margin, margin:w-margin]
|
| 14 |
|
|
|
|
| 15 |
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
|
| 16 |
-
gray = cv2.GaussianBlur(gray, (3,3), 0)
|
| 17 |
-
thresh = cv2.
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
thresh,
|
| 21 |
-
config="--psm 6 -c tessedit_char_whitelist=-0123456789"
|
| 22 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
matches = re.findall(r"-?\d+", raw_text)
|
| 25 |
-
for m in matches:
|
| 26 |
-
try:
|
| 27 |
-
val = int(m)
|
| 28 |
-
if -2 <= val <= 12:
|
| 29 |
-
return val
|
| 30 |
-
except ValueError:
|
| 31 |
-
continue
|
| 32 |
return None
|
| 33 |
|
| 34 |
-
|
| 35 |
def extract_flip7_value(card_img):
|
| 36 |
h, w, _ = card_img.shape
|
| 37 |
margin = 50
|
|
@@ -152,7 +168,7 @@ def detect_cards_and_sum(image, game):
|
|
| 152 |
else:
|
| 153 |
tx, ty = int(cx), int(cy)
|
| 154 |
cv2.putText(annotated, str(val), (tx-10, ty-10),
|
| 155 |
-
cv2.FONT_HERSHEY_SIMPLEX, 1.
|
| 156 |
|
| 157 |
if not values:
|
| 158 |
# return the annotated image anyway so you can see what was (not) detected
|
|
|
|
| 9 |
|
| 10 |
def extract_skyjo_value(card_img):
|
| 11 |
h, w, _ = card_img.shape
|
| 12 |
+
margin = int(min(h, w) * 0.1) # Dynamic margin
|
| 13 |
roi = card_img[margin:h-margin, margin:w-margin]
|
| 14 |
|
| 15 |
+
# Preprocessing
|
| 16 |
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
|
| 17 |
+
gray = cv2.GaussianBlur(gray, (3, 3), 0)
|
| 18 |
+
thresh = cv2.adaptiveThreshold(
|
| 19 |
+
gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
| 20 |
+
cv2.THRESH_BINARY_INV, 11, 2
|
|
|
|
|
|
|
| 21 |
)
|
| 22 |
+
kernel = np.ones((2, 2), np.uint8)
|
| 23 |
+
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
|
| 24 |
+
|
| 25 |
+
# Try OCR on both original and rotated card
|
| 26 |
+
for angle in [0, 180]:
|
| 27 |
+
if angle == 180:
|
| 28 |
+
rotated = cv2.rotate(thresh, cv2.ROTATE_180)
|
| 29 |
+
raw_text = pytesseract.image_to_string(
|
| 30 |
+
rotated,
|
| 31 |
+
config="--psm 10 --oem 3 -c tessedit_char_whitelist=-0123456789"
|
| 32 |
+
)
|
| 33 |
+
else:
|
| 34 |
+
raw_text = pytesseract.image_to_string(
|
| 35 |
+
thresh,
|
| 36 |
+
config="--psm 10 --oem 3 -c tessedit_char_whitelist=-0123456789"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Extract numbers
|
| 40 |
+
matches = re.findall(r"-?\d+", raw_text)
|
| 41 |
+
for m in matches:
|
| 42 |
+
try:
|
| 43 |
+
val = int(m)
|
| 44 |
+
if -2 <= val <= 12:
|
| 45 |
+
return val
|
| 46 |
+
except ValueError:
|
| 47 |
+
continue
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return None
|
| 50 |
|
|
|
|
| 51 |
def extract_flip7_value(card_img):
|
| 52 |
h, w, _ = card_img.shape
|
| 53 |
margin = 50
|
|
|
|
| 168 |
else:
|
| 169 |
tx, ty = int(cx), int(cy)
|
| 170 |
cv2.putText(annotated, str(val), (tx-10, ty-10),
|
| 171 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 3)
|
| 172 |
|
| 173 |
if not values:
|
| 174 |
# return the annotated image anyway so you can see what was (not) detected
|