-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract lib, parallel download-decode step
- Loading branch information
1 parent
8b971c8
commit 8cae90a
Showing
6 changed files
with
169 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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 | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package loader | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"image" | ||
"image/jpeg" | ||
"image/png" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type Result struct { | ||
Img image.Image | ||
Err error | ||
} | ||
|
||
func ImageAsync(ctx context.Context, filePathOrUrl string) chan Result { | ||
res := make(chan Result) | ||
go func() { | ||
defer close(res) | ||
|
||
select { | ||
case res <- img(ctx, filePathOrUrl): | ||
return | ||
case <-ctx.Done(): | ||
res <- Result{Err: ctx.Err()} | ||
return | ||
} | ||
}() | ||
|
||
return res | ||
} | ||
|
||
func img(ctx context.Context, filePathOrUrl string) Result { | ||
// Read | ||
var imgReader io.Reader | ||
var fileName string | ||
|
||
parsedUrl, isUrl := parseUrl(filePathOrUrl) | ||
if isUrl { | ||
resp, err := getByUrl(ctx, filePathOrUrl) | ||
if err != nil { | ||
return Result{Err: err} | ||
} | ||
defer resp.Body.Close() | ||
imgReader = resp.Body | ||
fileName = parsedUrl.Path | ||
} else { | ||
var err error | ||
if imgReader, err = os.Open(filePathOrUrl); err != nil { | ||
return Result{Err: err} | ||
} | ||
fileName = filePathOrUrl | ||
} | ||
|
||
return decode(fileName, imgReader) | ||
} | ||
|
||
func parseUrl(rawurl string) (*url.URL, bool) { | ||
parsedUrl, err := url.ParseRequestURI(rawurl) | ||
if err != nil { | ||
return parsedUrl, false | ||
} | ||
return parsedUrl, true | ||
} | ||
|
||
func decode(fileName string, f io.Reader) Result { | ||
ext := filepath.Ext(fileName) | ||
|
||
result := Result{} | ||
if ext == ".jpeg" || ext == ".jpg" { | ||
result.Img, result.Err = jpeg.Decode(f) | ||
return result | ||
} | ||
|
||
if ext == ".png" { | ||
result.Img, result.Err = png.Decode(f) | ||
return result | ||
} | ||
|
||
result.Err = fmt.Errorf("not supported file extension: %s", ext) | ||
return result | ||
} | ||
|
||
func getByUrl(ctx context.Context, url string) (*http.Response, error) { | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := http.DefaultClient.Do(req.WithContext(ctx)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
resp.Body.Close() | ||
return nil, err | ||
} | ||
|
||
return resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters