ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

moto 中 AWS Rekognition 服务的模拟实现指南:支持的操作、硬编码返回值与源码解析

moto 中 AWS Rekognition 服务的模拟实现指南:支持的操作、硬编码返回值与源码解析 Mock测试【免费下载链接】motoA library that allows you to easily mock out tests based on AWS infrastructure.项目地址https://gitcode.com/gh_mirrors/mo/moto点击查看免费下载本指南以 docs/docs/services/rekognition.rst 为骨架系统梳理 moto 对 AWS Rekognition 服务的模拟实现已支持的 8 个 API、各接口的返回结构、底层源码实现位置与测试验证方式。读完本文你将能在本地测试中正确使用 Rekognition 的 mock 能力并清楚哪些接口返回的是硬编码数据、哪些接口尚未实现。Rekognition 在 moto 中的实现位置与整体架构Rekognition 是 AWS 的图像与视频分析服务人脸比对、标签识别、文本检测等。moto 以标准的路由层 响应层 后端模型层三段式结构实现该服务对应源码分布在 4 个文件中文件职责moto/rekognition/urls.py定义服务 URL 匹配规则将请求路由到响应处理类moto/rekognition/responses.pyRekognitionResponse类负责解析请求并序列化 JSON 响应moto/rekognition/models.pyRekognitionBackend类存储模拟状态并生成各接口返回值moto/rekognition/init.py导出后端注册表供 moto 全局发现在 moto/rekognition/urls.py 中服务端点通过正则https?://rekognition\.(.)\.amazonaws\.com匹配所有请求统一走{0}/$路由交给RekognitionResponse.dispatch分发同时 moto/backend_index.py 也注册了同一正则确保 moto 能识别 Rekognition 区域端点。后端实例由 moto/rekognition/models.py 末尾的rekognition_backends BackendDict(RekognitionBackend, rekognition)按账号与区域管理与 moto 其他服务一致每个 account/region 组合都有独立的模拟实例。支持范围10% 覆盖率与 8 个已实现操作原文档也是 IMPLEMENTATION_COVERAGE.md 的生成依据通过勾选清单明确了 Rekognition 的实现进度约 10% 的 API 已实现共 8 个其余 50 个操作未实现。已实现列表如下已实现操作说明参数是否生效compare_faces比对两张图片中的人脸相似度参数不被处理返回固定数据detect_custom_labels调用自定义模型检测标签参数不被处理返回固定数据detect_labels检测图片中的物体标签参数不被处理返回固定数据detect_text检测图片中的文本参数不被处理返回固定数据get_face_search获取人脸搜索任务结果返回硬编码值参数不生效get_text_detection获取文本检测任务结果返回硬编码值参数不生效start_face_search启动人脸搜索异步任务生成任务 IDJobIdstart_text_detection启动文本检测异步任务生成任务 IDJobId原文档对get_face_search与get_text_detection特别标注了 This returns hardcoded values and none of the parameters are taken into account.返回硬编码值且所有参数都不被处理这一点在源码 moto/rekognition/models.py 的 docstring 中完全一致属于项目明确声明的行为。核心操作详解调用链与返回结构1. 异步任务启动start_face_search 与 start_text_detection这两个操作模拟 AWS 的异步视频分析流程调用后返回一个 64 位任务 ID后续通过get_face_search/get_text_detection查询结果。调用链为RekognitionResponse.start_face_searchmoto/rekognition/responses.py→RekognitionBackend.start_face_searchmoto/rekognition/models.py。后端通过_job_id()生成任务 ID实现如下def _job_id(self) - str: return .join( random.choice(string.ascii_uppercase string.digits) for _ in range(64) )即从大写字母与数字中随机抽取 64 个字符组成 JobId随机源为moto.moto_api._internal.mock_random在测试中可被set_initial_no_auth_action_exception等机制控制。响应层返回 HTTP 200并设置Content-Type: application/x-amz-json-1.1头响应体为{JobId:...}。注意该接口的CollectionId、VideoS3 对象信息等入参在源码中并未被读取或存储仅用于满足 boto3 客户端调用时的参数校验。2. 查询任务结果get_face_search 与 get_text_detection这两个接口返回完全硬编码的结果且不校验 JobId 是否存在——传入任意 JobId 都会得到相同响应。返回结构包含 6 个字段JobStatus固定为SUCCEEDED_job_status()StatusMessage固定为空字符串VideoMetadata固定视频元数据见下文Persons/TextDetections固定的人物/文本检测数组NextToken固定为空字符串_next_token()TextModelVersion固定为3.1_text_model_version()。固定的VideoMetadatamoto/rekognition/models.py为{ Codec: h264, DurationMillis: 15020, Format: QuickTime / MOV, FrameRate: 24.0, FrameHeight: 720, FrameWidth: 1280, ColorRange: LIMITED, }get_face_search的Persons中FaceMatches引用了ExternalImageId: Dave_Bloggs的人脸包含BoundingBox、LandmarkseyeLeft/eyeRight/mouthLeft/mouthRight/nose 五点、PoseRoll/Yaw/Pitch、QualityBrightness/Sharpness与高达 99.99 的Confidence与Similarity值。get_text_detection的TextDetections则包含 Hello world / Hello / world / Goodbye world 等文本按LINE行与WORD词类型组织WORD通过ParentId关联到所属行时间戳分布在 0ms 与 1000ms。3. 图片分析compare_faces、detect_labels、detect_text、detect_custom_labels这四个同步接口同样返回硬编码数据所有入参Image、MaxLabels、SimilarityThreshold、ProjectVersionArn、MinConfidence等均不参与计算compare_faces返回FaceMatches固定 1 条Similarity: 100.0的完整人脸结构、SourceImageOrientationCorrection与TargetImageOrientationCorrection固定为ROTATE_90、UnmatchedFaces固定 1 条未匹配人脸以及SourceImageFace。定义见 moto/rekognition/models.py。detect_labels返回Labels固定 Mobile Phone 标签含Parents: [{Name: Phone}]、Aliases: [{Name: Cell Phone}]、Categories: [{Name: Technology and Computing}]、Instances中的 BoundingBox 与 DominantColors、ImagePropertiesQuality、DominantColors、Foreground、Background 三部分以及LabelModelVersion: 3.0。detect_text返回TextDetectionsITS MONDAY but keep Smiling 的 LINE 与 WORD 结构含Id、ParentId、Geometry的 BoundingBox 与 Polygon和TextModelVersion: 3.0。detect_custom_labels返回CustomLabels固定 1 条Name: MyLogoConfidence: 77.77含 BoundingBox。这些方法在 moto/rekognition/models.py 中均为无参或忽略参数的方法直接返回_face_matches()、_mobile_phone_label()、_detect_text_text_detections()、_detect_custom_labels_detections()等私有方法生成的固定数据由 moto/rekognition/responses.py 包装成标准 JSON 响应。测试验证测试用例与可断言的固定值已实现操作的预期行为全部由 tests/test_rekognition/test_rekognition.py 覆盖可作为返回内容是否如预期的直接依据测试函数关键断言test_start_face_searchHTTP 200响应含JobIdtest_start_text_detectionHTTP 200响应含JobIdtest_compare_faces响应含FaceMatchestest_detect_labels响应含Labelstest_detect_text响应含TextDetectionstest_get_face_searchJobStatus SUCCEEDED且Persons[0][FaceMatches][0][Face][ExternalImageId] Dave_Bloggstest_get_text_detectionTextDetections[0][TextDetection][DetectedText] Hello worldtest_detect_custom_labelsCustomLabels[0][Name] MyLogo从测试代码可以看到mock 使用统一采用mock_aws装饰器 boto3.client(rekognition, region_name...)的方式区域使用如ap-southeast-1、us-east-2等均可正常响应。实际使用示例以下完整示例展示了如何用 moto 在本地测试中驱动 Rekognition 的已支持接口import boto3 from moto import mock_aws mock_aws def test_rekognition_mock(): client boto3.client(rekognition, region_nameap-southeast-1) # 1. 启动异步任务拿到 JobId resp client.start_face_search( CollectionIdmy-collection, Video{S3Object: {Bucket: bucket, Name: video.mp4}}, ) job_id resp[JobId] assert isinstance(job_id, str) and len(job_id) 64 # 2. 查询任务结果硬编码SUCCEEDED Dave_Bloggs result client.get_face_search(JobIdjob_id) assert result[JobStatus] SUCCEEDED assert ( result[Persons][0][FaceMatches][0][Face][ExternalImageId] Dave_Bloggs ) # 3. 图片同步分析均返回固定数据 image {S3Object: {Bucket: bucket, Name: photo.jpg}} assert Labels in client.detect_labels(Imageimage, MaxLabels10) assert TextDetections in client.detect_text(Imageimage) assert FaceMatches in client.compare_faces( SimilarityThreshold80, SourceImageimage, TargetImageimage, ) assert client.detect_custom_labels( Imageimage, ProjectVersionArnarn:aws:rekognition:us-east-2:123456789012:project/logo/version/v1, MinConfidence80, )[CustomLabels][0][Name] MyLogo要点提示所有已实现接口都不依赖真实的 S3 文件内容Video/Image参数仅需满足 boto3 的结构校验S3Object的Bucket/Name即可对于start_face_search、start_text_detection之外的接口传入任何参数值都不会影响返回结果若需在真实 AWS 上运行请将mock_aws装饰器移除并配置真实凭证但此时返回结果将是真实模型推断与本文描述的行为完全不同。未实现操作与使用边界原文档清单中以下 50 个操作均未实现调用会抛出异常或不受支持包括但不限于create_collection/delete_collection/describe_collection、index_faces、search_faces、search_faces_by_image、detect_faces、recognize_celebrities、list_collections、list_faces、create_project与create_project_version等项目生命周期管理接口以及start_label_detection、start_content_moderation、start_person_tracking、get_label_detection、get_content_moderation等视频分析接口还有tag_resource/untag_resource/list_tags_for_resource等资源标签操作。因此在编写依赖 Rekognition 的测试时需注意集合Collection类操作不可用无法通过 mock 创建集合或把人脸写入集合get_face_search返回的FaceMatches是硬编码的 Dave_Bloggs 数据与任何实际集合状态无关状态管理缺失get_face_search/get_text_detection不校验 JobId也不维护任务状态机同一请求无论调用多少次、传什么 JobId结果都恒定若你的业务测试强依赖未实现接口可考虑使用 moto 的moto_api相关能力或自行在测试层打桩stub具体扩展方式可参考 moto 其他服务的实现模式例如同样位于 moto/backend_index.py 中注册的其他服务。小结moto 对 Rekognition 的支持定位是最小可用模拟8 个已实现接口能覆盖启动异步任务 → 查询硬编码结果 → 同步图片分析这条基本调用链路足以支撑不校验具体识别内容的单元测试与集成测试。但其数据完全硬编码、参数不生效、覆盖率仅 10% 的特性决定了它更适合作为流程性验证而非内容验证工具——在断言具体识别结果前请先以 tests/test_rekognition/test_rekognition.py 中的固定值为准核对预期。赞分享Mock测试【免费下载链接】motoA library that allows you to easily mock out tests based on AWS infrastructure.项目地址https://gitcode.com/gh_mirrors/mo/moto点击查看免费下载相关推荐moto 中 AWS DevOps Agentdevops-agent服务的 Mock 支持已实现操作、使用方式与源码实现解析moto 中 AWS DevOps Agentdevops agent服务的 Mock 支持已实现操作、使用方式与源码实现解析 本篇以 moto 仓库中Mock测试moto 中 AWS Config 服务的实现全解已支持 API、Recorder/Aggregator 机制与源码级剖析moto 中 AWS Config 服务的实现全解已支持 API、Recorder/Aggregator 机制与源码级剖析 本文以 docs/docs/serMock测试Moto 中的 AWS App Mesh 模拟实现AppMeshBackend 已支持 API 全解析与实战指南Moto 中的 AWS App Mesh 模拟实现AppMeshBackend 已支持 API 全解析与实战指南 本文聚焦开源库 Moto 对 AWS AppMock测试上一篇终极指南5分钟掌握原神抽卡数据分析免费导出详细记录下一篇GhostTrack 批量定位3 步搞定多目标追踪创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表