|
| 1 | +from finegrain import EditorAPIContext, ImageOutParams, OKResult, OKResultWithImage |
| 2 | + |
| 3 | + |
| 4 | +async def test_segment_bbox( |
| 5 | + fgctx: EditorAPIContext, |
| 6 | + coffee_plant_bytes: bytes, |
| 7 | + output_dir: str | None, |
| 8 | +) -> None: |
| 9 | + st_input = await fgctx.call_async.upload_image(coffee_plant_bytes) |
| 10 | + |
| 11 | + bbox_r = await fgctx.call_async.infer_bbox(st_input, product_name="coffee cup") |
| 12 | + assert isinstance(bbox_r, OKResult) |
| 13 | + |
| 14 | + segment_r = await fgctx.call_async.segment( |
| 15 | + st_input, |
| 16 | + bbox=bbox_r.bbox, |
| 17 | + with_image=ImageOutParams(resolution="DISPLAY", image_format="WEBP"), |
| 18 | + ) |
| 19 | + assert isinstance(segment_r, OKResultWithImage) |
| 20 | + |
| 21 | + if output_dir: |
| 22 | + with open(f"{output_dir}/test-segment-coffee-plant-bbox.webp", "wb") as f: |
| 23 | + f.write(segment_r.image) |
| 24 | + |
| 25 | + |
| 26 | +async def test_segment_prompt_param( |
| 27 | + fgctx: EditorAPIContext, |
| 28 | + coffee_plant_bytes: bytes, |
| 29 | + output_dir: str | None, |
| 30 | +) -> None: |
| 31 | + st_input = await fgctx.call_async.upload_image(coffee_plant_bytes) |
| 32 | + |
| 33 | + segment_r = await fgctx.call_async.segment( |
| 34 | + st_input, |
| 35 | + prompt="coffee cup", |
| 36 | + with_image=ImageOutParams(resolution="DISPLAY", image_format="WEBP"), |
| 37 | + mask_quality="low", |
| 38 | + ) |
| 39 | + assert isinstance(segment_r, OKResultWithImage) |
| 40 | + |
| 41 | + if output_dir: |
| 42 | + with open(f"{output_dir}/test-segment-coffee-plant-prompt-param.webp", "wb") as f: |
| 43 | + f.write(segment_r.image) |
| 44 | + |
| 45 | + |
| 46 | +async def test_segment_prompt_meta( |
| 47 | + fgctx: EditorAPIContext, |
| 48 | + coffee_plant_bytes: bytes, |
| 49 | + output_dir: str | None, |
| 50 | +) -> None: |
| 51 | + create_r = await fgctx.call_async.create_state( |
| 52 | + file=coffee_plant_bytes, |
| 53 | + meta={"prompt": "coffee cup"}, |
| 54 | + ) |
| 55 | + assert isinstance(create_r, OKResult) |
| 56 | + |
| 57 | + segment_r = await fgctx.call_async.segment( |
| 58 | + create_r.state_id, |
| 59 | + with_image=ImageOutParams(resolution="DISPLAY", image_format="WEBP"), |
| 60 | + mask_quality="low", |
| 61 | + ) |
| 62 | + assert isinstance(segment_r, OKResultWithImage) |
| 63 | + |
| 64 | + if output_dir: |
| 65 | + with open(f"{output_dir}/test-segment-coffee-plant-prompt-meta.webp", "wb") as f: |
| 66 | + f.write(segment_r.image) |
0 commit comments