From 393ca1783d9ade6b6ed528afd81f5a366286d493 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Sun, 27 Jan 2019 17:41:57 +0700 Subject: [PATCH] parallel unlimited download --- avg/difference.go | 32 ------------- avg/distance.go | 44 ++++++++++++++++++ main_test.go => avg/distance_test.go | 4 +- .../test-png-damaged.png | Bin .../test-png-original.png | Bin .../test-png-scaled-down.png | Bin go.mod | 1 - go.sum | 2 - loader/loader.go | 33 +++++++------ main.go | 18 +------ 10 files changed, 67 insertions(+), 67 deletions(-) delete mode 100644 avg/difference.go create mode 100644 avg/distance.go rename main_test.go => avg/distance_test.go (91%) rename test-png-damaged.png => avg/test-png-damaged.png (100%) rename test-png-original.png => avg/test-png-original.png (100%) rename test-png-scaled-down.png => avg/test-png-scaled-down.png (100%) diff --git a/avg/difference.go b/avg/difference.go deleted file mode 100644 index 3f651db..0000000 --- a/avg/difference.go +++ /dev/null @@ -1,32 +0,0 @@ -package avg - -import ( - "github.com/Nr90/imgsim" - "image" -) - -func Difference(img1, img2 image.Image) int { - // Hashing - ahash1 := imgsim.AverageHash(img1) - ahash2 := imgsim.AverageHash(img2) - - // distance - avgDistance := imgsim.Distance(ahash1, ahash2) - - return avgDistance % 64 // because 64 bit hash -} - -//func Difference(img1, img2 image.Image) int { -// // Hashing -// ahash1 := imgsim.AverageHash(img1) -// ahash2 := imgsim.AverageHash(img2) -// -// dhash1 := imgsim.DifferenceHash(img1) -// dhash2 := imgsim.DifferenceHash(img2) -// -// // distance -// avgDistance := imgsim.Distance(ahash1, ahash2) -// diffDistance := imgsim.Distance(dhash1, dhash2) -// -// return (avgDistance + diffDistance) % 128 -//} diff --git a/avg/distance.go b/avg/distance.go new file mode 100644 index 0000000..91bc817 --- /dev/null +++ b/avg/distance.go @@ -0,0 +1,44 @@ +package avg + +import ( + "context" + "fmt" + "github.com/AskAlexSharov/imgdiff/loader" + "github.com/Nr90/imgsim" +) + +func Distance(ctx context.Context, fileName1, fileName2 string) (int, error) { + ch := loader.ImagesAsync(ctx, fileName1, fileName2) + + // Hashing + r1 := <-ch + if r1.Err != nil { + return 0, r1.Err + } + ahash1 := imgsim.AverageHash(r1.Img) + + r2 := <-ch + if r2.Err != nil { + return 0, r2.Err + } + ahash2 := imgsim.AverageHash(r2.Img) + + fmt.Println(ahash1) + fmt.Println(ahash2) + return imgsim.Distance(ahash1, ahash2) % 64, nil // because 64 bit hash +} + +//func Difference(img1, img2 image.Image) int { +// // Hashing +// ahash1 := imgsim.AverageHash(img1) +// ahash2 := imgsim.AverageHash(img2) +// +// dhash1 := imgsim.DifferenceHash(img1) +// dhash2 := imgsim.DifferenceHash(img2) +// +// // distance +// avgDistance := imgsim.Distance(ahash1, ahash2) +// diffDistance := imgsim.Distance(dhash1, dhash2) +// +// return (avgDistance + diffDistance) % 128 +//} diff --git a/main_test.go b/avg/distance_test.go similarity index 91% rename from main_test.go rename to avg/distance_test.go index 5b7f45c..9e8cf43 100644 --- a/main_test.go +++ b/avg/distance_test.go @@ -1,4 +1,4 @@ -package main +package avg import ( "context" @@ -33,7 +33,7 @@ func Test_distance(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - gotDistance, err := readAndGetDistance(context.Background(), tt.file1, tt.file2) + gotDistance, err := Distance(context.Background(), tt.file1, tt.file2) if err != nil { t.Errorf("unexpected error: %v", err) return diff --git a/test-png-damaged.png b/avg/test-png-damaged.png similarity index 100% rename from test-png-damaged.png rename to avg/test-png-damaged.png diff --git a/test-png-original.png b/avg/test-png-original.png similarity index 100% rename from test-png-original.png rename to avg/test-png-original.png diff --git a/test-png-scaled-down.png b/avg/test-png-scaled-down.png similarity index 100% rename from test-png-scaled-down.png rename to avg/test-png-scaled-down.png diff --git a/go.mod b/go.mod index 0a2df11..9aec23e 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,5 @@ module github.com/AskAlexSharov/imgdiff require ( github.com/Nr90/imgsim v0.0.0-20180202144352-5caa057144b0 - github.com/aws/aws-lambda-go v1.8.1 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect ) diff --git a/go.sum b/go.sum index 04b0bf3..b2bc360 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,4 @@ github.com/Nr90/imgsim v0.0.0-20180202144352-5caa057144b0 h1:8cjsGKoi/1QBb0V2ps3iBra9c4o+qY/NaJ15NsdjmQ4= github.com/Nr90/imgsim v0.0.0-20180202144352-5caa057144b0/go.mod h1:PSWPVD+KeWK3XVt0i/AahAMRw38OZ1k1vJpJLuvIY1w= -github.com/aws/aws-lambda-go v1.8.1 h1:nHBpP6XC30bwF6qWKrw/BrK2A8i4GKmSZzajTBIJS4A= -github.com/aws/aws-lambda-go v1.8.1/go.mod h1:zUsUQhAUjYzR8AuduJPCfhBuKWUaDbQiPOG+ouzmE1A= github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= diff --git a/loader/loader.go b/loader/loader.go index baa8910..a41f775 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -11,6 +11,7 @@ import ( "net/url" "os" "path/filepath" + "sync" ) type Result struct { @@ -18,24 +19,30 @@ type Result struct { Err error } -func ImageAsync(ctx context.Context, filePathOrUrl string) chan Result { - res := make(chan Result) +func ImagesAsync(ctx context.Context, filePathOrUrls ...string) chan Result { + resultCh := make(chan Result) + wg := sync.WaitGroup{} + wg.Add(len(filePathOrUrls)) + for _, filePathOrUrl := range filePathOrUrls { + go func(filePathOrUrl string) { + defer wg.Done() + select { + case resultCh <- Image(ctx, filePathOrUrl): + case <-ctx.Done(): + resultCh <- Result{Err: ctx.Err()} + } + }(filePathOrUrl) + } + go func() { - defer close(res) - - select { - case res <- img(ctx, filePathOrUrl): - return - case <-ctx.Done(): - res <- Result{Err: ctx.Err()} - return - } + wg.Wait() + close(resultCh) }() - return res + return resultCh } -func img(ctx context.Context, filePathOrUrl string) Result { +func Image(ctx context.Context, filePathOrUrl string) Result { // Read var imgReader io.Reader var fileName string diff --git a/main.go b/main.go index 79032ef..6f5d8b2 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "github.com/AskAlexSharov/imgdiff/avg" - "github.com/AskAlexSharov/imgdiff/loader" "os" ) @@ -14,24 +13,9 @@ func main() { return } - dist, err := readAndGetDistance(context.Background(), os.Args[1], os.Args[2]) + dist, err := avg.Distance(context.Background(), os.Args[1], os.Args[2]) if err != nil { panic(err) } fmt.Printf("Diffeernce: %d%%\n", dist) } - -func readAndGetDistance(ctx context.Context, fileName1, fileName2 string) (int, error) { - ch1 := loader.ImageAsync(ctx, fileName1) - ch2 := loader.ImageAsync(ctx, fileName2) - r1 := <-ch1 - r2 := <-ch2 - if r1.Err != nil { - return 0, r1.Err - } - if r2.Err != nil { - return 0, r2.Err - } - - return avg.Difference(r1.Img, r2.Img), nil -}