Skip to content

Commit

Permalink
parallel unlimited download
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov committed Jan 27, 2019
1 parent 8cae90a commit 393ca17
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 67 deletions.
32 changes: 0 additions & 32 deletions avg/difference.go

This file was deleted.

44 changes: 44 additions & 0 deletions avg/distance.go
Original file line number Diff line number Diff line change
@@ -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
//}
4 changes: 2 additions & 2 deletions main_test.go → avg/distance_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package avg

import (
"context"
Expand Down Expand Up @@ -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
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
33 changes: 20 additions & 13 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,38 @@ import (
"net/url"
"os"
"path/filepath"
"sync"
)

type Result struct {
Img image.Image
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
Expand Down
18 changes: 1 addition & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"github.com/AskAlexSharov/imgdiff/avg"
"github.com/AskAlexSharov/imgdiff/loader"
"os"
)

Expand All @@ -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
}

0 comments on commit 393ca17

Please sign in to comment.