MCPcopy
hub / github.com/IPADS-SAI/MobiAgent / match_keyword_in_frame

Method match_keyword_in_frame

utils/advanced_ocr.py:389–441  ·  view source on GitHub ↗

在帧中搜索关键词 Args: frame: 帧数据 keyword: 要搜索的关键词 enable_ocr: 是否启用OCR识别 Returns: bool: 是否找到关键词

(self, frame: Dict[str, Any], keyword: str, enable_ocr: bool = True)

Source from the content-addressed store, hash-verified

387 return self.process_text(combined_text)
388
389 def match_keyword_in_frame(self, frame: Dict[str, Any], keyword: str, enable_ocr: bool = True) -> bool:
390 """
391 在帧中搜索关键词
392
393 Args:
394 frame: 帧数据
395 keyword: 要搜索的关键词
396 enable_ocr: 是否启用OCR识别
397
398 Returns:
399 bool: 是否找到关键词
400 """
401 if not keyword:
402 return False
403
404 # 首先在现有文本字段中搜索
405 text_fields = ['text', 'task_description', 'reasoning']
406 for field in text_fields:
407 if field in frame and frame[field]:
408 processed = self.process_text(str(frame[field]))
409 if self.smart_text_contains(processed, keyword):
410 logger.debug(f"在字段 {field} 中找到关键词: {keyword}")
411 return True
412
413 # 在XML文本中搜索
414 if 'xml_text' in frame and frame['xml_text']:
415 xml_text = self.extract_xml_text(frame['xml_text'])
416 if xml_text:
417 processed = self.process_text(xml_text)
418 if self.smart_text_contains(processed, keyword):
419 logger.debug(f"在XML文本中找到关键词: {keyword}")
420 return True
421
422 # 使用OCR在图像中搜索
423 if enable_ocr and 'image' in frame and frame['image']:
424 if os.path.exists(frame['image']):
425 ocr_text, backup_text = self.extract_text_from_image(frame['image'])
426
427 # 在主OCR结果中搜索
428 if ocr_text:
429 processed = self.process_text(ocr_text)
430 if self.smart_text_contains(processed, keyword):
431 logger.debug(f"在OCR文本中找到关键词: {keyword}")
432 return True
433
434 # 在备用OCR结果中搜索
435 if backup_text and backup_text != ocr_text:
436 processed = self.process_text(backup_text)
437 if self.smart_text_contains(processed, keyword):
438 logger.debug(f"在备用OCR文本中找到关键词: {keyword}")
439 return True
440
441 return False
442
443 def get_text_similarity(self, text1: str, text2: str) -> float:
444 """计算两个文本的相似度"""

Callers 1

match_text_in_frameFunction · 0.80

Calls 5

process_textMethod · 0.95
smart_text_containsMethod · 0.95
extract_xml_textMethod · 0.95
debugMethod · 0.80

Tested by

no test coverage detected