ballagb19 commited on
Commit
6318f60
·
verified ·
1 Parent(s): c6f573a

Update captcha_solver/solvers/image_grid_solver.py: full-image VQA + improved solver for canvas text challenges

Browse files
captcha_solver/solvers/image_grid_solver.py CHANGED
@@ -119,30 +119,30 @@ class ImageGridSolver(BaseSolver):
119
  def _moondream(self) -> SolveAttempt:
120
  if self._img is None:
121
  return SolveAttempt(answer="", confidence=0.0, solver_name="image_grid.moondream", error="no image")
122
- tiles = self._split_grid(self._img)
123
  if not self._hint:
124
  try:
125
  out = self.ctx.moondream.query(
126
- self._img, "Describe what is shown in each of the 9 tiles in this 3x3 grid image. Be specific about objects."
127
  )
128
  except Exception as exc:
129
  return SolveAttempt(answer="", confidence=0.0, solver_name="image_grid.moondream", error=str(exc))
130
  return SolveAttempt(answer=out, confidence=0.4, solver_name="image_grid.moondream")
131
- matches: list[int] = []
 
132
  try:
133
- for i, tile in enumerate(tiles):
134
- out = self.ctx.moondream.query(
135
- tile, f"Does this image contain a {self._hint}? Answer yes or no only."
136
- )
137
- if self._format_yes_no(out) is True:
138
- matches.append(i + 1)
 
 
 
139
  except Exception as exc:
140
  return SolveAttempt(answer="", confidence=0.0, solver_name="image_grid.moondream", error=str(exc))
141
- return SolveAttempt(
142
- answer=",".join(str(m) for m in matches) if matches else "",
143
- confidence=0.55 if matches else 0.1,
144
- solver_name="image_grid.moondream",
145
- )
146
 
147
 
148
  def infer_hint_from_text(text: str) -> Optional[str]:
 
119
  def _moondream(self) -> SolveAttempt:
120
  if self._img is None:
121
  return SolveAttempt(answer="", confidence=0.0, solver_name="image_grid.moondream", error="no image")
122
+
123
  if not self._hint:
124
  try:
125
  out = self.ctx.moondream.query(
126
+ self._img, "Look at this captcha challenge image. Read all the text visible in the image. What does it say?"
127
  )
128
  except Exception as exc:
129
  return SolveAttempt(answer="", confidence=0.0, solver_name="image_grid.moondream", error=str(exc))
130
  return SolveAttempt(answer=out, confidence=0.4, solver_name="image_grid.moondream")
131
+
132
+ # Full-image VQA: ask Moondream to analyze the whole image
133
  try:
134
+ prompt = (
135
+ f"This is a hCaptcha challenge. The instruction is: '{self._hint}'. "
136
+ f"Look at the image carefully. It shows several tiles each containing a letter and a number. "
137
+ f"Tell me: what specific letter+number combinations should be clicked? "
138
+ f"For example, if the instruction is 'click all the number 3 tiles', "
139
+ f"list the tiles that have the number 3 under them. "
140
+ f"Return the answer as a list of letter+number pairs, e.g. 'A3, B3, C3'."
141
+ )
142
+ out = self.ctx.moondream.query(self._img, prompt)
143
  except Exception as exc:
144
  return SolveAttempt(answer="", confidence=0.0, solver_name="image_grid.moondream", error=str(exc))
145
+ return SolveAttempt(answer=out, confidence=0.5, solver_name="image_grid.moondream")
 
 
 
 
146
 
147
 
148
  def infer_hint_from_text(text: str) -> Optional[str]: