PaulMartrenchar commited on
Commit
1d42af3
·
1 Parent(s): 104b989

Test with whitening what is not black

Browse files
Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -9,33 +9,35 @@ VALID_PATTERN = re.compile(r"-?\d+")
9
 
10
  def extract_skyjo_value(card_img):
11
  h, w, _ = card_img.shape
12
-
13
- # Crop the top-left corner (adjust these values based on your card layout)
14
- crop_margin_top = int(h / 11) # pixels from the top edges
15
- crop_margin_left = int(w / 7) # pixels from the left edges
16
- crop_size = int(w / 3) # size of the cropped region (square)
17
  roi = card_img[
18
  crop_margin_top:crop_margin_top + crop_size,
19
- crop_margin_left:crop_margin_left + crop_size
20
  ]
21
 
22
- # Preprocess the cropped region
23
  gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
24
- gray = cv2.GaussianBlur(gray, (3, 3), 0)
25
- thresh = cv2.adaptiveThreshold(
26
- gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
27
- cv2.THRESH_BINARY_INV, 11, 2
28
- )
29
  kernel = np.ones((2, 2), np.uint8)
30
  thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
31
 
32
- # Run OCR on the cropped region
 
 
 
33
  raw_text = pytesseract.image_to_string(
34
  thresh,
35
  config="--psm 10 --oem 3 -c tessedit_char_whitelist=-0123456789"
36
  )
37
-
38
  print("Raw OCR text:", raw_text)
 
39
  # Extract numbers
40
  matches = re.findall(r"-?\d+", raw_text)
41
  for m in matches:
@@ -45,10 +47,10 @@ def extract_skyjo_value(card_img):
45
  return val
46
  except ValueError:
47
  continue
48
-
49
  return None
50
 
51
 
 
52
  def extract_flip7_value(card_img):
53
  h, w, _ = card_img.shape
54
  margin = 50
 
9
 
10
  def extract_skyjo_value(card_img):
11
  h, w, _ = card_img.shape
12
+ # Crop the top-left corner
13
+ crop_margin_top = int(h / 11)
14
+ crop_margin_left = int(w / 7)
15
+ crop_size = int(w / 3)
 
16
  roi = card_img[
17
  crop_margin_top:crop_margin_top + crop_size,
18
+ crop_margin_left:crop_margin_left + crop_size
19
  ]
20
 
21
+ # Convert to grayscale
22
  gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
23
+
24
+ # Threshold to keep only black/dark pixels (adjust threshold value as needed)
25
+ _, thresh = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY_INV)
26
+
27
+ # Optional: morphological closing to fill small gaps
28
  kernel = np.ones((2, 2), np.uint8)
29
  thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
30
 
31
+ # Invert back to black on white for OCR
32
+ thresh = cv2.bitwise_not(thresh)
33
+
34
+ # Run OCR on the thresholded image
35
  raw_text = pytesseract.image_to_string(
36
  thresh,
37
  config="--psm 10 --oem 3 -c tessedit_char_whitelist=-0123456789"
38
  )
 
39
  print("Raw OCR text:", raw_text)
40
+
41
  # Extract numbers
42
  matches = re.findall(r"-?\d+", raw_text)
43
  for m in matches:
 
47
  return val
48
  except ValueError:
49
  continue
 
50
  return None
51
 
52
 
53
+
54
  def extract_flip7_value(card_img):
55
  h, w, _ = card_img.shape
56
  margin = 50