emboss

package module
v0.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2024 License: BSD-3-Clause Imports: 25 Imported by: 2

README

go-text-emboss

Go package for interacting with the sfomuseum/swift-text-emboss command-line, www and grpc tools

Background

For background, please see the Searching Text in Images on the Aviation Collection Website blog post.

Documentation

Documentation is incomplete.

Example

Error handling omitted for the sake of brevity.

Local

Local text "embossing" depends on their being a copy of the text-emboss tool that can be invoked in a shell command.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/sfomuseum/go-text-emboss"
)

func main() {

	ctx := context.Background()
	ex, _ := emboss.NewEmbosser(ctx, "local:///usr/local/sfomuseum/bin/text-emboss")

	for _, path := range flag.Args() {
		rsp, _ := ex.EmbossText(ctx, path)
		fmt.Println(rsp.Text)
	}
}
Remote (HTTP)

Remote (HTTP) text "embossing" depends on their being a copy of the text-emboss-server tool running that can be accessed over HTTP.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/sfomuseum/go-text-emboss"
)

func main() {

	ctx := context.Background()
	ex, _ := emboss.NewEmbosser(ctx, "http://localhost:8080")

	for _, path := range flag.Args() {
		rsp, _ := ex.EmbossText(ctx, path)
		fmt.Println(rsp.Text)
	}
}
Remote (gRPC)

Remote (gRPC) text "embossing" depends on their being a copy of the text-emboss-server tool running that can be accessed over TCP.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/sfomuseum/go-text-emboss"
)

func main() {

	ctx := context.Background()
	ex, _ := emboss.NewEmbosser(ctx, "grpc://localhost:1234")

	for _, path := range flag.Args() {
		rsp, _ := ex.EmbossText(ctx, path)
		fmt.Println(rsp.Text)
	}
}

Tools

emboss
$> ./bin/emboss -h
Usage of ./bin/emboss:
  -as-json
    	Return results as a JSON-encoded dictionary containing text, source and creation time properties.
  -embosser-uri string
    	A valid sfomuseum/go-text-emboss.Embosser URI. (default "local:///usr/local/sfomuseum/bin/text-emboss")

For example, assuming there is a copy of the text-emboss tool at /usr/local/sfomuseum/bin/text-emboss:

$> ./bin/emboss .fixtures/menu.jpg 
Mood-lit Libations
Champagne Powder Cocktail
Champagne served with St. Germain
elderflower liqueur and hibiscus syrup
Mile-High Manhattan
Stranahans whiskey served with
sweet vermouth
Peach Collins On The Rockies
Silver Tree vodka, Leopold Bros peach
liqueur, lemon juice and agave nectar
Colorado Craft Beer
California Wines
"america

Or, assuming the there is a copy of the text-emboss-server tool listening for requests on http://localhost:8080:

$> ./bin/emboss -embosser-uri http://localhost:8080 .fixtures/menu.jpg
Mood-lit Libations
Champagne Powder Cocktail
Champagne served with St. Germain
elderflower liqueur and hibiscus syrup
Mile-High Manhattan
Stranahans whiskey served with
sweet vermouth
Peach Collins On The Rockies
Silver Tree vodka, Leopold Bros peach
liqueur, lemon juice and agave nectar
Colorado Craft Beer
California Wines
"america

Or, assuming the there is a copy of the text-emboss-server tool listening for requests on localhost:1234:

$> ./bin/emboss -embosser-uri grpc://localhost:1234 .fixtures/menu.jpg
Mood-lit Libations
Champagne Powder Cocktail
Champagne served with St. Germain
elderflower liqueur and hibiscus syrup
Mile-High Manhattan
Stranahans whiskey served with
sweet vermouth
Peach Collins On The Rockies
Silver Tree vodka, Leopold Bros peach
liqueur, lemon juice and agave nectar
Colorado Craft Beer
California Wines
"america
JSON

To return provenance and creation time for the text extracted from an image pass the -as-json flag which will return a JSON-encoded dictionary containing that information.

$> ./bin/emboss -embosser-uri 'grpc://localhost:8080' -as-json ./fixtures/menu.jpg | jq
{
  "text": "Mood-lit Libations\nChampagne Powder Cocktail\nChampagne served with St. Germain\nelderflower liqueur and hibiscus syrup\nMile-High Manhattan\nStranahans whiskey served with\nsweet vermouth\nPeach Collins On The Rockies\nSilver Tree vodka, Leopold Bros peach\nliqueur, lemon juice and agave nectar\nColorado Craft Beer\nCalifornia Wines\namerica",
  "source": "com.apple.visionkit.VNImageRequestHandler#Version 14.1.2 (Build 23B92)",
  "created": 1701900635
}

Note: The source key is an arbitrary string used to identify the processes, or models, from which image text was derived. As of this writing this string has no standard formatting or requirements. If and when those conventions are established this package will be updated to use them.

Tests

To run tests you will need to specify the custom -local-embosser-uri, -grpc-embosser-uri and -http-embosser-uri flag with values specific to your system. For example:

$> go test -v -local-embosser-uri local:///usr/local/sfomuseum/bin/text-emboss -http-embosser-uri http://localhost:8080 -grpc-embosser-uri grpc://localhost:1234
=== RUN   TestGRPCEmbosser
--- PASS: TestGRPCEmbosser (0.09s)
=== RUN   TestGRPCEmbosserWithReader
--- PASS: TestGRPCEmbosserWithReader (0.07s)
=== RUN   TestHTTPEmbosser
--- PASS: TestHTTPEmbosser (0.73s)
=== RUN   TestHTTPEmbosserWithReader
--- PASS: TestHTTPEmbosserWithReader (0.85s)
=== RUN   TestLocalEmbosser
--- PASS: TestLocalEmbosser (0.31s)
=== RUN   TestLocalEmbosserWithReader
--- PASS: TestLocalEmbosserWithReader (0.29s)
=== RUN   TestLocalEmbosserWithReaderAndPath
--- PASS: TestLocalEmbosserWithReaderAndPath (0.30s)
=== RUN   TestNullEmbosser
--- PASS: TestNullEmbosser (0.00s)
=== RUN   TestNullEmbosserWithReader
--- PASS: TestNullEmbosserWithReader (0.00s)
PASS
ok  	github.com/sfomuseum/go-text-emboss	2.791s

Note that the TestLocal tests are only applicable on OS X (darwin) systems.

See also

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateImageFormFile

func CreateImageFormFile(w *multipart.Writer, name, filename string) (io.Writer, error)

func RegisterEmbosser

func RegisterEmbosser(ctx context.Context, scheme string, init_func EmbosserInitializationFunc) error

RegisterEmbosser registers 'scheme' as a key pointing to 'init_func' in an internal lookup table used to create new `Embosser` instances by the `NewEmbosser` method.

func Schemes

func Schemes() []string

Schemes returns the list of schemes that have been registered.

Types

type EmbossTextResult added in v0.0.8

type EmbossTextResult struct {
	Text    string `json:"text"`
	Source  string `json:"source"`
	Created int64  `json:"created"`
}

func (*EmbossTextResult) String added in v0.0.8

func (r *EmbossTextResult) String() string

type Embosser

type Embosser interface {
	EmbossText(context.Context, string) (*EmbossTextResult, error)
	EmbossTextWithReader(context.Context, string, io.Reader) (*EmbossTextResult, error)
	Close(context.Context) error
}

func NewEmbosser

func NewEmbosser(ctx context.Context, uri string) (Embosser, error)

NewEmbosser returns a new `Embosser` instance configured by 'uri'. The value of 'uri' is parsed as a `url.URL` and its scheme is used as the key for a corresponding `EmbosserInitializationFunc` function used to instantiate the new `Embosser`. It is assumed that the scheme (and initialization function) have been registered by the `RegisterEmbosser` method.

func NewGrpcEmbosser added in v0.0.5

func NewGrpcEmbosser(ctx context.Context, uri string) (Embosser, error)

func NewHTTPEmbosser added in v0.0.4

func NewHTTPEmbosser(ctx context.Context, uri string) (Embosser, error)

func NewHTTPEmbosserWithClient added in v0.0.4

func NewHTTPEmbosserWithClient(ctx context.Context, uri string, client *http.Client) (Embosser, error)

func NewLocalEmbosser

func NewLocalEmbosser(ctx context.Context, uri string) (Embosser, error)

func NewNullEmbosser added in v0.0.3

func NewNullEmbosser(ctx context.Context, uri string) (Embosser, error)

type EmbosserInitializationFunc

type EmbosserInitializationFunc func(ctx context.Context, uri string) (Embosser, error)

EmbosserInitializationFunc is a function defined by individual embosser package and used to create an instance of that embosser

type GrpcEmbosser added in v0.0.5

type GrpcEmbosser struct {
	// contains filtered or unexported fields
}

func (*GrpcEmbosser) Close added in v0.0.5

func (e *GrpcEmbosser) Close(ctx context.Context) error

func (*GrpcEmbosser) EmbossText added in v0.0.5

func (e *GrpcEmbosser) EmbossText(ctx context.Context, path string) (*EmbossTextResult, error)

func (*GrpcEmbosser) EmbossTextWithReader added in v0.0.5

func (e *GrpcEmbosser) EmbossTextWithReader(ctx context.Context, path string, im_r io.Reader) (*EmbossTextResult, error)

type HTTPEmbosser added in v0.0.4

type HTTPEmbosser struct {
	// contains filtered or unexported fields
}

func (*HTTPEmbosser) Close added in v0.0.5

func (e *HTTPEmbosser) Close(ctx context.Context) error

func (*HTTPEmbosser) EmbossText added in v0.0.4

func (e *HTTPEmbosser) EmbossText(ctx context.Context, path string) (*EmbossTextResult, error)

func (*HTTPEmbosser) EmbossTextWithReader added in v0.0.4

func (e *HTTPEmbosser) EmbossTextWithReader(ctx context.Context, path string, im_r io.Reader) (*EmbossTextResult, error)

type LocalEmbosser

type LocalEmbosser struct {
	// contains filtered or unexported fields
}

func (*LocalEmbosser) Close added in v0.0.5

func (e *LocalEmbosser) Close(ctx context.Context) error

func (*LocalEmbosser) EmbossText

func (e *LocalEmbosser) EmbossText(ctx context.Context, path string) (*EmbossTextResult, error)

func (*LocalEmbosser) EmbossTextWithReader added in v0.0.2

func (e *LocalEmbosser) EmbossTextWithReader(ctx context.Context, path string, r io.Reader) (*EmbossTextResult, error)

type NullEmbosser added in v0.0.3

type NullEmbosser struct {
	// contains filtered or unexported fields
}

func (*NullEmbosser) Close added in v0.0.5

func (e *NullEmbosser) Close(ctx context.Context) error

func (*NullEmbosser) EmbossText added in v0.0.3

func (e *NullEmbosser) EmbossText(ctx context.Context, path string) (*EmbossTextResult, error)

func (*NullEmbosser) EmbossTextWithReader added in v0.0.3

func (e *NullEmbosser) EmbossTextWithReader(ctx context.Context, path string, r io.Reader) (*EmbossTextResult, error)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL