notAI-tech / NudeNet

Lightweight nudity detection
https://nudenet.notai.tech/
GNU Affero General Public License v3.0
1.76k stars 342 forks source link

call from golang app #63

Closed gzhhong closed 3 years ago

gzhhong commented 3 years ago

Hello,

I started the classifier by docker run -it -p8080:8080 notaitech/nudenet:classifier, then I try to use golang app to send picture to the classifier and waiting the result, could you please show me the parameters to build the http request? I can't find the example in the project.

Many thanks,

James

bedapudi6788 commented 3 years ago

https://github.com/notAI-tech/fastDeploy/blob/0fa6506936b2ccc288c387425b2ceed0ca18504f/cli/fastDeploy-file_client.py#L56

You have to send the image as a base64 encoded image. An example is shown above in python.

bedapudi6788 commented 3 years ago

thanks to @preetham for this example.

package main

import (
    "bytes"
    "context"
    "encoding/base64"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "net"
    "net/http"
    "os"
    "time"
)

type config struct {
    url     string
    path    string
    webhook string
}

type payload struct {
    Data    data   `json:"data"`
    Webhook string `json:"webhook"`
}

type data struct {
    Filename string `json:"file_name"`
}

func readenv() *config {
    path := os.Getenv("FILE_PATH")
    url := os.Getenv("ENDPOINT")
    webhook := os.Getenv("WEBHOOK")
    return &config{
        url:     url,
        path:    path,
        webhook: webhook,
    }
}

func buildRequest(url, webhook string, fileData []byte) (*http.Request, error) {
    encodedData := &bytes.Buffer{}
    encoder := base64.NewEncoder(base64.StdEncoding, encodedData)
    defer encoder.Close()
    encoder.Write(fileData)
    p := payload{
        Data: data{
            Filename: encodedData.String(),
        },
        Webhook: webhook,
    }
    payloadBytes, err := json.Marshal(p)
    if err != nil {
        return nil, err
    }
    return http.NewRequest(http.MethodPost, url, bytes.NewBuffer(payloadBytes))
}

func initHTTPClient(timeout, dialTimeout time.Duration) *http.Client {
    return &http.Client{
        Timeout: time.Millisecond * timeout,
        Transport: &http.Transport{
            DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
                return net.DialTimeout(network, addr, time.Duration(dialTimeout*time.Millisecond))
            },
        },
    }
}

func main() {
    config := readenv()
    fileData, err := ioutil.ReadFile(config.path)
    if err != nil {
        log.Fatal(err)
    }
    httpClient := initHTTPClient(2000, 2000)
    req, err := buildRequest(config.url, config.webhook, fileData)
    if err != nil {
        log.Fatal(err)
    }
    req.Header.Set("Content-Type", "application/json")
    response, err := httpClient.Do(req)
    if response != nil {
        defer response.Body.Close()
    }
    if err != nil {
        log.Fatal(err)
    }
    responseBody, err := ioutil.ReadAll(response.Body)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(responseBody))
    if response.StatusCode != http.StatusOK {
        log.Fatal("Request failed")
    }
}

Example Usage

FILE_PATH=/Users/praneeth/Desktop/a.png ENDPOINT="http://localhost:8080/sync" go run main_go.go
gzhhong commented 3 years ago

Hello Bedapudi and Preetham,

Thanks for the great support!

Regards

James

Bedapudi Praneeth notifications@github.com 于2020年11月18日周三 下午5:35写道:

thanks to @preetham https://github.com/preetham for this example.

package main import ( "bytes" "context" "encoding/base64" "encoding/json" "fmt" "io/ioutil" "log" "net" "net/http" "os" "time" ) type config struct { url string path string webhook string } type payload struct { Data data json:"data" Webhook string json:"webhook" } type data struct { Filename string json:"file_name" } func readenv() config { path := os.Getenv("FILE_PATH") url := os.Getenv("ENDPOINT") webhook := os.Getenv("WEBHOOK") return &config{ url: url, path: path, webhook: webhook, } } func buildRequest(url, webhook string, fileData []byte) (http.Request, error) { encodedData := &bytes.Buffer{} encoder := base64.NewEncoder(base64.StdEncoding, encodedData) defer encoder.Close() encoder.Write(fileData) p := payload{ Data: data{ Filename: encodedData.String(), }, Webhook: webhook, } payloadBytes, err := json.Marshal(p) if err != nil { return nil, err } return http.NewRequest(http.MethodPost, url, bytes.NewBuffer(payloadBytes)) } func initHTTPClient(timeout, dialTimeout time.Duration) http.Client { return &http.Client{ Timeout: time.Millisecond timeout, Transport: &http.Transport{ DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { return net.DialTimeout(network, addr, time.Duration(dialTimeout*time.Millisecond)) }, }, } } func main() { config := readenv() fileData, err := ioutil.ReadFile(config.path) if err != nil { log.Fatal(err) } httpClient := initHTTPClient(2000, 2000) req, err := buildRequest(config.url, config.webhook, fileData) if err != nil { log.Fatal(err) } req.Header.Set("Content-Type", "application/json") response, err := httpClient.Do(req) if response != nil { defer response.Body.Close() } if err != nil { log.Fatal(err) } responseBody, err := ioutil.ReadAll(response.Body) if err != nil { log.Fatal(err) } fmt.Println(string(responseBody)) if response.StatusCode != http.StatusOK { log.Fatal("Request failed") } }

Example Usage

FILE_PATH=/Users/praneeth/Desktop/a.png ENDPOINT="http://localhost:8080/sync" go run main_go.go

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/notAI-tech/NudeNet/issues/63#issuecomment-729555360, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEO73RSRR7YYAMUGA2SLFDSQOIMJANCNFSM4TXYNVBQ .